fewer temporary string allocations for the compiler's renderer (#7484)
This commit is contained in:
parent
f108e89670
commit
abfcbaa74c
1 changed files with 20 additions and 15 deletions
|
|
@ -173,25 +173,30 @@ proc put(g: var TSrcGen, kind: TTokType, s: string) =
|
||||||
else:
|
else:
|
||||||
g.pendingWhitespace = s.len
|
g.pendingWhitespace = s.len
|
||||||
|
|
||||||
proc toNimChar(c: char): string =
|
proc addNimChar(dst: var string; c: char): void =
|
||||||
case c
|
case c
|
||||||
of '\0': result = "\\x00" # not "\\0" to avoid ambiguous cases like "\\012".
|
of '\0': dst.add "\\x00" # not "\\0" to avoid ambiguous cases like "\\012".
|
||||||
of '\a': result = "\\a" # \x07
|
of '\a': dst.add "\\a" # \x07
|
||||||
of '\b': result = "\\b" # \x08
|
of '\b': dst.add "\\b" # \x08
|
||||||
of '\t': result = "\\t" # \x09
|
of '\t': dst.add "\\t" # \x09
|
||||||
of '\L': result = "\\L" # \x0A
|
of '\L': dst.add "\\L" # \x0A
|
||||||
of '\v': result = "\\v" # \x0B
|
of '\v': dst.add "\\v" # \x0B
|
||||||
of '\f': result = "\\f" # \x0C
|
of '\f': dst.add "\\f" # \x0C
|
||||||
of '\c': result = "\\c" # \x0D
|
of '\c': dst.add "\\c" # \x0D
|
||||||
of '\e': result = "\\e" # \x1B
|
of '\e': dst.add "\\e" # \x1B
|
||||||
of '\x01'..'\x06', '\x0E'..'\x1A', '\x1C'..'\x1F', '\x80'..'\xFF':
|
of '\x01'..'\x06', '\x0E'..'\x1A', '\x1C'..'\x1F', '\x80'..'\xFF':
|
||||||
result = "\\x" & strutils.toHex(ord(c), 2)
|
dst.add "\\x"
|
||||||
of '\'', '\"', '\\': result = '\\' & c
|
dst.add strutils.toHex(ord(c), 2)
|
||||||
else: result = c & ""
|
of '\'', '\"', '\\':
|
||||||
|
dst.add '\\'
|
||||||
|
dst.add c
|
||||||
|
else:
|
||||||
|
dst.add c
|
||||||
|
|
||||||
proc makeNimString(s: string): string =
|
proc makeNimString(s: string): string =
|
||||||
result = "\""
|
result = "\""
|
||||||
for i in countup(0, len(s)-1): add(result, toNimChar(s[i]))
|
for c in s:
|
||||||
|
result.addNimChar c
|
||||||
add(result, '\"')
|
add(result, '\"')
|
||||||
|
|
||||||
proc putComment(g: var TSrcGen, s: string) =
|
proc putComment(g: var TSrcGen, s: string) =
|
||||||
|
|
@ -364,7 +369,7 @@ proc atom(g: TSrcGen; n: PNode): string =
|
||||||
of nkStrLit: result = makeNimString(n.strVal)
|
of nkStrLit: result = makeNimString(n.strVal)
|
||||||
of nkRStrLit: result = "r\"" & replace(n.strVal, "\"", "\"\"") & '\"'
|
of nkRStrLit: result = "r\"" & replace(n.strVal, "\"", "\"\"") & '\"'
|
||||||
of nkTripleStrLit: result = "\"\"\"" & n.strVal & "\"\"\""
|
of nkTripleStrLit: result = "\"\"\"" & n.strVal & "\"\"\""
|
||||||
of nkCharLit: result = '\'' & toNimChar(chr(int(n.intVal))) & '\''
|
of nkCharLit: result = "\'"; result.addNimChar chr(int(n.intVal)); result.add '\''
|
||||||
of nkIntLit: result = litAux(g, n, n.intVal, 4)
|
of nkIntLit: result = litAux(g, n, n.intVal, 4)
|
||||||
of nkInt8Lit: result = litAux(g, n, n.intVal, 1) & "\'i8"
|
of nkInt8Lit: result = litAux(g, n, n.intVal, 1) & "\'i8"
|
||||||
of nkInt16Lit: result = litAux(g, n, n.intVal, 2) & "\'i16"
|
of nkInt16Lit: result = litAux(g, n, n.intVal, 2) & "\'i16"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue