| ... | @@ -1358,6 +1358,7 @@ pub fn Formatter(comptime format_fn: anytype) type { | ... | @@ -1358,6 +1358,7 @@ pub fn Formatter(comptime format_fn: anytype) type { |
| 1358 | /// * A prefix of "0x" implies radix=16, | 1358 | /// * A prefix of "0x" implies radix=16, |
| 1359 | /// * Otherwise radix=10 is assumed. | 1359 | /// * Otherwise radix=10 is assumed. |
| 1360 | /// | 1360 | /// |
| | 1361 | /// Ignores '_' character in `buf`. |
| 1361 | /// See also `parseUnsigned`. | 1362 | /// See also `parseUnsigned`. |
| 1362 | pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { | 1363 | pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { |
| 1363 | if (buf.len == 0) return error.InvalidCharacter; | 1364 | if (buf.len == 0) return error.InvalidCharacter; |
| ... | @@ -1394,12 +1395,18 @@ test "parseInt" { | ... | @@ -1394,12 +1395,18 @@ test "parseInt" { |
| 1394 | | 1395 | |
| 1395 | // autodectect the radix | 1396 | // autodectect the radix |
| 1396 | std.testing.expect((try parseInt(i32, "111", 0)) == 111); | 1397 | 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); |
| 1397 | std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7); | 1400 | std.testing.expect((try parseInt(i32, "+0b111", 0)) == 7); |
| | 1401 | std.testing.expect((try parseInt(i32, "+0b1_11", 0)) == 7); |
| 1398 | std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73); | 1402 | std.testing.expect((try parseInt(i32, "+0o111", 0)) == 73); |
| | 1403 | std.testing.expect((try parseInt(i32, "+0o11_1", 0)) == 73); |
| 1399 | std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273); | 1404 | std.testing.expect((try parseInt(i32, "+0x111", 0)) == 273); |
| 1400 | std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7); | 1405 | std.testing.expect((try parseInt(i32, "-0b111", 0)) == -7); |
| | 1406 | std.testing.expect((try parseInt(i32, "-0b11_1", 0)) == -7); |
| 1401 | std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73); | 1407 | std.testing.expect((try parseInt(i32, "-0o111", 0)) == -73); |
| 1402 | std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273); | 1408 | std.testing.expect((try parseInt(i32, "-0x111", 0)) == -273); |
| | 1409 | std.testing.expect((try parseInt(i32, "-0x1_11", 0)) == -273); |
| 1403 | | 1410 | |
| 1404 | // bare binary/octal/decimal prefix is invalid | 1411 | // bare binary/octal/decimal prefix is invalid |
| 1405 | std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0)); | 1412 | std.testing.expectError(error.InvalidCharacter, parseInt(u32, "0b", 0)); |
| ... | @@ -1448,6 +1455,7 @@ fn parseWithSign( | ... | @@ -1448,6 +1455,7 @@ fn parseWithSign( |
| 1448 | var x: T = 0; | 1455 | var x: T = 0; |
| 1449 | | 1456 | |
| 1450 | for (buf_start) |c| { | 1457 | for (buf_start) |c| { |
| | 1458 | if (c == '_') continue; |
| 1451 | const digit = try charToDigit(c, buf_radix); | 1459 | const digit = try charToDigit(c, buf_radix); |
| 1452 | | 1460 | |
| 1453 | if (x != 0) x = try math.mul(T, x, try math.cast(T, buf_radix)); | 1461 | if (x != 0) x = try math.mul(T, x, try math.cast(T, buf_radix)); |
| ... | @@ -1466,6 +1474,7 @@ fn parseWithSign( | ... | @@ -1466,6 +1474,7 @@ fn parseWithSign( |
| 1466 | /// * A prefix of "0x" implies radix=16, | 1474 | /// * A prefix of "0x" implies radix=16, |
| 1467 | /// * Otherwise radix=10 is assumed. | 1475 | /// * Otherwise radix=10 is assumed. |
| 1468 | /// | 1476 | /// |
| | 1477 | /// Ignores '_' character in `buf`. |
| 1469 | /// See also `parseInt`. | 1478 | /// See also `parseInt`. |
| 1470 | pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { | 1479 | pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError!T { |
| 1471 | return parseWithSign(T, buf, radix, .Pos); | 1480 | return parseWithSign(T, buf, radix, .Pos); |
| ... | @@ -1474,9 +1483,11 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError | ... | @@ -1474,9 +1483,11 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseIntError |
| 1474 | test "parseUnsigned" { | 1483 | test "parseUnsigned" { |
| 1475 | std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124); | 1484 | std.testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124); |
| 1476 | std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535); | 1485 | std.testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535); |
| | 1486 | std.testing.expect((try parseUnsigned(u16, "65_535", 10)) == 65535); |
| 1477 | std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10)); | 1487 | std.testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10)); |
| 1478 | | 1488 | |
| 1479 | std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); | 1489 | std.testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); |
| | 1490 | std.testing.expect((try parseUnsigned(u64, "0f_fff_fff_fff_fff_fff", 16)) == 0xffffffffffffffff); |
| 1480 | std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16)); | 1491 | std.testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16)); |
| 1481 | | 1492 | |
| 1482 | std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); | 1493 | std.testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); |