diff options
Diffstat (limited to 'lib/std/fmt/parse_float.zig')
| -rw-r--r-- | lib/std/fmt/parse_float.zig | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/std/fmt/parse_float.zig b/lib/std/fmt/parse_float.zig index 3173d749b4..1f6d7222ac 100644 --- a/lib/std/fmt/parse_float.zig +++ b/lib/std/fmt/parse_float.zig @@ -34,7 +34,7 @@ // - Only supports round-to-zero // - Does not handle denormals -const std = @import("../std.zig"); +const std = @import("std"); const ascii = std.ascii; // The mantissa field in FloatRepr is 64bit wide and holds only 19 digits @@ -231,6 +231,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { } else if (c == '.') { i += 1; state = .LeadingFractionalZeros; + } else if (c == '_') { + i += 1; } else { state = .MantissaIntegral; } @@ -259,6 +261,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { } else if (c == '.') { i += 1; state = .MantissaFractional; + } else if (c == '_') { + i += 1; } else { state = .MantissaFractional; } @@ -276,6 +280,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { } else if (c == 'e' or c == 'E') { i += 1; state = .ExponentSign; + } else if (c == '_') { + i += 1; } else { state = .ExponentSign; } @@ -283,6 +289,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { .ExponentSign => { if (c == '+') { i += 1; + } else if (c == '_') { + return error.InvalidCharacter; } else if (c == '-') { negative_exp = true; i += 1; @@ -293,6 +301,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { .LeadingExponentZeros => { if (c == '0') { i += 1; + } else if (c == '_') { + i += 1; } else { state = .Exponent; } @@ -305,6 +315,8 @@ fn parseRepr(s: []const u8, n: *FloatRepr) !ParseResult { } i += 1; + } else if (c == '_') { + i += 1; } else { return error.InvalidCharacter; } @@ -405,6 +417,7 @@ test "fmt.parseFloat" { try expectEqual(try parseFloat(T, "-INF"), -std.math.inf(T)); try expectEqual(try parseFloat(T, "0.4e0066999999999999999999999999999999999999999999999999999"), std.math.inf(T)); + try expect(approxEqAbs(T, try parseFloat(T, "0_1_2_3_4_5_6.7_8_9_0_0_0e0_0_1_0"), @as(T, 123456.789000e10), epsilon)); if (T != f16) { try expect(approxEqAbs(T, try parseFloat(T, "1e-2"), 0.01, epsilon)); |
