...
1
16
17 package utf8
18
19 import (
20 `github.com/bytedance/sonic/internal/rt`
21 `github.com/bytedance/sonic/internal/native/types`
22 `github.com/bytedance/sonic/internal/native`
23 )
24
25
26 func CorrectWith(dst []byte, src []byte, repl string) []byte {
27 sstr := rt.Mem2Str(src)
28 sidx := 0
29
30
31 m := types.NewStateMachine()
32 m.Sp = 0
33
34 for sidx < len(sstr) {
35 scur := sidx
36 ecode := native.ValidateUTF8(&sstr, &sidx, m)
37
38 if m.Sp != 0 {
39 if m.Sp > len(sstr) {
40 panic("numbers of invalid utf8 exceed the string len!")
41 }
42 }
43
44 for i := 0; i < m.Sp; i++ {
45 ipos := m.Vt[i]
46 dst = append(dst, sstr[scur:ipos]...)
47 dst = append(dst, repl...)
48 scur = m.Vt[i] + 1
49 }
50
51 dst = append(dst, sstr[scur:sidx]...)
52
53
54 if ecode != 0 {
55 m.Sp = 0
56 }
57 }
58
59 types.FreeStateMachine(m)
60 return dst
61 }
62
63
64 func Validate(src []byte) bool {
65 return ValidateString(rt.Mem2Str(src))
66 }
67
68
69 func ValidateString(src string) bool {
70 return native.ValidateUTF8Fast(&src) == 0
71 }
72
View as plain text