authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-12-28 14:35:53+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2019-12-28 16:49:59+11:00
logab6065407d74fc8d63d398c60f9fe653374d9d6d
treef8d53192f40c96e6738d83264ebc683aee91ce87
parent5843a6e3bc1a6353f76ebca57d6099337b90139a
signature Commit is signed but in an unrecognized format.

std: simplify utf8ToUtf16Le

Also faster, on my machine unicode/throughput_test.zig now gives e.g. > original utf8ToUtf16Le: elapsed: 1048 ns (0 ms) > new utf8ToUtf16Le: elapsed: 971 ns (0 ms)

1 files changed, 14 insertions(+), 26 deletions(-)

lib/std/unicode.zig+14-26
...@@ -576,33 +576,21 @@ pub fn utf8ToUtf16Le(utf16le: []u16, utf8: []const u8) !usize {...@@ -576,33 +576,21 @@ pub fn utf8ToUtf16Le(utf16le: []u16, utf8: []const u8) !usize {
576 var dest_i: usize = 0;576 var dest_i: usize = 0;
577 var src_i: usize = 0;577 var src_i: usize = 0;
578 while (src_i < utf8.len) {578 while (src_i < utf8.len) {
579 const byte = utf8[src_i];579 const n = utf8ByteSequenceLength(utf8[src_i]) catch return error.InvalidUtf8;
580 const n = @clz(u8, ~byte);580 const next_src_i = src_i + n;
581 switch (n) {581 const codepoint = utf8Decode(utf8[src_i..next_src_i]) catch return error.InvalidUtf8;
582 0 => {582 if (codepoint < 0x10000) {
583 utf16le[dest_i] = byte;583 const short = @intCast(u16, codepoint);
584 dest_i += 1;584 utf16le[dest_i] = mem.nativeToLittle(u16, short);
585 src_i += 1;585 dest_i += 1;
586 continue;586 } else {
587 },587 const high = @intCast(u16, (codepoint - 0x10000) >> 10) + 0xD800;
588 2, 3, 4 => {588 const low = @intCast(u16, codepoint & 0x3FF) + 0xDC00;
589 const next_src_i = src_i + n;589 utf16le[dest_i] = mem.nativeToLittle(u16, high);
590 const codepoint = utf8Decode(utf8[src_i..next_src_i]) catch return error.InvalidUtf8;590 utf16le[dest_i + 1] = mem.nativeToLittle(u16, low);
591 if (codepoint < 0x10000) {591 dest_i += 2;
592 const short = @intCast(u16, codepoint);
593 utf16le[dest_i] = mem.nativeToLittle(u16, short);
594 dest_i += 1;
595 } else {
596 const high = @intCast(u16, (codepoint - 0x10000) >> 10) + 0xD800;
597 const low = @intCast(u16, codepoint & 0x3FF) + 0xDC00;
598 utf16le[dest_i] = mem.nativeToLittle(u16, high);
599 utf16le[dest_i + 1] = mem.nativeToLittle(u16, low);
600 dest_i += 2;
601 }
602 src_i = next_src_i;
603 },
604 else => return error.InvalidUtf8,
605 }592 }
593 src_i = next_src_i;
606 }594 }
607 return dest_i;595 return dest_i;
608}596}