Skip to content

fix compiler error in 'std.fmt.Parser.until' #22129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -341,13 +341,13 @@ 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 {
var result: []const u8 = &[_]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
Expand Down Expand Up @@ -2763,3 +2763,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(':'));
}
}