...
Source file
src/unicode/utf8/example_test.go
1
2
3
4
5 package utf8_test
6
7 import (
8 "fmt"
9 "unicode/utf8"
10 )
11
12 func ExampleDecodeLastRune() {
13 b := []byte("Hello, 世界")
14
15 for len(b) > 0 {
16 r, size := utf8.DecodeLastRune(b)
17 fmt.Printf("%c %v\n", r, size)
18
19 b = b[:len(b)-size]
20 }
21
22
23
24
25
26
27
28
29
30
31 }
32
33 func ExampleDecodeLastRuneInString() {
34 str := "Hello, 世界"
35
36 for len(str) > 0 {
37 r, size := utf8.DecodeLastRuneInString(str)
38 fmt.Printf("%c %v\n", r, size)
39
40 str = str[:len(str)-size]
41 }
42
43
44
45
46
47
48
49
50
51
52
53 }
54
55 func ExampleDecodeRune() {
56 b := []byte("Hello, 世界")
57
58 for len(b) > 0 {
59 r, size := utf8.DecodeRune(b)
60 fmt.Printf("%c %v\n", r, size)
61
62 b = b[size:]
63 }
64
65
66
67
68
69
70
71
72
73
74 }
75
76 func ExampleDecodeRuneInString() {
77 str := "Hello, 世界"
78
79 for len(str) > 0 {
80 r, size := utf8.DecodeRuneInString(str)
81 fmt.Printf("%c %v\n", r, size)
82
83 str = str[size:]
84 }
85
86
87
88
89
90
91
92
93
94
95 }
96
97 func ExampleEncodeRune() {
98 r := '世'
99 buf := make([]byte, 3)
100
101 n := utf8.EncodeRune(buf, r)
102
103 fmt.Println(buf)
104 fmt.Println(n)
105
106
107
108 }
109
110 func ExampleEncodeRune_outOfRange() {
111 runes := []rune{
112
113 -1,
114
115 0x110000,
116
117 utf8.RuneError,
118 }
119 for i, c := range runes {
120 buf := make([]byte, 3)
121 size := utf8.EncodeRune(buf, c)
122 fmt.Printf("%d: %d %[2]s %d\n", i, buf, size)
123 }
124
125
126
127
128 }
129
130 func ExampleFullRune() {
131 buf := []byte{228, 184, 150}
132 fmt.Println(utf8.FullRune(buf))
133 fmt.Println(utf8.FullRune(buf[:2]))
134
135
136
137 }
138
139 func ExampleFullRuneInString() {
140 str := "世"
141 fmt.Println(utf8.FullRuneInString(str))
142 fmt.Println(utf8.FullRuneInString(str[:2]))
143
144
145
146 }
147
148 func ExampleRuneCount() {
149 buf := []byte("Hello, 世界")
150 fmt.Println("bytes =", len(buf))
151 fmt.Println("runes =", utf8.RuneCount(buf))
152
153
154
155 }
156
157 func ExampleRuneCountInString() {
158 str := "Hello, 世界"
159 fmt.Println("bytes =", len(str))
160 fmt.Println("runes =", utf8.RuneCountInString(str))
161
162
163
164 }
165
166 func ExampleRuneLen() {
167 fmt.Println(utf8.RuneLen('a'))
168 fmt.Println(utf8.RuneLen('界'))
169
170
171
172 }
173
174 func ExampleRuneStart() {
175 buf := []byte("a界")
176 fmt.Println(utf8.RuneStart(buf[0]))
177 fmt.Println(utf8.RuneStart(buf[1]))
178 fmt.Println(utf8.RuneStart(buf[2]))
179
180
181
182
183 }
184
185 func ExampleValid() {
186 valid := []byte("Hello, 世界")
187 invalid := []byte{0xff, 0xfe, 0xfd}
188
189 fmt.Println(utf8.Valid(valid))
190 fmt.Println(utf8.Valid(invalid))
191
192
193
194 }
195
196 func ExampleValidRune() {
197 valid := 'a'
198 invalid := rune(0xfffffff)
199
200 fmt.Println(utf8.ValidRune(valid))
201 fmt.Println(utf8.ValidRune(invalid))
202
203
204
205 }
206
207 func ExampleValidString() {
208 valid := "Hello, 世界"
209 invalid := string([]byte{0xff, 0xfe, 0xfd})
210
211 fmt.Println(utf8.ValidString(valid))
212 fmt.Println(utf8.ValidString(invalid))
213
214
215
216 }
217
218 func ExampleAppendRune() {
219 buf1 := utf8.AppendRune(nil, 0x10000)
220 buf2 := utf8.AppendRune([]byte("init"), 0x10000)
221 fmt.Println(string(buf1))
222 fmt.Println(string(buf2))
223
224
225
226 }
227
View as plain text