Merge pull request #2164 from oderwat/fix-tohex
Fixing toHex() to not wrap for long lens + Test
This commit is contained in:
commit
43c023c8dd
2 changed files with 32 additions and 27 deletions
|
|
@ -395,11 +395,13 @@ proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
|
||||||
const
|
const
|
||||||
HexChars = "0123456789ABCDEF"
|
HexChars = "0123456789ABCDEF"
|
||||||
var
|
var
|
||||||
shift: BiggestInt
|
n = x
|
||||||
result = newString(len)
|
result = newString(len)
|
||||||
for j in countdown(len-1, 0):
|
for j in countdown(len-1, 0):
|
||||||
result[j] = HexChars[toU32(x shr shift) and 0xF'i32]
|
result[j] = HexChars[n and 0xF]
|
||||||
shift = shift + 4
|
n = n shr 4
|
||||||
|
# handle negative overflow
|
||||||
|
if n == 0 and x < 0: n = -1
|
||||||
|
|
||||||
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect,
|
||||||
rtl, extern: "nsuIntToStr".} =
|
rtl, extern: "nsuIntToStr".} =
|
||||||
|
|
|
||||||
|
|
@ -2,18 +2,18 @@ discard """
|
||||||
file: "tstrutil.nim"
|
file: "tstrutil.nim"
|
||||||
output: "ha/home/a1xyz/usr/bin"
|
output: "ha/home/a1xyz/usr/bin"
|
||||||
"""
|
"""
|
||||||
# test the new strutils module
|
# test the new strutils module
|
||||||
|
|
||||||
import
|
import
|
||||||
strutils
|
strutils
|
||||||
|
|
||||||
proc testStrip() =
|
proc testStrip() =
|
||||||
write(stdout, strip(" ha "))
|
write(stdout, strip(" ha "))
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
testStrip()
|
testStrip()
|
||||||
for p in split("/home/a1:xyz:/usr/bin", {':'}):
|
for p in split("/home/a1:xyz:/usr/bin", {':'}):
|
||||||
write(stdout, p)
|
write(stdout, p)
|
||||||
|
|
||||||
proc testDelete =
|
proc testDelete =
|
||||||
var s = "0123456789ABCDEFGH"
|
var s = "0123456789ABCDEFGH"
|
||||||
|
|
@ -25,25 +25,28 @@ proc testDelete =
|
||||||
assert s == "1236789ABCDEFG"
|
assert s == "1236789ABCDEFG"
|
||||||
|
|
||||||
testDelete()
|
testDelete()
|
||||||
|
|
||||||
assert(insertSep($1000_000) == "1_000_000")
|
assert(insertSep($1000_000) == "1_000_000")
|
||||||
assert(insertSep($232) == "232")
|
assert(insertSep($232) == "232")
|
||||||
assert(insertSep($12345, ',') == "12,345")
|
assert(insertSep($12345, ',') == "12,345")
|
||||||
assert(insertSep($0) == "0")
|
assert(insertSep($0) == "0")
|
||||||
|
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
|
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffix") == 0)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
|
assert(editDistance("prefix__hallo_suffix", "prefix__hallo_suffi1") == 1)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
|
assert(editDistance("prefix__hallo_suffix", "prefix__HALLO_suffix") == 5)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
|
assert(editDistance("prefix__hallo_suffix", "prefix__ha_suffix") == 3)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix") == 14)
|
assert(editDistance("prefix__hallo_suffix", "prefix") == 14)
|
||||||
assert(editDistance("prefix__hallo_suffix", "suffix") == 14)
|
assert(editDistance("prefix__hallo_suffix", "suffix") == 14)
|
||||||
assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
|
assert(editDistance("prefix__hallo_suffix", "prefix__hao_suffix") == 2)
|
||||||
|
|
||||||
assert "/1/2/3".rfind('/') == 4
|
assert "/1/2/3".rfind('/') == 4
|
||||||
assert "/1/2/3".rfind('/', 1) == 0
|
assert "/1/2/3".rfind('/', 1) == 0
|
||||||
assert "/1/2/3".rfind('0') == -1
|
assert "/1/2/3".rfind('0') == -1
|
||||||
|
|
||||||
main()
|
assert(toHex(100i16, 32) == "00000000000000000000000000000064")
|
||||||
#OUT ha/home/a1xyz/usr/bin
|
assert(toHex(-100i16, 32) == "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9C")
|
||||||
|
|
||||||
|
main()
|
||||||
|
#OUT ha/home/a1xyz/usr/bin
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue