fix parseInt failing when a correct base is given

This commit is contained in:
Mikko Kaihlavirta 2023-06-24 13:06:56 +03:00 committed by Andrew Kelley
parent ea1d64dae4
commit fa6cea22bf

View file

@ -1753,6 +1753,14 @@ test "parseInt" {
try std.testing.expect((try parseInt(u8, "255", 10)) == 255); try std.testing.expect((try parseInt(u8, "255", 10)) == 255);
try std.testing.expectError(error.Overflow, parseInt(u8, "256", 10)); 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 // +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);
try std.testing.expect((try parseInt(u8, "+0", 10)) == 0); try std.testing.expect((try parseInt(u8, "+0", 10)) == 0);
@ -1804,28 +1812,25 @@ fn parseWithSign(
var buf_base = base; var buf_base = base;
var buf_start = buf; var buf_start = buf;
if (base == 0) {
// Treat is as a decimal number by default. if (buf.len > 2 and buf[0] == '0') {
buf_base = 10; switch (std.ascii.toLower(buf[1])) {
// Detect the base by looking at buf prefix. 'b' => {
if (buf.len > 2 and buf[0] == '0') { if (base == 0) buf_base = 2;
switch (std.ascii.toLower(buf[1])) { buf_start = buf[2..];
'b' => { },
buf_base = 2; 'o' => {
buf_start = buf[2..]; if (base == 0) buf_base = 8;
}, buf_start = buf[2..];
'o' => { },
buf_base = 8; 'x' => {
buf_start = buf[2..]; if (base == 0) buf_base = 16;
}, buf_start = buf[2..];
'x' => { },
buf_base = 16; else => {},
buf_start = buf[2..];
},
else => {},
}
} }
} }
if (buf_base == 0) buf_base = 10;
const add = switch (sign) { const add = switch (sign) {
.pos => math.add, .pos => math.add,