改进正则的方法
This commit is contained in:
parent
8ca66dad60
commit
fdedd9ddb2
22
gjson.go
22
gjson.go
@ -1080,6 +1080,22 @@ func parseObject(c *parseContext, i int, path string) (int, bool) {
|
|||||||
}
|
}
|
||||||
return i, false
|
return i, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func escapeRegexpString(rpv string) string {
|
||||||
|
var content []byte
|
||||||
|
lrpv := len(rpv)
|
||||||
|
for i := 0; i < lrpv; i++ {
|
||||||
|
rpvChar := rpv[i]
|
||||||
|
if rpvChar == '\\' {
|
||||||
|
content = append(content, rpv[i+1])
|
||||||
|
i++
|
||||||
|
} else {
|
||||||
|
content = append(content, rpvChar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(content)
|
||||||
|
}
|
||||||
|
|
||||||
func queryMatches(rp *arrayPathResult, value Result) bool {
|
func queryMatches(rp *arrayPathResult, value Result) bool {
|
||||||
rpv := rp.query.value
|
rpv := rp.query.value
|
||||||
if len(rpv) > 2 && rpv[0] == '"' && rpv[len(rpv)-1] == '"' {
|
if len(rpv) > 2 && rpv[0] == '"' && rpv[len(rpv)-1] == '"' {
|
||||||
@ -1102,14 +1118,12 @@ func queryMatches(rp *arrayPathResult, value Result) bool {
|
|||||||
return value.Str >= rpv
|
return value.Str >= rpv
|
||||||
case "%":
|
case "%":
|
||||||
if rp.re == nil {
|
if rp.re == nil {
|
||||||
rpv = strings.Replace(rpv, `\"`, `"`, -1)
|
rp.re = regexp.MustCompile(escapeRegexpString(rpv))
|
||||||
rp.re = regexp.MustCompile(rpv)
|
|
||||||
}
|
}
|
||||||
return rp.re.MatchString(value.Str)
|
return rp.re.MatchString(value.Str)
|
||||||
case "!%":
|
case "!%":
|
||||||
if rp.re == nil {
|
if rp.re == nil {
|
||||||
rpv = strings.Replace(rpv, `\"`, `"`, -1)
|
rp.re = regexp.MustCompile(escapeRegexpString(rpv))
|
||||||
rp.re = regexp.MustCompile(rpv)
|
|
||||||
}
|
}
|
||||||
return !rp.re.MatchString(value.Str)
|
return !rp.re.MatchString(value.Str)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user