Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libs/impore/re: Add note about the requirement of matches to be pre… #19081

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/impure/re.nim
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ proc findBounds*(buf: cstring, pattern: Regex, matches: var openArray[string],
## and the captured
## substrings in the array `matches`. If it does not match, nothing
## is written into `matches` and `(-1,0)` is returned.
##
## Note: The memory for `matches` needs to be allocated before this function is
## called, otherwise it will just remain empty.
var
rtarray = initRtArray[cint]((matches.len+1)*3)
rawMatches = rtarray.getRawData
Expand All @@ -171,6 +174,14 @@ proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
## and the captured substrings in the array `matches`.
## If it does not match, nothing
## is written into `matches` and `(-1,0)` is returned.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
runnableExamples:
var matches = newSeq[string](1)
let (first, last) = findBounds("Hello World", re"(W\w+)", matches)
doAssert first == 6
doAssert last == 10
doAssert matches[0] == "World"
result = findBounds(cstring(s), pattern, matches,
min(start, MaxReBufSize), min(s.len, MaxReBufSize))

Expand All @@ -182,6 +193,8 @@ proc findBounds*(buf: cstring, pattern: Regex,
## and the captured substrings in the array `matches`.
## If it does not match, nothing is written into `matches` and
## `(-1,0)` is returned.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
var
rtarray = initRtArray[cint]((matches.len+1)*3)
rawMatches = rtarray.getRawData
Expand All @@ -202,6 +215,14 @@ proc findBounds*(s: string, pattern: Regex,
## and the captured substrings in the array `matches`.
## If it does not match, nothing is written into `matches` and
## `(-1,0)` is returned.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
runnableExamples:
var matches = newSeq[tuple[first, last: int]](1)
let (first, last) = findBounds("Hello World", re"(\w+)", matches)
doAssert first == 0
doAssert last == 4
doAssert matches[0] == (0, 4)
result = findBounds(cstring(s), pattern, matches,
min(start, MaxReBufSize), min(s.len, MaxReBufSize))

Expand Down Expand Up @@ -255,13 +276,17 @@ proc matchLen*(s: string, pattern: Regex, matches: var openArray[string],
## the same as `match`, but it returns the length of the match,
## if there is no match, `-1` is returned. Note that a match length
## of zero can happen.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
result = matchOrFind(cstring(s), pattern, matches, start.cint, s.len.cint, pcre.ANCHORED)

proc matchLen*(buf: cstring, pattern: Regex, matches: var openArray[string],
start = 0, bufSize: int): int {.inline.} =
## the same as `match`, but it returns the length of the match,
## if there is no match, `-1` is returned. Note that a match length
## of zero can happen.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
return matchOrFind(buf, pattern, matches, start.cint, bufSize.cint, pcre.ANCHORED)

proc matchLen*(s: string, pattern: Regex, start = 0): int {.inline.} =
Expand Down Expand Up @@ -292,6 +317,7 @@ proc match*(s: string, pattern: Regex, matches: var openArray[string],
## match, nothing is written into `matches` and `false` is
## returned.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
runnableExamples:
import std/sequtils
var matches: array[2, string]
Expand All @@ -306,6 +332,8 @@ proc match*(buf: cstring, pattern: Regex, matches: var openArray[string],
## match, nothing is written into `matches` and `false` is
## returned.
## `buf` has length `bufSize` (not necessarily `'\0'` terminated).
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
result = matchLen(buf, pattern, matches, start, bufSize) != -1

proc find*(buf: cstring, pattern: Regex, matches: var openArray[string],
Expand All @@ -314,6 +342,8 @@ proc find*(buf: cstring, pattern: Regex, matches: var openArray[string],
## substrings in the array `matches`. If it does not match, nothing
## is written into `matches` and `-1` is returned.
## `buf` has length `bufSize` (not necessarily `'\0'` terminated).
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
var
rtarray = initRtArray[cint]((matches.len+1)*3)
rawMatches = rtarray.getRawData
Expand All @@ -332,6 +362,8 @@ proc find*(s: string, pattern: Regex, matches: var openArray[string],
## returns the starting position of `pattern` in `s` and the captured
## substrings in the array `matches`. If it does not match, nothing
## is written into `matches` and `-1` is returned.
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
result = find(cstring(s), pattern, matches, start, s.len)

proc find*(buf: cstring, pattern: Regex, start = 0, bufSize: int): int =
Expand Down Expand Up @@ -433,6 +465,8 @@ proc contains*(s: string, pattern: Regex, start = 0): bool {.inline.} =
proc contains*(s: string, pattern: Regex, matches: var openArray[string],
start = 0): bool {.inline.} =
## same as `find(s, pattern, matches, start) >= 0`
##
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
return find(s, pattern, matches, start) >= 0

proc startsWith*(s: string, prefix: Regex): bool {.inline.} =
Expand Down