Revert code

This commit is contained in:
zhouzhihong 2022-08-25 10:49:30 +08:00
parent fd42cc208a
commit 2da032b556
2 changed files with 30 additions and 8 deletions

23
cmd/main.go Normal file
View File

@ -0,0 +1,23 @@
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
b := []byte("你好")
sz := 0
var r rune
for i := 0; i < len(b); {
r, sz = utf8.DecodeRune(b[i:])
i += sz
fmt.Println(r, sz, i, string(r))
}
// fmt.Println(len(b))
}

View File

@ -69,36 +69,35 @@ type Match struct {
Bytes []byte // the actual bytes matched during scanning. Bytes []byte // the actual bytes matched during scanning.
} }
func computeLineCol(s []byte, prevTC, tc, line, col int) (int, int) { func computeLineCol(text []byte, prevTC, tc, line, col int) (int, int) {
// s := []rune(string(text))
if tc < 0 { if tc < 0 {
return line, col return line, col
} }
if tc < prevTC { if tc < prevTC {
for i := prevTC; i > tc && i > 0; i-- { for i := prevTC; i > tc && i > 0; i-- {
if s[i] == '\n' { if text[i] == '\n' {
line-- line--
} }
} }
col = 0 col = 0
for i := tc; i >= 0; i-- { for i := tc; i >= 0; i-- {
if s[i] == '\n' { if text[i] == '\n' {
break break
} }
col++ col++
} }
return line, col return line, col
} }
for i := prevTC + 1; i <= tc && i < len(s); i++ { for i := prevTC + 1; i <= tc && i < len(text); i++ {
if s[i] == '\n' { if text[i] == '\n' {
col = 0 col = 0
line++ line++
} else { } else {
col++ col++
} }
} }
if prevTC == tc && tc == 0 && tc < len(s) { if prevTC == tc && tc == 0 && tc < len(text) {
if s[tc] == '\n' { if text[tc] == '\n' {
line++ line++
col-- col--
} }