simple micro-optimizations of ropes' runtime-formatting (#21962)

This commit is contained in:
heterodoxic
2023-05-30 13:41:56 +02:00
committed by GitHub
parent 40f88da90b
commit 546af8c571
2 changed files with 8 additions and 15 deletions

View File

@@ -216,13 +216,9 @@ macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope =
elif frmt[i] == '#' and frmt[i+1] == '#':
inc(i, 2)
strLit.add("#")
var start = i
while i < frmt.len:
if frmt[i] != '$' and frmt[i] != '#': inc(i)
else: break
if i - 1 >= start:
strLit.add(substr(frmt, start, i - 1))
else:
strLit.add(frmt[i])
inc(i)
flushStrLit()
result.add newCall(ident"rope", resVar)

View File

@@ -21,8 +21,8 @@ type
# though it is not necessary)
Rope* = string
proc newRopeAppender*(): string {.inline.} =
result = newString(0)
proc newRopeAppender*(cap = 80): string {.inline.} =
result = newStringOfCap(cap)
proc freeze*(r: Rope) {.inline.} = discard
@@ -102,12 +102,9 @@ proc runtimeFormat*(frmt: FormatStr, args: openArray[Rope]): Rope =
inc(i)
else:
doAssert false, "invalid format string: " & frmt
var start = i
while i < frmt.len:
if frmt[i] != '$': inc(i)
else: break
if i - 1 >= start:
result.add(substr(frmt, start, i - 1))
else:
result.add(frmt[i])
inc(i)
proc `%`*(frmt: static[FormatStr], args: openArray[Rope]): Rope =
runtimeFormat(frmt, args)