|
| 1 | +package textmatch |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "regexp/syntax" |
| 6 | + "unicode" |
| 7 | +) |
| 8 | + |
| 9 | +func compile(s string) (Pattern, error) { |
| 10 | + reSyntax, err := syntax.Parse(s, syntax.Perl) |
| 11 | + if err == nil { |
| 12 | + if optimized := compileOptimized(s, reSyntax); optimized != nil { |
| 13 | + return optimized, nil |
| 14 | + } |
| 15 | + } |
| 16 | + return regexp.Compile(s) |
| 17 | +} |
| 18 | + |
| 19 | +func compileOptimized(s string, re *syntax.Regexp) Pattern { |
| 20 | + // .* |
| 21 | + isAny := func(re *syntax.Regexp) bool { |
| 22 | + return re.Op == syntax.OpStar && re.Sub[0].Op == syntax.OpAnyCharNotNL |
| 23 | + } |
| 24 | + // "literal" |
| 25 | + isLit := func(re *syntax.Regexp) bool { |
| 26 | + return re.Op == syntax.OpLiteral |
| 27 | + } |
| 28 | + // ^ |
| 29 | + isBegin := func(re *syntax.Regexp) bool { |
| 30 | + return re.Op == syntax.OpBeginText |
| 31 | + } |
| 32 | + // $ |
| 33 | + isEnd := func(re *syntax.Regexp) bool { |
| 34 | + return re.Op == syntax.OpEndText |
| 35 | + } |
| 36 | + |
| 37 | + // TODO: analyze what kind of regexps people use in rules |
| 38 | + // more often and optimize those as well. |
| 39 | + |
| 40 | + // lit => strings.Contains($input, lit) |
| 41 | + if re.Op == syntax.OpLiteral { |
| 42 | + return &containsLiteralMatcher{value: newInputValue(string(re.Rune))} |
| 43 | + } |
| 44 | + |
| 45 | + // `.*` lit `.*` => strings.Contains($input, lit) |
| 46 | + if re.Op == syntax.OpConcat && len(re.Sub) == 3 { |
| 47 | + if isAny(re.Sub[0]) && isLit(re.Sub[1]) && isAny(re.Sub[2]) { |
| 48 | + return &containsLiteralMatcher{value: newInputValue(string(re.Sub[1].Rune))} |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // `^` lit => strings.HasPrefix($input, lit) |
| 53 | + if re.Op == syntax.OpConcat && len(re.Sub) == 2 { |
| 54 | + if isBegin(re.Sub[0]) && isLit(re.Sub[1]) { |
| 55 | + return &prefixLiteralMatcher{value: newInputValue(string(re.Sub[1].Rune))} |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // lit `$` => strings.HasSuffix($input, lit) |
| 60 | + if re.Op == syntax.OpConcat && len(re.Sub) == 2 { |
| 61 | + if isLit(re.Sub[0]) && isEnd(re.Sub[1]) { |
| 62 | + return &suffixLiteralMatcher{value: newInputValue(string(re.Sub[0].Rune))} |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // `^` lit `$` => $input == lit |
| 67 | + if re.Op == syntax.OpConcat && len(re.Sub) == 3 { |
| 68 | + if isBegin(re.Sub[0]) && isLit(re.Sub[1]) && isEnd(re.Sub[2]) { |
| 69 | + return &eqLiteralMatcher{value: newInputValue(string(re.Sub[1].Rune))} |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // `^\p{Lu}` => prefixRunePredMatcher:unicode.IsUpper |
| 74 | + // `^\p{Ll}` => prefixRunePredMatcher:unicode.IsLower |
| 75 | + switch s { |
| 76 | + case `^\p{Lu}`: |
| 77 | + return &prefixRunePredMatcher{pred: unicode.IsUpper} |
| 78 | + case `^\p{Ll}`: |
| 79 | + return &prefixRunePredMatcher{pred: unicode.IsLower} |
| 80 | + } |
| 81 | + |
| 82 | + // Can't optimize. |
| 83 | + return nil |
| 84 | +} |
0 commit comments