Added string field to Reg, that contains the expression string; wrote method to return the string

implementPCREMatchingRules
Aadhavan Srinivasan 4 weeks ago
parent d4e3942d27
commit c577064977

@ -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
}

Loading…
Cancel
Save