Remove findAll, rename findAllStr to findAll

This commit is contained in:
Flaviu Tamas 2015-01-19 15:22:02 -05:00
commit a5693675fb
3 changed files with 9 additions and 14 deletions

View file

@ -50,8 +50,7 @@ Arguments are the same as link:#proc-find[`find(...)`]
Variants: Variants:
- `proc findAll(...)` returns a `seq[RegexMatch]` - `proc findAll(...)` returns a `seq[string]`
- `proc findAllStr(...)` returns a `seq[string]`
[[proc-split]] [[proc-split]]
==== split(string, Regex, maxsplit = -1): seq[string] ==== split(string, Regex, maxsplit = -1): seq[string]

View file

@ -372,10 +372,7 @@ proc find*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
## if `endpos == -1`, then `endpos = str.len` ## if `endpos == -1`, then `endpos = str.len`
return str.matchImpl(pattern, start, endpos, 0) return str.matchImpl(pattern, start, endpos, 0)
proc findAll*(str: string, pattern: Regex, start = 0, endpos = -1): seq[RegexMatch] = proc findAll*(str: string, pattern: Regex, start = 0, endpos = -1): seq[string] =
accumulateResult(str.findIter(pattern, start, endpos))
proc findAllStr*(str: string, pattern: Regex, start = 0, endpos = -1): seq[string] =
result = @[] result = @[]
for match in str.findIter(pattern, start, endpos): for match in str.findIter(pattern, start, endpos):
result.add(match.match) result.add(match.match)

View file

@ -1,22 +1,21 @@
import unittest import unittest, sequtils, nre
include nre
suite "find": suite "find":
test "find text": test "find text":
check("3213a".find(re"[a-z]").match == "a") check("3213a".find(re"[a-z]").match == "a")
check("1 2 3 4 5 6 7 8 ".findAll(re" ").map( check(toSeq(findIter("1 2 3 4 5 6 7 8 ", re" ")).map(
proc (a: RegexMatch): string = a.match proc (a: RegexMatch): string = a.match
) == @[" ", " ", " ", " ", " ", " ", " ", " "]) ) == @[" ", " ", " ", " ", " ", " ", " ", " "])
test "find bounds": test "find bounds":
check("1 2 3 4 5 ".findAll(re" ").map( check(toSeq(findIter("1 2 3 4 5 ", re" ")).map(
proc (a: RegexMatch): Slice[int] = a.matchBounds proc (a: RegexMatch): Slice[int] = a.matchBounds
) == @[1..2, 3..4, 5..6, 7..8, 9..10]) ) == @[1..2, 3..4, 5..6, 7..8, 9..10])
test "overlapping find": test "overlapping find":
check("222".findAllStr(re"22") == @["22"]) check("222".findAll(re"22") == @["22"])
check("2222".findAllStr(re"22") == @["22", "22"]) check("2222".findAll(re"22") == @["22", "22"])
test "len 0 find": test "len 0 find":
check("".findAllStr(re"\ ") == newSeq[string]()) check("".findAll(re"\ ") == newSeq[string]())
check("".findAllStr(re"") == @[""]) check("".findAll(re"") == @[""])