fix skipPrefixes bug

This commit is contained in:
jangko 2023-07-07 12:56:30 +07:00
parent 7fc30a8f1c
commit 3e9d64c896
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
3 changed files with 31 additions and 8 deletions

View File

@ -214,9 +214,13 @@ func skipPrefixes(current_idx: var int, str: string, radix: range[2..16]) {.inli
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)"
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.

View File

@ -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

20
tests/test_bugfix.nim Normal file
View File

@ -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)