Skip to content

Commit b8e8eaa

Browse files
BigInt runnableExamples: octal, binary, hex constructor (#16868)
Co-authored-by: Timothee Cour <[email protected]>
1 parent 2c70734 commit b8e8eaa

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/std/jsbigints.nim

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@ func big*(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)".} =
1010
## Constructor for `JsBigInt`.
1111
runnableExamples:
1212
doAssert big(1234567890) == big"1234567890"
13+
doAssert 0b1111100111.big == 0o1747.big and 0o1747.big == 999.big
1314

1415
func big*(integer: cstring): JsBigInt {.importjs: "BigInt(#)".} =
1516
## Constructor for `JsBigInt`.
1617
runnableExamples:
1718
doAssert big"-1" == big"1" - big"2"
19+
# supports decimal, binary, octal, hex:
20+
doAssert big"12" == 12.big
21+
doAssert big"0b101" == 0b101.big
22+
doAssert big"0o701" == 0o701.big
23+
doAssert big"0xdeadbeaf" == 0xdeadbeaf.big
24+
doAssert big"0xffffffffffffffff" == (1.big shl 64.big) - 1.big
1825

1926
func toCstring*(this: JsBigInt; radix: 2..36): cstring {.importjs: "#.toString(#)".} =
2027
## Converts from `JsBigInt` to `cstring` representation.
@@ -190,17 +197,18 @@ proc high*(_: typedesc[JsBigInt]): JsBigInt {.error:
190197

191198

192199
runnableExamples:
193-
let big1: JsBigInt = big"2147483647"
194-
let big2: JsBigInt = big"666"
195-
doAssert JsBigInt isnot int
196-
doAssert big1 != big2
197-
doAssert big1 > big2
198-
doAssert big1 >= big2
199-
doAssert big2 < big1
200-
doAssert big2 <= big1
201-
doAssert not(big1 == big2)
202-
let z = JsBigInt.default
203-
doAssert $z == "0n"
200+
block:
201+
let big1: JsBigInt = big"2147483647"
202+
let big2: JsBigInt = big"666"
203+
doAssert JsBigInt isnot int
204+
doAssert big1 != big2
205+
doAssert big1 > big2
206+
doAssert big1 >= big2
207+
doAssert big2 < big1
208+
doAssert big2 <= big1
209+
doAssert not(big1 == big2)
210+
let z = JsBigInt.default
211+
doAssert $z == "0n"
204212
block:
205213
var a: seq[JsBigInt]
206214
a.setLen 2

0 commit comments

Comments
 (0)