authorgravatar for ayushbardhan5@gmail.comiddev5 <ayushbardhan5@gmail.com> 2022-02-15 23:11:52+05:30
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-16 11:27:10+01:00
log77e5b042a084e227515e68d8baca5258c2019eaf
tree37fba97bd9953e6d0d55ee8446823099d236304c
parent5283a52af565521094bcd1186efc179b4be7ee8c

fmt: allow uppercase prefix to be parsed in std.fmt.parseInt()

std.fmt.parseHexFloat allow both 0x and 0X as prefix, so in order to keep things consistent. This commit modifies std.fmt.parseWithSign to check for the prefix case insensitively in order to allow both upper case and lower case prefix. This change now allows: 0O, 0B and 0X as prefixes for parsing.

1 files changed, 4 insertions(+), 1 deletions(-)

lib/std/fmt.zig+4-1
......@@ -1716,14 +1716,17 @@ test "parseInt" {
17161716 try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
17171717 try std.testing.expect((try parseInt(i32, "1_1_1", 0)) == 111);
17181718 try std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7);
1719 try std.testing.expect((try parseInt(i32, "+0B111", 0)) == 7);
17191720 try std.testing.expect((try parseInt(i32, "+0b1_11", 0)) == 7);
17201721 try std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73);
1722 try std.testing.expect((try parseInt(i32, "+0O111", 0)) == 73);
17211723 try std.testing.expect((try parseInt(i32, "+0o11_1", 0)) == 73);
17221724 try std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273);
17231725 try std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7);
17241726 try std.testing.expect((try parseInt(i32, "-0b11_1", 0)) == -7);
17251727 try std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73);
17261728 try std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273);
1729 try std.testing.expect((try parseInt(i32, "-0X111", 0)) == -273);
17271730 try std.testing.expect((try parseInt(i32, "-0x1_11", 0)) == -273);
17281731
17291732 // bare binary/octal/decimal prefix is invalid
......@@ -1747,7 +1750,7 @@ fn parseWithSign(
17471750 buf_radix = 10;
17481751 // Detect the radix by looking at buf prefix.
17491752 if (buf.len > 2 and buf[0] == '0') {
1750 switch (buf[1]) {
1753 switch (std.ascii.toLower(buf[1])) {
17511754 'b' => {
17521755 buf_radix = 2;
17531756 buf_start = buf[2..];