mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
feat(reader): work for cjk in findSubMatchReader
Change-Id: Id30a946c4e78e7cdaf0fa7af9300b98f754e5b80
This commit is contained in:
parent
60df4aadee
commit
023c1d9d21
2 changed files with 26 additions and 7 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package text
|
package text
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
@ -537,24 +538,26 @@ func matchReader(r Reader, reg *regexp.Regexp) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func findSubMatchReader(r Reader, reg *regexp.Regexp) [][]byte {
|
func findSubMatchReader(r Reader, reg *regexp.Regexp) [][]byte {
|
||||||
oldline, oldseg := r.Position()
|
oldLine, oldSeg := r.Position()
|
||||||
match := reg.FindReaderSubmatchIndex(r)
|
match := reg.FindReaderSubmatchIndex(r)
|
||||||
r.SetPosition(oldline, oldseg)
|
r.SetPosition(oldLine, oldSeg)
|
||||||
if match == nil {
|
if match == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
runes := make([]rune, 0, match[1]-match[0])
|
var bb bytes.Buffer
|
||||||
|
bb.Grow(match[1] - match[0])
|
||||||
for i := 0; i < match[1]; {
|
for i := 0; i < match[1]; {
|
||||||
r, size, _ := readRuneReader(r)
|
r, size, _ := readRuneReader(r)
|
||||||
i += size
|
i += size
|
||||||
runes = append(runes, r)
|
bb.WriteRune(r)
|
||||||
}
|
}
|
||||||
result := [][]byte{}
|
bs := bb.Bytes()
|
||||||
|
var result [][]byte
|
||||||
for i := 0; i < len(match); i += 2 {
|
for i := 0; i < len(match); i += 2 {
|
||||||
result = append(result, []byte(string(runes[match[i]:match[i+1]])))
|
result = append(result, bs[match[i]:match[i+1]])
|
||||||
}
|
}
|
||||||
|
|
||||||
r.SetPosition(oldline, oldseg)
|
r.SetPosition(oldLine, oldSeg)
|
||||||
r.Advance(match[1] - match[0])
|
r.Advance(match[1] - match[0])
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
text/reader_test.go
Normal file
16
text/reader_test.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
package text
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFindSubMatchReader(t *testing.T) {
|
||||||
|
s := "微笑"
|
||||||
|
r := NewReader([]byte(":" + s + ":"))
|
||||||
|
reg := regexp.MustCompile(`:(\p{L}+):`)
|
||||||
|
match := r.FindSubMatch(reg)
|
||||||
|
if len(match) != 2 || string(match[1]) != s {
|
||||||
|
t.Fatal("no match cjk")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue