Makes regex constant & moves single quote replace logic to parseOneOfParam2

pull/541/head
Jonathan Thom 5 years ago
parent 432c17028a
commit 685d3e21f3
No known key found for this signature in database
GPG Key ID: B6E703AC3AD25FFE
  1. 10
      baked_in.go
  2. 2
      regexes.go

@ -9,7 +9,6 @@ import (
"net/url" "net/url"
"os" "os"
"reflect" "reflect"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -178,8 +177,10 @@ func parseOneOfParam2(s string) []string {
oneofValsCacheRWLock.RUnlock() oneofValsCacheRWLock.RUnlock()
if !ok { if !ok {
oneofValsCacheRWLock.Lock() oneofValsCacheRWLock.Lock()
re := regexp.MustCompile(`'[^']*'|\S+`) vals = splitParamsRegex.FindAllString(s, -1)
vals = re.FindAllString(s, -1) for i := 0; i < len(vals); i++ {
vals[i] = strings.Replace(vals[i], "'", "", -1)
}
oneofValsCache[s] = vals oneofValsCache[s] = vals
oneofValsCacheRWLock.Unlock() oneofValsCacheRWLock.Unlock()
} }
@ -215,8 +216,7 @@ func isOneOf(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface())) panic(fmt.Sprintf("Bad field type %T", field.Interface()))
} }
for i := 0; i < len(vals); i++ { for i := 0; i < len(vals); i++ {
val := strings.Replace(vals[i], "'", "", -1) if vals[i] == v {
if val == v {
return true return true
} }
} }

@ -46,6 +46,7 @@ const (
uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})` uRLEncodedRegexString = `(%[A-Fa-f0-9]{2})`
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?` hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>` hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
splitParamsRegexString = `'[^']*'|\S+`
) )
var ( var (
@ -92,4 +93,5 @@ var (
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString) uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString) hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
hTMLRegex = regexp.MustCompile(hTMLRegexString) hTMLRegex = regexp.MustCompile(hTMLRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
) )

Loading…
Cancel
Save