Skip to content

Commit 685d3e2

Browse files
committed
Makes regex constant & moves single quote replace logic to parseOneOfParam2
1 parent 432c170 commit 685d3e2

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

baked_in.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/url"
1010
"os"
1111
"reflect"
12-
"regexp"
1312
"strconv"
1413
"strings"
1514
"sync"
@@ -178,8 +177,10 @@ func parseOneOfParam2(s string) []string {
178177
oneofValsCacheRWLock.RUnlock()
179178
if !ok {
180179
oneofValsCacheRWLock.Lock()
181-
re := regexp.MustCompile(`'[^']*'|\S+`)
182-
vals = re.FindAllString(s, -1)
180+
vals = splitParamsRegex.FindAllString(s, -1)
181+
for i := 0; i < len(vals); i++ {
182+
vals[i] = strings.Replace(vals[i], "'", "", -1)
183+
}
183184
oneofValsCache[s] = vals
184185
oneofValsCacheRWLock.Unlock()
185186
}
@@ -215,8 +216,7 @@ func isOneOf(fl FieldLevel) bool {
215216
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
216217
}
217218
for i := 0; i < len(vals); i++ {
218-
val := strings.Replace(vals[i], "'", "", -1)
219-
if val == v {
219+
if vals[i] == v {
220220
return true
221221
}
222222
}

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const (
4646
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
4747
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
4848
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
49+
splitParamsRegexString = `'[^']*'|\S+`
4950
)
5051

5152
var (
@@ -92,4 +93,5 @@ var (
9293
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
9394
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
9495
hTMLRegex = regexp.MustCompile(hTMLRegexString)
96+
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
9597
)

0 commit comments

Comments
 (0)