新特性 #1

Merged
zhouzhihong merged 3 commits from dev into main 2022-08-25 14:41:32 +08:00
2 changed files with 30 additions and 8 deletions
Showing only changes of commit 2da032b556 - Show all commits

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.
}
func computeLineCol(s []byte, prevTC, tc, line, col int) (int, int) {
// s := []rune(string(text))
func computeLineCol(text []byte, prevTC, tc, line, col int) (int, int) {
if tc < 0 {
return line, col
}
if tc < prevTC {
for i := prevTC; i > tc && i > 0; i-- {
if s[i] == '\n' {
if text[i] == '\n' {
line--
}
}
col = 0
for i := tc; i >= 0; i-- {
if s[i] == '\n' {
if text[i] == '\n' {
break
}
col++
}
return line, col
}
for i := prevTC + 1; i <= tc && i < len(s); i++ {
if s[i] == '\n' {
for i := prevTC + 1; i <= tc && i < len(text); i++ {
if text[i] == '\n' {
col = 0
line++
} else {
col++
}
}
if prevTC == tc && tc == 0 && tc < len(s) {
if s[tc] == '\n' {
if prevTC == tc && tc == 0 && tc < len(text) {
if text[tc] == '\n' {
line++
col--
}