diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-08-10 15:51:17 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-10 15:51:17 -0400 |
| commit | c4b9466da7592b95246909908619b68db5389ceb (patch) | |
| tree | 6c55cc3ecb3e289fe2c13e346b70560fceaafbef /std/unicode.zig | |
| parent | d927f347de1f5a19545fc235f8779c2326409543 (diff) | |
| parent | 598e80957e6eccc13ade72ce2693dcd60934763d (diff) | |
| download | zig-c4b9466da7592b95246909908619b68db5389ceb.tar.gz zig-c4b9466da7592b95246909908619b68db5389ceb.zip | |
Merge pull request #1294 from ziglang/async-fs
introduce std.event.fs for async file system functions
Diffstat (limited to 'std/unicode.zig')
| -rw-r--r-- | std/unicode.zig | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/std/unicode.zig b/std/unicode.zig index 8a9d4a9214..0e7b4cdc3e 100644 --- a/std/unicode.zig +++ b/std/unicode.zig @@ -188,6 +188,7 @@ pub const Utf8View = struct { return Utf8View{ .bytes = s }; } + /// TODO: https://github.com/ziglang/zig/issues/425 pub fn initComptime(comptime s: []const u8) Utf8View { if (comptime init(s)) |r| { return r; @@ -199,7 +200,7 @@ pub const Utf8View = struct { } } - pub fn iterator(s: *const Utf8View) Utf8Iterator { + pub fn iterator(s: Utf8View) Utf8Iterator { return Utf8Iterator{ .bytes = s.bytes, .i = 0, @@ -530,3 +531,20 @@ test "utf16leToUtf8" { assert(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); } } + +/// TODO support codepoints bigger than 16 bits +/// TODO type for null terminated pointer +pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![]u16 { + var result = std.ArrayList(u16).init(allocator); + // optimistically guess that it will not require surrogate pairs + try result.ensureCapacity(utf8.len + 1); + + const view = try Utf8View.init(utf8); + var it = view.iterator(); + while (it.nextCodepoint()) |codepoint| { + try result.append(@intCast(u16, codepoint)); // TODO surrogate pairs + } + + try result.append(0); + return result.toOwnedSlice(); +} |
