| ... | @@ -934,7 +934,7 @@ fn utf16LeToUtf8ArrayListImpl( | ... | @@ -934,7 +934,7 @@ fn utf16LeToUtf8ArrayListImpl( |
| 934 | .cannot_encode_surrogate_half => Utf16LeToUtf8AllocError, | 934 | .cannot_encode_surrogate_half => Utf16LeToUtf8AllocError, |
| 935 | .can_encode_surrogate_half => mem.Allocator.Error, | 935 | .can_encode_surrogate_half => mem.Allocator.Error, |
| 936 | })!void { | 936 | })!void { |
| 937 | assert(result.capacity >= utf16le.len); | 937 | assert(result.unusedCapacitySlice().len >= utf16le.len); |
| 938 | | 938 | |
| 939 | var remaining = utf16le; | 939 | var remaining = utf16le; |
| 940 | vectorized: { | 940 | vectorized: { |
| ... | @@ -979,7 +979,7 @@ fn utf16LeToUtf8ArrayListImpl( | ... | @@ -979,7 +979,7 @@ fn utf16LeToUtf8ArrayListImpl( |
| 979 | pub const Utf16LeToUtf8AllocError = mem.Allocator.Error || Utf16LeToUtf8Error; | 979 | pub const Utf16LeToUtf8AllocError = mem.Allocator.Error || Utf16LeToUtf8Error; |
| 980 | | 980 | |
| 981 | pub fn utf16LeToUtf8ArrayList(result: *std.ArrayList(u8), utf16le: []const u16) Utf16LeToUtf8AllocError!void { | 981 | pub fn utf16LeToUtf8ArrayList(result: *std.ArrayList(u8), utf16le: []const u16) Utf16LeToUtf8AllocError!void { |
| 982 | try result.ensureTotalCapacityPrecise(utf16le.len); | 982 | try result.ensureUnusedCapacity(utf16le.len); |
| 983 | return utf16LeToUtf8ArrayListImpl(result, utf16le, .cannot_encode_surrogate_half); | 983 | return utf16LeToUtf8ArrayListImpl(result, utf16le, .cannot_encode_surrogate_half); |
| 984 | } | 984 | } |
| 985 | | 985 | |
| ... | @@ -1138,7 +1138,7 @@ test utf16LeToUtf8 { | ... | @@ -1138,7 +1138,7 @@ test utf16LeToUtf8 { |
| 1138 | } | 1138 | } |
| 1139 | | 1139 | |
| 1140 | fn utf8ToUtf16LeArrayListImpl(result: *std.ArrayList(u16), utf8: []const u8, comptime surrogates: Surrogates) !void { | 1140 | fn utf8ToUtf16LeArrayListImpl(result: *std.ArrayList(u16), utf8: []const u8, comptime surrogates: Surrogates) !void { |
| 1141 | assert(result.capacity >= utf8.len); | 1141 | assert(result.unusedCapacitySlice().len >= utf8.len); |
| 1142 | | 1142 | |
| 1143 | var remaining = utf8; | 1143 | var remaining = utf8; |
| 1144 | vectorized: { | 1144 | vectorized: { |
| ... | @@ -1176,7 +1176,7 @@ fn utf8ToUtf16LeArrayListImpl(result: *std.ArrayList(u16), utf8: []const u8, com | ... | @@ -1176,7 +1176,7 @@ fn utf8ToUtf16LeArrayListImpl(result: *std.ArrayList(u16), utf8: []const u8, com |
| 1176 | } | 1176 | } |
| 1177 | | 1177 | |
| 1178 | pub fn utf8ToUtf16LeArrayList(result: *std.ArrayList(u16), utf8: []const u8) error{ InvalidUtf8, OutOfMemory }!void { | 1178 | pub fn utf8ToUtf16LeArrayList(result: *std.ArrayList(u16), utf8: []const u8) error{ InvalidUtf8, OutOfMemory }!void { |
| 1179 | try result.ensureTotalCapacityPrecise(utf8.len); | 1179 | try result.ensureUnusedCapacity(utf8.len); |
| 1180 | return utf8ToUtf16LeArrayListImpl(result, utf8, .cannot_encode_surrogate_half); | 1180 | return utf8ToUtf16LeArrayListImpl(result, utf8, .cannot_encode_surrogate_half); |
| 1181 | } | 1181 | } |
| 1182 | | 1182 | |
| ... | @@ -1351,6 +1351,64 @@ test utf8ToUtf16LeAllocZ { | ... | @@ -1351,6 +1351,64 @@ test utf8ToUtf16LeAllocZ { |
| 1351 | } | 1351 | } |
| 1352 | } | 1352 | } |
| 1353 | | 1353 | |
| | 1354 | test "ArrayList functions on a re-used list" { |
| | 1355 | // utf8ToUtf16LeArrayList |
| | 1356 | { |
| | 1357 | var list = std.ArrayList(u16).init(testing.allocator); |
| | 1358 | defer list.deinit(); |
| | 1359 | |
| | 1360 | const init_slice = utf8ToUtf16LeStringLiteral("abcdefg"); |
| | 1361 | try list.ensureTotalCapacityPrecise(init_slice.len); |
| | 1362 | list.appendSliceAssumeCapacity(init_slice); |
| | 1363 | |
| | 1364 | try utf8ToUtf16LeArrayList(&list, "hijklmnopqrstuvwyxz"); |
| | 1365 | |
| | 1366 | try testing.expectEqualSlices(u16, utf8ToUtf16LeStringLiteral("abcdefghijklmnopqrstuvwyxz"), list.items); |
| | 1367 | } |
| | 1368 | |
| | 1369 | // utf16LeToUtf8ArrayList |
| | 1370 | { |
| | 1371 | var list = std.ArrayList(u8).init(testing.allocator); |
| | 1372 | defer list.deinit(); |
| | 1373 | |
| | 1374 | const init_slice = "abcdefg"; |
| | 1375 | try list.ensureTotalCapacityPrecise(init_slice.len); |
| | 1376 | list.appendSliceAssumeCapacity(init_slice); |
| | 1377 | |
| | 1378 | try utf16LeToUtf8ArrayList(&list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz")); |
| | 1379 | |
| | 1380 | try testing.expectEqualStrings("abcdefghijklmnopqrstuvwyxz", list.items); |
| | 1381 | } |
| | 1382 | |
| | 1383 | // wtf8ToWtf16LeArrayList |
| | 1384 | { |
| | 1385 | var list = std.ArrayList(u16).init(testing.allocator); |
| | 1386 | defer list.deinit(); |
| | 1387 | |
| | 1388 | const init_slice = utf8ToUtf16LeStringLiteral("abcdefg"); |
| | 1389 | try list.ensureTotalCapacityPrecise(init_slice.len); |
| | 1390 | list.appendSliceAssumeCapacity(init_slice); |
| | 1391 | |
| | 1392 | try wtf8ToWtf16LeArrayList(&list, "hijklmnopqrstuvwyxz"); |
| | 1393 | |
| | 1394 | try testing.expectEqualSlices(u16, utf8ToUtf16LeStringLiteral("abcdefghijklmnopqrstuvwyxz"), list.items); |
| | 1395 | } |
| | 1396 | |
| | 1397 | // wtf16LeToWtf8ArrayList |
| | 1398 | { |
| | 1399 | var list = std.ArrayList(u8).init(testing.allocator); |
| | 1400 | defer list.deinit(); |
| | 1401 | |
| | 1402 | const init_slice = "abcdefg"; |
| | 1403 | try list.ensureTotalCapacityPrecise(init_slice.len); |
| | 1404 | list.appendSliceAssumeCapacity(init_slice); |
| | 1405 | |
| | 1406 | try wtf16LeToWtf8ArrayList(&list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz")); |
| | 1407 | |
| | 1408 | try testing.expectEqualStrings("abcdefghijklmnopqrstuvwyxz", list.items); |
| | 1409 | } |
| | 1410 | } |
| | 1411 | |
| 1354 | /// Converts a UTF-8 string literal into a UTF-16LE string literal. | 1412 | /// Converts a UTF-8 string literal into a UTF-16LE string literal. |
| 1355 | pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) catch |err| @compileError(err):0]u16 { | 1413 | pub fn utf8ToUtf16LeStringLiteral(comptime utf8: []const u8) *const [calcUtf16LeLen(utf8) catch |err| @compileError(err):0]u16 { |
| 1356 | return comptime blk: { | 1414 | return comptime blk: { |
| ... | @@ -1685,7 +1743,7 @@ pub const Wtf8Iterator = struct { | ... | @@ -1685,7 +1743,7 @@ pub const Wtf8Iterator = struct { |
| 1685 | }; | 1743 | }; |
| 1686 | | 1744 | |
| 1687 | pub fn wtf16LeToWtf8ArrayList(result: *std.ArrayList(u8), utf16le: []const u16) mem.Allocator.Error!void { | 1745 | pub fn wtf16LeToWtf8ArrayList(result: *std.ArrayList(u8), utf16le: []const u16) mem.Allocator.Error!void { |
| 1688 | try result.ensureTotalCapacityPrecise(utf16le.len); | 1746 | try result.ensureUnusedCapacity(utf16le.len); |
| 1689 | return utf16LeToUtf8ArrayListImpl(result, utf16le, .can_encode_surrogate_half); | 1747 | return utf16LeToUtf8ArrayListImpl(result, utf16le, .can_encode_surrogate_half); |
| 1690 | } | 1748 | } |
| 1691 | | 1749 | |
| ... | @@ -1714,7 +1772,7 @@ pub fn wtf16LeToWtf8(wtf8: []u8, wtf16le: []const u16) usize { | ... | @@ -1714,7 +1772,7 @@ pub fn wtf16LeToWtf8(wtf8: []u8, wtf16le: []const u16) usize { |
| 1714 | } | 1772 | } |
| 1715 | | 1773 | |
| 1716 | pub fn wtf8ToWtf16LeArrayList(result: *std.ArrayList(u16), wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }!void { | 1774 | pub fn wtf8ToWtf16LeArrayList(result: *std.ArrayList(u16), wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }!void { |
| 1717 | try result.ensureTotalCapacityPrecise(wtf8.len); | 1775 | try result.ensureUnusedCapacity(wtf8.len); |
| 1718 | return utf8ToUtf16LeArrayListImpl(result, wtf8, .can_encode_surrogate_half); | 1776 | return utf8ToUtf16LeArrayListImpl(result, wtf8, .can_encode_surrogate_half); |
| 1719 | } | 1777 | } |
| 1720 | | 1778 | |