-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bugs in scenarios of Chinese characters or incorrect using of match.Index #50
Comments
Thanks for finding this! This is because internally the matching engine always uses a rune slice and If the user passes in a rune slice (e.g. I believe the best backward-compatible fix would be to keep The For now, to work around this issue you'll need to map the runes to the string index yourself: func getRunesAndMap(in string) ([]rune, map[int]int) {
ret := make([]rune, len(in))
idxMap := make(map[int]int)
i := 0
for strIdx, r := range in {
idxMap[i] = strIdx
ret[i] = r
i++
}
return ret[:i], idxMap
}
func TestReplaceMultiByte(t *testing.T) {
var sample = "<title>错<style"
var runes, idxMap = getRunesAndMap(sample)
regex := MustCompile("<style", IgnoreCase|Singleline)
match, err := regex.FindRunesMatch(runes)
if err != nil {
panic(err)
}
if match != nil {
t, err := regex.Replace(sample, "xxx", idxMap[match.Index], -1)
if err != nil {
panic(err)
}
fmt.Printf("%s", t)
}
} |
I find another solution. |
the following codes fails
if i search some words/regex successfully, and then replace something from match.Index instead of -1, the codes fails.
however, if removed the Chinese character
错
, the codes succeeds.so, in such scenario, what should beginning index be if I want to replace all and don't want to replace from -1(begining)
The text was updated successfully, but these errors were encountered: