Skip to content

Commit

Permalink
fix endsWith with arbitrary bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Jan 15, 2024
1 parent a3f5389 commit 2eb9f6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -735,13 +735,17 @@ func endsWith*(s: string, pattern: Regex2): bool {.inline, raises: [].} =

debugCheckUtf8(s, pattern)
result = false
let binFlag = regexArbitraryBytes in pattern.toRegex.flags
var
m: RegexMatch2
i = 0
while i < s.len:
result = match(s, pattern, m, i)
if result: return
s.runeIncAt(i)
if binFlag:
inc i
else:
s.runeIncAt(i)

func addsubstr(
result: var string, s: string, first, last: int
Expand Down
9 changes: 9 additions & 0 deletions tests/tests2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3154,6 +3154,15 @@ when not defined(js) or NimMajor >= 2:
m.groupsCount == 0
check match("\x02\xF8\x95", re2(r"\x{2F895}", flags)) and
m.groupsCount == 0
check startsWith("\xff\xf0", re2(r"\xff", flags))
check(not startsWith("\xf0\xff", re2(r"\xff", flags)))
check endsWith("\xf0\xff", re2(r"\xff", flags))
check(not endsWith("\xff\xf0", re2(r"\xff", flags)))
check split("\xf0\xff\xf0", re2(r"\xff", flags)) == @["\xf0", "\xf0"]
check splitIncl("\xf0\xff\xf0", re2(r"(\xff)", flags)) ==
@["\xf0", "\xff", "\xf0"]
check contains("\xf0\xff\xf0", re2(r"\xff", flags))
check(not contains("\xf0\xf0", re2(r"\xff", flags)))

test "tarbitrary_bytes_flags":
let flags = {regexArbitraryBytes}
Expand Down

0 comments on commit 2eb9f6c

Please sign in to comment.