|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package lazyregexp is a thin wrapper over regexp, allowing the use of global |
| 6 | +// regexp variables without forcing them to be compiled at init. |
| 7 | +package lazyregexp |
| 8 | + |
| 9 | +import ( |
| 10 | + "os" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + |
| 14 | + "github.com/grafana/regexp" |
| 15 | +) |
| 16 | + |
| 17 | +// Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be |
| 18 | +// compiled the first time it is needed. |
| 19 | +type Regexp struct { |
| 20 | + str string |
| 21 | + posix bool |
| 22 | + once sync.Once |
| 23 | + rx *regexp.Regexp |
| 24 | +} |
| 25 | + |
| 26 | +func (r *Regexp) Re() *regexp.Regexp { |
| 27 | + r.once.Do(r.build) |
| 28 | + return r.rx |
| 29 | +} |
| 30 | + |
| 31 | +func (r *Regexp) build() { |
| 32 | + if r.posix { |
| 33 | + r.rx = regexp.MustCompilePOSIX(r.str) |
| 34 | + } else { |
| 35 | + r.rx = regexp.MustCompile(r.str) |
| 36 | + } |
| 37 | + r.str = "" |
| 38 | +} |
| 39 | + |
| 40 | +func (r *Regexp) FindSubmatch(s []byte) [][]byte { |
| 41 | + return r.Re().FindSubmatch(s) |
| 42 | +} |
| 43 | + |
| 44 | +func (r *Regexp) FindStringSubmatch(s string) []string { |
| 45 | + return r.Re().FindStringSubmatch(s) |
| 46 | +} |
| 47 | + |
| 48 | +func (r *Regexp) FindStringSubmatchIndex(s string) []int { |
| 49 | + return r.Re().FindStringSubmatchIndex(s) |
| 50 | +} |
| 51 | + |
| 52 | +func (r *Regexp) ReplaceAllString(src, repl string) string { |
| 53 | + return r.Re().ReplaceAllString(src, repl) |
| 54 | +} |
| 55 | + |
| 56 | +func (r *Regexp) FindString(s string) string { |
| 57 | + return r.Re().FindString(s) |
| 58 | +} |
| 59 | + |
| 60 | +func (r *Regexp) FindAllString(s string, n int) []string { |
| 61 | + return r.Re().FindAllString(s, n) |
| 62 | +} |
| 63 | + |
| 64 | +func (r *Regexp) MatchString(s string) bool { |
| 65 | + return r.Re().MatchString(s) |
| 66 | +} |
| 67 | + |
| 68 | +func (r *Regexp) SubexpNames() []string { |
| 69 | + return r.Re().SubexpNames() |
| 70 | +} |
| 71 | + |
| 72 | +func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string { |
| 73 | + return r.Re().FindAllStringSubmatch(s, n) |
| 74 | +} |
| 75 | + |
| 76 | +func (r *Regexp) Split(s string, n int) []string { |
| 77 | + return r.Re().Split(s, n) |
| 78 | +} |
| 79 | + |
| 80 | +func (r *Regexp) ReplaceAllLiteralString(src, repl string) string { |
| 81 | + return r.Re().ReplaceAllLiteralString(src, repl) |
| 82 | +} |
| 83 | + |
| 84 | +func (r *Regexp) FindAllIndex(b []byte, n int) [][]int { |
| 85 | + return r.Re().FindAllIndex(b, n) |
| 86 | +} |
| 87 | + |
| 88 | +func (r *Regexp) Match(b []byte) bool { |
| 89 | + return r.Re().Match(b) |
| 90 | +} |
| 91 | + |
| 92 | +func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string { |
| 93 | + return r.Re().ReplaceAllStringFunc(src, repl) |
| 94 | +} |
| 95 | + |
| 96 | +func (r *Regexp) ReplaceAll(src, repl []byte) []byte { |
| 97 | + return r.Re().ReplaceAll(src, repl) |
| 98 | +} |
| 99 | + |
| 100 | +func (r *Regexp) SubexpIndex(s string) int { |
| 101 | + return r.Re().SubexpIndex(s) |
| 102 | +} |
| 103 | + |
| 104 | +var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test") |
| 105 | + |
| 106 | +// New creates a new lazy regexp, delaying the compiling work until it is first |
| 107 | +// needed. If the code is being run as part of tests, the regexp compiling will |
| 108 | +// happen immediately. |
| 109 | +func New(str string) *Regexp { |
| 110 | + lr := &Regexp{str: str} |
| 111 | + if inTest { |
| 112 | + // In tests, always compile the regexps early. |
| 113 | + lr.Re() |
| 114 | + } |
| 115 | + return lr |
| 116 | +} |
| 117 | + |
| 118 | +// NewPOSIX creates a new lazy regexp, delaying the compiling work until it is |
| 119 | +// first needed. If the code is being run as part of tests, the regexp |
| 120 | +// compiling will happen immediately. |
| 121 | +func NewPOSIX(str string) *Regexp { |
| 122 | + lr := &Regexp{str: str, posix: true} |
| 123 | + if inTest { |
| 124 | + // In tests, always compile the regexps early. |
| 125 | + lr.Re() |
| 126 | + } |
| 127 | + return lr |
| 128 | +} |
0 commit comments