diff options
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/algorithm.nim | 2 | ||||
-rwxr-xr-x | lib/pure/pegs.nim | 24 | ||||
-rwxr-xr-x | lib/pure/strutils.nim | 4 |
3 files changed, 23 insertions, 7 deletions
diff --git a/lib/pure/algorithm.nim b/lib/pure/algorithm.nim index c9e5b0e14..517819e1c 100644 --- a/lib/pure/algorithm.nim +++ b/lib/pure/algorithm.nim @@ -1,7 +1,7 @@ # # # Nimrod's Runtime Library -# (c) Copyright 2010 Andreas Rumpf +# (c) Copyright 2011 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index de968bff4..334f5dcd3 100755 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -874,7 +874,7 @@ proc endsWith*(s: string, suffix: TPeg, start = 0): bool {. for i in start .. s.len-1: if matchLen(s, suffix, i) == s.len - i: return true -proc replace*(s: string, sub: TPeg, by: string): string {. +proc replacef*(s: string, sub: TPeg, by: string): string {. nosideEffect, rtl, extern: "npegs$1".} = ## Replaces `sub` in `s` by the string `by`. Captures can be accessed in `by` ## with the notation ``$i`` and ``$#`` (see strutils.`%`). Examples: @@ -898,7 +898,23 @@ proc replace*(s: string, sub: TPeg, by: string): string {. else: addf(result, by, caps) inc(i, x) - # copy the rest: + add(result, copy(s, i)) + +proc replace*(s: string, sub: TPeg, by = ""): string {. + nosideEffect, rtl, extern: "npegs$1".} = + ## Replaces `sub` in `s` by the string `by`. Captures cannot be accessed + ## in `by`. + result = "" + var i = 0 + var caps: array[0..maxSubpatterns-1, string] + while i < s.len: + var x = matchLen(s, sub, caps, i) + if x <= 0: + add(result, s[i]) + inc(i) + else: + addf(result, by, caps) + inc(i, x) add(result, copy(s, i)) proc parallelReplace*(s: string, subs: openArray[ @@ -1691,7 +1707,7 @@ when isMainModule: """ assert($g2 == "((A B) / (C D))") assert match("cccccdddddd", g2) - assert("var1=key; var2=key2".replace(peg"{\ident}'='{\ident}", "$1<-$2$2") == + assert("var1=key; var2=key2".replacef(peg"{\ident}'='{\ident}", "$1<-$2$2") == "var1<-keykey; var2<-key2key2") assert "var1=key; var2=key2".endsWith(peg"{\ident}'='{\ident}") @@ -1722,7 +1738,7 @@ when isMainModule: assert match("EINE ÜBERSICHT UND AUSSERDEM", peg"(\upper \white*)+") assert(not match("456678", peg"(\letter)+")) - assert("var1 = key; var2 = key2".replace( + assert("var1 = key; var2 = key2".replacef( peg"\skip(\s*) {\ident}'='{\ident}", "$1<-$2$2") == "var1<-keykey;var2<-key2key2") diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 0673a9588..435f522eb 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -687,7 +687,7 @@ proc contains*(s: string, chars: set[char]): bool {.noSideEffect.} = ## Same as ``find(s, chars) >= 0``. return find(s, chars) >= 0 -proc replace*(s, sub, by: string): string {.noSideEffect, +proc replace*(s, sub: string, by = ""): string {.noSideEffect, rtl, extern: "nsuReplaceStr".} = ## Replaces `sub` in `s` by the string `by`. var a: TSkipTable @@ -800,7 +800,7 @@ proc escape*(s: string, prefix = "\"", suffix = "\""): string {.noSideEffect, ## by ``\xHH`` where ``HH`` is its hexadecimal value. ## The procedure has been designed so that its output is usable for many ## different common syntaxes. The resulting string is prefixed with - ## ``prefix`` and suffixed with ``suffix``. Both may be empty strings. + ## `prefix` and suffixed with `suffix`. Both may be empty strings. result = prefix for c in items(s): case c |