remove $ for ptr/ref, prefer using string over array of char
This commit is contained in:
parent
1f7fc7f279
commit
0852be2dec
3 changed files with 23 additions and 29 deletions
|
|
@ -16,27 +16,27 @@ proc reprInt(x: int64): string {.compilerproc.} = return $x
|
|||
proc reprFloat(x: float): string {.compilerproc.} = return $x
|
||||
|
||||
proc reprPointer(x: pointer): string {.compilerproc.} =
|
||||
var buf: array[0..59, char]
|
||||
discard c_sprintf(buf, "%p", x)
|
||||
return newString(buf)
|
||||
result = newString(60)
|
||||
let newLen = c_sprintf(result[0].addr, "%p", x)
|
||||
result.setLen newLen
|
||||
|
||||
proc `$`(x: uint64): string =
|
||||
if x == 0:
|
||||
result = "0"
|
||||
else:
|
||||
var buf: array[60, char]
|
||||
result = newString(60)
|
||||
var i = 0
|
||||
var n = x
|
||||
while n != 0:
|
||||
let nn = n div 10'u64
|
||||
buf[i] = char(n - 10'u64 * nn + ord('0'))
|
||||
result[i] = char(n - 10'u64 * nn + ord('0'))
|
||||
inc i
|
||||
n = nn
|
||||
result.setLen i
|
||||
|
||||
let half = i div 2
|
||||
# Reverse
|
||||
for t in 0 .. < half: swap(buf[t], buf[i-t-1])
|
||||
result = newString(buf)
|
||||
for t in 0 .. < half: swap(result[t], result[i-t-1])
|
||||
|
||||
proc reprStrAux(result: var string, s: cstring; len: int) =
|
||||
if cast[pointer](s) == nil:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue