From 9fa585b2c7e12e5fa20871615778b6051e2b52fe Mon Sep 17 00:00:00 2001 From: Saurabh Date: Tue, 3 Dec 2024 10:50:02 +0530 Subject: [PATCH 1/3] fix compiler error in 'std.fmt.Parser.until' --- 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 2f16d849b051..0ae92e99f958 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -340,7 +340,7 @@ 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: u21) []const u8 { + pub fn until(comptime self: *@This(), ch: u21) []const u8 { var result: []const u8 = &[_]u8{}; while (self.peek(0)) |code_point| { if (code_point == ch) From 0156b0a2f63ad8c175eb11eab1a07112df45b5c3 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Tue, 10 Dec 2024 08:20:16 +0530 Subject: [PATCH 2/3] use 'nextCodepoint' instead of ++ --- 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 0ae92e99f958..3e74b58bea5a 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -340,14 +340,14 @@ 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(comptime self: *@This(), ch: u21) []const u8 { - var result: []const u8 = &[_]u8{}; + pub fn until(self: *@This(), ch: u21) []const u8 { + const start = self.iter.i; while (self.peek(0)) |code_point| { if (code_point == ch) break; - result = result ++ (self.iter.nextCodepointSlice() orelse &[_]u8{}); + _ = self.iter.nextCodepoint(); } - return result; + return self.iter.bytes[start..self.iter.i]; } // Returns one character, if available From 1a096ac77902bc8004429ec7f9d92e8cdea33abb Mon Sep 17 00:00:00 2001 From: Saurabh M Date: Tue, 17 Dec 2024 05:50:45 +0530 Subject: [PATCH 3/3] added unit tests for the 'until' method in Parser struct --- lib/std/fmt.zig | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 3e74b58bea5a..4e80c3333e37 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -11,6 +11,7 @@ const unicode = std.unicode; const meta = std.meta; const lossyCast = math.lossyCast; const expectFmt = std.testing.expectFmt; +const testing = std.testing; pub const default_max_depth = 3; @@ -315,7 +316,6 @@ pub const Specifier = union(enum) { /// Allows to implement formatters compatible with std.fmt without replicating /// the standard library behavior. pub const Parser = struct { - pos: usize = 0, iter: std.unicode.Utf8Iterator, // Returns a decimal number or null if the current character is not a @@ -2762,3 +2762,47 @@ test hex { try std.testing.expectEqualStrings("[00efcdab78563412]", s); } } + +test "parser until" { + { // return substring till ':' + var parser: Parser = .{ + .iter = .{ .bytes = "abc:1234", .i = 0 }, + }; + try testing.expectEqualStrings("abc", parser.until(':')); + } + + { // return the entire string - `ch` not found + var parser: Parser = .{ + .iter = .{ .bytes = "abc1234", .i = 0 }, + }; + try testing.expectEqualStrings("abc1234", parser.until(':')); + } + + { // substring is empty - `ch` is the only character + var parser: Parser = .{ + .iter = .{ .bytes = ":", .i = 0 }, + }; + try testing.expectEqualStrings("", parser.until(':')); + } + + { // empty string and `ch` not found + var parser: Parser = .{ + .iter = .{ .bytes = "", .i = 0 }, + }; + try testing.expectEqualStrings("", parser.until(':')); + } + + { // substring starts at index 2 and goes upto `ch` + var parser: Parser = .{ + .iter = .{ .bytes = "abc:1234", .i = 2 }, + }; + try testing.expectEqualStrings("c", parser.until(':')); + } + + { // substring starts at index 4 and goes upto the end - `ch` not found + var parser: Parser = .{ + .iter = .{ .bytes = "abc1234", .i = 4 }, + }; + try testing.expectEqualStrings("234", parser.until(':')); + } +}