Skip to content

Commit

Permalink
Makes regex constant & moves single quote replace logic to parseOneOf…
Browse files Browse the repository at this point in the history
…Param2
  • Loading branch information
JonathanWThom committed Nov 21, 2019
1 parent 432c170 commit 2ba4e5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 6 additions & 6 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -178,8 +177,11 @@ func parseOneOfParam2(s string) []string {
oneofValsCacheRWLock.RUnlock()
if !ok {
oneofValsCacheRWLock.Lock()
re := regexp.MustCompile(`'[^']*'|\S+`)
vals = re.FindAllString(s, -1)
vals = splitParamsRegex.FindAllString(s, -1)
for i := 0; i < len(vals); i++ {
val := strings.Replace(vals[i], "'", "", -1)
vals[i] = val
}
oneofValsCache[s] = vals
oneofValsCacheRWLock.Unlock()
}
Expand All @@ -200,7 +202,6 @@ func isHTML(fl FieldLevel) bool {

func isOneOf(fl FieldLevel) bool {
vals := parseOneOfParam2(fl.Param())

field := fl.Field()

var v string
Expand All @@ -215,8 +216,7 @@ func isOneOf(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
for i := 0; i < len(vals); i++ {
val := strings.Replace(vals[i], "'", "", -1)
if val == v {
if vals[i] == v {
return true
}
}
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
splitParamsRegexString = `'[^']*'|\S+`
)

var (
Expand Down Expand Up @@ -92,4 +93,5 @@ var (
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
hTMLRegex = regexp.MustCompile(hTMLRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
)

0 comments on commit 2ba4e5d

Please sign in to comment.