Skip to content
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

fix: add uint parsing function #142

Merged
merged 2 commits into from
Nov 15, 2024
Merged
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
37 changes: 34 additions & 3 deletions clap/parsers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,46 @@ test "string" {
try std.testing.expectEqualStrings("aa", try string("aa"));
}

/// A parser that uses `std.fmt.parseInt` to parse the string into an integer value.
/// See `std.fmt.parseInt` documentation for more information.
/// A parser that uses `std.fmt.parseInt` or `std.fmt.parseUnsigned` to parse the string into an integer value.
/// See `std.fmt.parseInt` and `std.fmt.parseUnsigned` documentation for more information.
pub fn int(comptime T: type, comptime base: u8) fn ([]const u8) std.fmt.ParseIntError!T {
return struct {
fn parse(in: []const u8) std.fmt.ParseIntError!T {
return std.fmt.parseInt(T, in, base);
return switch (@typeInfo(T).int.signedness) {
.signed => std.fmt.parseInt(T, in, base),
.unsigned => std.fmt.parseUnsigned(T, in, base),
};
}
}.parse;
}

test "int" {
try std.testing.expectEqual(@as(i8, 0), try int(i8, 10)("0"));
try std.testing.expectEqual(@as(i8, 1), try int(i8, 10)("1"));
try std.testing.expectEqual(@as(i8, 10), try int(i8, 10)("10"));
try std.testing.expectEqual(@as(i8, 0b10), try int(i8, 2)("10"));
try std.testing.expectEqual(@as(i8, 0x10), try int(i8, 0)("0x10"));
try std.testing.expectEqual(@as(i8, 0b10), try int(i8, 0)("0b10"));
try std.testing.expectEqual(@as(i16, 0), try int(i16, 10)("0"));
try std.testing.expectEqual(@as(i16, 1), try int(i16, 10)("1"));
try std.testing.expectEqual(@as(i16, 10), try int(i16, 10)("10"));
try std.testing.expectEqual(@as(i16, 0b10), try int(i16, 2)("10"));
try std.testing.expectEqual(@as(i16, 0x10), try int(i16, 0)("0x10"));
try std.testing.expectEqual(@as(i16, 0b10), try int(i16, 0)("0b10"));

try std.testing.expectEqual(@as(i8, 0), try int(i8, 10)("-0"));
try std.testing.expectEqual(@as(i8, -1), try int(i8, 10)("-1"));
try std.testing.expectEqual(@as(i8, -10), try int(i8, 10)("-10"));
try std.testing.expectEqual(@as(i8, -0b10), try int(i8, 2)("-10"));
try std.testing.expectEqual(@as(i8, -0x10), try int(i8, 0)("-0x10"));
try std.testing.expectEqual(@as(i8, -0b10), try int(i8, 0)("-0b10"));
try std.testing.expectEqual(@as(i16, 0), try int(i16, 10)("-0"));
try std.testing.expectEqual(@as(i16, -1), try int(i16, 10)("-1"));
try std.testing.expectEqual(@as(i16, -10), try int(i16, 10)("-10"));
try std.testing.expectEqual(@as(i16, -0b10), try int(i16, 2)("-10"));
try std.testing.expectEqual(@as(i16, -0x10), try int(i16, 0)("-0x10"));
try std.testing.expectEqual(@as(i16, -0b10), try int(i16, 0)("-0b10"));

try std.testing.expectEqual(@as(u8, 0), try int(u8, 10)("0"));
try std.testing.expectEqual(@as(u8, 1), try int(u8, 10)("1"));
try std.testing.expectEqual(@as(u8, 10), try int(u8, 10)("10"));
Expand All @@ -47,6 +76,8 @@ test "int" {
try std.testing.expectEqual(@as(u16, 0b10), try int(u16, 2)("10"));
try std.testing.expectEqual(@as(u16, 0x10), try int(u16, 0)("0x10"));
try std.testing.expectEqual(@as(u16, 0b10), try int(u16, 0)("0b10"));

try std.testing.expectEqual(std.fmt.ParseIntError.InvalidCharacter, int(u8, 10)("-10"));
}

/// A parser that uses `std.fmt.parseFloat` to parse the string into an float value.
Expand Down
Loading