Added 'mustCompile' which panicks if there is an error compiling

master
Aadhavan Srinivasan 2 days ago
parent 6869cd00a2
commit f629a0f08f

@ -1110,10 +1110,11 @@ func thompson(re []postfixNode) (Reg, error) {
} }
// Compiles the given regular expression into a Reg type, suitable for use with the // Compile compiles the given regular expression into a [Reg].
// matching functions. The second return value is non-nil if a compilation error has //
// occured. As such, the error value must be checked before using the Reg returned by this function. // An error value != nil indicates that the regex was invalid; the error message should provide
// The second parameter is an optional list of flags, passed to the parsing function shuntingYard. // detailed information on the nature of the error.
// The second parameter is a sequence of zero or more [ReFlag] values, that modify the behavior of the regex.
func Compile(re string, flags ...ReFlag) (Reg, error) { func Compile(re string, flags ...ReFlag) (Reg, error) {
nodes, err := shuntingYard(re, flags...) nodes, err := shuntingYard(re, flags...)
if err != nil { if err != nil {
@ -1125,3 +1126,12 @@ func Compile(re string, flags ...ReFlag) (Reg, error) {
} }
return reg, nil return reg, nil
} }
// MustCompile panicks if Compile returns an error. They are identical in all other respects.
func MustCompile(re string, flags ...ReFlag) Reg {
reg, err := Compile(re, flags...)
if err != nil {
panic(err)
}
return reg
}

Loading…
Cancel
Save