From 7ad10422793e56dcd50a4254beb8fc9d6c7f5eae Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 24 Jun 2023 13:03:51 -0700 Subject: [PATCH] Revert "fix parseInt failing when a correct base is given" This reverts commit fa6cea22bf9cd5ff6a9dd882166cf7e479acfd6c. Apologies for the merge. I thought this was a bug fix, but I see that it is implementing a proposal that I intended to reject. --- lib/std/fmt.zig | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 84e4fd648c..d983aba369 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1753,14 +1753,6 @@ test "parseInt" { try std.testing.expect((try parseInt(u8, "255", 10)) == 255); try std.testing.expectError(error.Overflow, parseInt(u8, "256", 10)); - // Test different bases - try std.testing.expect((try parseInt(i32, "0b10", 2)) == 0b10); - try std.testing.expect((try parseInt(i32, "0o10", 8)) == 0o10); - try std.testing.expect((try parseInt(i32, "0x10", 16)) == 0x10); - try std.testing.expect((try parseInt(i32, "10", 2)) == 0b10); - try std.testing.expect((try parseInt(i32, "10", 8)) == 0o10); - try std.testing.expect((try parseInt(i32, "10", 16)) == 0x10); - // +0 and -0 should work for unsigned try std.testing.expect((try parseInt(u8, "-0", 10)) == 0); try std.testing.expect((try parseInt(u8, "+0", 10)) == 0); @@ -1812,25 +1804,28 @@ fn parseWithSign( var buf_base = base; var buf_start = buf; - - if (buf.len > 2 and buf[0] == '0') { - switch (std.ascii.toLower(buf[1])) { - 'b' => { - if (base == 0) buf_base = 2; - buf_start = buf[2..]; - }, - 'o' => { - if (base == 0) buf_base = 8; - buf_start = buf[2..]; - }, - 'x' => { - if (base == 0) buf_base = 16; - buf_start = buf[2..]; - }, - else => {}, + if (base == 0) { + // Treat is as a decimal number by default. + buf_base = 10; + // Detect the base by looking at buf prefix. + if (buf.len > 2 and buf[0] == '0') { + switch (std.ascii.toLower(buf[1])) { + 'b' => { + buf_base = 2; + buf_start = buf[2..]; + }, + 'o' => { + buf_base = 8; + buf_start = buf[2..]; + }, + 'x' => { + buf_base = 16; + buf_start = buf[2..]; + }, + else => {}, + } } } - if (buf_base == 0) buf_base = 10; const add = switch (sign) { .pos => math.add,