Debian Codegraph @

bytcase/example_test.go

raw
1 package bytcase_test
2
3 import (
4 "fmt"
5 "sort"
6 "unicode/utf8"
7
8 "github.com/charlievieth/strcase/bytcase"
9 )
10
11 // // Index returns the index of the first instance of substr in s,
12 // // or -1 if substr is not present in s.
13 // // If ignoreCase is true the match is case-insensitive.
14 // func Index(s, substr string, ignoreCase bool) int {
15 // if ignoreCase {
16 // return bytcase.Index(s, substr)
17 // }
18 // return strings.Index(s, substr)
19 // }
20
21 func ExampleCompare() {
22 // ASCII
23 fmt.Println(bytcase.Compare([]byte("A"), []byte("b")))
24 fmt.Println(bytcase.Compare([]byte("A"), []byte("a")))
25 fmt.Println(bytcase.Compare([]byte("B"), []byte("a")))
26
27 // Unicode
28 fmt.Println(bytcase.Compare([]byte("s"), []byte("ſ")))
29 fmt.Println(bytcase.Compare([]byte("αβδ"), []byte("ΑΒΔ")))
30
31 // All invalid UTF-8 sequences are considered equal
32 fmt.Println(bytcase.Compare([]byte("\xff"), []byte(string(utf8.RuneError))))
33 // Output:
34 // -1
35 // 0
36 // 1
37 // 0
38 // 0
39 // 0
40 }
41
42 // Case insensitive sort using [bytcase.Compare].
43 func ExampleCompare_sort() {
44 a := [][]byte{
45 []byte("b"),
46 []byte("a"),
47 []byte("α"),
48 []byte("B"),
49 []byte("Α"), // U+0391
50 []byte("A"),
51 }
52 sort.SliceStable(a, func(i, j int) bool {
53 return bytcase.Compare(a[i], a[j]) < 0
54 })
55 fmt.Printf("%q\n", a)
56 // Output:
57 // ["a" "A" "b" "B" "α" "Α"]
58 }
59
60 // Case insensitive search using [bytcase.Compare].
61 func ExampleCompare_search() {
62 a := [][]byte{
63 []byte("a"),
64 []byte("b"),
65 []byte("α"),
66 }
67 s := []byte("B") // string being searched for
68 i := sort.Search(len(a), func(i int) bool {
69 return bytcase.Compare(a[i], s) >= 0
70 })
71
72 fmt.Printf("%d: %q\n", i, a[i])
73 // Output:
74 // 1: "b"
75 }
76
77 // Using [bytcase.Compare] and [sort.Find] to search a string slice.
78 func ExampleCompare_find() {
79 a := []string{
80 "a",
81 "b",
82 "α",
83 }
84 for _, s := range []string{"A", "B", "Z"} {
85 i, found := sort.Find(len(a), func(i int) int {
86 return bytcase.Compare([]byte(s), []byte(a[i]))
87 })
88 if found {
89 fmt.Printf("%q found at index %d\n", s, i)
90 } else {
91 fmt.Printf("%q not found", s)
92 }
93 }
94 // Output:
95 // "A" found at index 0
96 // "B" found at index 1
97 // "Z" not found
98 }
99
100 func ExampleContains() {
101 fmt.Println(bytcase.Contains([]byte("SeaFood"), []byte("foo")))
102 fmt.Println(bytcase.Contains([]byte("SeaFood"), []byte("bar")))
103 fmt.Println(bytcase.Contains([]byte("SeaFood"), []byte("")))
104 fmt.Println(bytcase.Contains([]byte(""), []byte("")))
105 fmt.Println(bytcase.Contains([]byte("ΑΔΕΛΦΟΣΎΝΗΣ"), []byte("αδελφοσύνης")))
106
107 // All invalid UTF-8 sequences are considered equal
108 fmt.Println(bytcase.Contains([]byte("\xed\xa0\x80\x80"), []byte("\xed\xbf\xbf\x80")))
109 // Output:
110 // true
111 // false
112 // true
113 // true
114 // true
115 // true
116 }
117
118 func ExampleContains_invalid() {
119 // All invalid UTF-8 sequences are considered equal
120 fmt.Println(bytcase.Contains([]byte("a\xff"), []byte(string(utf8.RuneError))))
121 fmt.Println(bytcase.Contains([]byte("abc\xed\xa0\x80\x80"), []byte("\xed\xbf\xbf\x80")))
122 // Output:
123 // true
124 // true
125 }
126
127 func ExampleContainsAny() {
128 fmt.Println(bytcase.ContainsAny([]byte("team"), []byte("I")))
129 fmt.Println(bytcase.ContainsAny([]byte("fail"), []byte("UI")))
130 fmt.Println(bytcase.ContainsAny([]byte("ure"), []byte("UI")))
131 fmt.Println(bytcase.ContainsAny([]byte("failure"), []byte("UI")))
132 fmt.Println(bytcase.ContainsAny([]byte("foo"), []byte("")))
133 fmt.Println(bytcase.ContainsAny([]byte(""), []byte("")))
134 fmt.Println(bytcase.ContainsAny([]byte("αβδ"), []byte("Α")))
135 // Output:
136 // false
137 // true
138 // true
139 // true
140 // false
141 // false
142 // true
143 }
144
145 func ExampleContainsRune() {
146 // Finds whether a string contains a particular Unicode code point.
147 fmt.Println(bytcase.ContainsRune([]byte("aardvark"), 'A'))
148 fmt.Println(bytcase.ContainsRune([]byte("timeout"), 'A'))
149 // Output:
150 // true
151 // false
152 }
153
154 func ExampleCount() {
155 fmt.Println(bytcase.Count([]byte("cheese"), []byte("e")))
156 fmt.Println(bytcase.Count([]byte("five"), []byte("")))
157 fmt.Println(bytcase.Count([]byte("ΑΒΔ"), []byte("α")))
158 fmt.Println(bytcase.Count([]byte("ΑΒΔ"), []byte("")))
159 // Output:
160 // 3
161 // 5
162 // 1
163 // 4
164 }
165
166 func ExampleHasPrefix() {
167 fmt.Println(bytcase.HasPrefix([]byte("Gopher"), []byte("go")))
168 fmt.Println(bytcase.HasPrefix([]byte("Gopher"), []byte("c")))
169 fmt.Println(bytcase.HasPrefix([]byte("Gopher"), []byte("")))
170 // Moonlight Night (Mayakovsky) - 1916
171 fmt.Println(bytcase.HasPrefix([]byte("А вот и полная повисла в воздухе."), []byte("А ВОТ")))
172 // Output:
173 // true
174 // false
175 // true
176 // true
177 }
178
179 func ExampleHasSuffix() {
180 fmt.Println(bytcase.HasSuffix([]byte("Amigo"), []byte("GO")))
181 fmt.Println(bytcase.HasSuffix([]byte("Amigo"), []byte("AMI")))
182 fmt.Println(bytcase.HasSuffix([]byte("Amigo"), []byte("")))
183 // Moonlight Night (Mayakovsky) - 1916
184 fmt.Println(bytcase.HasSuffix([]byte("А вот и полная повисла в воздухе."), []byte("В Воздухе.")))
185 // Output:
186 // true
187 // false
188 // true
189 // true
190 }
191
192 func ExampleIndex() {
193 fmt.Println(bytcase.Index([]byte("chicken"), []byte("KEN")))
194 fmt.Println(bytcase.Index([]byte("chicken"), []byte("DMR")))
195 fmt.Println(bytcase.Index([]byte("日a本b語ç日ð本ê語"), []byte("Ç日Ð本Ê")))
196 // Output:
197 // 4
198 // -1
199 // 11
200 }
201
202 func ExampleIndex_invalid() {
203 // All invalid UTF-8 sequences are considered equal
204 fmt.Println(bytcase.Index([]byte("a\xff"), []byte(string(utf8.RuneError))))
205 fmt.Println(bytcase.Index([]byte("abc\xed\xa0\x80\x80"), []byte("\xed\xbf\xbf\x80")))
206 // Output:
207 // 1
208 // 3
209 }
210
211 func ExampleIndexAny() {
212 fmt.Println(bytcase.IndexAny([]byte("chicken"), []byte("AEIOUY")))
213 fmt.Println(bytcase.IndexAny([]byte("crwth"), []byte("AEIOUY")))
214 // Kelvin K (U+212A) matches ASCII 'K' and 'k'
215 fmt.Println(bytcase.IndexAny([]byte("45K"), []byte("k")))
216 // Latin small letter long S 'ſ' matches ASCII 'S' and 's'
217 fmt.Println(bytcase.IndexAny([]byte("salsa"), []byte("ſ")))
218 // Output:
219 // 2
220 // -1
221 // 2
222 // 0
223 }
224
225 func ExampleIndexByte() {
226 fmt.Println(bytcase.IndexByte([]byte("golang"), 'G'))
227 fmt.Println(bytcase.IndexByte([]byte("gophers"), 'H'))
228 fmt.Println(bytcase.IndexByte([]byte("golang"), 'X'))
229 // Latin small letter long S 'ſ' matches ASCII 'S' and 's'
230 fmt.Println(bytcase.IndexByte([]byte("ſinfulneſs"), 's'))
231 // K
232 // Output:
233 // 0
234 // 3
235 // -1
236 // 0
237 }
238
239 func ExampleIndexRune() {
240 fmt.Println(bytcase.IndexRune([]byte("chicken"), 'K'))
241 // U+212A is the code point for Kelvin K
242 fmt.Println(bytcase.IndexRune([]byte("chicken"), '\u212A'))
243 fmt.Println(bytcase.IndexRune([]byte("chicken"), 'D'))
244 fmt.Println(bytcase.IndexRune([]byte("日a本b語ç日"), 'Ç'))
245 // Output:
246 // 4
247 // 4
248 // -1
249 // 11
250 }
251
252 func ExampleLastIndex() {
253 fmt.Println(bytcase.Index([]byte("go gopher"), []byte("GO")))
254 fmt.Println(bytcase.LastIndex([]byte("go gopher"), []byte("GO")))
255 fmt.Println(bytcase.LastIndex([]byte("go gopher"), []byte("rodent")))
256 // Moonlight Night (Mayakovsky) - 1916
257 fmt.Println(bytcase.LastIndex([]byte("А вот и полная повисла в воздухе."), []byte("ПОЛНАЯ")))
258 // Output:
259 // 0
260 // 3
261 // -1
262 // 13
263 }
264
265 func ExampleLastIndexAny() {
266 fmt.Println(bytcase.LastIndexAny([]byte("go gopher"), []byte("GO")))
267 fmt.Println(bytcase.LastIndexAny([]byte("go gopher"), []byte("RODENT")))
268 fmt.Println(bytcase.LastIndexAny([]byte("go gopher"), []byte("FAIL")))
269 fmt.Println(bytcase.LastIndexAny([]byte("Картѣ"), []byte("РТ") /* U+0420 & U+0422 */))
270 // Output:
271 // 4
272 // 8
273 // -1
274 // 6
275 }
276
277 func ExampleLastIndexByte() {
278 fmt.Println(bytcase.LastIndexByte([]byte("Hello, world"), 'L'))
279 fmt.Println(bytcase.LastIndexByte([]byte("Hello, world"), 'O'))
280 // Kelvin K (U+212A) matches ASCII 'K' and 'k'
281 fmt.Println(bytcase.LastIndexByte([]byte("Hello, \u212Aelvin"), 'k'))
282 // Output:
283 // 10
284 // 8
285 // 7
286 }
287
288 func ExampleEqualFold() {
289 fmt.Println(bytcase.EqualFold([]byte("Go"), []byte("go")))
290 // true because comparison uses simple case-folding
291 fmt.Println(bytcase.EqualFold([]byte("AB"), []byte("ab")))
292 // false because comparison does not use full case-folding
293 fmt.Println(bytcase.EqualFold([]byte("ß"), []byte("ss")))
294 // Output:
295 // true
296 // true
297 // false
298 }
299
300 func ExampleCut() {
301 show := func(s, sep string) {
302 before, after, found := bytcase.Cut([]byte(s), []byte(sep))
303 fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
304 }
305 show("Gopher", "GO")
306 show("Gopher", "Ph")
307 show("Gopher", "Er")
308 show("Gopher", "Badger")
309 show("123 αβδ 456", "ΑΒΔ")
310 // Output:
311 // Cut("Gopher", "GO") = "", "pher", true
312 // Cut("Gopher", "Ph") = "Go", "er", true
313 // Cut("Gopher", "Er") = "Goph", "", true
314 // Cut("Gopher", "Badger") = "Gopher", "", false
315 // Cut("123 αβδ 456", "ΑΒΔ") = "123 ", " 456", true
316 }
317
318 func ExampleCutPrefix() {
319 show := func(s, sep string) {
320 after, found := bytcase.CutPrefix([]byte(s), []byte(sep))
321 fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found)
322 }
323 show("Gopher", "Go")
324 show("Gopher", "Ph")
325 // Output:
326 // CutPrefix("Gopher", "Go") = "pher", true
327 // CutPrefix("Gopher", "Ph") = "Gopher", false
328 }
329
330 func ExampleCutSuffix() {
331 show := func(s, sep string) {
332 before, found := bytcase.CutSuffix([]byte(s), []byte(sep))
333 fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found)
334 }
335 show("Gopher", "Go")
336 show("Gopher", "Er")
337 // Output:
338 // CutSuffix("Gopher", "Go") = "Gopher", false
339 // CutSuffix("Gopher", "Er") = "Goph", true
340 }
341
342 func ExampleIndexNonASCII() {
343 fmt.Println(bytcase.IndexNonASCII([]byte("日a本b語ç日")))
344 fmt.Println(bytcase.IndexNonASCII([]byte("abc語")))
345 fmt.Println(bytcase.IndexNonASCII([]byte("abc")))
346 // Output:
347 // 0
348 // 3
349 // -1
350 }
351
352 func ExampleContainsNonASCII() {
353 fmt.Println(bytcase.ContainsNonASCII([]byte("日a本b語ç日")))
354 fmt.Println(bytcase.ContainsNonASCII([]byte("abc語")))
355 fmt.Println(bytcase.ContainsNonASCII([]byte("abc")))
356 // Output:
357 // true
358 // true
359 // false
360 }