Debian Codegraph @

example_test.go

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