libs/impore/re: Add note about the requirement of matches to be pre-allocated (#19081)
Add few runnableExamples for `findBounds` for clarity. Fixes https://github.com/nim-lang/Nim/issues/18775
This commit is contained in:
parent
e01e543fce
commit
12d2901e89
1 changed files with 34 additions and 0 deletions
|
|
@ -152,6 +152,9 @@ proc findBounds*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
||||||
## and the captured
|
## and the captured
|
||||||
## substrings in the array `matches`. If it does not match, nothing
|
## substrings in the array `matches`. If it does not match, nothing
|
||||||
## is written into `matches` and `(-1,0)` is returned.
|
## 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
|
var
|
||||||
rtarray = initRtArray[cint]((matches.len+1)*3)
|
rtarray = initRtArray[cint]((matches.len+1)*3)
|
||||||
rawMatches = rtarray.getRawData
|
rawMatches = rtarray.getRawData
|
||||||
|
|
@ -171,6 +174,14 @@ proc findBounds*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
## and the captured substrings in the array `matches`.
|
## and the captured substrings in the array `matches`.
|
||||||
## If it does not match, nothing
|
## If it does not match, nothing
|
||||||
## is written into `matches` and `(-1,0)` is returned.
|
## 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,
|
result = findBounds(cstring(s), pattern, matches,
|
||||||
min(start, MaxReBufSize), min(s.len, MaxReBufSize))
|
min(start, MaxReBufSize), min(s.len, MaxReBufSize))
|
||||||
|
|
||||||
|
|
@ -182,6 +193,8 @@ proc findBounds*(buf: cstring, pattern: Regex,
|
||||||
## and the captured substrings in the array `matches`.
|
## and the captured substrings in the array `matches`.
|
||||||
## If it does not match, nothing is written into `matches` and
|
## If it does not match, nothing is written into `matches` and
|
||||||
## `(-1,0)` is returned.
|
## `(-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
|
var
|
||||||
rtarray = initRtArray[cint]((matches.len+1)*3)
|
rtarray = initRtArray[cint]((matches.len+1)*3)
|
||||||
rawMatches = rtarray.getRawData
|
rawMatches = rtarray.getRawData
|
||||||
|
|
@ -202,6 +215,14 @@ proc findBounds*(s: string, pattern: Regex,
|
||||||
## and the captured substrings in the array `matches`.
|
## and the captured substrings in the array `matches`.
|
||||||
## If it does not match, nothing is written into `matches` and
|
## If it does not match, nothing is written into `matches` and
|
||||||
## `(-1,0)` is returned.
|
## `(-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,
|
result = findBounds(cstring(s), pattern, matches,
|
||||||
min(start, MaxReBufSize), min(s.len, MaxReBufSize))
|
min(start, MaxReBufSize), min(s.len, MaxReBufSize))
|
||||||
|
|
||||||
|
|
@ -255,6 +276,8 @@ proc matchLen*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
## the same as `match`, but it returns the length of the match,
|
## 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
|
## if there is no match, `-1` is returned. Note that a match length
|
||||||
## of zero can happen.
|
## 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)
|
result = matchOrFind(cstring(s), pattern, matches, start.cint, s.len.cint, pcre.ANCHORED)
|
||||||
|
|
||||||
proc matchLen*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
proc matchLen*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
||||||
|
|
@ -262,6 +285,8 @@ proc matchLen*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
||||||
## the same as `match`, but it returns the length of the match,
|
## 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
|
## if there is no match, `-1` is returned. Note that a match length
|
||||||
## of zero can happen.
|
## 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)
|
return matchOrFind(buf, pattern, matches, start.cint, bufSize.cint, pcre.ANCHORED)
|
||||||
|
|
||||||
proc matchLen*(s: string, pattern: Regex, start = 0): int {.inline.} =
|
proc matchLen*(s: string, pattern: Regex, start = 0): int {.inline.} =
|
||||||
|
|
@ -292,6 +317,7 @@ proc match*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
## match, nothing is written into `matches` and `false` is
|
## match, nothing is written into `matches` and `false` is
|
||||||
## returned.
|
## returned.
|
||||||
##
|
##
|
||||||
|
## .. note:: The memory for `matches` needs to be allocated before this function is called, otherwise it will just remain empty.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
import std/sequtils
|
import std/sequtils
|
||||||
var matches: array[2, string]
|
var matches: array[2, string]
|
||||||
|
|
@ -306,6 +332,8 @@ proc match*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
||||||
## match, nothing is written into `matches` and `false` is
|
## match, nothing is written into `matches` and `false` is
|
||||||
## returned.
|
## returned.
|
||||||
## `buf` has length `bufSize` (not necessarily `'\0'` terminated).
|
## `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
|
result = matchLen(buf, pattern, matches, start, bufSize) != -1
|
||||||
|
|
||||||
proc find*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
proc find*(buf: cstring, pattern: Regex, matches: var openArray[string],
|
||||||
|
|
@ -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
|
## substrings in the array `matches`. If it does not match, nothing
|
||||||
## is written into `matches` and `-1` is returned.
|
## is written into `matches` and `-1` is returned.
|
||||||
## `buf` has length `bufSize` (not necessarily `'\0'` terminated).
|
## `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
|
var
|
||||||
rtarray = initRtArray[cint]((matches.len+1)*3)
|
rtarray = initRtArray[cint]((matches.len+1)*3)
|
||||||
rawMatches = rtarray.getRawData
|
rawMatches = rtarray.getRawData
|
||||||
|
|
@ -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
|
## returns the starting position of `pattern` in `s` and the captured
|
||||||
## substrings in the array `matches`. If it does not match, nothing
|
## substrings in the array `matches`. If it does not match, nothing
|
||||||
## is written into `matches` and `-1` is returned.
|
## 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)
|
result = find(cstring(s), pattern, matches, start, s.len)
|
||||||
|
|
||||||
proc find*(buf: cstring, pattern: Regex, start = 0, bufSize: int): int =
|
proc find*(buf: cstring, pattern: Regex, start = 0, bufSize: int): int =
|
||||||
|
|
@ -433,6 +465,8 @@ proc contains*(s: string, pattern: Regex, start = 0): bool {.inline.} =
|
||||||
proc contains*(s: string, pattern: Regex, matches: var openArray[string],
|
proc contains*(s: string, pattern: Regex, matches: var openArray[string],
|
||||||
start = 0): bool {.inline.} =
|
start = 0): bool {.inline.} =
|
||||||
## same as `find(s, pattern, matches, start) >= 0`
|
## 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
|
return find(s, pattern, matches, start) >= 0
|
||||||
|
|
||||||
proc startsWith*(s: string, prefix: Regex): bool {.inline.} =
|
proc startsWith*(s: string, prefix: Regex): bool {.inline.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue