This commit is contained in:
Araq 2012-05-20 12:09:47 +02:00
commit a36a856a2f
3 changed files with 22 additions and 16 deletions

View file

@ -201,20 +201,20 @@ proc find*(s: string, pattern: TRegEx, start = 0): int =
return rawMatches[0] return rawMatches[0]
iterator findAll*(s: string, pattern: TRegEx, start = 0): string = iterator findAll*(s: string, pattern: TRegEx, start = 0): string =
## yields all matching captures of pattern in `s`. ## yields all matching *substrings* of `s` that match `pattern`.
var matches: array[0..MaxSubpatterns-1, string] var i = int32(start)
var i = start var rawMatches: array[0..maxSubpatterns * 3 - 1, cint]
while true: while true:
var j = find(s, pattern, matches, i) let res = pcre.Exec(pattern.h, pattern.e, s, len(s), i, 0'i32,
if j < 0: break cast[ptr cint](addr(rawMatches)), maxSubpatterns * 3)
i = j if res < 0'i32: break
for k in 0..maxSubPatterns-1: let a = rawMatches[0]
if isNil(matches[k]): break let b = rawMatches[1]
inc(i, matches[k].len) yield substr(s, int(a), int(b)-1)
yield matches[k] i = b
proc findAll*(s: string, pattern: TRegEx, start = 0): seq[string] = proc findAll*(s: string, pattern: TRegEx, start = 0): seq[string] =
## returns all matching captures of pattern in `s`. ## returns all matching *substrings* of `s` that match `pattern`.
## If it does not match, @[] is returned. ## If it does not match, @[] is returned.
accumulateResult(findAll(s, pattern, start)) accumulateResult(findAll(s, pattern, start))
@ -450,3 +450,7 @@ when isMainModule:
for word in split("00232this02939is39an22example111", re"\d+"): for word in split("00232this02939is39an22example111", re"\d+"):
writeln(stdout, word) writeln(stdout, word)
for x in findAll("abcdef", re"^{.}", 3):
assert x == "d"
for x in findAll("abcdef", re".", 3):
echo x

View file

@ -829,7 +829,7 @@ proc find*(s: string, pattern: TPeg,
return -1 return -1
iterator findAll*(s: string, pattern: TPeg, start = 0): string = iterator findAll*(s: string, pattern: TPeg, start = 0): string =
## yields all matching captures of pattern in `s`. ## yields all matching *substrings* of `s` that match `pattern`.
var c: TCaptures var c: TCaptures
c.origStart = start c.origStart = start
var i = start var i = start
@ -837,12 +837,12 @@ iterator findAll*(s: string, pattern: TPeg, start = 0): string =
c.ml = 0 c.ml = 0
var L = rawMatch(s, pattern, i, c) var L = rawMatch(s, pattern, i, c)
if L < 0: break if L < 0: break
for k in 0..c.ml-1: yield substr(s, c.matches[k][0], c.matches[k][1]) yield substr(s, i, i+L-1)
inc(i, L) inc(i, L)
proc findAll*(s: string, pattern: TPeg, start = 0): seq[string] {. proc findAll*(s: string, pattern: TPeg, start = 0): seq[string] {.
nosideEffect, rtl, extern: "npegs$1".} = nosideEffect, rtl, extern: "npegs$1".} =
## returns all matching captures of pattern in `s`. ## returns all matching *substrings* of `s` that match `pattern`.
## If it does not match, @[] is returned. ## If it does not match, @[] is returned.
accumulateResult(findAll(s, pattern, start)) accumulateResult(findAll(s, pattern, start))
@ -1744,7 +1744,7 @@ when isMainModule:
else: else:
assert false assert false
for x in findAll("abcdef", peg"{.}", 3): for x in findAll("abcdef", peg".", 3):
echo x echo x
for x in findAll("abcdef", peg"^{.}", 3): for x in findAll("abcdef", peg"^{.}", 3):

View file

@ -69,6 +69,8 @@ Changes affecting backwards compatibility
type class matching all proc types. Use ``proc ()`` to get the old meaning type class matching all proc types. Use ``proc ()`` to get the old meaning
denoting a proc expecing no arguments and returing no value. denoting a proc expecing no arguments and returing no value.
- Deprecated ``system.GC_setStrategy``. - Deprecated ``system.GC_setStrategy``.
- ``re.findAll`` and ``pegs.findAll`` don't return *captures* anymore but
matching *substrings*.
Compiler Additions Compiler Additions