| ... | ... | @@ -553,8 +553,9 @@ fn testDecode(bytes: []const u8) !u21 { |
| 553 | 553 | /// Caller must free returned memory. |
| 554 | 554 | pub fn utf16leToUtf8Alloc(allocator: *mem.Allocator, utf16le: []const u16) ![]u8 { |
| 555 | 555 | var result = std.ArrayList(u8).init(allocator); |
| 556 | errdefer result.deinit(); |
| 556 | 557 | // optimistically guess that it will all be ascii. |
| 557 | | try result.ensureCapacity(utf16le.len); |
| 558 | try result.ensureTotalCapacity(utf16le.len); |
| 558 | 559 | var out_index: usize = 0; |
| 559 | 560 | var it = Utf16LeIterator.init(utf16le); |
| 560 | 561 | while (try it.nextCodepoint()) |codepoint| { |
| ... | ... | @@ -569,9 +570,10 @@ pub fn utf16leToUtf8Alloc(allocator: *mem.Allocator, utf16le: []const u16) ![]u8 |
| 569 | 570 | |
| 570 | 571 | /// Caller must free returned memory. |
| 571 | 572 | pub fn utf16leToUtf8AllocZ(allocator: *mem.Allocator, utf16le: []const u16) ![:0]u8 { |
| 572 | | var result = try std.ArrayList(u8).initCapacity(allocator, utf16le.len); |
| 573 | var result = std.ArrayList(u8).init(allocator); |
| 574 | errdefer result.deinit(); |
| 573 | 575 | // optimistically guess that it will all be ascii. |
| 574 | | try result.ensureCapacity(utf16le.len); |
| 576 | try result.ensureTotalCapacity(utf16le.len); |
| 575 | 577 | var out_index: usize = 0; |
| 576 | 578 | var it = Utf16LeIterator.init(utf16le); |
| 577 | 579 | while (try it.nextCodepoint()) |codepoint| { |
| ... | ... | @@ -653,10 +655,18 @@ test "utf16leToUtf8" { |
| 653 | 655 | defer std.testing.allocator.free(utf8); |
| 654 | 656 | try testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); |
| 655 | 657 | } |
| 658 | |
| 659 | { |
| 660 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdcdc); |
| 661 | mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdcdc); |
| 662 | const result = utf16leToUtf8Alloc(std.testing.allocator, &utf16le); |
| 663 | try std.testing.expectError(error.UnexpectedSecondSurrogateHalf, result); |
| 664 | } |
| 656 | 665 | } |
| 657 | 666 | |
| 658 | 667 | pub fn utf8ToUtf16LeWithNull(allocator: *mem.Allocator, utf8: []const u8) ![:0]u16 { |
| 659 | 668 | var result = std.ArrayList(u16).init(allocator); |
| 669 | errdefer result.deinit(); |
| 660 | 670 | // optimistically guess that it will not require surrogate pairs |
| 661 | 671 | try result.ensureCapacity(utf8.len + 1); |
| 662 | 672 | |
| ... | ... | @@ -718,6 +728,10 @@ test "utf8ToUtf16Le" { |
| 718 | 728 | try testing.expectEqual(@as(usize, 2), length); |
| 719 | 729 | try testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", mem.sliceAsBytes(utf16le[0..])); |
| 720 | 730 | } |
| 731 | { |
| 732 | const result = utf8ToUtf16Le(utf16le[0..], "\xf4\x90\x80\x80"); |
| 733 | try testing.expectError(error.InvalidUtf8, result); |
| 734 | } |
| 721 | 735 | } |
| 722 | 736 | |
| 723 | 737 | test "utf8ToUtf16LeWithNull" { |
| ... | ... | @@ -733,6 +747,10 @@ test "utf8ToUtf16LeWithNull" { |
| 733 | 747 | try testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", mem.sliceAsBytes(utf16[0..])); |
| 734 | 748 | try testing.expect(utf16[2] == 0); |
| 735 | 749 | } |
| 750 | { |
| 751 | const result = utf8ToUtf16LeWithNull(testing.allocator, "\xf4\x90\x80\x80"); |
| 752 | try testing.expectError(error.InvalidUtf8, result); |
| 753 | } |
| 736 | 754 | } |
| 737 | 755 | |
| 738 | 756 | /// Converts a UTF-8 string literal into a UTF-16LE string literal. |