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

No capture optimization for find bounds and split #140

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tests/tests2
tests/tests2.js
tests/tests_misc
tests/tests_misc.js
tests/test_bug
docs/ugh
bin/*
bench/bench
13 changes: 11 additions & 2 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,19 @@ func match*(s: string, pattern: Regex2): bool {.inline, raises: [].} =
when defined(noRegexOpt):
template findSomeOptTpl(s, pattern, ms, i): untyped =
findSomeImpl(s, pattern, ms, i)
template findSomeOptTpl(s, pattern, ms, i, flags): untyped =
findSomeImpl(s, pattern, ms, i, flags)
else:
template findSomeOptTpl(s, pattern, ms, i): untyped =
if pattern.litOpt.canOpt:
findSomeOptImpl(s, pattern, ms, i)
else:
findSomeImpl(s, pattern, ms, i)
template findSomeOptTpl(s, pattern, ms, i, flags): untyped =
if pattern.litOpt.canOpt:
findSomeOptImpl(s, pattern, ms, i, flags)
else:
findSomeImpl(s, pattern, ms, i, flags)

iterator findAll*(
s: string,
Expand Down Expand Up @@ -668,9 +675,10 @@ iterator findAllBounds*(
var i = start
var i2 = start-1
var ms: RegexMatches2
let flags = {mfNoCaptures}
while i <= len(s):
doAssert(i > i2); i2 = i
i = findSomeOptTpl(s, pattern.toRegex, ms, i)
i = findSomeOptTpl(s, pattern.toRegex, ms, i, flags)
#debugEcho i
if i < 0: break
for ab in ms.bounds:
Expand Down Expand Up @@ -735,9 +743,10 @@ iterator split*(s: string, sep: Regex2): string {.inline, raises: [].} =
i2 = -1
done = false
ms: RegexMatches2
flags = {mfNoCaptures}
while not done:
doAssert(i > i2); i2 = i
i = findSomeOptTpl(s, sep.toRegex, ms, i)
i = findSomeOptTpl(s, sep.toRegex, ms, i, flags)
done = i < 0 or i >= len(s)
if done: ms.dummyMatch(s.len)
for ab in ms.bounds:
Expand Down
15 changes: 9 additions & 6 deletions src/regex/nfafindall2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ func submatch(
if matched:
case ntn.kind
of reGroupStart:
captx = capts.diverge captx
capts[captx, ntn.idx].a = i
if mfNoCaptures notin flags:
captx = capts.diverge captx
capts[captx, ntn.idx].a = i
of reGroupEnd:
captx = capts.diverge captx
capts[captx, ntn.idx].b = i-1
if mfNoCaptures notin flags:
captx = capts.diverge captx
capts[captx, ntn.idx].b = i-1
of assertionKind - lookaroundKind:
matched = match(ntn, cPrev.Rune, c.Rune)
of lookaroundKind:
Expand Down Expand Up @@ -268,7 +270,8 @@ func findSomeOptImpl*(
text: string,
regex: Regex,
ms: var RegexMatches2,
start: Natural
start: Natural,
flags: MatchFlags = {}
): int =
template regexSize: untyped =
max(regex.litOpt.nfa.s.len, regex.nfa.s.len)
Expand All @@ -280,7 +283,7 @@ func findSomeOptImpl*(
doAssert opt.nfa.s.len > 0
initMaybeImpl(ms, regexSize, groupsLen)
ms.clear()
let flags = regex.flags.toMatchFlags + {mfFindMatchOpt}
let flags = regex.flags.toMatchFlags + flags + {mfFindMatchOpt}
let hasLits = opt.lits.len > 0
let step = max(1, opt.lits.len)
var limit = start.int
Expand Down
Loading