feat: more efficient regexp use in parser

This commit is contained in:
James Houlahan
2020-08-17 17:31:31 +02:00
parent 6a7a77fc51
commit a2cf5374b9
3 changed files with 15 additions and 13 deletions

View File

@ -37,13 +37,13 @@ type Visit func(*Part) (interface{}, error)
type VisitorRule func(*Part, Visit) (interface{}, error)
type visitorRule struct {
re string
re *regexp.Regexp
fn VisitorRule
}
func (v *Visitor) RegisterRule(contentTypeRegex string, fn VisitorRule) *Visitor {
v.rules = append(v.rules, &visitorRule{
re: contentTypeRegex,
re: regexp.MustCompile(contentTypeRegex),
fn: fn,
})
@ -69,7 +69,7 @@ func (v *Visitor) visit(p *Part) (interface{}, error) {
func (v *Visitor) getRuleForContentType(contentType string) *visitorRule {
for _, rule := range v.rules {
if regexp.MustCompile(rule.re).MatchString(contentType) {
if rule.re.MatchString(contentType) {
return rule
}
}