Skip to content

std.fmt: check result types in parseInt functions #24256

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1514,17 +1514,27 @@ pub fn Formatter(comptime formatFn: anytype) type {
///
/// Ignores '_' character in `buf`.
/// See also `parseUnsigned`.
///
/// Asserts 'T' is an integer type
pub fn parseInt(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
if (@typeInfo(T) != .int) {
@compileError("Cannot parse an integer into a non-integer type");
}
return parseIntWithGenericCharacter(T, u8, buf, base);
}

/// Like `parseInt`, but with a generic `Character` type.
///
/// Asserts that 'Result' is an integer type
pub fn parseIntWithGenericCharacter(
comptime Result: type,
comptime Character: type,
buf: []const Character,
base: u8,
) ParseIntError!Result {
if (@typeInfo(Result) != .int) {
@compileError("Cannot parse an integer into a non-integer type");
}
if (buf.len == 0) return error.InvalidCharacter;
if (buf[0] == '+') return parseIntWithSign(Result, Character, buf[1..], base, .pos);
if (buf[0] == '-') return parseIntWithSign(Result, Character, buf[1..], base, .neg);
Expand Down Expand Up @@ -1591,13 +1601,30 @@ test parseInt {
try std.testing.expectEqual(@as(i5, -16), try std.fmt.parseInt(i5, "-10", 16));
}

/// Parses an integer with a specified `sign`, accepting a generic `Character`
/// type for width and a buffer of `Character`s, in the specified `base` of an
/// integral type of value `Result`
///
/// When `base` is zero the string prefix is examined to detect the true base:
/// * A prefix of "0b" implies base=2,
/// * A prefix of "0o" implies base=8,
/// * A prefix of "0x" implies base=16,
/// * Otherwise base=10 is assumed.
///
/// Ignores '_' character in `buf`.
///
/// Asserts that `Result` is an integer type
fn parseIntWithSign(
comptime Result: type,
comptime Character: type,
buf: []const Character,
base: u8,
comptime sign: enum { pos, neg },
) ParseIntError!Result {
if (@typeInfo(Result) != .int) {
@compileError("Cannot parse an integer into a non-integer type");
}

if (buf.len == 0) return error.InvalidCharacter;

var buf_base = base;
Expand Down Expand Up @@ -1670,7 +1697,12 @@ fn parseIntWithSign(
///
/// Ignores '_' character in `buf`.
/// See also `parseInt`.
///
/// Asserts 'T' is an integer type
pub fn parseUnsigned(comptime T: type, buf: []const u8, base: u8) ParseIntError!T {
if (@typeInfo(T) != .int) {
@compileError("Cannot parse an integer into a non-integer type");
}
return parseIntWithSign(T, u8, buf, base, .pos);
}

Expand Down