authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-04-20 16:53:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-20 17:08:05-04:00
logb4aec0e31db2c9ea7bc2c892f0e557a0de8f4735
treec0c804d4b2165d255dec74e297edddbf9fdf3b40
parent6b944492b5f35ce61591e1231dc6c8d44ccf17e3

stage2: make std.fmt.parseInt ignore `_`


2 files changed, 12 insertions(+), 0 deletions(-)

lib/std/fmt.zig+11
......@@ -1358,6 +1358,7 @@ pub fn Formatter(comptime format_fn: anytype) type {
13581358/// * A prefix of "0x" implies radix=16,
13591359/// * Otherwise radix=10 is assumed.
13601360///
1361/// Ignores '_' character in `buf`.
13611362/// See also `parseUnsigned`.
13621363pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {
13631364 if (buf.len == 0) return error.InvalidCharacter;
......@@ -1394,12 +1395,18 @@ test "parseInt" {
13941395
13951396 // autodectect the radix
13961397 std.testing.expect((try parseInt(i32, "111", 0)) == 111);
1398 std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
1399 std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
13971400 std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7);
1401 std.testing.expect((try parseInt(i32, "+0b1_11", 0)) == 7);
13981402 std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73);
1403 std.testing.expect((try parseInt(i32, "+0o11_1", 0)) == 73);
13991404 std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273);
14001405 std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7);
1406 std.testing.expect((try parseInt(i32, "-0b11_1", 0)) == -7);
14011407 std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73);
14021408 std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273);
1409 std.testing.expect((try parseInt(i32, "-0x1_11", 0)) == -273);
14031410
14041411 // bare binary/octal/decimal prefix is invalid
14051412 std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0));
......@@ -1448,6 +1455,7 @@ fn parseWithSign(
14481455 var x: T = 0;
14491456
14501457 for (buf_start) |c| {
1458 if (c == '_') continue;
14511459 const digit = try charToDigit(c, buf_radix);
14521460
14531461 if (x != 0) x = try math.mul(T, x, try math.cast(T, buf_radix));
......@@ -1466,6 +1474,7 @@ fn parseWithSign(
14661474/// * A prefix of "0x" implies radix=16,
14671475/// * Otherwise radix=10 is assumed.
14681476///
1477/// Ignores '_' character in `buf`.
14691478/// See also `parseInt`.
14701479pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T {
14711480 return parseWithSign(T, buf, radix, .Pos);
......@@ -1474,9 +1483,11 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError
14741483test "parseUnsigned" {
14751484 std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124);
14761485 std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535);
1486 std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535);
14771487 std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10));
14781488
14791489 std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff);
1490 std.testing.expect((try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16)) == 0xffffffffffffffff);
14801491 std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16));
14811492
14821493 std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF);
src/AstGen.zig+1
......@@ -5111,6 +5111,7 @@ fn integerLiteral(
51115111 };
51125112 return rvalue(gz, scope, rl, result, node);
51135113 } else |err| {
5114 assert(err != error.InvalidCharacter);
51145115 return gz.astgen.failNode(node, "TODO implement int literals that don't fit in a u64", .{});
51155116 }
51165117}