improvements to addInt and $ for integer types (#18592)
* improvements to $(SomeInteger) and addInt * remove mIntToStr, mInt64ToStr * improvements * fix tests/pragmas/tinjectstmt.nim; the diff is harmless, cgen code is identical with -d:danger or debug mode * rm tests/system/tstrmantle.nim * revert compiler/jsgen.nim for -d:nimVersion140
This commit is contained in:
parent
7b58dc2de0
commit
394f4ac7bb
12 changed files with 162 additions and 186 deletions
|
|
@ -7,15 +7,18 @@ ok0
|
|||
ok1
|
||||
onInject: 3
|
||||
onInject: 4
|
||||
0
|
||||
onInject: 5
|
||||
0
|
||||
onInject: 6
|
||||
1
|
||||
onInject: 7
|
||||
onInject: 8
|
||||
1
|
||||
onInject: 9
|
||||
onInject: 10
|
||||
onInject: 11
|
||||
2
|
||||
ok2
|
||||
onInject: 9
|
||||
onInject: 12
|
||||
'''
|
||||
"""
|
||||
|
||||
|
|
@ -25,7 +28,7 @@ onInject: 9
|
|||
{.injectStmt.} pragma can be used to inject a statement before every
|
||||
other statement in the current module. It's now undocumented and may be removed
|
||||
in the future and replaced with something more general and without its limitations.
|
||||
e.g. (e.g. doesn't work in VM or js backends).
|
||||
(e.g. doesn't work in VM or js backends).
|
||||
]#
|
||||
|
||||
from system/ansi_c import c_printf
|
||||
|
|
@ -44,5 +47,5 @@ proc main()=
|
|||
echo a
|
||||
echo "ok2"
|
||||
|
||||
static: main() # xxx injectStmt not honred in VM
|
||||
static: main() # xxx injectStmt not honored in VM
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -102,40 +102,41 @@ block: # #14350, #16674, #16686 for JS
|
|||
doAssert nil2 == cstring("")
|
||||
|
||||
block:
|
||||
block:
|
||||
let x = -1'i8
|
||||
let y = uint32(x)
|
||||
|
||||
doAssert $y == "4294967295"
|
||||
|
||||
block:
|
||||
let x = -1'i16
|
||||
let y = uint32(x)
|
||||
|
||||
doAssert $y == "4294967295"
|
||||
|
||||
block:
|
||||
let x = -1'i32
|
||||
let y = uint32(x)
|
||||
|
||||
doAssert $y == "4294967295"
|
||||
when defined(js): # bug #18591
|
||||
let a1 = -1'i8
|
||||
let a2 = uint8(a1)
|
||||
# if `uint8(a1)` changes meaning to `cast[uint8](a1)` in future, update this test;
|
||||
# until then, this is the correct semantics.
|
||||
let a3 = $a2
|
||||
doAssert a2 < 3
|
||||
doAssert a3 == "-1"
|
||||
proc intToStr(a: uint8): cstring {.importjs: "(# + \"\")".}
|
||||
doAssert $intToStr(a2) == "-1"
|
||||
else:
|
||||
block:
|
||||
let x = -1'i8
|
||||
let y = uint32(x)
|
||||
doAssert $y == "4294967295"
|
||||
block:
|
||||
let x = -1'i16
|
||||
let y = uint32(x)
|
||||
doAssert $y == "4294967295"
|
||||
block:
|
||||
let x = -1'i32
|
||||
let y = uint32(x)
|
||||
doAssert $y == "4294967295"
|
||||
block:
|
||||
proc foo1(arg: int): string =
|
||||
let x = uint32(arg)
|
||||
$x
|
||||
doAssert $foo1(-1) == "4294967295"
|
||||
|
||||
block:
|
||||
let x = 4294967295'u32
|
||||
doAssert $x == "4294967295"
|
||||
|
||||
block:
|
||||
doAssert $(4294967295'u32) == "4294967295"
|
||||
|
||||
|
||||
block:
|
||||
proc foo1(arg: int): string =
|
||||
let x = uint32(arg)
|
||||
$x
|
||||
|
||||
doAssert $foo1(-1) == "4294967295"
|
||||
|
||||
|
||||
proc main()=
|
||||
block:
|
||||
let a = -0.0
|
||||
|
|
@ -159,6 +160,32 @@ proc main()=
|
|||
|
||||
doAssert $uint32.high == "4294967295"
|
||||
|
||||
block: # addInt
|
||||
var res = newStringOfCap(24)
|
||||
template test2(a, b) =
|
||||
res.setLen(0)
|
||||
res.addInt a
|
||||
doAssert res == b
|
||||
|
||||
for i in 0 .. 9:
|
||||
res.addInt int64(i)
|
||||
doAssert res == "0123456789"
|
||||
|
||||
res.setLen(0)
|
||||
for i in -9 .. 0:
|
||||
res.addInt int64(i)
|
||||
doAssert res == "-9-8-7-6-5-4-3-2-10"
|
||||
|
||||
when not defined(js):
|
||||
test2 high(int64), "9223372036854775807"
|
||||
test2 low(int64), "-9223372036854775808"
|
||||
|
||||
test2 high(int32), "2147483647"
|
||||
test2 low(int32), "-2147483648"
|
||||
test2 high(int16), "32767"
|
||||
test2 low(int16), "-32768"
|
||||
test2 high(int8), "127"
|
||||
test2 low(int8), "-128"
|
||||
|
||||
static: main()
|
||||
main()
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
var res = newStringOfCap(24)
|
||||
|
||||
for i in 0 .. 9:
|
||||
res.addInt int64(i)
|
||||
|
||||
doAssert res == "0123456789"
|
||||
|
||||
res.setLen(0)
|
||||
|
||||
for i in -9 .. 0:
|
||||
res.addInt int64(i)
|
||||
|
||||
doAssert res == "-9-8-7-6-5-4-3-2-10"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt high(int64)
|
||||
doAssert res == "9223372036854775807"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt low(int64)
|
||||
doAssert res == "-9223372036854775808"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt high(int32)
|
||||
doAssert res == "2147483647"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt low(int32)
|
||||
doAssert res == "-2147483648"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt high(int16)
|
||||
doAssert res == "32767"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt low(int16)
|
||||
doAssert res == "-32768"
|
||||
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt high(int8)
|
||||
doAssert res == "127"
|
||||
|
||||
res.setLen(0)
|
||||
res.addInt low(int8)
|
||||
doAssert res == "-128"
|
||||
Loading…
Add table
Add a link
Reference in a new issue