From c577064977b7921f0db200b17a144343ebf92162 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Sun, 9 Feb 2025 08:58:46 -0500 Subject: [PATCH] Added string field to Reg, that contains the expression string; wrote method to return the string --- regex/compile.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/regex/compile.go b/regex/compile.go index 8e010dc..8dbcf37 100644 --- a/regex/compile.go +++ b/regex/compile.go @@ -12,18 +12,24 @@ var notDotChars []rune // A Reg represents the result of compiling a regular expression. It contains // the startState of the NFA representation of the regex, and the number of capturing -// groups in the regex. +// groups in the regex. It also contains the expression string. type Reg struct { start *nfaState numGroups int + str string } -// numSubexp eturns the number of sub-expressions in the given [Reg]. This is equivalent +// NumSubexp returns the number of sub-expressions in the given [Reg]. This is equivalent // to the number of capturing groups. func (r Reg) NumSubexp() int { return r.numGroups } +// String returns the string used to compile the expression. +func (r Reg) String() string { + return r.str +} + const concatRune rune = 0xF0001 // Flags for shuntingYard - control its behavior @@ -1128,7 +1134,8 @@ func thompson(re []postfixNode) (Reg, error) { concatenate(nfa[0], &lastState) - return Reg{nfa[0], numGroups}, nil + // The string is empty here, because we add it in Compile() + return Reg{nfa[0], numGroups, ""}, nil } @@ -1146,6 +1153,7 @@ func Compile(re string, flags ...ReFlag) (Reg, error) { if err != nil { return Reg{}, fmt.Errorf("error compiling regex: %w", err) } + reg.str = re return reg, nil }