diff options
Diffstat (limited to 'lib/pure')
-rwxr-xr-x | lib/pure/parseutils.nim | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index 0e1619cdd..c78dbcf6f 100755 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -274,30 +274,30 @@ type ikDollar, ## escaped ``$`` part of the interpolated string ikVar, ## ``var`` part of the interpolated string ikExpr ## ``expr`` part of the interpolated string - + iterator interpolatedFragments*(s: string): tuple[kind: TInterpolatedKind, - value: string] = - ## Tokenizes the string `s` into substrings for interpolation purposes. - ## - ## Example: - ## - ## .. code-block:: nimrod - ## for k, v in interpolatedFragments(" $this is ${an example} $$"): - ## echo "(", k, ", \"", v, "\")" - ## - ## Results in: - ## - ## .. code-block:: nimrod - ## (ikString, " ") - ## (ikExpr, "this") - ## (ikString, " is ") - ## (ikExpr, "an example") + value: string] = + ## Tokenizes the string `s` into substrings for interpolation purposes. + ## + ## Example: + ## + ## .. code-block:: nimrod + ## for k, v in interpolatedFragments(" $this is ${an example} $$"): + ## echo "(", k, ", \"", v, "\")" + ## + ## Results in: + ## + ## .. code-block:: nimrod + ## (ikString, " ") + ## (ikExpr, "this") + ## (ikString, " is ") + ## (ikExpr, "an example") ## (ikString, " ") - ## (ikDollar, "$") + ## (ikDollar, "$") var i = 0 - var kind: TInterpolatedKind - while true: - var j = i + var kind: TInterpolatedKind + while true: + var j = i if s[j] == '$': if s[j+1] == '{': inc j, 2 @@ -333,15 +333,15 @@ iterator interpolatedFragments*(s: string): tuple[kind: TInterpolatedKind, while j < s.len and s[j] != '$': inc j kind = ikStr if j > i: - # do not copy the trailing } for ikExpr: - yield (kind, substr(s, i, j-1-ord(kind == ikExpr))) - else: - break - i = j + # do not copy the trailing } for ikExpr: + yield (kind, substr(s, i, j-1-ord(kind == ikExpr))) + else: + break + i = j when isMainModule: - for k, v in interpolatedFragments("$test{} $this is ${an{ example}} "): - echo "(", k, ", \"", v, "\")" + for k, v in interpolatedFragments("$test{} $this is ${an{ example}} "): + echo "(", k, ", \"", v, "\")" {.pop.} |