Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Mar 7, 2024
1 parent 4e68020 commit b04db22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/regex.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 1462,7 @@ when isMainModule:
doAssert replace("\xff\xff", re2(r"\xff", flags), "abc") == "abcabc"
doAssert(not match("\xf0", re2(r"\xff", flags)))
doAssert replace("\xf0", re2(r"\xff", flags), "abc") == "\xf0"
doAssert match("\x02\xF8\x95", re2(r"(?u). (?<=\x{2F895})", flags))

doAssert graph(toRegex(re2"^a $")) == """digraph graphname {
0 [label="q0";color=blue];
Expand Down
6 changes: 3 additions & 3 deletions src/regex/nfafindall2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 117,7 @@ func fillMatchImpl*(
mi: MatchItemIdx,
ms: RegexMatches2,
regex: Regex
) {.inline.} =
) =
template capt: untyped = ms.m[mi].capt
if m.namedGroups.len != regex.namedGroups.len:
m.namedGroups = regex.namedGroups
Expand All @@ -130,7 130,7 @@ func fillMatchImpl*(
m.captures[i] = nonCapture
m.boundaries = ms.m[mi].bounds

func dummyMatch*(ms: var RegexMatches2, i: int) {.inline.} =
func dummyMatch*(ms: var RegexMatches2, i: int) =
## hack to support `split` last value.
## we need to add the end boundary if
## it has not matched the end
Expand Down Expand Up @@ -182,7 182,7 @@ func submatch(
matched = match(ntn, cPrev.Rune, c.Rune)
of lookaroundKind:
let freezed = capts.freeze()
lookAroundTpl()
matched = lookAround(ntn, capts, captx, text, look, i, flags)
capts.unfreeze freezed
if captx != -1:
capts.keepAlive captx
Expand Down
34 changes: 21 additions & 13 deletions src/regex/nfamatch2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 34,43 @@ type
behind*: BehindSig
smL*: SmLookaround

template lookAroundTpl*: untyped {.dirty.} =
func lookAround*(
ntn: Node,
capts: var Capts3,
captIdx: var int32,
text: string,
look: var Lookaround,
start: int,
flags: MatchFlags
): bool =
template smL: untyped = look.smL
template smLa: untyped = smL.lastA
template smLb: untyped = smL.lastB
template zNfa: untyped = ntn.subExp.nfa
template subNfa: untyped = ntn.subExp.nfa
var flags2 = {mfAnchored}
if ntn.subExp.reverseCapts:
flags2.incl mfReverseCapts
if mfBytesInput in flags:
flags2.incl mfBytesInput
smL.grow()
smL.last.setLen zNfa.s.len
matched = case ntn.kind
smL.last.setLen subNfa.s.len
result = case ntn.kind
of reLookahead:
look.ahead(
smLa, smLb, capts, captx,
text, zNfa, look, i, flags2)
smLa, smLb, capts, captIdx, text, subNfa, look, start, flags2
)
of reNotLookahead:
not look.ahead(
smLa, smLb, capts, captx,
text, zNfa, look, i, flags2)
smLa, smLb, capts, captIdx, text, subNfa, look, start, flags2
)
of reLookbehind:
look.behind(
smLa, smLb, capts, captx,
text, zNfa, look, i, 0, flags2) != -1
smLa, smLb, capts, captIdx, text, subNfa, look, start, 0, flags2
) != -1
of reNotLookbehind:
look.behind(
smLa, smLb, capts, captx,
text, zNfa, look, i, 0, flags2) == -1
smLa, smLb, capts, captIdx, text, subNfa, look, start, 0, flags2
) == -1
else:
doAssert false
false
Expand Down Expand Up @@ -108,7 116,7 @@ template nextStateTpl(bwMatch = false): untyped {.dirty.} =
matched = match(ntn, cPrev.Rune, c)
of lookaroundKind:
let freezed = capts.freeze()
lookAroundTpl()
matched = lookAround(ntn, capts, captx, text, look, i, flags)
capts.unfreeze freezed
if captx != -1:
capts.keepAlive captx
Expand Down

0 comments on commit b04db22

Please sign in to comment.