Change endpos default from -1 to int.high

This commit is contained in:
Oleh Prypin 2015-04-09 23:42:11 +03:00
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 === Operations
[[proc-find]] [[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. 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` `start` :: The start point at which to start matching. `|abc` is `0`; `a|bc`
is `1` is `1`
`endpos` :: The maximum index for a match; `-1` means the end of the string, `endpos` :: The maximum index for a match; `int.high` means the end of the
otherwise it's an exclusive upper bound. string, otherwise it's an exclusive upper bound.
[[proc-match]] [[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. 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") == This means that `"foo".match(re"f") == true`, but `"foo".match(re"o") ==
false`. false`.
[[iter-find]] [[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 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"`. 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 = newSeq[Slice[cint]](ceil(vecsize / 2).int)
result.pcreMatchBounds.setLen(vecsize div 3) 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, let execRet = pcre.exec(pattern.pcreObj,
pattern.pcreExtra, pattern.pcreExtra,
@ -328,14 +328,14 @@ proc matchImpl(str: string, pattern: Regex, start, endpos: int, flags: int): Opt
else: else:
raise newException(AssertionError, "Internal error: errno " & $execRet) 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) 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 # see pcredemo for explaination
let matchesCrLf = pattern.matchesCrLf() let matchesCrLf = pattern.matchesCrLf()
let unicode = (getinfo[cint](pattern, pcre.INFO_OPTIONS) and pcre.UTF8) > 0 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 offset = start
var match: Option[RegexMatch] var match: Option[RegexMatch]
@ -371,14 +371,14 @@ iterator findIter*(str: string, pattern: Regex, start = 0, endpos = -1): RegexMa
# do while # do while
break 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 ## Returns a `RegexMatch` if there is a match between `start` and `endpos`, otherwise
## it returns nil. ## 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) 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 = @[] 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)