Skip to content

Commit

Permalink
Move regexp compilation up for early error return
Browse files Browse the repository at this point in the history
  • Loading branch information
Tit Petric committed May 14, 2024
1 parent cca81cb commit b684b38
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ func newRouteRegexp(tpl string, typ regexpType, options routeRegexpOptions) (*ro
pattern.WriteByte('$')
}

// Compile full regexp.
patternStr := pattern.String()
reg, errCompile := RegexpCompileFunc(patternStr)
if errCompile != nil {
return nil, errCompile
}

// Check for capturing groups which used to work in older versions
if reg.NumSubexp() != len(idxs)/2 {
panic(fmt.Sprintf("route %s contains capture groups in its regexp. ", template) +
"Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)")
}

var wildcardHostPort bool
if typ == regexpTypeHost {
Expand All @@ -140,18 +151,6 @@ func newRouteRegexp(tpl string, typ regexpType, options routeRegexpOptions) (*ro
reverse.WriteByte('/')
}

// Compile full regexp.
reg, errCompile := RegexpCompileFunc(patternStr)
if errCompile != nil {
return nil, errCompile
}

// Check for capturing groups which used to work in older versions
if reg.NumSubexp() != len(idxs)/2 {
panic(fmt.Sprintf("route %s contains capture groups in its regexp. ", template) +
"Only non-capturing groups are accepted: e.g. (?:pattern) instead of (pattern)")
}

// Done!
return &routeRegexp{
template: template,
Expand Down

0 comments on commit b684b38

Please sign in to comment.