compiler_ropes: ropef -> %, appf -> addf

This commit is contained in:
Jacek Sieka
2015-03-29 22:12:39 +08:00
parent 171996465f
commit 91f42a2943

View File

@@ -78,11 +78,7 @@ type
proc toRope*(s: string): PRope proc toRope*(s: string): PRope
proc toRope*(i: BiggestInt): PRope proc toRope*(i: BiggestInt): PRope
proc ropef*(frmt: TFormatStr, args: varargs[PRope]): PRope
proc appf*(c: var PRope, frmt: TFormatStr, args: varargs[PRope])
# returns true if the rope r is the same as the contents of file f
proc ropeInvariant*(r: PRope): bool
# exported for debugging
# implementation # implementation
var errorHandler*: proc(err: TRopesError, msg: string, useWarning = false) var errorHandler*: proc(err: TRopesError, msg: string, useWarning = false)
@@ -247,38 +243,38 @@ var
rnl* = tnl.newRope rnl* = tnl.newRope
softRnl* = tnl.newRope softRnl* = tnl.newRope
proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope = proc `%`*(frmt: TFormatStr, args: openArray[PRope]): PRope =
var i = 0 var i = 0
var length = len(frmt) var length = len(frmt)
result = nil result = nil
var num = 0 var num = 0
while i <= length - 1: while i < length:
if frmt[i] == '$': if frmt[i] == '$':
inc(i) # skip '$' inc(i) # skip '$'
case frmt[i] case frmt[i]
of '$': of '$':
app(result, "$") add(result, "$")
inc(i) inc(i)
of '#': of '#':
inc(i) inc(i)
app(result, args[num]) add(result, args[num])
inc(num) inc(num)
of '0'..'9': of '0'..'9':
var j = 0 var j = 0
while true: while true:
j = (j * 10) + ord(frmt[i]) - ord('0') j = j * 10 + ord(frmt[i]) - ord('0')
inc(i) inc(i)
if (i > length + 0 - 1) or not (frmt[i] in {'0'..'9'}): break if (i > length - 1) or frmt[i] notin {'0'..'9'}: break
num = j num = j
if j > high(args) + 1: if j > high(args) + 1:
errorHandler(rInvalidFormatStr, $(j)) errorHandler(rInvalidFormatStr, $(j))
else: else:
app(result, args[j - 1]) add(result, args[j-1])
of 'n': of 'n':
app(result, softRnl) add(result, softRnl)
inc i inc(i)
of 'N': of 'N':
app(result, rnl) add(result, rnl)
inc(i) inc(i)
else: else:
errorHandler(rInvalidFormatStr, $(frmt[i])) errorHandler(rInvalidFormatStr, $(frmt[i]))
@@ -287,9 +283,18 @@ proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope =
if frmt[i] != '$': inc(i) if frmt[i] != '$': inc(i)
else: break else: break
if i - 1 >= start: if i - 1 >= start:
app(result, substr(frmt, start, i - 1)) add(result, substr(frmt, start, i - 1))
assert(ropeInvariant(result)) assert(ropeInvariant(result))
proc addf*(c: var PRope, frmt: TFormatStr, args: openArray[PRope]) =
add(c, frmt % args)
# TODO Compatibility names
proc ropef*(frmt: TFormatStr, args: varargs[PRope]): PRope =
result = frmt % args
proc appf*(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) =
addf(c, frmt, args)
when true: when true:
template `~`*(r: string): PRope = r.ropef template `~`*(r: string): PRope = r.ropef
else: else:
@@ -301,9 +306,6 @@ else:
return r return r
{.pop.} {.pop.}
proc appf(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) =
app(c, ropef(frmt, args))
const const
bufSize = 1024 # 1 KB is reasonable bufSize = 1024 # 1 KB is reasonable