diff options
author | Araq <rumpf_a@web.de> | 2011-09-26 07:45:33 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-09-26 07:45:33 +0200 |
commit | 7c34357856f0922fb265d81f1d215008b2dd8c14 (patch) | |
tree | 63bf05691c6f5c0f6da96ef03116b5895d016cda /lib/pure/parseutils.nim | |
parent | 14968fba46a0c1128f38859b339c7384f9f96cd4 (diff) | |
download | Nim-7c34357856f0922fb265d81f1d215008b2dd8c14.tar.gz |
bugfix: $ escaping in interpolatedFragments
Diffstat (limited to 'lib/pure/parseutils.nim')
-rwxr-xr-x | lib/pure/parseutils.nim | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index 001872db2..0e1619cdd 100755 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -269,8 +269,9 @@ proc parseFloat*(s: string, number: var float, start = 0): int {. type TInterpolatedKind* = enum ## describes for `interpolatedFragments` ## which part of the interpolated string is - ## yielded; for example in "str$var${expr}" - ikStr, ## ``str`` part of the interpolated string + ## yielded; for example in "str$$$var${expr}" + ikStr, ## ``str`` part of the interpolated string + ikDollar, ## escaped ``$`` part of the interpolated string ikVar, ## ``var`` part of the interpolated string ikExpr ## ``expr`` part of the interpolated string @@ -281,7 +282,7 @@ iterator interpolatedFragments*(s: string): tuple[kind: TInterpolatedKind, ## Example: ## ## .. code-block:: nimrod - ## for k, v in interpolatedFragments(" $this is ${an example} "): + ## for k, v in interpolatedFragments(" $this is ${an example} $$"): ## echo "(", k, ", \"", v, "\")" ## ## Results in: @@ -291,12 +292,13 @@ iterator interpolatedFragments*(s: string): tuple[kind: TInterpolatedKind, ## (ikExpr, "this") ## (ikString, " is ") ## (ikExpr, "an example") - ## (ikString, " ") + ## (ikString, " ") + ## (ikDollar, "$") var i = 0 var kind: TInterpolatedKind while true: var j = i - if s[j] == '$' and s[j+1] != '$': + if s[j] == '$': if s[j+1] == '{': inc j, 2 var nesting = 0 @@ -320,11 +322,15 @@ iterator interpolatedFragments*(s: string): tuple[kind: TInterpolatedKind, while s[j] in IdentChars: inc(j) inc i # skip $ kind = ikVar + elif s[j+1] == '$': + inc j, 2 + inc i # skip $ + kind = ikDollar else: raise newException(EInvalidValue, "Unable to parse a varible name at " & s[i..s.len]) else: - while j < s.len and (s[j] != '$' or s[j+1] == '$'): inc j + while j < s.len and s[j] != '$': inc j kind = ikStr if j > i: # do not copy the trailing } for ikExpr: |