Skip to content

Commit

Permalink
[std/re]fix terrible and strange interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout authored May 16, 2021
1 parent 3614523 commit c218f2b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/impure/re.nim
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ proc matchOrFind(buf: cstring, pattern: Regex, matches: var openArray[string],
return rawMatches[1] - rawMatches[0]

proc findBounds*(buf: cstring, pattern: Regex, matches: var openArray[string],
start = 0, bufSize: int): tuple[first, last: int] =
start = 0, bufSize = 0): tuple[first, last: int] =

This comment has been minimized.

Copy link
@timotheecour

timotheecour May 16, 2021

Member

but won't that result in this being legal:
proc find("foo", pat, matches)
and silently doing the wrong thing (treating input as length 0), which is surprising?

what if we "fixed" the api by deprecating this and adding:

proc find*(buf: openArray[char], pattern: Regex, matches: var openArray[string],start = 0)

could that work without ambiguities?

This comment has been minimized.

Copy link
@ringabout

ringabout May 16, 2021

Author Member

see #18028

## returns the starting position and end position of `pattern` in `buf`
## (where `buf` has length `bufSize` and is not necessarily `'\0'` terminated),
## and the captured
Expand Down Expand Up @@ -200,7 +200,7 @@ proc findBounds*(s: string, pattern: Regex,
result = findBounds(cstring(s), pattern, matches, start, s.len)

proc findBounds*(buf: cstring, pattern: Regex,
start = 0, bufSize: int): tuple[first, last: int] =
start = 0, bufSize = 0): tuple[first, last: int] =
## returns the `first` and `last` position of `pattern` in `buf`,
## where `buf` has length `bufSize` (not necessarily `'\0'` terminated).
## If it does not match, `(-1,0)` is returned.
Expand Down Expand Up @@ -239,7 +239,7 @@ proc matchLen*(s: string, pattern: Regex, matches: var openArray[string],
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.} =
start = 0, bufSize = 0): 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.
Expand Down Expand Up @@ -281,7 +281,7 @@ proc match*(s: string, pattern: Regex, matches: var openArray[string],
result = matchLen(cstring(s), pattern, matches, start, s.len) != -1

proc match*(buf: cstring, pattern: Regex, matches: var openArray[string],
start = 0, bufSize: int): bool {.inline.} =
start = 0, bufSize = 0): bool {.inline.} =
## returns `true` if `buf[start..<bufSize]` matches the `pattern` and
## the captured substrings in the array `matches`. If it does not
## match, nothing is written into `matches` and `false` is
Expand Down Expand Up @@ -315,7 +315,7 @@ proc find*(s: string, pattern: Regex, matches: var openArray[string],
## is written into `matches` and `-1` is returned.
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 = 0): int =
## returns the starting position of `pattern` in `buf`,
## where `buf` has length `bufSize` (not necessarily `'\0'` terminated).
## If it does not match, `-1` is returned.
Expand Down Expand Up @@ -359,7 +359,7 @@ iterator findAll*(s: string, pattern: Regex, start = 0): string =
yield substr(s, int(a), int(b)-1)
i = b

iterator findAll*(buf: cstring, pattern: Regex, start = 0, bufSize: int): string =
iterator findAll*(buf: cstring, pattern: Regex, start = 0, bufSize = 0): string =
## Yields all matching `substrings` of `s` that match `pattern`.
##
## Note that since this is an iterator you should not modify the string you
Expand Down

0 comments on commit c218f2b

Please sign in to comment.