Skip to content

Commit

Permalink
Fix #14901: parseFloat parsing 0x successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
Evin Yulo authored and Vexu committed Mar 16, 2023
1 parent da05097 commit b3af5d0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/std/fmt/parse_float.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ test "fmt.parseFloat hex.f16" {
}

test "fmt.parseFloat hex.f32" {
try testing.expectError(error.InvalidCharacter, parseFloat(f32, "0x"));
try testing.expectEqual(try parseFloat(f32, "0x1p0"), 1.0);
try testing.expectEqual(try parseFloat(f32, "-0x1p-1"), -0.5);
try testing.expectEqual(try parseFloat(f32, "0x10p+10"), 16384.0);
Expand Down
2 changes: 2 additions & 0 deletions lib/std/fmt/parse_float/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ fn parsePartialNumberBase(comptime T: type, stream: *FloatStream, negative: bool
tryParseDigits(MantissaT, stream, &mantissa, info.base);
var int_end = stream.offsetTrue();
var n_digits = @intCast(isize, stream.offsetTrue());
// the base being 16 implies a 0x prefix, which shouldn't be included in the digit count
if (info.base == 16) n_digits -= 2;

// handle dot with the following digits
var exponent: i64 = 0;
Expand Down

0 comments on commit b3af5d0

Please sign in to comment.