From 42516c0086faa41c0d8613759ebc263bd1e989e9 Mon Sep 17 00:00:00 2001 From: Araq Date: Fri, 7 Oct 2011 09:02:08 +0200 Subject: code generator supports constant sequences; more consistent compile time evaluation --- lib/pure/pegs.nim | 10 +++++----- lib/pure/strutils.nim | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) (limited to 'lib/pure') diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index 2a680299d..25637cfee 100755 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -1651,20 +1651,20 @@ proc peg*(pattern: string): TPeg = ## peg"{\ident} \s* '=' \s* {.*}" result = parsePeg(pattern, "pattern") -proc escapePeg*(s: string): string = +proc escapePeg*(s: string): string = ## escapes `s` so that it is matched verbatim when used as a peg. result = "" var inQuote = false - for c in items(s): + for c in items(s): case c - of '\0'..'\31', '\'', '"', '\\': - if inQuote: + of '\0'..'\31', '\'', '"', '\\': + if inQuote: result.add('\'') inQuote = false result.add("\\x") result.add(toHex(ord(c), 2)) else: - if not inQuote: + if not inQuote: result.add('\'') inQuote = true result.add(c) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 53aa34c83..5f91b65b5 100755 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -8,8 +8,8 @@ # ## This module contains various string utility routines. -## See the module `re` for regular expression support. -## See the module `pegs` for PEG support. +## See the module `re `_ for regular expression support. +## See the module `pegs `_ for PEG support. import parseutils @@ -369,6 +369,19 @@ proc splitLines*(s: string): seq[string] {.noSideEffect, ## sequence of substrings. accumulateResult(splitLines(s)) +proc countLines*(s: string): int {.noSideEffect, + rtl, extern: "nsuCountLines".} = + ## same as ``len(splitLines(s))``, but much more efficient. + var i = 0 + while i < s.len: + case s[i] + of '\c': + if s[i+1] == '\l': inc i + inc result + of '\l': inc result + else: nil + inc i + proc split*(s: string, seps: set[char] = Whitespace): seq[string] {. noSideEffect, rtl, extern: "nsuSplitCharSet".} = ## The same as the `split` iterator, but is a proc that returns a @@ -754,7 +767,7 @@ proc replace*(s: string, sub, by: char): string {.noSideEffect, proc delete*(s: var string, first, last: int) {.noSideEffect, rtl, extern: "nsuDelete".} = - ## Deletes in `s` the characters at position `first`..`last`. This modifies + ## Deletes in `s` the characters at position `first` .. `last`. This modifies ## `s` itself, it does not return a copy. var i = first var j = last+1 @@ -865,8 +878,8 @@ proc validIdentifier*(s: string): bool {.noSideEffect, proc editDistance*(a, b: string): int {.noSideEffect, rtl, extern: "nsuEditDistance".} = - ## returns the edit distance between `a` and `b`. This uses the - ## `Levenshtein`:idx: distance algorithm with only a linear memory overhead. + ## returns the edit distance between `a` and `b`. This uses the + ## `Levenshtein`:idx: distance algorithm with only a linear memory overhead. ## This implementation is highly optimized! var len1 = a.len var len2 = b.len @@ -958,7 +971,7 @@ proc c_sprintf(buf, frmt: CString) {.nodecl, importc: "sprintf", varargs, noSideEffect.} type - TFloatFormat* = enum + TFloatFormat* = enum ## the different modes of floating point formating ffDefault, ## use the shorter floating point notation ffDecimal, ## use decimal floating point notation ffScientific ## use scientific notation (using ``e`` character) -- cgit 1.4.1-2-gfad0