Change endpos default from -1 to int.high

This commit is contained in:
Oleh Prypin
2015-04-09 23:42:11 +03:00
parent 4e83fc5867
commit 7e44c08270
2 changed files with 12 additions and 12 deletions

View File

@@ -29,24 +29,24 @@ provides in its standard library is inadequate:
=== Operations
[[proc-find]]
==== find(string, Regex, start = 0, endpos = -1): RegexMatch
==== find(string, Regex, start = 0, endpos = int.high): RegexMatch
Finds the given pattern in the string between the end and start positions.
`start` :: The start point at which to start matching. `|abc` is `0`; `a|bc`
is `1`
`endpos` :: The maximum index for a match; `-1` means the end of the string,
otherwise it's an exclusive upper bound.
`endpos` :: The maximum index for a match; `int.high` means the end of the
string, otherwise it's an exclusive upper bound.
[[proc-match]]
==== match(string, Regex, start = 0, endpos = -1): RegexMatch
==== match(string, Regex, start = 0, endpos = int.high): RegexMatch
Like link:#proc-find[`find(...)`], but anchored to the start of the string.
This means that `"foo".match(re"f") == true`, but `"foo".match(re"o") ==
false`.
[[iter-find]]
==== iterator findIter(string, Regex, start = 0, endpos = -1): RegexMatch
==== iterator findIter(string, Regex, start = 0, endpos = int.high): RegexMatch
Works the same as link:#proc-find[`find(...)`], but finds every non-overlapping
match. `"2222".find(re"22")` is `"22", "22"`, not `"22", "22", "22"`.

View File

@@ -311,7 +311,7 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
result.pcreMatchBounds = newSeq[Slice[cint]](ceil(vecsize / 2).int)
result.pcreMatchBounds.setLen(vecsize div 3)
let strlen = if endpos == -1: str.len else: endpos
let strlen = if endpos == int.high: str.len else: endpos
let execRet = pcre.exec(pattern.pcreObj,
pattern.pcreExtra,
@@ -328,14 +328,14 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
else:
raise newException(AssertionError, "Internal error: errno " & $execRet)
proc match*(str: string, pattern: Regex, start = 0, endpos = -1): Option[RegexMatch] =
proc match*(str: string, pattern: Regex, start = 0, endpos = int.high): Option[RegexMatch] =
return str.matchImpl(pattern, start, endpos, pcre.ANCHORED)
iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMatch =
iterator findIter*(str: string, pattern: Regex, start = 0, endpos = int.high): RegexMatch =
# see pcredemo for explaination
let matchesCrLf = pattern.matchesCrLf()
let unicode = (getinfo[cint](pattern, pcre.INFO_OPTIONS) and pcre.UTF8) > 0
let endpos = if endpos == -1: str.len else: endpos
let endpos = if endpos == int.high: str.len else: endpos
var offset = start
var match: Option[RegexMatch]
@@ -371,14 +371,14 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMa
# do while
break
proc find*(str: string, pattern: Regex, start = 0, endpos = -1): Option[RegexMatch] =
proc find*(str: string, pattern: Regex, start = 0, endpos = int.high): Option[RegexMatch] =
## Returns a `RegexMatch` if there is a match between `start` and `endpos`, otherwise
## it returns nil.
##
## if `endpos == -1`, then `endpos = str.len`
## if `endpos == int.high`, then `endpos = str.len`
return str.matchImpl(pattern, start, endpos, 0)
proc findAll*(str: string, pattern: Regex, start = 0, endpos = -1): seq[string] =
proc findAll*(str: string, pattern: Regex, start = 0, endpos = int.high): seq[string] =
result = @[]
for match in str.findIter(pattern, start, endpos):
result.add(match.match)