Merge pull request #2493 from arnetheduck/more-comp-ropes
More comp ropes
This commit is contained in:
commit
badd1a79f0
2 changed files with 141 additions and 129 deletions
|
|
@ -82,6 +82,7 @@ var errorHandler*: proc(err: RopesError, msg: string, useWarning = false)
|
||||||
# avoid dependency on msgs.nim
|
# avoid dependency on msgs.nim
|
||||||
|
|
||||||
proc len*(a: Rope): int =
|
proc len*(a: Rope): int =
|
||||||
|
## the rope's length
|
||||||
if a == nil: result = 0
|
if a == nil: result = 0
|
||||||
else: result = a.length
|
else: result = a.length
|
||||||
|
|
||||||
|
|
@ -134,6 +135,7 @@ proc insertInCache(s: string): Rope =
|
||||||
cache[h] = result
|
cache[h] = result
|
||||||
|
|
||||||
proc rope*(s: string): Rope =
|
proc rope*(s: string): Rope =
|
||||||
|
## Converts a string to a rope.
|
||||||
if s.len == 0:
|
if s.len == 0:
|
||||||
result = nil
|
result = nil
|
||||||
else:
|
else:
|
||||||
|
|
@ -141,34 +143,14 @@ proc rope*(s: string): Rope =
|
||||||
assert(ropeInvariant(result))
|
assert(ropeInvariant(result))
|
||||||
|
|
||||||
proc rope*(i: BiggestInt): Rope =
|
proc rope*(i: BiggestInt): Rope =
|
||||||
|
## Converts an int to a rope.
|
||||||
inc gCacheIntTries
|
inc gCacheIntTries
|
||||||
result = rope($i)
|
result = rope($i)
|
||||||
|
|
||||||
proc rope*(f: BiggestFloat): Rope =
|
proc rope*(f: BiggestFloat): Rope =
|
||||||
|
## Converts a float to a rope.
|
||||||
result = rope($f)
|
result = rope($f)
|
||||||
|
|
||||||
proc ropeSeqInsert(rs: var RopeSeq, r: Rope, at: Natural) =
|
|
||||||
var length = len(rs)
|
|
||||||
if at > length:
|
|
||||||
setLen(rs, at + 1)
|
|
||||||
else:
|
|
||||||
setLen(rs, length + 1) # move old rope elements:
|
|
||||||
for i in countdown(length, at + 1):
|
|
||||||
rs[i] = rs[i - 1] # this is correct, I used pen and paper to validate it
|
|
||||||
rs[at] = r
|
|
||||||
|
|
||||||
proc newRecRopeToStr(result: var string, resultLen: var int, r: Rope) =
|
|
||||||
var stack = @[r]
|
|
||||||
while len(stack) > 0:
|
|
||||||
var it = pop(stack)
|
|
||||||
while it.data == nil:
|
|
||||||
add(stack, it.right)
|
|
||||||
it = it.left
|
|
||||||
assert(it.data != nil)
|
|
||||||
copyMem(addr(result[resultLen]), addr(it.data[0]), it.length)
|
|
||||||
inc(resultLen, it.length)
|
|
||||||
assert(resultLen <= len(result))
|
|
||||||
|
|
||||||
proc `&`*(a, b: Rope): Rope =
|
proc `&`*(a, b: Rope): Rope =
|
||||||
if a == nil:
|
if a == nil:
|
||||||
result = b
|
result = b
|
||||||
|
|
@ -181,45 +163,46 @@ proc `&`*(a, b: Rope): Rope =
|
||||||
result.right = b
|
result.right = b
|
||||||
|
|
||||||
proc `&`*(a: Rope, b: string): Rope =
|
proc `&`*(a: Rope, b: string): Rope =
|
||||||
|
## the concatenation operator for ropes.
|
||||||
result = a & rope(b)
|
result = a & rope(b)
|
||||||
|
|
||||||
proc `&`*(a: string, b: Rope): Rope =
|
proc `&`*(a: string, b: Rope): Rope =
|
||||||
|
## the concatenation operator for ropes.
|
||||||
result = rope(a) & b
|
result = rope(a) & b
|
||||||
|
|
||||||
proc `&`*(a: openArray[Rope]): Rope =
|
proc `&`*(a: openArray[Rope]): Rope =
|
||||||
|
## the concatenation operator for an openarray of ropes.
|
||||||
for i in countup(0, high(a)): result = result & a[i]
|
for i in countup(0, high(a)): result = result & a[i]
|
||||||
|
|
||||||
proc add*(a: var Rope, b: Rope) =
|
proc add*(a: var Rope, b: Rope) =
|
||||||
|
## adds `b` to the rope `a`.
|
||||||
a = a & b
|
a = a & b
|
||||||
|
|
||||||
proc add*(a: var Rope, b: string) =
|
proc add*(a: var Rope, b: string) =
|
||||||
|
## adds `b` to the rope `a`.
|
||||||
a = a & b
|
a = a & b
|
||||||
|
|
||||||
proc `$`*(p: Rope): string =
|
iterator leaves*(r: Rope): string =
|
||||||
if p == nil:
|
## iterates over any leaf string in the rope `r`.
|
||||||
result = ""
|
if r != nil:
|
||||||
else:
|
var stack = @[r]
|
||||||
result = newString(p.length)
|
while stack.len > 0:
|
||||||
var resultLen = 0
|
var it = stack.pop
|
||||||
newRecRopeToStr(result, resultLen, p)
|
while isNil(it.data):
|
||||||
|
stack.add(it.right)
|
||||||
|
it = it.left
|
||||||
|
assert(it != nil)
|
||||||
|
assert(it.data != nil)
|
||||||
|
yield it.data
|
||||||
|
|
||||||
proc ropeConcat*(a: varargs[Rope]): Rope =
|
iterator items*(r: Rope): char =
|
||||||
# not overloaded version of concat to speed-up `rfmt` a little bit
|
## iterates over any character in the rope `r`.
|
||||||
for i in countup(0, high(a)): result = result & a[i]
|
for s in leaves(r):
|
||||||
|
for c in items(s): yield c
|
||||||
|
|
||||||
proc prepend*(a: var Rope, b: Rope) = a = b & a
|
proc writeRope*(f: File, r: Rope) =
|
||||||
proc prepend*(a: var Rope, b: string) = a = b & a
|
## writes a rope to a file.
|
||||||
|
for s in leaves(r): write(f, s)
|
||||||
proc writeRope*(f: File, c: Rope) =
|
|
||||||
var stack = @[c]
|
|
||||||
while len(stack) > 0:
|
|
||||||
var it = pop(stack)
|
|
||||||
while it.data == nil:
|
|
||||||
add(stack, it.right)
|
|
||||||
it = it.left
|
|
||||||
assert(it != nil)
|
|
||||||
assert(it.data != nil)
|
|
||||||
write(f, it.data)
|
|
||||||
|
|
||||||
proc writeRope*(head: Rope, filename: string, useWarning = false) =
|
proc writeRope*(head: Rope, filename: string, useWarning = false) =
|
||||||
var f: File
|
var f: File
|
||||||
|
|
@ -229,6 +212,19 @@ proc writeRope*(head: Rope, filename: string, useWarning = false) =
|
||||||
else:
|
else:
|
||||||
errorHandler(rCannotOpenFile, filename, useWarning)
|
errorHandler(rCannotOpenFile, filename, useWarning)
|
||||||
|
|
||||||
|
proc `$`*(r: Rope): string =
|
||||||
|
## converts a rope back to a string.
|
||||||
|
result = newString(r.len)
|
||||||
|
setLen(result, 0)
|
||||||
|
for s in leaves(r): add(result, s)
|
||||||
|
|
||||||
|
proc ropeConcat*(a: varargs[Rope]): Rope =
|
||||||
|
# not overloaded version of concat to speed-up `rfmt` a little bit
|
||||||
|
for i in countup(0, high(a)): result = result & a[i]
|
||||||
|
|
||||||
|
proc prepend*(a: var Rope, b: Rope) = a = b & a
|
||||||
|
proc prepend*(a: var Rope, b: string) = a = b & a
|
||||||
|
|
||||||
var
|
var
|
||||||
rnl* = tnl.newRope
|
rnl* = tnl.newRope
|
||||||
softRnl* = tnl.newRope
|
softRnl* = tnl.newRope
|
||||||
|
|
@ -291,6 +287,7 @@ proc `%`*(frmt: FormatStr, args: openArray[Rope]): Rope =
|
||||||
assert(ropeInvariant(result))
|
assert(ropeInvariant(result))
|
||||||
|
|
||||||
proc addf*(c: var Rope, frmt: FormatStr, args: openArray[Rope]) =
|
proc addf*(c: var Rope, frmt: FormatStr, args: openArray[Rope]) =
|
||||||
|
## shortcut for ``add(c, frmt % args)``.
|
||||||
add(c, frmt % args)
|
add(c, frmt % args)
|
||||||
|
|
||||||
when true:
|
when true:
|
||||||
|
|
@ -307,12 +304,17 @@ else:
|
||||||
const
|
const
|
||||||
bufSize = 1024 # 1 KB is reasonable
|
bufSize = 1024 # 1 KB is reasonable
|
||||||
|
|
||||||
proc auxEqualsFile(r: Rope, f: File, buf: var array[bufSize, char],
|
proc equalsFile*(r: Rope, f: File): bool =
|
||||||
bpos, blen: var int): bool =
|
## returns true if the contents of the file `f` equal `r`.
|
||||||
if r.data != nil:
|
var
|
||||||
var dpos = 0
|
buf: array[bufSize, char]
|
||||||
let dlen = r.data.len
|
bpos = buf.len
|
||||||
while dpos < dlen:
|
blen = buf.len
|
||||||
|
|
||||||
|
for s in leaves(r):
|
||||||
|
var spos = 0
|
||||||
|
let slen = s.len
|
||||||
|
while spos < slen:
|
||||||
if bpos == blen:
|
if bpos == blen:
|
||||||
# Read more data
|
# Read more data
|
||||||
bpos = 0
|
bpos = 0
|
||||||
|
|
@ -320,26 +322,19 @@ proc auxEqualsFile(r: Rope, f: File, buf: var array[bufSize, char],
|
||||||
if blen == 0: # no more data in file
|
if blen == 0: # no more data in file
|
||||||
result = false
|
result = false
|
||||||
return
|
return
|
||||||
let n = min(blen - bpos, dlen - dpos)
|
let n = min(blen - bpos, slen - spos)
|
||||||
if not equalMem(addr(buf[bpos]), addr(r.data[dpos]), n):
|
# TODO There's gotta be a better way of comparing here...
|
||||||
|
if not equalMem(addr(buf[bpos]), cast[pointer](cast[int](cstring(s))+spos), n):
|
||||||
result = false
|
result = false
|
||||||
return
|
return
|
||||||
dpos += n
|
spos += n
|
||||||
bpos += n
|
bpos += n
|
||||||
result = true
|
|
||||||
else:
|
|
||||||
result = auxEqualsFile(r.left, f, buf, bpos, blen) and
|
|
||||||
auxEqualsFile(r.right, f, buf, bpos, blen)
|
|
||||||
|
|
||||||
proc equalsFile*(r: Rope, f: File): bool =
|
result = readBuffer(f, addr(buf[0]), 1) == 0 # check that we've read all
|
||||||
var
|
|
||||||
buf: array[bufSize, char]
|
|
||||||
bpos = bufSize
|
|
||||||
blen = bufSize
|
|
||||||
result = auxEqualsFile(r, f, buf, bpos, blen) and
|
|
||||||
readBuffer(f, addr(buf[0]), 1) == 0 # check that we've read all
|
|
||||||
|
|
||||||
proc equalsFile*(r: Rope, filename: string): bool =
|
proc equalsFile*(r: Rope, filename: string): bool =
|
||||||
|
## returns true if the contents of the file `f` equal `r`. If `f` does not
|
||||||
|
## exist, false is returned.
|
||||||
var f: File
|
var f: File
|
||||||
result = open(f, filename)
|
result = open(f, filename)
|
||||||
if result:
|
if result:
|
||||||
|
|
|
||||||
|
|
@ -52,9 +52,9 @@ proc len*(a: Rope): int {.rtl, extern: "nro$1".} =
|
||||||
## the rope's length
|
## the rope's length
|
||||||
if a == nil: result = 0
|
if a == nil: result = 0
|
||||||
else: result = a.length
|
else: result = a.length
|
||||||
|
|
||||||
proc newRope(): Rope = new(result)
|
proc newRope(): Rope = new(result)
|
||||||
proc newRope(data: string): Rope =
|
proc newRope(data: string): Rope =
|
||||||
new(result)
|
new(result)
|
||||||
result.length = len(data)
|
result.length = len(data)
|
||||||
result.data = data
|
result.data = data
|
||||||
|
|
@ -65,18 +65,18 @@ var
|
||||||
|
|
||||||
when countCacheMisses:
|
when countCacheMisses:
|
||||||
var misses, hits: int
|
var misses, hits: int
|
||||||
|
|
||||||
proc splay(s: string, tree: Rope, cmpres: var int): Rope =
|
proc splay(s: string, tree: Rope, cmpres: var int): Rope =
|
||||||
var c: int
|
var c: int
|
||||||
var t = tree
|
var t = tree
|
||||||
N.left = nil
|
N.left = nil
|
||||||
N.right = nil # reset to nil
|
N.right = nil # reset to nil
|
||||||
var le = N
|
var le = N
|
||||||
var r = N
|
var r = N
|
||||||
while true:
|
while true:
|
||||||
c = cmp(s, t.data)
|
c = cmp(s, t.data)
|
||||||
if c < 0:
|
if c < 0:
|
||||||
if (t.left != nil) and (s < t.left.data):
|
if (t.left != nil) and (s < t.left.data):
|
||||||
var y = t.left
|
var y = t.left
|
||||||
t.left = y.right
|
t.left = y.right
|
||||||
y.right = t
|
y.right = t
|
||||||
|
|
@ -85,8 +85,8 @@ proc splay(s: string, tree: Rope, cmpres: var int): Rope =
|
||||||
r.left = t
|
r.left = t
|
||||||
r = t
|
r = t
|
||||||
t = t.left
|
t = t.left
|
||||||
elif c > 0:
|
elif c > 0:
|
||||||
if (t.right != nil) and (s > t.right.data):
|
if (t.right != nil) and (s > t.right.data):
|
||||||
var y = t.right
|
var y = t.right
|
||||||
t.right = y.left
|
t.right = y.left
|
||||||
y.left = t
|
y.left = t
|
||||||
|
|
@ -95,8 +95,8 @@ proc splay(s: string, tree: Rope, cmpres: var int): Rope =
|
||||||
le.right = t
|
le.right = t
|
||||||
le = t
|
le = t
|
||||||
t = t.right
|
t = t.right
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
cmpres = c
|
cmpres = c
|
||||||
le.right = t.left
|
le.right = t.left
|
||||||
r.left = t.right
|
r.left = t.right
|
||||||
|
|
@ -104,50 +104,50 @@ proc splay(s: string, tree: Rope, cmpres: var int): Rope =
|
||||||
t.right = N.left
|
t.right = N.left
|
||||||
result = t
|
result = t
|
||||||
|
|
||||||
proc insertInCache(s: string, tree: Rope): Rope =
|
proc insertInCache(s: string, tree: Rope): Rope =
|
||||||
var t = tree
|
var t = tree
|
||||||
if t == nil:
|
if t == nil:
|
||||||
result = newRope(s)
|
result = newRope(s)
|
||||||
when countCacheMisses: inc(misses)
|
when countCacheMisses: inc(misses)
|
||||||
return
|
return
|
||||||
var cmp: int
|
var cmp: int
|
||||||
t = splay(s, t, cmp)
|
t = splay(s, t, cmp)
|
||||||
if cmp == 0:
|
if cmp == 0:
|
||||||
# We get here if it's already in the Tree
|
# We get here if it's already in the Tree
|
||||||
# Don't add it again
|
# Don't add it again
|
||||||
result = t
|
result = t
|
||||||
when countCacheMisses: inc(hits)
|
when countCacheMisses: inc(hits)
|
||||||
else:
|
else:
|
||||||
when countCacheMisses: inc(misses)
|
when countCacheMisses: inc(misses)
|
||||||
result = newRope(s)
|
result = newRope(s)
|
||||||
if cmp < 0:
|
if cmp < 0:
|
||||||
result.left = t.left
|
result.left = t.left
|
||||||
result.right = t
|
result.right = t
|
||||||
t.left = nil
|
t.left = nil
|
||||||
else:
|
else:
|
||||||
# i > t.item:
|
# i > t.item:
|
||||||
result.right = t.right
|
result.right = t.right
|
||||||
result.left = t
|
result.left = t
|
||||||
t.right = nil
|
t.right = nil
|
||||||
|
|
||||||
proc rope*(s: string): Rope {.rtl, extern: "nro$1Str".} =
|
proc rope*(s: string): Rope {.rtl, extern: "nro$1Str".} =
|
||||||
## Converts a string to a rope.
|
## Converts a string to a rope.
|
||||||
if s.len == 0:
|
if s.len == 0:
|
||||||
result = nil
|
result = nil
|
||||||
elif cacheEnabled:
|
elif cacheEnabled:
|
||||||
result = insertInCache(s, cache)
|
result = insertInCache(s, cache)
|
||||||
cache = result
|
cache = result
|
||||||
else:
|
else:
|
||||||
result = newRope(s)
|
result = newRope(s)
|
||||||
|
|
||||||
proc rope*(i: BiggestInt): Rope {.rtl, extern: "nro$1BiggestInt".} =
|
proc rope*(i: BiggestInt): Rope {.rtl, extern: "nro$1BiggestInt".} =
|
||||||
## Converts an int to a rope.
|
## Converts an int to a rope.
|
||||||
result = rope($i)
|
result = rope($i)
|
||||||
|
|
||||||
proc rope*(f: BiggestFloat): Rope {.rtl, extern: "nro$1BiggestFloat".} =
|
proc rope*(f: BiggestFloat): Rope {.rtl, extern: "nro$1BiggestFloat".} =
|
||||||
## Converts a float to a rope.
|
## Converts a float to a rope.
|
||||||
result = rope($f)
|
result = rope($f)
|
||||||
|
|
||||||
proc enableCache*() {.rtl, extern: "nro$1".} =
|
proc enableCache*() {.rtl, extern: "nro$1".} =
|
||||||
## Enables the caching of leaves. This reduces the memory footprint at
|
## Enables the caching of leaves. This reduces the memory footprint at
|
||||||
## the cost of runtime efficiency.
|
## the cost of runtime efficiency.
|
||||||
|
|
@ -160,9 +160,9 @@ proc disableCache*() {.rtl, extern: "nro$1".} =
|
||||||
|
|
||||||
proc `&`*(a, b: Rope): Rope {.rtl, extern: "nroConcRopeRope".} =
|
proc `&`*(a, b: Rope): Rope {.rtl, extern: "nroConcRopeRope".} =
|
||||||
## the concatenation operator for ropes.
|
## the concatenation operator for ropes.
|
||||||
if a == nil:
|
if a == nil:
|
||||||
result = b
|
result = b
|
||||||
elif b == nil:
|
elif b == nil:
|
||||||
result = a
|
result = a
|
||||||
else:
|
else:
|
||||||
result = newRope()
|
result = newRope()
|
||||||
|
|
@ -177,16 +177,16 @@ proc `&`*(a, b: Rope): Rope {.rtl, extern: "nroConcRopeRope".} =
|
||||||
else:
|
else:
|
||||||
result.left = a
|
result.left = a
|
||||||
result.right = b
|
result.right = b
|
||||||
|
|
||||||
proc `&`*(a: Rope, b: string): Rope {.rtl, extern: "nroConcRopeStr".} =
|
proc `&`*(a: Rope, b: string): Rope {.rtl, extern: "nroConcRopeStr".} =
|
||||||
## the concatenation operator for ropes.
|
## the concatenation operator for ropes.
|
||||||
result = a & rope(b)
|
result = a & rope(b)
|
||||||
|
|
||||||
proc `&`*(a: string, b: Rope): Rope {.rtl, extern: "nroConcStrRope".} =
|
proc `&`*(a: string, b: Rope): Rope {.rtl, extern: "nroConcStrRope".} =
|
||||||
## the concatenation operator for ropes.
|
## the concatenation operator for ropes.
|
||||||
result = rope(a) & b
|
result = rope(a) & b
|
||||||
|
|
||||||
proc `&`*(a: openArray[Rope]): Rope {.rtl, extern: "nroConcOpenArray".} =
|
proc `&`*(a: openArray[Rope]): Rope {.rtl, extern: "nroConcOpenArray".} =
|
||||||
## the concatenation operator for an openarray of ropes.
|
## the concatenation operator for an openarray of ropes.
|
||||||
for i in countup(0, high(a)): result = result & a[i]
|
for i in countup(0, high(a)): result = result & a[i]
|
||||||
|
|
||||||
|
|
@ -219,7 +219,7 @@ iterator leaves*(r: Rope): string =
|
||||||
## iterates over any leaf string in the rope `r`.
|
## iterates over any leaf string in the rope `r`.
|
||||||
if r != nil:
|
if r != nil:
|
||||||
var stack = @[r]
|
var stack = @[r]
|
||||||
while stack.len > 0:
|
while stack.len > 0:
|
||||||
var it = stack.pop
|
var it = stack.pop
|
||||||
while isConc(it):
|
while isConc(it):
|
||||||
stack.add(it.right)
|
stack.add(it.right)
|
||||||
|
|
@ -227,7 +227,7 @@ iterator leaves*(r: Rope): string =
|
||||||
assert(it != nil)
|
assert(it != nil)
|
||||||
assert(it.data != nil)
|
assert(it.data != nil)
|
||||||
yield it.data
|
yield it.data
|
||||||
|
|
||||||
iterator items*(r: Rope): char =
|
iterator items*(r: Rope): char =
|
||||||
## iterates over any character in the rope `r`.
|
## iterates over any character in the rope `r`.
|
||||||
for s in leaves(r):
|
for s in leaves(r):
|
||||||
|
|
@ -237,7 +237,7 @@ proc write*(f: File, r: Rope) {.rtl, extern: "nro$1".} =
|
||||||
## writes a rope to a file.
|
## writes a rope to a file.
|
||||||
for s in leaves(r): write(f, s)
|
for s in leaves(r): write(f, s)
|
||||||
|
|
||||||
proc `$`*(r: Rope): string {.rtl, extern: "nroToString".}=
|
proc `$`*(r: Rope): string {.rtl, extern: "nroToString".}=
|
||||||
## converts a rope back to a string.
|
## converts a rope back to a string.
|
||||||
result = newString(r.len)
|
result = newString(r.len)
|
||||||
setLen(result, 0)
|
setLen(result, 0)
|
||||||
|
|
@ -251,25 +251,25 @@ when false:
|
||||||
new(result)
|
new(result)
|
||||||
result.length = -idx
|
result.length = -idx
|
||||||
|
|
||||||
proc compileFrmt(frmt: string): Rope =
|
proc compileFrmt(frmt: string): Rope =
|
||||||
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:
|
while i < length:
|
||||||
if frmt[i] == '$':
|
if frmt[i] == '$':
|
||||||
inc(i)
|
inc(i)
|
||||||
case frmt[i]
|
case frmt[i]
|
||||||
of '$':
|
of '$':
|
||||||
add(result, "$")
|
add(result, "$")
|
||||||
inc(i)
|
inc(i)
|
||||||
of '#':
|
of '#':
|
||||||
inc(i)
|
inc(i)
|
||||||
add(result, compiledArg(num+1))
|
add(result, compiledArg(num+1))
|
||||||
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 frmt[i] notin {'0'..'9'}: break
|
if frmt[i] notin {'0'..'9'}: break
|
||||||
|
|
@ -285,10 +285,10 @@ when false:
|
||||||
add(s, compiledArg(j))
|
add(s, compiledArg(j))
|
||||||
else: raise newException(EInvalidValue, "invalid format string")
|
else: raise newException(EInvalidValue, "invalid format string")
|
||||||
var start = i
|
var start = i
|
||||||
while i < length:
|
while i < length:
|
||||||
if frmt[i] != '$': inc(i)
|
if frmt[i] != '$': inc(i)
|
||||||
else: break
|
else: break
|
||||||
if i - 1 >= start:
|
if i - 1 >= start:
|
||||||
add(result, substr(frmt, start, i-1))
|
add(result, substr(frmt, start, i-1))
|
||||||
|
|
||||||
proc `%`*(frmt: string, args: openArray[Rope]): Rope {.
|
proc `%`*(frmt: string, args: openArray[Rope]): Rope {.
|
||||||
|
|
@ -340,29 +340,46 @@ proc addf*(c: var Rope, frmt: string, args: openArray[Rope]) {.
|
||||||
## shortcut for ``add(c, frmt % args)``.
|
## shortcut for ``add(c, frmt % args)``.
|
||||||
add(c, frmt % args)
|
add(c, frmt % args)
|
||||||
|
|
||||||
|
const
|
||||||
|
bufSize = 1024 # 1 KB is reasonable
|
||||||
|
|
||||||
proc equalsFile*(r: Rope, f: File): bool {.rtl, extern: "nro$1File".} =
|
proc equalsFile*(r: Rope, f: File): bool {.rtl, extern: "nro$1File".} =
|
||||||
## returns true if the contents of the file `f` equal `r`.
|
## returns true if the contents of the file `f` equal `r`.
|
||||||
var bufSize = 1024 # reasonable start value
|
var
|
||||||
var buf = alloc(bufSize)
|
buf: array[bufSize, char]
|
||||||
for s in leaves(r):
|
bpos = buf.len
|
||||||
if s.len > bufSize:
|
blen = buf.len
|
||||||
bufSize = max(bufSize * 2, s.len)
|
|
||||||
buf = realloc(buf, bufSize)
|
|
||||||
var readBytes = readBuffer(f, buf, s.len)
|
|
||||||
result = readBytes == s.len and equalMem(buf, cstring(s), s.len)
|
|
||||||
if not result: break
|
|
||||||
if result:
|
|
||||||
result = readBuffer(f, buf, 1) == 0 # really at the end of file?
|
|
||||||
dealloc(buf)
|
|
||||||
|
|
||||||
proc equalsFile*(r: Rope, f: string): bool {.rtl, extern: "nro$1Str".} =
|
for s in leaves(r):
|
||||||
|
var spos = 0
|
||||||
|
let slen = s.len
|
||||||
|
while spos < slen:
|
||||||
|
if bpos == blen:
|
||||||
|
# Read more data
|
||||||
|
bpos = 0
|
||||||
|
blen = readBuffer(f, addr(buf[0]), buf.len)
|
||||||
|
if blen == 0: # no more data in file
|
||||||
|
result = false
|
||||||
|
return
|
||||||
|
let n = min(blen - bpos, slen - spos)
|
||||||
|
# TODO There's gotta be a better way of comparing here...
|
||||||
|
if not equalMem(addr(buf[bpos]),
|
||||||
|
cast[pointer](cast[int](cstring(s))+spos), n):
|
||||||
|
result = false
|
||||||
|
return
|
||||||
|
spos += n
|
||||||
|
bpos += n
|
||||||
|
|
||||||
|
result = readBuffer(f, addr(buf[0]), 1) == 0 # check that we've read all
|
||||||
|
|
||||||
|
proc equalsFile*(r: Rope, filename: string): bool {.rtl, extern: "nro$1Str".} =
|
||||||
## returns true if the contents of the file `f` equal `r`. If `f` does not
|
## returns true if the contents of the file `f` equal `r`. If `f` does not
|
||||||
## exist, false is returned.
|
## exist, false is returned.
|
||||||
var bin: File
|
var f: File
|
||||||
result = open(bin, f)
|
result = open(f, filename)
|
||||||
if result:
|
if result:
|
||||||
result = equalsFile(r, bin)
|
result = equalsFile(r, f)
|
||||||
close(bin)
|
close(f)
|
||||||
|
|
||||||
new(N) # init dummy node for splay algorithm
|
new(N) # init dummy node for splay algorithm
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue