mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-10-28 10:45:48 +08:00
std: update Arraylist in unicode.zig to unmanaged
This commit is contained in:
1 parent
cb98d75813
commit
1420479be7
4 files changed
+114
-113
No files matched your search
+28
-28
@@ -17143,8 +17143,8 @@ fn argvToScriptCommandLineWindows(
|
||||
/// Arguments, not including the script name itself. Expected to be encoded as WTF-8.
|
||||
script_args: []const []const u8,
|
||||
) ArgvToScriptCommandLineError![:0]u16 {
|
||||
var buf = try std.array_list.Managed(u8).initCapacity(allocator, 64);
|
||||
defer buf.deinit();
|
||||
var buf: std.ArrayList(u8) = try .initCapacity(allocator, 64);
|
||||
defer buf.deinit(allocator);
|
||||
|
||||
// `/d` disables execution of AutoRun commands.
|
||||
// `/e:ON` and `/v:OFF` are needed for BatBadBut mitigation:
|
||||
@@ -17167,12 +17167,12 @@ fn argvToScriptCommandLineWindows(
|
||||
if (std.mem.findAny(u16, script_path, &[_]u16{
|
||||
std.mem.nativeToLittle(u16, '\\'), std.mem.nativeToLittle(u16, '/'),
|
||||
}) == null) {
|
||||
try buf.appendSlice(".\\");
|
||||
try buf.appendSlice(allocator, ".\\");
|
||||
}
|
||||
// Note that we don't do any escaping/mitigations for this argument, since the relevant
|
||||
// characters (", %, etc) are illegal in file paths and this function should only be called
|
||||
// with script paths that have been verified to exist.
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&buf, script_path);
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(allocator, &buf, script_path);
|
||||
buf.appendAssumeCapacity('"');
|
||||
|
||||
for (script_args) |arg| {
|
||||
@@ -17186,7 +17186,7 @@ fn argvToScriptCommandLineWindows(
|
||||
}
|
||||
|
||||
// Separate args with a space.
|
||||
try buf.append(' ');
|
||||
try buf.append(allocator, ' ');
|
||||
|
||||
// Need to quote if the argument is empty (otherwise the arg would just be lost)
|
||||
// or if the last character is a `\`, since then something like "%~2" in a .bat
|
||||
@@ -17206,7 +17206,7 @@ fn argvToScriptCommandLineWindows(
|
||||
}
|
||||
}
|
||||
if (needs_quotes) {
|
||||
try buf.append('"');
|
||||
try buf.append(allocator, '"');
|
||||
}
|
||||
var backslashes: usize = 0;
|
||||
for (arg) |c| {
|
||||
@@ -17215,8 +17215,8 @@ fn argvToScriptCommandLineWindows(
|
||||
backslashes += 1;
|
||||
},
|
||||
'"' => {
|
||||
try buf.appendNTimes('\\', backslashes);
|
||||
try buf.append('"');
|
||||
try buf.appendNTimes(allocator, '\\', backslashes);
|
||||
try buf.append(allocator, '"');
|
||||
backslashes = 0;
|
||||
},
|
||||
// Replace `%` with `%%cd:~,%`.
|
||||
@@ -17233,22 +17233,22 @@ fn argvToScriptCommandLineWindows(
|
||||
// we'll still be left with `%foo%` (the literal string).
|
||||
'%' => {
|
||||
// the trailing `%` is appended outside the switch
|
||||
try buf.appendSlice("%%cd:~,");
|
||||
try buf.appendSlice(allocator, "%%cd:~,");
|
||||
backslashes = 0;
|
||||
},
|
||||
else => {
|
||||
backslashes = 0;
|
||||
},
|
||||
}
|
||||
try buf.append(c);
|
||||
try buf.append(allocator, c);
|
||||
}
|
||||
if (needs_quotes) {
|
||||
try buf.appendNTimes('\\', backslashes);
|
||||
try buf.append('"');
|
||||
try buf.appendNTimes(allocator, '\\', backslashes);
|
||||
try buf.append(allocator, '"');
|
||||
}
|
||||
}
|
||||
|
||||
try buf.append('"');
|
||||
try buf.append(allocator, '"');
|
||||
|
||||
return try std.unicode.wtf8ToWtf16LeAllocZ(allocator, buf.items);
|
||||
}
|
||||
@@ -17266,8 +17266,8 @@ fn argvToCommandLineWindows(
|
||||
allocator: Allocator,
|
||||
argv: []const []const u8,
|
||||
) ArgvToCommandLineError![:0]u16 {
|
||||
var buf = std.array_list.Managed(u8).init(allocator);
|
||||
defer buf.deinit();
|
||||
var buf: std.ArrayList(u8) = .empty;
|
||||
defer buf.deinit(allocator);
|
||||
|
||||
if (argv.len != 0) {
|
||||
const arg0 = argv[0];
|
||||
@@ -17293,15 +17293,15 @@ fn argvToCommandLineWindows(
|
||||
}
|
||||
}
|
||||
if (needs_quotes) {
|
||||
try buf.append('"');
|
||||
try buf.appendSlice(arg0);
|
||||
try buf.append('"');
|
||||
try buf.append(allocator, '"');
|
||||
try buf.appendSlice(allocator, arg0);
|
||||
try buf.append(allocator, '"');
|
||||
} else {
|
||||
try buf.appendSlice(arg0);
|
||||
try buf.appendSlice(allocator, arg0);
|
||||
}
|
||||
|
||||
for (argv[1..]) |arg| {
|
||||
try buf.append(' ');
|
||||
try buf.append(allocator, ' ');
|
||||
|
||||
// Subsequent arguments must be quoted if they contain spaces, tabs or double quotes,
|
||||
// or if they are empty. For simplicity and for maximum compatibility with different
|
||||
@@ -17313,11 +17313,11 @@ fn argvToCommandLineWindows(
|
||||
}
|
||||
} else arg.len == 0;
|
||||
if (!needs_quotes) {
|
||||
try buf.appendSlice(arg);
|
||||
try buf.appendSlice(allocator, arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
try buf.append('"');
|
||||
try buf.append(allocator, '"');
|
||||
var backslash_count: usize = 0;
|
||||
for (arg) |byte| {
|
||||
switch (byte) {
|
||||
@@ -17325,19 +17325,19 @@ fn argvToCommandLineWindows(
|
||||
backslash_count += 1;
|
||||
},
|
||||
'"' => {
|
||||
try buf.appendNTimes('\\', backslash_count * 2 + 1);
|
||||
try buf.append('"');
|
||||
try buf.appendNTimes(allocator, '\\', backslash_count * 2 + 1);
|
||||
try buf.append(allocator, '"');
|
||||
backslash_count = 0;
|
||||
},
|
||||
else => {
|
||||
try buf.appendNTimes('\\', backslash_count);
|
||||
try buf.append(byte);
|
||||
try buf.appendNTimes(allocator, '\\', backslash_count);
|
||||
try buf.append(allocator, byte);
|
||||
backslash_count = 0;
|
||||
},
|
||||
}
|
||||
}
|
||||
try buf.appendNTimes('\\', backslash_count * 2);
|
||||
try buf.append('"');
|
||||
try buf.appendNTimes(allocator, '\\', backslash_count * 2);
|
||||
try buf.append(allocator, '"');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+76
-75
@@ -948,7 +948,8 @@ test fmtUtf8 {
|
||||
}
|
||||
|
||||
fn utf16LeToUtf8ArrayListImpl(
|
||||
result: *std.array_list.Managed(u8),
|
||||
allocator: Allocator,
|
||||
result: *std.ArrayList(u8),
|
||||
utf16le: []const u16,
|
||||
comptime surrogates: Surrogates,
|
||||
) (switch (surrogates) {
|
||||
@@ -984,14 +985,14 @@ fn utf16LeToUtf8ArrayListImpl(
|
||||
var it = Utf16LeIterator.init(remaining);
|
||||
while (try it.nextCodepoint()) |codepoint| {
|
||||
const utf8_len = utf8CodepointSequenceLength(codepoint) catch unreachable;
|
||||
assert((utf8Encode(codepoint, try result.addManyAsSlice(utf8_len)) catch unreachable) == utf8_len);
|
||||
assert((utf8Encode(codepoint, try result.addManyAsSlice(allocator, utf8_len)) catch unreachable) == utf8_len);
|
||||
}
|
||||
},
|
||||
.can_encode_surrogate_half => {
|
||||
var it = Wtf16LeIterator.init(remaining);
|
||||
while (it.nextCodepoint()) |codepoint| {
|
||||
const utf8_len = utf8CodepointSequenceLength(codepoint) catch unreachable;
|
||||
assert((wtf8Encode(codepoint, try result.addManyAsSlice(utf8_len)) catch unreachable) == utf8_len);
|
||||
assert((wtf8Encode(codepoint, try result.addManyAsSlice(allocator, utf8_len)) catch unreachable) == utf8_len);
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -999,29 +1000,29 @@ fn utf16LeToUtf8ArrayListImpl(
|
||||
|
||||
pub const Utf16LeToUtf8AllocError = Allocator.Error || Utf16LeToUtf8Error;
|
||||
|
||||
pub fn utf16LeToUtf8ArrayList(result: *std.array_list.Managed(u8), utf16le: []const u16) Utf16LeToUtf8AllocError!void {
|
||||
try result.ensureUnusedCapacity(utf16le.len);
|
||||
return utf16LeToUtf8ArrayListImpl(result, utf16le, .cannot_encode_surrogate_half);
|
||||
pub fn utf16LeToUtf8ArrayList(allocator: Allocator, result: *std.ArrayList(u8), utf16le: []const u16) Utf16LeToUtf8AllocError!void {
|
||||
try result.ensureUnusedCapacity(allocator, utf16le.len);
|
||||
return utf16LeToUtf8ArrayListImpl(allocator, result, utf16le, .cannot_encode_surrogate_half);
|
||||
}
|
||||
|
||||
/// Caller owns returned memory.
|
||||
pub fn utf16LeToUtf8Alloc(allocator: Allocator, utf16le: []const u16) Utf16LeToUtf8AllocError![]u8 {
|
||||
// optimistically guess that it will all be ascii.
|
||||
var result = try std.array_list.Managed(u8).initCapacity(allocator, utf16le.len);
|
||||
errdefer result.deinit();
|
||||
var result = try std.ArrayList(u8).initCapacity(allocator, utf16le.len);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf16LeToUtf8ArrayListImpl(&result, utf16le, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSlice();
|
||||
try utf16LeToUtf8ArrayListImpl(allocator, &result, utf16le, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSlice(allocator);
|
||||
}
|
||||
|
||||
/// Caller owns returned memory.
|
||||
pub fn utf16LeToUtf8AllocZ(allocator: Allocator, utf16le: []const u16) Utf16LeToUtf8AllocError![:0]u8 {
|
||||
// optimistically guess that it will all be ascii (and allocate space for the null terminator)
|
||||
var result = try std.array_list.Managed(u8).initCapacity(allocator, utf16le.len + 1);
|
||||
errdefer result.deinit();
|
||||
var result = try std.ArrayList(u8).initCapacity(allocator, utf16le.len + 1);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf16LeToUtf8ArrayListImpl(&result, utf16le, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(0);
|
||||
try utf16LeToUtf8ArrayListImpl(allocator, &result, utf16le, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(allocator, 0);
|
||||
}
|
||||
|
||||
pub const Utf16LeToUtf8Error = Utf16LeIterator.NextCodepointError;
|
||||
@@ -1149,7 +1150,7 @@ test utf16LeToUtf8 {
|
||||
}
|
||||
}
|
||||
|
||||
fn utf8ToUtf16LeArrayListImpl(result: *std.array_list.Managed(u16), utf8: []const u8, comptime surrogates: Surrogates) !void {
|
||||
fn utf8ToUtf16LeArrayListImpl(allocator: Allocator, result: *std.ArrayList(u16), utf8: []const u8, comptime surrogates: Surrogates) !void {
|
||||
assert(result.unusedCapacitySlice().len >= utf8.len);
|
||||
|
||||
var remaining = utf8;
|
||||
@@ -1178,36 +1179,36 @@ fn utf8ToUtf16LeArrayListImpl(result: *std.array_list.Managed(u16), utf8: []cons
|
||||
var it = view.iterator();
|
||||
while (it.nextCodepoint()) |codepoint| {
|
||||
if (codepoint < 0x10000) {
|
||||
try result.append(mem.nativeToLittle(u16, @intCast(codepoint)));
|
||||
try result.append(allocator, mem.nativeToLittle(u16, @intCast(codepoint)));
|
||||
} else {
|
||||
const high = @as(u16, @intCast((codepoint - 0x10000) >> 10)) + 0xD800;
|
||||
const low = @as(u16, @intCast(codepoint & 0x3FF)) + 0xDC00;
|
||||
try result.appendSlice(&.{ mem.nativeToLittle(u16, high), mem.nativeToLittle(u16, low) });
|
||||
try result.appendSlice(allocator, &.{ mem.nativeToLittle(u16, high), mem.nativeToLittle(u16, low) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn utf8ToUtf16LeArrayList(result: *std.array_list.Managed(u16), utf8: []const u8) error{ InvalidUtf8, OutOfMemory }!void {
|
||||
try result.ensureUnusedCapacity(utf8.len);
|
||||
return utf8ToUtf16LeArrayListImpl(result, utf8, .cannot_encode_surrogate_half);
|
||||
pub fn utf8ToUtf16LeArrayList(allocator: Allocator, result: *std.ArrayList(u16), utf8: []const u8) error{ InvalidUtf8, OutOfMemory }!void {
|
||||
try result.ensureUnusedCapacity(allocator, utf8.len);
|
||||
return utf8ToUtf16LeArrayListImpl(allocator, result, utf8, .cannot_encode_surrogate_half);
|
||||
}
|
||||
|
||||
pub fn utf8ToUtf16LeAlloc(allocator: Allocator, utf8: []const u8) error{ InvalidUtf8, OutOfMemory }![]u16 {
|
||||
// optimistically guess that it will not require surrogate pairs
|
||||
var result = try std.array_list.Managed(u16).initCapacity(allocator, utf8.len);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u16) = try .initCapacity(allocator, utf8.len);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf8ToUtf16LeArrayListImpl(&result, utf8, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSlice();
|
||||
try utf8ToUtf16LeArrayListImpl(allocator, &result, utf8, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSlice(allocator);
|
||||
}
|
||||
|
||||
pub fn utf8ToUtf16LeAllocZ(allocator: Allocator, utf8: []const u8) error{ InvalidUtf8, OutOfMemory }![:0]u16 {
|
||||
// optimistically guess that it will not require surrogate pairs
|
||||
var result = try std.array_list.Managed(u16).initCapacity(allocator, utf8.len + 1);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u16) = try .initCapacity(allocator, utf8.len + 1);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf8ToUtf16LeArrayListImpl(&result, utf8, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(0);
|
||||
try utf8ToUtf16LeArrayListImpl(allocator, &result, utf8, .cannot_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(allocator, 0);
|
||||
}
|
||||
|
||||
/// Returns index of next character. If exact fit, returned index equals output slice length.
|
||||
@@ -1292,21 +1293,21 @@ test utf8ToUtf16Le {
|
||||
|
||||
test utf8ToUtf16LeArrayList {
|
||||
{
|
||||
var list = std.array_list.Managed(u16).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
try utf8ToUtf16LeArrayList(&list, "𐐷");
|
||||
var list: std.ArrayList(u16) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
try utf8ToUtf16LeArrayList(std.testing.allocator, &list, "𐐷");
|
||||
try testing.expectEqualSlices(u8, "\x01\xd8\x37\xdc", mem.sliceAsBytes(list.items));
|
||||
}
|
||||
{
|
||||
var list = std.array_list.Managed(u16).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
try utf8ToUtf16LeArrayList(&list, "\u{10FFFF}");
|
||||
var list: std.ArrayList(u16) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
try utf8ToUtf16LeArrayList(std.testing.allocator, &list, "\u{10FFFF}");
|
||||
try testing.expectEqualSlices(u8, "\xff\xdb\xff\xdf", mem.sliceAsBytes(list.items));
|
||||
}
|
||||
{
|
||||
var list = std.array_list.Managed(u16).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
const result = utf8ToUtf16LeArrayList(&list, "\xf4\x90\x80\x80");
|
||||
var list: std.ArrayList(u16) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
const result = utf8ToUtf16LeArrayList(std.testing.allocator, &list, "\xf4\x90\x80\x80");
|
||||
try testing.expectError(error.InvalidUtf8, result);
|
||||
}
|
||||
}
|
||||
@@ -1367,56 +1368,56 @@ test utf8ToUtf16LeAllocZ {
|
||||
test "ArrayList functions on a re-used list" {
|
||||
// utf8ToUtf16LeArrayList
|
||||
{
|
||||
var list = std.array_list.Managed(u16).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
var list: std.ArrayList(u16) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
|
||||
const init_slice = utf8ToUtf16LeStringLiteral("abcdefg");
|
||||
try list.ensureTotalCapacityPrecise(init_slice.len);
|
||||
try list.ensureTotalCapacityPrecise(std.testing.allocator, init_slice.len);
|
||||
list.appendSliceAssumeCapacity(init_slice);
|
||||
|
||||
try utf8ToUtf16LeArrayList(&list, "hijklmnopqrstuvwyxz");
|
||||
try utf8ToUtf16LeArrayList(std.testing.allocator, &list, "hijklmnopqrstuvwyxz");
|
||||
|
||||
try testing.expectEqualSlices(u16, utf8ToUtf16LeStringLiteral("abcdefghijklmnopqrstuvwyxz"), list.items);
|
||||
}
|
||||
|
||||
// utf16LeToUtf8ArrayList
|
||||
{
|
||||
var list = std.array_list.Managed(u8).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
var list: std.ArrayList(u8) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
|
||||
const init_slice = "abcdefg";
|
||||
try list.ensureTotalCapacityPrecise(init_slice.len);
|
||||
try list.ensureTotalCapacityPrecise(std.testing.allocator, init_slice.len);
|
||||
list.appendSliceAssumeCapacity(init_slice);
|
||||
|
||||
try utf16LeToUtf8ArrayList(&list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz"));
|
||||
try utf16LeToUtf8ArrayList(std.testing.allocator, &list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz"));
|
||||
|
||||
try testing.expectEqualStrings("abcdefghijklmnopqrstuvwyxz", list.items);
|
||||
}
|
||||
|
||||
// wtf8ToWtf16LeArrayList
|
||||
{
|
||||
var list = std.array_list.Managed(u16).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
var list: std.ArrayList(u16) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
|
||||
const init_slice = utf8ToUtf16LeStringLiteral("abcdefg");
|
||||
try list.ensureTotalCapacityPrecise(init_slice.len);
|
||||
try list.ensureTotalCapacityPrecise(std.testing.allocator, init_slice.len);
|
||||
list.appendSliceAssumeCapacity(init_slice);
|
||||
|
||||
try wtf8ToWtf16LeArrayList(&list, "hijklmnopqrstuvwyxz");
|
||||
try wtf8ToWtf16LeArrayList(std.testing.allocator, &list, "hijklmnopqrstuvwyxz");
|
||||
|
||||
try testing.expectEqualSlices(u16, utf8ToUtf16LeStringLiteral("abcdefghijklmnopqrstuvwyxz"), list.items);
|
||||
}
|
||||
|
||||
// wtf16LeToWtf8ArrayList
|
||||
{
|
||||
var list = std.array_list.Managed(u8).init(testing.allocator);
|
||||
defer list.deinit();
|
||||
var list: std.ArrayList(u8) = .empty;
|
||||
defer list.deinit(std.testing.allocator);
|
||||
|
||||
const init_slice = "abcdefg";
|
||||
try list.ensureTotalCapacityPrecise(init_slice.len);
|
||||
try list.ensureTotalCapacityPrecise(std.testing.allocator, init_slice.len);
|
||||
list.appendSliceAssumeCapacity(init_slice);
|
||||
|
||||
try wtf16LeToWtf8ArrayList(&list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz"));
|
||||
try wtf16LeToWtf8ArrayList(std.testing.allocator, &list, utf8ToUtf16LeStringLiteral("hijklmnopqrstuvwyxz"));
|
||||
|
||||
try testing.expectEqualStrings("abcdefghijklmnopqrstuvwyxz", list.items);
|
||||
}
|
||||
@@ -1795,56 +1796,56 @@ pub const Wtf8Iterator = struct {
|
||||
}
|
||||
};
|
||||
|
||||
pub fn wtf16LeToWtf8ArrayList(result: *std.array_list.Managed(u8), utf16le: []const u16) Allocator.Error!void {
|
||||
try result.ensureUnusedCapacity(utf16le.len);
|
||||
return utf16LeToUtf8ArrayListImpl(result, utf16le, .can_encode_surrogate_half);
|
||||
pub fn wtf16LeToWtf8ArrayList(allocator: Allocator, result: *std.ArrayList(u8), utf16le: []const u16) Allocator.Error!void {
|
||||
try result.ensureUnusedCapacity(allocator, utf16le.len);
|
||||
return utf16LeToUtf8ArrayListImpl(allocator, result, utf16le, .can_encode_surrogate_half);
|
||||
}
|
||||
|
||||
/// Caller must free returned memory.
|
||||
pub fn wtf16LeToWtf8Alloc(allocator: Allocator, wtf16le: []const u16) Allocator.Error![]u8 {
|
||||
// optimistically guess that it will all be ascii.
|
||||
var result = try std.array_list.Managed(u8).initCapacity(allocator, wtf16le.len);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u8) = try .initCapacity(allocator, wtf16le.len);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf16LeToUtf8ArrayListImpl(&result, wtf16le, .can_encode_surrogate_half);
|
||||
return result.toOwnedSlice();
|
||||
try utf16LeToUtf8ArrayListImpl(allocator, &result, wtf16le, .can_encode_surrogate_half);
|
||||
return result.toOwnedSlice(allocator);
|
||||
}
|
||||
|
||||
/// Caller must free returned memory.
|
||||
pub fn wtf16LeToWtf8AllocZ(allocator: Allocator, wtf16le: []const u16) Allocator.Error![:0]u8 {
|
||||
// optimistically guess that it will all be ascii (and allocate space for the null terminator)
|
||||
var result = try std.array_list.Managed(u8).initCapacity(allocator, wtf16le.len + 1);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u8) = try .initCapacity(allocator, wtf16le.len + 1);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf16LeToUtf8ArrayListImpl(&result, wtf16le, .can_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(0);
|
||||
try utf16LeToUtf8ArrayListImpl(allocator, &result, wtf16le, .can_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(allocator, 0);
|
||||
}
|
||||
|
||||
pub fn wtf16LeToWtf8(wtf8: []u8, wtf16le: []const u16) usize {
|
||||
return utf16LeToUtf8Impl(wtf8, wtf16le, .can_encode_surrogate_half) catch |err| switch (err) {};
|
||||
}
|
||||
|
||||
pub fn wtf8ToWtf16LeArrayList(result: *std.array_list.Managed(u16), wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }!void {
|
||||
try result.ensureUnusedCapacity(wtf8.len);
|
||||
return utf8ToUtf16LeArrayListImpl(result, wtf8, .can_encode_surrogate_half);
|
||||
pub fn wtf8ToWtf16LeArrayList(allocator: Allocator, result: *std.ArrayList(u16), wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }!void {
|
||||
try result.ensureUnusedCapacity(allocator, wtf8.len);
|
||||
return utf8ToUtf16LeArrayListImpl(allocator, result, wtf8, .can_encode_surrogate_half);
|
||||
}
|
||||
|
||||
pub fn wtf8ToWtf16LeAlloc(allocator: Allocator, wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }![]u16 {
|
||||
// optimistically guess that it will not require surrogate pairs
|
||||
var result = try std.array_list.Managed(u16).initCapacity(allocator, wtf8.len);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u16) = try .initCapacity(allocator, wtf8.len);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf8ToUtf16LeArrayListImpl(&result, wtf8, .can_encode_surrogate_half);
|
||||
return result.toOwnedSlice();
|
||||
try utf8ToUtf16LeArrayListImpl(allocator, &result, wtf8, .can_encode_surrogate_half);
|
||||
return result.toOwnedSlice(allocator);
|
||||
}
|
||||
|
||||
pub fn wtf8ToWtf16LeAllocZ(allocator: Allocator, wtf8: []const u8) error{ InvalidWtf8, OutOfMemory }![:0]u16 {
|
||||
// optimistically guess that it will not require surrogate pairs
|
||||
var result = try std.array_list.Managed(u16).initCapacity(allocator, wtf8.len + 1);
|
||||
errdefer result.deinit();
|
||||
var result: std.ArrayList(u16) = try .initCapacity(allocator, wtf8.len + 1);
|
||||
errdefer result.deinit(allocator);
|
||||
|
||||
try utf8ToUtf16LeArrayListImpl(&result, wtf8, .can_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(0);
|
||||
try utf8ToUtf16LeArrayListImpl(allocator, &result, wtf8, .can_encode_surrogate_half);
|
||||
return result.toOwnedSliceSentinel(allocator, 0);
|
||||
}
|
||||
|
||||
/// Returns index of next character. If exact fit, returned index equals output slice length.
|
||||
|
||||
@@ -601,13 +601,13 @@ pub const Installation = struct {
|
||||
return error.VersionTooLong;
|
||||
}
|
||||
|
||||
var version: std.array_list.Managed(u8) = try .initCapacity(gpa, version_without_0.len + 2);
|
||||
errdefer version.deinit();
|
||||
var version: std.ArrayList(u8) = try .initCapacity(gpa, version_without_0.len + 2);
|
||||
errdefer version.deinit(gpa);
|
||||
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&version, version_without_0);
|
||||
try version.appendSlice(".0");
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(gpa, &version, version_without_0);
|
||||
try version.appendSlice(gpa, ".0");
|
||||
|
||||
break :version try version.toOwnedSlice();
|
||||
break :version try version.toOwnedSlice(gpa);
|
||||
};
|
||||
errdefer gpa.free(version);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ fn testArgv(expected_args: []const [*:0]const u16) !void {
|
||||
.vector = std.os.windows.peb().ProcessParameters.CommandLine.slice(),
|
||||
};
|
||||
const args = try raw_args.toSlice(allocator);
|
||||
var wtf8_buf = std.array_list.Managed(u8).init(allocator);
|
||||
var wtf8_buf: std.ArrayList(u8) = .empty;
|
||||
|
||||
var eql = true;
|
||||
if (args.len != expected_args.len) eql = false;
|
||||
@@ -28,7 +28,7 @@ fn testArgv(expected_args: []const [*:0]const u16) !void {
|
||||
const min_len = @min(expected_args.len, args.len);
|
||||
for (expected_args[0..min_len], args[0..min_len], 0..) |expected_arg, arg_wtf8, i| {
|
||||
wtf8_buf.clearRetainingCapacity();
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(expected_arg));
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(allocator, &wtf8_buf, std.mem.span(expected_arg));
|
||||
if (!std.mem.eql(u8, wtf8_buf.items, arg_wtf8)) {
|
||||
std.debug.print("{}: expected: \"{f}\"\n", .{ i, std.zig.fmtString(wtf8_buf.items) });
|
||||
std.debug.print("{}: actual: \"{f}\"\n", .{ i, std.zig.fmtString(arg_wtf8) });
|
||||
@@ -38,7 +38,7 @@ fn testArgv(expected_args: []const [*:0]const u16) !void {
|
||||
if (!eql) {
|
||||
for (expected_args[min_len..], min_len..) |arg, i| {
|
||||
wtf8_buf.clearRetainingCapacity();
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(arg));
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(allocator, &wtf8_buf, std.mem.span(arg));
|
||||
std.debug.print("{}: expected: \"{f}\"\n", .{ i, std.zig.fmtString(wtf8_buf.items) });
|
||||
}
|
||||
for (args[min_len..], min_len..) |arg, i| {
|
||||
@@ -47,13 +47,13 @@ fn testArgv(expected_args: []const [*:0]const u16) !void {
|
||||
const peb = std.os.windows.peb();
|
||||
const lpCmdLine: [*:0]u16 = @ptrCast(peb.ProcessParameters.CommandLine.Buffer);
|
||||
wtf8_buf.clearRetainingCapacity();
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(lpCmdLine));
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(allocator, &wtf8_buf, std.mem.span(lpCmdLine));
|
||||
std.debug.print("command line: \"{f}\"\n", .{std.zig.fmtString(wtf8_buf.items)});
|
||||
std.debug.print("expected argv:\n", .{});
|
||||
std.debug.print("&.{{\n", .{});
|
||||
for (expected_args) |arg| {
|
||||
wtf8_buf.clearRetainingCapacity();
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(&wtf8_buf, std.mem.span(arg));
|
||||
try std.unicode.wtf16LeToWtf8ArrayList(allocator, &wtf8_buf, std.mem.span(arg));
|
||||
std.debug.print(" \"{f}\",\n", .{std.zig.fmtString(wtf8_buf.items)});
|
||||
}
|
||||
std.debug.print("}}\n", .{});
|
||||
|
||||
Reference in new issue
Block a user