From 7877b647626333a6cedd0a57efb2efac8adf63f3 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Fri, 12 Jan 2024 21:07:49 +0100 Subject: [PATCH 01/16] rewrote parser --- lib/std/fmt.zig | 83 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index df6aac2e219b..5fd206c88403 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -23,7 +23,7 @@ pub const FormatOptions = struct { precision: ?usize = null, width: ?usize = null, alignment: Alignment = .right, - fill: u8 = ' ', + fill: u21 = ' ', }; /// Renders fmt string with args, calling `writer` with slices of bytes. @@ -211,14 +211,19 @@ fn cacheString(str: anytype) []const u8 { pub const Placeholder = struct { specifier_arg: []const u8, - fill: u8, + fill: u21, alignment: Alignment, arg: Specifier, width: Specifier, precision: Specifier, pub fn parse(comptime str: anytype) Placeholder { - comptime var parser = Parser{ .buf = &str }; + comptime var view = std.unicode.Utf8View.init(&str) catch |err| + @compileError(@errorName(err)); + comptime var parser = Parser{ + .buf = &str, + .iter = view.iterator(), + }; // Parse the positional argument number const arg = comptime parser.specifier() catch |err| @@ -297,21 +302,23 @@ pub const Specifier = union(enum) { pub const Parser = struct { buf: []const u8, pos: usize = 0, + iter: std.unicode.Utf8Iterator = undefined, // Returns a decimal number or null if the current character is not a // digit pub fn number(self: *@This()) ?usize { var r: ?usize = null; - while (self.pos < self.buf.len) : (self.pos += 1) { - switch (self.buf[self.pos]) { + while (self.peek(0)) |code_point| { + switch (code_point) { '0'...'9' => { if (r == null) r = 0; r.? *= 10; - r.? += self.buf[self.pos] - '0'; + r.? += code_point - '0'; }, else => break, } + _ = self.iter.nextCodepoint(); } return r; @@ -319,31 +326,27 @@ pub const Parser = struct { // Returns a substring of the input starting from the current position // and ending where `ch` is found or until the end if not found - pub fn until(self: *@This(), ch: u8) []const u8 { - const start = self.pos; - - if (start >= self.buf.len) - return &[_]u8{}; - - while (self.pos < self.buf.len) : (self.pos += 1) { - if (self.buf[self.pos] == ch) break; + pub fn until(self: *@This(), ch: u21) []const u8 { + var result: []const u8 = &[_]u8{}; + while (self.peek(0)) |code_point| { + if (code_point == ch) + break; + result = result ++ (self.iter.nextCodepointSlice() orelse &[_]u8{}); } - return self.buf[start..self.pos]; + return result; } // Returns one character, if available - pub fn char(self: *@This()) ?u8 { - if (self.pos < self.buf.len) { - const ch = self.buf[self.pos]; - self.pos += 1; - return ch; + pub fn char(self: *@This()) ?u21 { + if (self.iter.nextCodepoint()) |code_point| { + return code_point; } return null; } - pub fn maybe(self: *@This(), val: u8) bool { - if (self.pos < self.buf.len and self.buf[self.pos] == val) { - self.pos += 1; + pub fn maybe(self: *@This(), val: u21) bool { + if (self.peek(0) == val) { + self.iter.nextCodepoint(); return true; } return false; @@ -367,11 +370,26 @@ pub const Parser = struct { } // Returns the n-th next character or null if that's past the end - pub fn peek(self: *@This(), n: usize) ?u8 { - return if (self.pos + n < self.buf.len) self.buf[self.pos + n] else null; + pub fn peek(self: *@This(), n: usize) ?u21 { + const original_i = self.iter.i; + defer self.iter.i = original_i; + + var i = 0; + var code_point: ?u21 = null; + while (i <= n) : (i += 1) { + code_point = self.iter.nextCodepoint(); + if (code_point == null) return null; + } + return code_point; } }; +fn utf(char: u21) ![]const u8 { + var bytes: [4]u8 = undefined; + const len: u3 = try std.unicode.utf8Encode(char, &bytes); + return bytes[0..len]; +} + pub const ArgSetType = u32; const max_format_args = @typeInfo(ArgSetType).Int.bits; @@ -972,6 +990,13 @@ pub fn formatUnicodeCodepoint( return formatBuf(buf[0..len], options, writer); } +fn writeBytesNTimes(writer: anytype, bytes: []const u8, n: usize) !void { + var i: usize = 0; + while (i < n) : (i += 1) { + try writer.writeAll(bytes); + } +} + pub fn formatBuf( buf: []const u8, options: FormatOptions, @@ -988,17 +1013,17 @@ pub fn formatBuf( switch (options.alignment) { .left => { try writer.writeAll(buf); - try writer.writeByteNTimes(options.fill, padding); + try writeBytesNTimes(writer, (utf(options.fill) catch " "), padding); }, .center => { const left_padding = padding / 2; const right_padding = (padding + 1) / 2; - try writer.writeByteNTimes(options.fill, left_padding); + try writeBytesNTimes(writer, (utf(options.fill) catch " "), left_padding); try writer.writeAll(buf); - try writer.writeByteNTimes(options.fill, right_padding); + try writeBytesNTimes(writer, (utf(options.fill) catch " "), right_padding); }, .right => { - try writer.writeByteNTimes(options.fill, padding); + try writeBytesNTimes(writer, (utf(options.fill) catch " "), padding); try writer.writeAll(buf); }, } From c0fa3c56b0b2984ea93cfc70511bbc37a90f9458 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Fri, 12 Jan 2024 21:44:30 +0100 Subject: [PATCH 02/16] fixed ignored value --- lib/std/fmt.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 5fd206c88403..099def0cb321 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -346,7 +346,7 @@ pub const Parser = struct { pub fn maybe(self: *@This(), val: u21) bool { if (self.peek(0) == val) { - self.iter.nextCodepoint(); + _ = self.iter.nextCodepoint(); return true; } return false; From 84fd563b52131cfd5fa49418485d8dfd72dfce5e Mon Sep 17 00:00:00 2001 From: vinnichase Date: Fri, 12 Jan 2024 21:04:48 -0800 Subject: [PATCH 03/16] never mutated -> made const, removed catch for compiler error order Co-authored-by: Jacob Young --- lib/std/fmt.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 099def0cb321..f768dd310e3f 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -218,8 +218,7 @@ pub const Placeholder = struct { precision: Specifier, pub fn parse(comptime str: anytype) Placeholder { - comptime var view = std.unicode.Utf8View.init(&str) catch |err| - @compileError(@errorName(err)); + const view = std.unicode.Utf8View.initComptime(&str); comptime var parser = Parser{ .buf = &str, .iter = view.iterator(), From d279fe9534690a2da5999065e8e33976316ae69e Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 06:20:34 +0100 Subject: [PATCH 04/16] removed utf function and inlined utf encode --- lib/std/fmt.zig | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index f768dd310e3f..42a33ecd1155 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -383,12 +383,6 @@ pub const Parser = struct { } }; -fn utf(char: u21) ![]const u8 { - var bytes: [4]u8 = undefined; - const len: u3 = try std.unicode.utf8Encode(char, &bytes); - return bytes[0..len]; -} - pub const ArgSetType = u32; const max_format_args = @typeInfo(ArgSetType).Int.bits; @@ -1001,6 +995,11 @@ pub fn formatBuf( options: FormatOptions, writer: anytype, ) !void { + var fill_buffer: [4]u8 = undefined; + const fill_utf8 = if (std.unicode.utf8Encode(options.fill, &fill_buffer)) |len| + fill_buffer[0..len] + else |_| + " "; if (options.width) |min_width| { // In case of error assume the buffer content is ASCII-encoded const width = unicode.utf8CountCodepoints(buf) catch buf.len; @@ -1012,17 +1011,17 @@ pub fn formatBuf( switch (options.alignment) { .left => { try writer.writeAll(buf); - try writeBytesNTimes(writer, (utf(options.fill) catch " "), padding); + try writeBytesNTimes(writer, fill_utf8, padding); }, .center => { const left_padding = padding / 2; const right_padding = (padding + 1) / 2; - try writeBytesNTimes(writer, (utf(options.fill) catch " "), left_padding); + try writeBytesNTimes(writer, fill_utf8, left_padding); try writer.writeAll(buf); - try writeBytesNTimes(writer, (utf(options.fill) catch " "), right_padding); + try writeBytesNTimes(writer, fill_utf8, right_padding); }, .right => { - try writeBytesNTimes(writer, (utf(options.fill) catch " "), padding); + try writeBytesNTimes(writer, fill_utf8, padding); try writer.writeAll(buf); }, } From 6fea477ebf800c6241fe9f120b930cff64c55bfb Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 06:58:38 +0100 Subject: [PATCH 05/16] moved writeBytesNTimes to io.writer --- lib/std/fmt.zig | 15 ++++----------- lib/std/io/writer.zig | 7 +++++++ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 42a33ecd1155..ce5be08648d2 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -983,13 +983,6 @@ pub fn formatUnicodeCodepoint( return formatBuf(buf[0..len], options, writer); } -fn writeBytesNTimes(writer: anytype, bytes: []const u8, n: usize) !void { - var i: usize = 0; - while (i < n) : (i += 1) { - try writer.writeAll(bytes); - } -} - pub fn formatBuf( buf: []const u8, options: FormatOptions, @@ -1011,17 +1004,17 @@ pub fn formatBuf( switch (options.alignment) { .left => { try writer.writeAll(buf); - try writeBytesNTimes(writer, fill_utf8, padding); + try writer.writeBytesNTimes(writer, fill_utf8, padding); }, .center => { const left_padding = padding / 2; const right_padding = (padding + 1) / 2; - try writeBytesNTimes(writer, fill_utf8, left_padding); + try writer.writeBytesNTimes(writer, fill_utf8, left_padding); try writer.writeAll(buf); - try writeBytesNTimes(writer, fill_utf8, right_padding); + try writer.writeBytesNTimes(writer, fill_utf8, right_padding); }, .right => { - try writeBytesNTimes(writer, fill_utf8, padding); + try writer.writeBytesNTimes(writer, fill_utf8, padding); try writer.writeAll(buf); }, } diff --git a/lib/std/io/writer.zig b/lib/std/io/writer.zig index f1c0efda9037..5fe06120cda2 100644 --- a/lib/std/io/writer.zig +++ b/lib/std/io/writer.zig @@ -45,6 +45,13 @@ pub fn Writer( } } + pub fn writeBytesNTimes(self: Self, bytes: []const u8, n: usize) Error!void { + var i: usize = 0; + while (i < n) : (i += 1) { + try self.writeAll(bytes); + } + } + pub inline fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void { var bytes: [@divExact(@typeInfo(T).Int.bits, 8)]u8 = undefined; mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian); From 1df3df823d275a12f4eb30487f24713ee9d43e09 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 06:59:24 +0100 Subject: [PATCH 06/16] added test for padding fill char utf --- lib/std/fmt.zig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index ce5be08648d2..6654dafa8869 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -2809,6 +2809,15 @@ test "padding" { try expectFmt("a====", "{c:=<5}", .{'a'}); } +test "padding fill char utf" { + try expectFmt("──crêpe───", "{s:─^10}", .{"crêpe"}); + try expectFmt("─────crêpe", "{s:─>10}", .{"crêpe"}); + try expectFmt("crêpe─────", "{s:─<10}", .{"crêpe"}); + try expectFmt("────a", "{c:─>5}", .{'a'}); + try expectFmt("──a──", "{c:─^5}", .{'a'}); + try expectFmt("a────", "{c:─<5}", .{'a'}); +} + test "decimal float padding" { const number: f32 = 3.1415; try expectFmt("left-pad: **3.141\n", "left-pad: {d:*>7.3}\n", .{number}); From 28a9b457b284093294e75e0aecdd852d9c8993cc Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 07:11:01 +0100 Subject: [PATCH 07/16] moved writeBytesNTimes to io.writer: fixed arguments --- lib/std/fmt.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 6654dafa8869..c5844477e870 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1004,17 +1004,17 @@ pub fn formatBuf( switch (options.alignment) { .left => { try writer.writeAll(buf); - try writer.writeBytesNTimes(writer, fill_utf8, padding); + try writer.writeBytesNTimes(fill_utf8, padding); }, .center => { const left_padding = padding / 2; const right_padding = (padding + 1) / 2; - try writer.writeBytesNTimes(writer, fill_utf8, left_padding); + try writer.writeBytesNTimes(fill_utf8, left_padding); try writer.writeAll(buf); - try writer.writeBytesNTimes(writer, fill_utf8, right_padding); + try writer.writeBytesNTimes(fill_utf8, right_padding); }, .right => { - try writer.writeBytesNTimes(writer, fill_utf8, padding); + try writer.writeBytesNTimes(fill_utf8, padding); try writer.writeAll(buf); }, } From cf54db38b45d5425fc340275cf9abfec218dae21 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 16:54:27 +0100 Subject: [PATCH 08/16] moved unicode translation after exit condition --- lib/std/fmt.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index c5844477e870..1d327a62760d 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -988,11 +988,6 @@ pub fn formatBuf( options: FormatOptions, writer: anytype, ) !void { - var fill_buffer: [4]u8 = undefined; - const fill_utf8 = if (std.unicode.utf8Encode(options.fill, &fill_buffer)) |len| - fill_buffer[0..len] - else |_| - " "; if (options.width) |min_width| { // In case of error assume the buffer content is ASCII-encoded const width = unicode.utf8CountCodepoints(buf) catch buf.len; @@ -1001,6 +996,11 @@ pub fn formatBuf( if (padding == 0) return writer.writeAll(buf); + var fill_buffer: [4]u8 = undefined; + const fill_utf8 = if (std.unicode.utf8Encode(options.fill, &fill_buffer)) |len| + fill_buffer[0..len] + else |_| + " "; switch (options.alignment) { .left => { try writer.writeAll(buf); From b3b11daa0249858d029020de2bb227c502966c9b Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 17:49:02 +0100 Subject: [PATCH 09/16] handled encode error with replacement_character --- lib/std/fmt.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 1d327a62760d..6a01fbad5137 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -997,10 +997,13 @@ pub fn formatBuf( return writer.writeAll(buf); var fill_buffer: [4]u8 = undefined; - const fill_utf8 = if (std.unicode.utf8Encode(options.fill, &fill_buffer)) |len| + const fill_utf8 = if (unicode.utf8Encode(options.fill, &fill_buffer)) |len| fill_buffer[0..len] - else |_| - " "; + else |err| switch (err) { + error.Utf8CannotEncodeSurrogateHalf, + error.CodepointTooLarge, + => &unicode.utf8EncodeComptime(unicode.replacement_character), + }; switch (options.alignment) { .left => { try writer.writeAll(buf); From 3b16823248e46cc5c0f777acb18985f3ccdb9900 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 18:00:36 +0100 Subject: [PATCH 10/16] added utf8EncodeComptime --- lib/std/fmt.zig | 6 +++--- lib/std/unicode.zig | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 6a01fbad5137..b19cca5bfc7c 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -234,7 +234,7 @@ pub const Placeholder = struct { // Skip the colon, if present if (comptime parser.char()) |ch| { if (ch != ':') { - @compileError("expected : or }, found '" ++ [1]u8{ch} ++ "'"); + @compileError("expected : or }, found '" ++ unicode.utf8EncodeComptime(ch) ++ "'"); } } @@ -269,7 +269,7 @@ pub const Placeholder = struct { // Skip the dot, if present if (comptime parser.char()) |ch| { if (ch != '.') { - @compileError("expected . or }, found '" ++ [1]u8{ch} ++ "'"); + @compileError("expected . or }, found '" ++ unicode.utf8EncodeComptime(ch) ++ "'"); } } @@ -278,7 +278,7 @@ pub const Placeholder = struct { @compileError(@errorName(err)); if (comptime parser.char()) |ch| { - @compileError("extraneous trailing character '" ++ [1]u8{ch} ++ "'"); + @compileError("extraneous trailing character '" ++ unicode.utf8EncodeComptime(ch) ++ "'"); } return Placeholder{ diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index eacb62613ad7..ad14d2e006fc 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -69,6 +69,19 @@ pub fn utf8Encode(c: u21, out: []u8) !u3 { return length; } +pub inline fn utf8EncodeComptime(comptime c: u21) [ + utf8CodepointSequenceLength(c) catch |err| + @compileError(@errorName(err)) +]u8 { + comptime var result: [ + utf8CodepointSequenceLength(c) catch + unreachable + ]u8 = undefined; + comptime assert((utf8Encode(c, &result) catch |err| + @compileError(@errorName(err))) == result.len); + return result; +} + const Utf8DecodeError = Utf8Decode2Error || Utf8Decode3Error || Utf8Decode4Error; /// Decodes the UTF-8 codepoint encoded in the given slice of bytes. From 3ddf8fcfa2919d1a0f07e7986ea11c284c969a9b Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 18:46:54 +0100 Subject: [PATCH 11/16] added test for utf8EncodeComptime --- lib/std/unicode.zig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index ad14d2e006fc..0b2437c49296 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -538,6 +538,27 @@ fn testUtf8Encode() !void { try testing.expect(array[3] == 0b10001000); } +test "utf8 encode comptime" { + var array: [4]u8 = undefined; + array = try utf8EncodeComptime('€'); + try testing.expect(array[0] == 0b11100010); + try testing.expect(array[1] == 0b10000010); + try testing.expect(array[2] == 0b10101100); + + array = try utf8EncodeComptime('$'); + try testing.expect(array[0] == 0b00100100); + + array = utf8EncodeComptime('¢'); + try testing.expect(array[0] == 0b11000010); + try testing.expect(array[1] == 0b10100010); + + array = utf8EncodeComptime('𐍈'); + try testing.expect(array[0] == 0b11110000); + try testing.expect(array[1] == 0b10010000); + try testing.expect(array[2] == 0b10001101); + try testing.expect(array[3] == 0b10001000); +} + test "utf8 encode error" { try comptime testUtf8EncodeError(); try testUtf8EncodeError(); From f1ba826a40dfed08cfb563aed3eb3229aa1f5e8a Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 18:58:11 +0100 Subject: [PATCH 12/16] used comptime utf encode --- lib/std/fmt.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index b19cca5bfc7c..27ae6acb3da0 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -976,8 +976,7 @@ pub fn formatUnicodeCodepoint( var buf: [4]u8 = undefined; const len = unicode.utf8Encode(c, &buf) catch |err| switch (err) { error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => { - const len = unicode.utf8Encode(unicode.replacement_character, &buf) catch unreachable; - return formatBuf(buf[0..len], options, writer); + return formatBuf(unicode.utf8EncodeComptime(unicode.replacement_character), options, writer); }, }; return formatBuf(buf[0..len], options, writer); From d0bc6de308b585a9eeeffee38c6f133fdeaa8371 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 18:58:45 +0100 Subject: [PATCH 13/16] fixed test for utf8EncodeComptime --- lib/std/unicode.zig | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 0b2437c49296..807e3887d70f 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -539,24 +539,10 @@ fn testUtf8Encode() !void { } test "utf8 encode comptime" { - var array: [4]u8 = undefined; - array = try utf8EncodeComptime('€'); - try testing.expect(array[0] == 0b11100010); - try testing.expect(array[1] == 0b10000010); - try testing.expect(array[2] == 0b10101100); - - array = try utf8EncodeComptime('$'); - try testing.expect(array[0] == 0b00100100); - - array = utf8EncodeComptime('¢'); - try testing.expect(array[0] == 0b11000010); - try testing.expect(array[1] == 0b10100010); - - array = utf8EncodeComptime('𐍈'); - try testing.expect(array[0] == 0b11110000); - try testing.expect(array[1] == 0b10010000); - try testing.expect(array[2] == 0b10001101); - try testing.expect(array[3] == 0b10001000); + try testing.expectEqualSlices("€", utf8EncodeComptime('€')); + try testing.expectEqualSlices("$", utf8EncodeComptime('$')); + try testing.expectEqualSlices("¢", utf8EncodeComptime('¢')); + try testing.expectEqualSlices("𐍈", utf8EncodeComptime('𐍈')); } test "utf8 encode error" { From 4d8c96efaecbebd17a9077adab7a521a09e5055a Mon Sep 17 00:00:00 2001 From: vinnichase Date: Sat, 13 Jan 2024 10:18:42 -0800 Subject: [PATCH 14/16] forgot type in slice equality check Co-authored-by: Jacob Young --- lib/std/unicode.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 807e3887d70f..4d201236ff55 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -539,10 +539,10 @@ fn testUtf8Encode() !void { } test "utf8 encode comptime" { - try testing.expectEqualSlices("€", utf8EncodeComptime('€')); - try testing.expectEqualSlices("$", utf8EncodeComptime('$')); - try testing.expectEqualSlices("¢", utf8EncodeComptime('¢')); - try testing.expectEqualSlices("𐍈", utf8EncodeComptime('𐍈')); + try testing.expectEqualSlices(u8, "€", utf8EncodeComptime('€')); + try testing.expectEqualSlices(u8, "$", utf8EncodeComptime('$')); + try testing.expectEqualSlices(u8, "¢", utf8EncodeComptime('¢')); + try testing.expectEqualSlices(u8, "𐍈", utf8EncodeComptime('𐍈')); } test "utf8 encode error" { From d87ccb1bba0f3b55cedfb9da04d966cf6e514477 Mon Sep 17 00:00:00 2001 From: Vincenz Koop Date: Sat, 13 Jan 2024 19:28:44 +0100 Subject: [PATCH 15/16] forgot address-of --- lib/std/fmt.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 27ae6acb3da0..6bb7e26da86f 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -976,7 +976,7 @@ pub fn formatUnicodeCodepoint( var buf: [4]u8 = undefined; const len = unicode.utf8Encode(c, &buf) catch |err| switch (err) { error.Utf8CannotEncodeSurrogateHalf, error.CodepointTooLarge => { - return formatBuf(unicode.utf8EncodeComptime(unicode.replacement_character), options, writer); + return formatBuf(&unicode.utf8EncodeComptime(unicode.replacement_character), options, writer); }, }; return formatBuf(buf[0..len], options, writer); From 921ce6e1bd4c0fd415fd1b136646c7974a45c136 Mon Sep 17 00:00:00 2001 From: vinnichase Date: Sat, 13 Jan 2024 10:40:36 -0800 Subject: [PATCH 16/16] forgot address-of also in tests Co-authored-by: Jacob Young --- lib/std/unicode.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index 4d201236ff55..8aae6a1b5f2c 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -539,10 +539,10 @@ fn testUtf8Encode() !void { } test "utf8 encode comptime" { - try testing.expectEqualSlices(u8, "€", utf8EncodeComptime('€')); - try testing.expectEqualSlices(u8, "$", utf8EncodeComptime('$')); - try testing.expectEqualSlices(u8, "¢", utf8EncodeComptime('¢')); - try testing.expectEqualSlices(u8, "𐍈", utf8EncodeComptime('𐍈')); + try testing.expectEqualSlices(u8, "€", &utf8EncodeComptime('€')); + try testing.expectEqualSlices(u8, "$", &utf8EncodeComptime('$')); + try testing.expectEqualSlices(u8, "¢", &utf8EncodeComptime('¢')); + try testing.expectEqualSlices(u8, "𐍈", &utf8EncodeComptime('𐍈')); } test "utf8 encode error" {