mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 14:25:23 +00:00
simple micro-optimizations of ropes' runtime-formatting (#21962)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user