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

Fix the front anchor optimization in multiline mode #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions lib/Text/Regex/TDFA/TDFA.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ nfaToDFA ((startIndex,aQNFA),aTagOp,aGroupInfo) co eo = Regex dfa startIndex ind
dfa = indexesToDFA [startIndex]
indexBounds = bounds aQNFA
tagBounds = bounds aTagOp
ifa = (not (multiline co)) && isDFAFrontAnchored dfa
ifa = isDFAFrontAnchored (multiline co) dfa

indexesToDFA = {-# SCC "nfaToDFA.indexesToDFA" #-} Trie.lookupAsc trie -- Lookup in cache

Expand Down Expand Up @@ -294,14 +294,19 @@ bestTrans aTagOP (f:fs) | null fs = canonical f
cw [] yy = foldr (\y rest -> comp Nothing (Just y) `mappend` rest) mempty yy


isDFAFrontAnchored :: DFA -> Bool
isDFAFrontAnchored = isDTFrontAnchored . d_dt
isDFAFrontAnchored :: Bool -> DFA -> Bool
isDFAFrontAnchored isMultiline = isDTFrontAnchored . d_dt
where
isDTFrontAnchored :: DT -> Bool
isDTFrontAnchored (Simple' {}) = False
isDTFrontAnchored (Testing' {dt_test=wt,dt_a=a,dt_b=b}) | wt == Test_BOL = isDTLosing b
isDTFrontAnchored (Testing' {dt_test=wt,dt_a=a,dt_b=b}) | isAnchorTest = isDTLosing b
| otherwise = isDTFrontAnchored a && isDTFrontAnchored b
where
-- Which tests are an anchor depends on if we are in multline mode or not. If not,
-- both ^ and \` are anchors, if we are, then only \` is an anchor
isAnchorTest
| isMultiline = wt == Test_BOB
| otherwise = wt == Test_BOB || wt == Test_BOL
-- can DT never win or accept a character (when following trans_single)?
isDTLosing :: DT -> Bool
isDTLosing (Testing' {dt_a=a',dt_b=b'}) = isDTLosing a' && isDTLosing b'
Expand Down
4 changes: 4 additions & 0 deletions regex-tdfa.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,7 @@ test-suite regex-tdfa-unittest

if flag(force-O2)
ghc-options: -O2

other-modules: AdditionalTests
AdditionalTests.Generate
AdditionalTests.Fencepost
Loading