Use rune instead of byte when traversing strings

This commit is contained in:
Håkan Fouren 2023-02-05 01:58:27 +08:00
parent 9062c24a7e
commit f08cac491c
No known key found for this signature in database

View File

@ -1240,20 +1240,21 @@ func tokenize(query string) ([]string, error) {
return splitEscaped(query, ' ', -1), nil return splitEscaped(query, ' ', -1), nil
} }
func splitEscaped(query string, separator byte, maxSplit int) []string { func splitEscaped(query string, separator rune, maxSplit int) []string {
var token []byte var token []rune
var tokens []string var tokens []string
var splits = 1 splits := 1
for i := 0; i < len(query); i++ { runeQuery := []rune(query)
if (maxSplit < 0 || splits < maxSplit) && query[i] == separator { for i := 0; i < len(runeQuery); i++ {
if (maxSplit < 0 || splits < maxSplit) && runeQuery[i] == separator {
tokens = append(tokens, string(token)) tokens = append(tokens, string(token))
token = token[:0] token = token[:0]
splits++ splits++
} else if query[i] == '\\' && i+1 < len(query) { } else if runeQuery[i] == '\\' && i+1 < len(runeQuery) {
token = append(token, query[i], query[i+1]) token = append(token, runeQuery[i], runeQuery[i+1])
i++ i++
} else { } else {
token = append(token, query[i]) token = append(token, runeQuery[i])
} }
} }
tokens = append(tokens, string(token)) tokens = append(tokens, string(token))
@ -1261,10 +1262,11 @@ func splitEscaped(query string, separator byte, maxSplit int) []string {
} }
func containsUnescaped(query string, token string) bool { func containsUnescaped(query string, token string) bool {
for i := 0; i < len(query); i++ { runeQuery := []rune(query)
if query[i] == '\\' && i+1 < len(query) { for i := 0; i < len(runeQuery); i++ {
if runeQuery[i] == '\\' && i+1 < len(runeQuery) {
i++ i++
} else if query[i:i+len(token)] == token { } else if string(runeQuery[i:i+len(token)]) == token {
return true return true
} }
} }
@ -1272,12 +1274,13 @@ func containsUnescaped(query string, token string) bool {
} }
func deEscape(query string) string { func deEscape(query string) string {
var newQuery []byte runeQuery := []rune(query)
for i := 0; i < len(query); i++ { var newQuery []rune
if query[i] == '\\' && i+1 < len(query) { for i := 0; i < len(runeQuery); i++ {
if runeQuery[i] == '\\' && i+1 < len(runeQuery) {
i++ i++
} }
newQuery = append(newQuery, query[i]) newQuery = append(newQuery, runeQuery[i])
} }
return string(newQuery) return string(newQuery)
} }