|
| 1 | +package regexp |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "regexp" |
| 6 | + "sync" |
| 7 | +) |
| 8 | + |
| 9 | +// Regexp is a wrapper struct used for wrapping MustCompile regex expressions |
| 10 | +// used as global variables. Using this stucture helps speed the startup time |
| 11 | +// of apps that want to use global regex variables. This library initializes them on |
| 12 | +// first use as opposed to the start of the executable. |
| 13 | +type Regexp struct { |
| 14 | + once sync.Once |
| 15 | + regexp *regexp.Regexp |
| 16 | + val string |
| 17 | +} |
| 18 | + |
| 19 | +func Delayed(val string) Regexp { |
| 20 | + re := Regexp{ |
| 21 | + val: val, |
| 22 | + } |
| 23 | + if precompile { |
| 24 | + re.regexp = regexp.MustCompile(re.val) |
| 25 | + } |
| 26 | + return re |
| 27 | +} |
| 28 | + |
| 29 | +func (re *Regexp) compile() { |
| 30 | + if precompile { |
| 31 | + return |
| 32 | + } |
| 33 | + re.once.Do(func() { |
| 34 | + re.regexp = regexp.MustCompile(re.val) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +func (re *Regexp) Expand(dst []byte, template []byte, src []byte, match []int) []byte { |
| 39 | + re.compile() |
| 40 | + return re.regexp.Expand(dst, template, src, match) |
| 41 | +} |
| 42 | + |
| 43 | +func (re *Regexp) ExpandString(dst []byte, template string, src string, match []int) []byte { |
| 44 | + re.compile() |
| 45 | + return re.regexp.ExpandString(dst, template, src, match) |
| 46 | +} |
| 47 | +func (re *Regexp) Find(b []byte) []byte { |
| 48 | + re.compile() |
| 49 | + return re.regexp.Find(b) |
| 50 | +} |
| 51 | + |
| 52 | +func (re *Regexp) FindAll(b []byte, n int) [][]byte { |
| 53 | + re.compile() |
| 54 | + return re.regexp.FindAll(b, n) |
| 55 | +} |
| 56 | + |
| 57 | +func (re *Regexp) FindAllIndex(b []byte, n int) [][]int { |
| 58 | + re.compile() |
| 59 | + return re.regexp.FindAllIndex(b, n) |
| 60 | +} |
| 61 | + |
| 62 | +func (re *Regexp) FindAllString(s string, n int) []string { |
| 63 | + re.compile() |
| 64 | + return re.regexp.FindAllString(s, n) |
| 65 | +} |
| 66 | + |
| 67 | +func (re *Regexp) FindAllStringIndex(s string, n int) [][]int { |
| 68 | + re.compile() |
| 69 | + return re.regexp.FindAllStringIndex(s, n) |
| 70 | +} |
| 71 | + |
| 72 | +func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string { |
| 73 | + re.compile() |
| 74 | + return re.regexp.FindAllStringSubmatch(s, n) |
| 75 | +} |
| 76 | + |
| 77 | +func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int { |
| 78 | + re.compile() |
| 79 | + return re.regexp.FindAllStringSubmatchIndex(s, n) |
| 80 | +} |
| 81 | + |
| 82 | +func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte { |
| 83 | + re.compile() |
| 84 | + return re.regexp.FindAllSubmatch(b, n) |
| 85 | +} |
| 86 | + |
| 87 | +func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int { |
| 88 | + re.compile() |
| 89 | + return re.regexp.FindAllSubmatchIndex(b, n) |
| 90 | +} |
| 91 | + |
| 92 | +func (re *Regexp) FindIndex(b []byte) (loc []int) { |
| 93 | + re.compile() |
| 94 | + return re.regexp.FindIndex(b) |
| 95 | +} |
| 96 | + |
| 97 | +func (re *Regexp) FindReaderIndex(r io.RuneReader) (loc []int) { |
| 98 | + re.compile() |
| 99 | + return re.regexp.FindReaderIndex(r) |
| 100 | +} |
| 101 | + |
| 102 | +func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int { |
| 103 | + re.compile() |
| 104 | + return re.regexp.FindReaderSubmatchIndex(r) |
| 105 | +} |
| 106 | + |
| 107 | +func (re *Regexp) FindString(s string) string { |
| 108 | + re.compile() |
| 109 | + return re.regexp.FindString(s) |
| 110 | +} |
| 111 | + |
| 112 | +func (re *Regexp) FindStringIndex(s string) (loc []int) { |
| 113 | + re.compile() |
| 114 | + return re.regexp.FindStringIndex(s) |
| 115 | +} |
| 116 | + |
| 117 | +func (re *Regexp) FindStringSubmatch(s string) []string { |
| 118 | + re.compile() |
| 119 | + return re.regexp.FindStringSubmatch(s) |
| 120 | +} |
| 121 | + |
| 122 | +func (re *Regexp) FindStringSubmatchIndex(s string) []int { |
| 123 | + re.compile() |
| 124 | + return re.regexp.FindStringSubmatchIndex(s) |
| 125 | +} |
| 126 | + |
| 127 | +func (re *Regexp) FindSubmatch(b []byte) [][]byte { |
| 128 | + re.compile() |
| 129 | + return re.regexp.FindSubmatch(b) |
| 130 | +} |
| 131 | + |
| 132 | +func (re *Regexp) FindSubmatchIndex(b []byte) []int { |
| 133 | + re.compile() |
| 134 | + return re.regexp.FindSubmatchIndex(b) |
| 135 | +} |
| 136 | + |
| 137 | +func (re *Regexp) LiteralPrefix() (prefix string, complete bool) { |
| 138 | + re.compile() |
| 139 | + return re.regexp.LiteralPrefix() |
| 140 | +} |
| 141 | + |
| 142 | +func (re *Regexp) Longest() { |
| 143 | + re.compile() |
| 144 | + re.regexp.Longest() |
| 145 | +} |
| 146 | + |
| 147 | +func (re *Regexp) Match(b []byte) bool { |
| 148 | + re.compile() |
| 149 | + return re.regexp.Match(b) |
| 150 | +} |
| 151 | + |
| 152 | +func (re *Regexp) MatchReader(r io.RuneReader) bool { |
| 153 | + re.compile() |
| 154 | + return re.regexp.MatchReader(r) |
| 155 | +} |
| 156 | +func (re *Regexp) MatchString(s string) bool { |
| 157 | + re.compile() |
| 158 | + return re.regexp.MatchString(s) |
| 159 | +} |
| 160 | + |
| 161 | +func (re *Regexp) NumSubexp() int { |
| 162 | + re.compile() |
| 163 | + return re.regexp.NumSubexp() |
| 164 | +} |
| 165 | + |
| 166 | +func (re *Regexp) ReplaceAll(src, repl []byte) []byte { |
| 167 | + re.compile() |
| 168 | + return re.regexp.ReplaceAll(src, repl) |
| 169 | +} |
| 170 | + |
| 171 | +func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte { |
| 172 | + re.compile() |
| 173 | + return re.regexp.ReplaceAllFunc(src, repl) |
| 174 | +} |
| 175 | + |
| 176 | +func (re *Regexp) ReplaceAllLiteral(src, repl []byte) []byte { |
| 177 | + re.compile() |
| 178 | + return re.regexp.ReplaceAllLiteral(src, repl) |
| 179 | +} |
| 180 | + |
| 181 | +func (re *Regexp) ReplaceAllLiteralString(src, repl string) string { |
| 182 | + re.compile() |
| 183 | + return re.regexp.ReplaceAllLiteralString(src, repl) |
| 184 | +} |
| 185 | + |
| 186 | +func (re *Regexp) ReplaceAllString(src, repl string) string { |
| 187 | + re.compile() |
| 188 | + return re.regexp.ReplaceAllString(src, repl) |
| 189 | +} |
| 190 | + |
| 191 | +func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string { |
| 192 | + re.compile() |
| 193 | + return re.regexp.ReplaceAllStringFunc(src, repl) |
| 194 | +} |
| 195 | + |
| 196 | +func (re *Regexp) Split(s string, n int) []string { |
| 197 | + re.compile() |
| 198 | + return re.regexp.Split(s, n) |
| 199 | +} |
| 200 | + |
| 201 | +func (re *Regexp) String() string { |
| 202 | + re.compile() |
| 203 | + return re.regexp.String() |
| 204 | +} |
| 205 | + |
| 206 | +func (re *Regexp) SubexpIndex(name string) int { |
| 207 | + re.compile() |
| 208 | + return re.regexp.SubexpIndex(name) |
| 209 | +} |
| 210 | + |
| 211 | +func (re *Regexp) SubexpNames() []string { |
| 212 | + re.compile() |
| 213 | + return re.regexp.SubexpNames() |
| 214 | +} |
0 commit comments