fix skipPrefixes bug
This commit is contained in:
parent
7fc30a8f1c
commit
3e9d64c896
12
stint/io.nim
12
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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue