1 package uniseg_test
2
3 import (
4 "fmt"
5
6 "github.com/rivo/uniseg"
7 )
8
9 func ExampleGraphemeClusterCount() {
10 n := uniseg.GraphemeClusterCount("π©πͺπ³οΈβπ")
11 fmt.Println(n)
12
13 }
14
15 func ExampleFirstGraphemeCluster() {
16 b := []byte("π©πͺπ³οΈβπ!")
17 state := -1
18 var c []byte
19 for len(b) > 0 {
20 var width int
21 c, b, width, state = uniseg.FirstGraphemeCluster(b, state)
22 fmt.Println(string(c), width)
23 }
24
25
26
27 }
28
29 func ExampleFirstGraphemeClusterInString() {
30 str := "π©πͺπ³οΈβπ!"
31 state := -1
32 var c string
33 for len(str) > 0 {
34 var width int
35 c, str, width, state = uniseg.FirstGraphemeClusterInString(str, state)
36 fmt.Println(c, width)
37 }
38
39
40
41 }
42
43 func ExampleFirstWord() {
44 b := []byte("Hello, world!")
45 state := -1
46 var c []byte
47 for len(b) > 0 {
48 c, b, state = uniseg.FirstWord(b, state)
49 fmt.Printf("(%s)\n", string(c))
50 }
51
52
53
54
55
56 }
57
58 func ExampleFirstWordInString() {
59 str := "Hello, world!"
60 state := -1
61 var c string
62 for len(str) > 0 {
63 c, str, state = uniseg.FirstWordInString(str, state)
64 fmt.Printf("(%s)\n", c)
65 }
66
67
68
69
70
71 }
72
73 func ExampleFirstSentence() {
74 b := []byte("This is sentence 1.0. And this is sentence two.")
75 state := -1
76 var c []byte
77 for len(b) > 0 {
78 c, b, state = uniseg.FirstSentence(b, state)
79 fmt.Printf("(%s)\n", string(c))
80 }
81
82
83 }
84
85 func ExampleFirstSentenceInString() {
86 str := "This is sentence 1.0. And this is sentence two."
87 state := -1
88 var c string
89 for len(str) > 0 {
90 c, str, state = uniseg.FirstSentenceInString(str, state)
91 fmt.Printf("(%s)\n", c)
92 }
93
94
95 }
96
97 func ExampleFirstLineSegment() {
98 b := []byte("First line.\nSecond line.")
99 state := -1
100 var (
101 c []byte
102 mustBreak bool
103 )
104 for len(b) > 0 {
105 c, b, mustBreak, state = uniseg.FirstLineSegment(b, state)
106 fmt.Printf("(%s)", string(c))
107 if mustBreak {
108 fmt.Print("!")
109 }
110 }
111
112
113 }
114
115 func ExampleFirstLineSegmentInString() {
116 str := "First line.\nSecond line."
117 state := -1
118 var (
119 c string
120 mustBreak bool
121 )
122 for len(str) > 0 {
123 c, str, mustBreak, state = uniseg.FirstLineSegmentInString(str, state)
124 fmt.Printf("(%s)", c)
125 if mustBreak {
126 fmt.Println(" < must break")
127 } else {
128 fmt.Println(" < may break")
129 }
130 }
131
132
133
134
135
136 }
137
138 func ExampleStep_graphemes() {
139 b := []byte("π©πͺπ³οΈβπ!")
140 state := -1
141 var c []byte
142 for len(b) > 0 {
143 var boundaries int
144 c, b, boundaries, state = uniseg.Step(b, state)
145 fmt.Println(string(c), boundaries>>uniseg.ShiftWidth)
146 }
147
148
149
150 }
151
152 func ExampleStepString_graphemes() {
153 str := "π©πͺπ³οΈβπ!"
154 state := -1
155 var c string
156 for len(str) > 0 {
157 var boundaries int
158 c, str, boundaries, state = uniseg.StepString(str, state)
159 fmt.Println(c, boundaries>>uniseg.ShiftWidth)
160 }
161
162
163
164 }
165
166 func ExampleStep_word() {
167 b := []byte("Hello, world!")
168 state := -1
169 var (
170 c []byte
171 boundaries int
172 )
173 for len(b) > 0 {
174 c, b, boundaries, state = uniseg.Step(b, state)
175 fmt.Print(string(c))
176 if boundaries&uniseg.MaskWord != 0 {
177 fmt.Print("|")
178 }
179 }
180
181 }
182
183 func ExampleStepString_word() {
184 str := "Hello, world!"
185 state := -1
186 var (
187 c string
188 boundaries int
189 )
190 for len(str) > 0 {
191 c, str, boundaries, state = uniseg.StepString(str, state)
192 fmt.Print(c)
193 if boundaries&uniseg.MaskWord != 0 {
194 fmt.Print("|")
195 }
196 }
197
198 }
199
200 func ExampleStep_sentence() {
201 b := []byte("This is sentence 1.0. And this is sentence two.")
202 state := -1
203 var (
204 c []byte
205 boundaries int
206 )
207 for len(b) > 0 {
208 c, b, boundaries, state = uniseg.Step(b, state)
209 fmt.Print(string(c))
210 if boundaries&uniseg.MaskSentence != 0 {
211 fmt.Print("|")
212 }
213 }
214
215 }
216
217 func ExampleStepString_sentence() {
218 str := "This is sentence 1.0. And this is sentence two."
219 state := -1
220 var (
221 c string
222 boundaries int
223 )
224 for len(str) > 0 {
225 c, str, boundaries, state = uniseg.StepString(str, state)
226 fmt.Print(c)
227 if boundaries&uniseg.MaskSentence != 0 {
228 fmt.Print("|")
229 }
230 }
231
232 }
233
234 func ExampleStep_lineBreaking() {
235 b := []byte("First line.\nSecond line.")
236 state := -1
237 var (
238 c []byte
239 boundaries int
240 )
241 for len(b) > 0 {
242 c, b, boundaries, state = uniseg.Step(b, state)
243 fmt.Print(string(c))
244 if boundaries&uniseg.MaskLine == uniseg.LineCanBreak {
245 fmt.Print("|")
246 } else if boundaries&uniseg.MaskLine == uniseg.LineMustBreak {
247 fmt.Print("β")
248 }
249 }
250
251
252 }
253
254 func ExampleStepString_lineBreaking() {
255 str := "First line.\nSecond line."
256 state := -1
257 var (
258 c string
259 boundaries int
260 )
261 for len(str) > 0 {
262 c, str, boundaries, state = uniseg.StepString(str, state)
263 fmt.Print(c)
264 if boundaries&uniseg.MaskLine == uniseg.LineCanBreak {
265 fmt.Print("|")
266 } else if boundaries&uniseg.MaskLine == uniseg.LineMustBreak {
267 fmt.Print("β")
268 }
269 }
270
271
272 }
273
274 func ExampleGraphemes_graphemes() {
275 g := uniseg.NewGraphemes("π©πͺπ³οΈβπ")
276 for g.Next() {
277 fmt.Println(g.Str())
278 }
279
280
281 }
282
283 func ExampleGraphemes_word() {
284 g := uniseg.NewGraphemes("Hello, world!")
285 for g.Next() {
286 fmt.Print(g.Str())
287 if g.IsWordBoundary() {
288 fmt.Print("|")
289 }
290 }
291
292 }
293
294 func ExampleGraphemes_sentence() {
295 g := uniseg.NewGraphemes("This is sentence 1.0. And this is sentence two.")
296 for g.Next() {
297 fmt.Print(g.Str())
298 if g.IsSentenceBoundary() {
299 fmt.Print("|")
300 }
301 }
302
303 }
304
305 func ExampleGraphemes_lineBreaking() {
306 g := uniseg.NewGraphemes("First line.\nSecond line.")
307 for g.Next() {
308 fmt.Print(g.Str())
309 if g.LineBreak() == uniseg.LineCanBreak {
310 fmt.Print("|")
311 } else if g.LineBreak() == uniseg.LineMustBreak {
312 fmt.Print("β")
313 }
314 }
315 if g.LineBreak() == uniseg.LineMustBreak {
316 fmt.Print("\nNo clusters left. LineMustBreak")
317 }
318 g.Reset()
319 if g.LineBreak() == uniseg.LineDontBreak {
320 fmt.Print("\nIterator has been reset. LineDontBreak")
321 }
322
323
324
325
326 }
327
328 func ExampleStringWidth() {
329 fmt.Println(uniseg.StringWidth("Hello, δΈη"))
330
331 }
332
View as plain text