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