| ... | ... | @@ -571,8 +571,9 @@ pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![:0]u |
| 571 | 571 | } |
| 572 | 572 | } |
| 573 | 573 | |
| 574 | const len = result.len; |
| 574 | 575 | try result.append(0); |
| 575 | | return result.toOwnedSlice()[0..:0]; |
| 576 | return result.toOwnedSlice()[0..len :0]; |
| 576 | 577 | } |
| 577 | 578 | |
| 578 | 579 | /// Returns index of next character. If exact fit, returned index equals output slice length. |
| ... | ... | @@ -619,12 +620,14 @@ test "utf8ToUtf16LeWithNull" { |
| 619 | 620 | var bytes: [128]u8 = undefined; |
| 620 | 621 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; |
| 621 | 622 | const utf16 = try utf8ToUtf16LeWithNull(allocator, "𐐷"); |
| 622 | | testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc\x00\x00", @sliceToBytes(utf16[0..])); |
| 623 | testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", @sliceToBytes(utf16[0..])); |
| 624 | testing.expect(utf16[2] == 0); |
| 623 | 625 | } |
| 624 | 626 | { |
| 625 | 627 | var bytes: [128]u8 = undefined; |
| 626 | 628 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; |
| 627 | 629 | const utf16 = try utf8ToUtf16LeWithNull(allocator, "\u{10FFFF}"); |
| 628 | | testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf\x00\x00", @sliceToBytes(utf16[0..])); |
| 630 | testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", @sliceToBytes(utf16[0..])); |
| 631 | testing.expect(utf16[2] == 0); |
| 629 | 632 | } |
| 630 | 633 | } |