mirror of
https://github.com/ddworken/hishtory.git
synced 2024-11-26 18:23:27 +01:00
Use rune instead of byte when traversing strings
This commit is contained in:
parent
9062c24a7e
commit
f08cac491c
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user