| ... | @@ -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 | } |