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

Conversation

valchx
Copy link

@valchx valchx commented Nov 14, 2024

In order to get the more precise ParseIntError.InvalidCharacter error instead of ParseIntError.Overflow when a negative number is passed to a usize, uX typed argument, I use the fmt.parseUnsigned function.

In order to get the more precise `ParseIntError.InvalidCharacter` error
instead of `ParseIntError.Overflow` when a negative number is passed to
a usize, uX typed argument, I use the `fmt.parseUnsigned` function.
@valchx
Copy link
Author

valchx commented Nov 14, 2024

Thoughts:
At first, I wanted to write a custom error to specify that the leading character was '-' but I see that there are no custom errors in the parsers.zig file. So falling back on the default errors might be the wiser choice.

@Hejsil
Copy link
Owner

Hejsil commented Nov 14, 2024

Thoughs on making int check the signedness of T and call either parseInt or parseUnsigned?

@valchx
Copy link
Author

valchx commented Nov 14, 2024

@Hejsil
I'm relatively new to zig, after a bit of looking around the language ref I ended with something like this:

/// 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 switch (@typeInfo(T)) {
                .int => |int_info| if (int_info.signedness == .signed) std.fmt.parseInt(T, in, base) else std.fmt.parseUnsigned(T, in, base),
                else => @compileError("The int parse function only supports integer types."),
            };
        }
    }.parse;
}

What do you think ?

@Hejsil
Copy link
Owner

Hejsil commented Nov 14, 2024

I don't think the @compileError is nessesary (the compiler will emit an error on accessing .int anyways), so I would do this:

/// 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 switch (@typeInfo(T).int.signedness) {
                .signed => std.fmt.parseInt(T, in, base)
                .unsigned => std.fmt.parseUnsigned(T, in, base),
            };
        }
    }.parse;
}

@Hejsil Hejsil merged commit 2e58c8e into Hejsil:master Nov 15, 2024
13 checks passed
@Hejsil
Copy link
Owner

Hejsil commented Nov 15, 2024

Thanks! 👍

@valchx
Copy link
Author

valchx commented Nov 15, 2024

Thanks for the feedback!

@valchx valchx deleted the parse_unsigned_with_uint branch November 15, 2024 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants