aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormomumi <57862114+momumi@users.noreply.github.com>2020-03-22 13:45:31 +1000
committermomumi <57862114+momumi@users.noreply.github.com>2020-03-22 13:59:14 +1000
commit8de45e51438d7f593825ae2134ab7dfc46f3bab2 (patch)
treeebf5cb2d0f7a6e43241e9755f0f84a33e9b29ffb /lib
parent29324e6f393011ee010b80f2c88d946f082bef22 (diff)
downloadzig-8de45e51438d7f593825ae2134ab7dfc46f3bab2.tar.gz
zig-8de45e51438d7f593825ae2134ab7dfc46f3bab2.zip
update parsing of int literals in self-hosted
* update std.math.big.Int.setString() to ignore underscores and make it case insensitive * fix issue in ir.zig with leading zeroes in integer literals
Diffstat (limited to 'lib')
-rw-r--r--lib/std/math/big/int.zig25
1 files changed, 23 insertions, 2 deletions
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig
index 95d0764f68..3557a40798 100644
--- a/lib/std/math/big/int.zig
+++ b/lib/std/math/big/int.zig
@@ -373,6 +373,7 @@ pub const Int = struct {
const d = switch (ch) {
'0'...'9' => ch - '0',
'a'...'f' => (ch - 'a') + 0xa,
+ 'A'...'F' => (ch - 'A') + 0xa,
else => return error.InvalidCharForDigit,
};
@@ -393,8 +394,9 @@ pub const Int = struct {
/// Set self from the string representation `value`.
///
- /// value must contain only digits <= `base`. Base prefixes are not allowed (e.g. 0x43 should
- /// simply be 43).
+ /// `value` must contain only digits <= `base` and is case insensitive. Base prefixes are
+ /// not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are
+ /// ignored and can be used as digit separators.
///
/// Returns an error if memory could not be allocated or `value` has invalid digits for the
/// requested base.
@@ -415,6 +417,9 @@ pub const Int = struct {
try self.set(0);
for (value[i..]) |ch| {
+ if (ch == '_') {
+ continue;
+ }
const d = try charToDigit(ch, base);
const ap_d = Int.initFixed(([_]Limb{d})[0..]);
@@ -1582,6 +1587,22 @@ test "big.int string negative" {
testing.expect((try a.to(i32)) == -1023);
}
+test "big.int string set number with underscores" {
+ var a = try Int.init(testing.allocator);
+ defer a.deinit();
+
+ 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___");
+ testing.expect((try a.to(u128)) == 120317241209124781241290847124);
+}
+
+test "big.int string set case insensitive number" {
+ var a = try Int.init(testing.allocator);
+ defer a.deinit();
+
+ try a.setString(16, "aB_cD_eF");
+ testing.expect((try a.to(u32)) == 0xabcdef);
+}
+
test "big.int string set bad char error" {
var a = try Int.init(testing.allocator);
defer a.deinit();