From 9e12f9dcb32199b23efbd0c7e954daa3c962e1ea Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 9 Feb 2025 15:38:26 -0500 Subject: [PATCH] Added field to Reg, denoting if we prefer longest match (POSIX style) or not (perl style) --- regex/compile.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/regex/compile.go b/regex/compile.go index 9a703b9..da733de 100644 --- a/regex/compile.go +++ b/regex/compile.go @@ -14,9 +14,10 @@ var notDotChars []rune // the startState of the NFA representation of the regex, and the number of capturing // groups in the regex. It also contains the expression string. type Reg struct { - start *nfaState - numGroups int - str string + start *nfaState + numGroups int + str string + preferLongest bool } // NumSubexp returns the number of sub-expressions in the given [Reg]. This is equivalent @@ -30,6 +31,10 @@ func (r Reg) String() string { return r.str } +func (r Reg) Longest() { + r.preferLongest = true +} + const concatRune rune = 0xF0001 // Flags for shuntingYard - control its behavior @@ -1135,7 +1140,7 @@ func thompson(re []postfixNode) (Reg, error) { concatenate(nfa[0], &lastState) // The string is empty here, because we add it in Compile() - return Reg{nfa[0], numGroups, ""}, nil + return Reg{nfa[0], numGroups, "", false}, nil }