summary refs log tree commit diff stats
path: root/compiler/ropes.nim
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2012-11-21 17:49:50 +0200
committerZahary Karadjov <zahary@gmail.com>2012-11-28 01:15:13 +0200
commitf644e3079f451c1ee71376ada120a789c696423b (patch)
treef9bf8071693127ebe4bfb31eb4bca93140234618 /compiler/ropes.nim
parente6f3f46cd94325253f9ade2e8fb39a494fce5072 (diff)
downloadNim-f644e3079f451c1ee71376ada120a789c696423b.tar.gz
experimental compile-time rope formatting code
Diffstat (limited to 'compiler/ropes.nim')
-rwxr-xr-xcompiler/ropes.nim25
1 files changed, 23 insertions, 2 deletions
diff --git a/compiler/ropes.nim b/compiler/ropes.nim
index 50c89e4d9..a539418ca 100755
--- a/compiler/ropes.nim
+++ b/compiler/ropes.nim
@@ -127,10 +127,15 @@ proc RopeInvariant(r: PRope): bool =
                   #      if result then result := ropeInvariant(r.right);
                   #    end 
 
+var gCacheTries* = 0
+var gCacheMisses* = 0
+
 proc insertInCache(s: string): PRope = 
+  inc gCacheTries
   var h = hash(s) and high(cache)
   result = cache[h]
   if isNil(result) or result.data != s:
+    inc gCacheMisses
     result = newRope(s)
     cache[h] = result
   
@@ -186,6 +191,10 @@ proc con(a: string, b: PRope): PRope = result = con(toRope(a), b)
 proc con(a: varargs[PRope]): PRope = 
   for i in countup(0, high(a)): result = con(result, a[i])
 
+proc ropeConcat*(a: varargs[PRope]): PRope =
+  # not overloaded version of concat to speed-up `rfmt` a little bit
+  for i in countup(0, high(a)): result = con(result, a[i])
+
 proc toRope(i: BiggestInt): PRope = result = toRope($i)
 
 proc app(a: var PRope, b: PRope) = a = con(a, b)
@@ -212,6 +221,10 @@ proc WriteRope*(head: PRope, filename: string, useWarning = false) =
     rawMessage(if useWarning: warnCannotOpenFile else: errCannotOpenFile,
                filename)
 
+var
+  rnl* = tnl.newRope
+  softRnl* = tnl.newRope
+
 proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope = 
   var i = 0
   var length = len(frmt)
@@ -240,10 +253,10 @@ proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope =
         else:
           app(result, args[j - 1])
       of 'n':
-        if optLineDir notin gOptions: app(result, tnl)
+        app(result, softRnl)
         inc i
       of 'N':
-        app(result, tnl)
+        app(result, rnl)
         inc(i)
       else: InternalError("ropes: invalid format string $" & frmt[i])
     var start = i
@@ -254,6 +267,14 @@ proc ropef(frmt: TFormatStr, args: varargs[PRope]): PRope =
       app(result, substr(frmt, start, i - 1))
   assert(RopeInvariant(result))
 
+{.push stack_trace: off, line_trace: off.}
+proc `~`*(r: expr[string]): PRope =
+  # this is the new optimized "to rope" operator
+  # the mnemonic is that `~` looks a bit like a rope :)
+  var r {.global.} = r.ropef
+  return r
+{.pop.}
+
 proc appf(c: var PRope, frmt: TFormatStr, args: varargs[PRope]) = 
   app(c, ropef(frmt, args))