diff --git a/stint/io.nim b/stint/io.nim index 082fa47..ba2362f 100644 --- a/stint/io.nim +++ b/stint/io.nim @@ -213,10 +213,14 @@ func skipPrefixes(current_idx: var int, str: string, radix: range[2..16]) {.inli elif str[1] in {'o', 'O'}: doAssert radix == 8, "Parsing mismatch, 0o prefix is only valid for an octal number (base 8)" current_idx = 2 - elif str[1] in {'b', 'B'}: - # this check will fail if we have radix 16 and input "0bcdef12345" which is a valid hex - doAssert radix == 2, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2)" - current_idx = 2 + elif str[1] in {'b', 'B'}: + if radix == 2: + current_idx = 2 + elif radix == 16: + # allow something like "0bcdef12345" which is a valid hex + current_idx = 0 + else: + doAssert false, "Parsing mismatch, 0b prefix is only valid for a binary number (base 2), or hex number" func nextNonBlank(current_idx: var int, s: string) {.inline.} = ## Move the current index, skipping white spaces and "_" characters. diff --git a/tests/all_tests.nim b/tests/all_tests.nim index 5e6d1ea..8fb0fd4 100644 --- a/tests/all_tests.nim +++ b/tests/all_tests.nim @@ -1,5 +1,5 @@ # Stint -# Copyright 2018 Status Research & Development GmbH +# Copyright 2018-2023 Status Research & Development GmbH # Licensed under either of # # * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) @@ -31,9 +31,8 @@ import test_int_muldiv, test_int_exp - import test_io, test_conversion, - t_randomized_divmod - + t_randomized_divmod, + test_bugfix diff --git a/tests/test_bugfix.nim b/tests/test_bugfix.nim new file mode 100644 index 0000000..8867392 --- /dev/null +++ b/tests/test_bugfix.nim @@ -0,0 +1,20 @@ +# Copyright 2023 Status Research & Development GmbH +# Licensed under either of +# +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) +# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) +# +# at your option. This file may not be copied, modified, or distributed except according to those terms. + +import ../stint, unittest + +suite "various bugfix": + test "skipPrefixes bug": + let x = "0b1010101".parse(UInt128, 2) + let z = "0bcdef12345".parse(UInt128, 16) + + check x == 0b1010101.u128 + check z == 0x0bcdef12345.u128 + + expect(AssertionDefect): + discard "0bcdef12345".parse(UInt128, 10)