Source file
src/syscall/wtf8_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package syscall
22
23 import (
24 "unicode/utf16"
25 "unicode/utf8"
26 )
27
28 const (
29 surr1 = 0xd800
30 surr2 = 0xdc00
31 surr3 = 0xe000
32
33 tx = 0b10000000
34 t3 = 0b11100000
35 maskx = 0b00111111
36 mask3 = 0b00001111
37
38 rune1Max = 1<<7 - 1
39 rune2Max = 1<<11 - 1
40 )
41
42
43
44 func encodeWTF16(s string, buf []uint16) []uint16 {
45 for i := 0; i < len(s); {
46
47
48 r, size := utf8.DecodeRuneInString(s[i:])
49 if r == utf8.RuneError {
50
51 if sc := s[i:]; len(sc) >= 3 && sc[0] == 0xED && 0xA0 <= sc[1] && sc[1] <= 0xBF && 0x80 <= sc[2] && sc[2] <= 0xBF {
52 r = rune(sc[0]&mask3)<<12 + rune(sc[1]&maskx)<<6 + rune(sc[2]&maskx)
53 buf = append(buf, uint16(r))
54 i += 3
55 continue
56 }
57 }
58 i += size
59 buf = utf16.AppendRune(buf, r)
60 }
61 return buf
62 }
63
64
65
66 func decodeWTF16(s []uint16, buf []byte) []byte {
67 for i := 0; i < len(s); i++ {
68 var ar rune
69 switch r := s[i]; {
70 case r < surr1, surr3 <= r:
71
72 ar = rune(r)
73 case surr1 <= r && r < surr2 && i+1 < len(s) &&
74 surr2 <= s[i+1] && s[i+1] < surr3:
75
76 ar = utf16.DecodeRune(rune(r), rune(s[i+1]))
77 i++
78 default:
79
80
81
82 ar = rune(r)
83 if ar > utf8.MaxRune {
84 ar = utf8.RuneError
85 }
86 buf = append(buf, t3|byte(ar>>12), tx|byte(ar>>6)&maskx, tx|byte(ar)&maskx)
87 continue
88 }
89 buf = utf8.AppendRune(buf, ar)
90 }
91 return buf
92 }
93
View as plain text