| ... | ... | @@ -373,6 +373,7 @@ pub const Int = struct { |
| 373 | 373 | const d = switch (ch) { |
| 374 | 374 | '0'...'9' => ch - '0', |
| 375 | 375 | 'a'...'f' => (ch - 'a') + 0xa, |
| 376 | 'A'...'F' => (ch - 'A') + 0xa, |
| 376 | 377 | else => return error.InvalidCharForDigit, |
| 377 | 378 | }; |
| 378 | 379 | |
| ... | ... | @@ -393,8 +394,9 @@ pub const Int = struct { |
| 393 | 394 | |
| 394 | 395 | /// Set self from the string representation `value`. |
| 395 | 396 | /// |
| 396 | | /// value must contain only digits <= `base`. Base prefixes are not allowed (e.g. 0x43 should |
| 397 | | /// simply be 43). |
| 397 | /// `value` must contain only digits <= `base` and is case insensitive. Base prefixes are |
| 398 | /// not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are |
| 399 | /// ignored and can be used as digit separators. |
| 398 | 400 | /// |
| 399 | 401 | /// Returns an error if memory could not be allocated or `value` has invalid digits for the |
| 400 | 402 | /// requested base. |
| ... | ... | @@ -415,6 +417,9 @@ pub const Int = struct { |
| 415 | 417 | try self.set(0); |
| 416 | 418 | |
| 417 | 419 | for (value[i..]) |ch| { |
| 420 | if (ch == '_') { |
| 421 | continue; |
| 422 | } |
| 418 | 423 | const d = try charToDigit(ch, base); |
| 419 | 424 | |
| 420 | 425 | const ap_d = Int.initFixed(([_]Limb{d})[0..]); |
| ... | ... | @@ -1582,6 +1587,22 @@ test "big.int string negative" { |
| 1582 | 1587 | testing.expect((try a.to(i32)) == -1023); |
| 1583 | 1588 | } |
| 1584 | 1589 | |
| 1590 | test "big.int string set number with underscores" { |
| 1591 | var a = try Int.init(testing.allocator); |
| 1592 | defer a.deinit(); |
| 1593 | |
| 1594 | try a.setString(10, "__1_2_0_3_1_7_2_4_1_2_0_____9_1__2__4_7_8_1_2_4_1_2_9_0_8_4_7_1_2_4___"); |
| 1595 | testing.expect((try a.to(u128)) == 120317241209124781241290847124); |
| 1596 | } |
| 1597 | |
| 1598 | test "big.int string set case insensitive number" { |
| 1599 | var a = try Int.init(testing.allocator); |
| 1600 | defer a.deinit(); |
| 1601 | |
| 1602 | try a.setString(16, "aB_cD_eF"); |
| 1603 | testing.expect((try a.to(u32)) == 0xabcdef); |
| 1604 | } |
| 1605 | |
| 1585 | 1606 | test "big.int string set bad char error" { |
| 1586 | 1607 | var a = try Int.init(testing.allocator); |
| 1587 | 1608 | defer a.deinit(); |