From 7e44c08270004a7783e130e5b43dbca5d9a10245 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Thu, 9 Apr 2015 23:42:11 +0300 Subject: Change endpos default from -1 to int.high --- README.asciidoc | 10 +++++----- src/nre.nim | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index a54a786a4..dfdc305fe 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -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"`. diff --git a/src/nre.nim b/src/nre.nim index 14b15cf5c..a9bbc31d5 100644 --- a/src/nre.nim +++ b/src/nre.nim @@ -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) -- cgit 1.4.1-2-gfad0