-
Notifications
You must be signed in to change notification settings - Fork 0
/
searching.go
145 lines (119 loc) · 3.74 KB
/
searching.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
package main
import (
"bytes"
"log"
)
func contains(s, sub []byte) {
if bytes.Contains(s, sub) {
log.Printf("%s contains %s", s, sub)
} else {
log.Printf("%s does NOT contain %s", s, sub)
}
}
func count(s, sep []byte) {
log.Printf("%s contains %d instance(s) of %s", s, bytes.Count(s, sep), sep)
}
func hasPrefix(s, prefix []byte) {
if bytes.HasPrefix(s, prefix) {
log.Printf("%s has the prefix %s", s, prefix)
} else {
log.Printf("%s does NOT have the prefix %s", s, prefix)
}
}
func hasSuffix(s, suffix []byte) {
if bytes.HasSuffix(s, suffix) {
log.Printf("%s has the suffix %s", s, suffix)
} else {
log.Printf("%s does NOT have the suffix %s", s, suffix)
}
}
func index(s, sep []byte) {
if i := bytes.Index(s, sep); i == -1 {
log.Printf("%s does NOT appear in %s", sep, s)
} else {
log.Printf("%s appears at index %d in %s", sep, i, s)
}
}
func indexAny(s []byte, chars string) {
if i := bytes.IndexAny(s, chars); i == -1 {
log.Printf("No unicode characters in %q appear in %s", chars, s)
} else {
log.Printf("A unicode character in %q appears at index %d in %s", chars, i, s)
}
}
func indexByte(s []byte, b byte) {
if i := bytes.IndexByte(s, b); i == -1 {
log.Printf("%q does NOT appear in %s", b, s)
} else {
log.Printf("%q appears at index %d in %s", b, i, s)
}
}
func indexFunc(s []byte, f func(rune) bool) {
if i := bytes.IndexFunc(s, f); i == -1 {
log.Printf("Something controlled by %#v does NOT appear in %s", f, s)
} else {
log.Printf("Something controlled by %#v appears at index %d in %s", f, i, s)
}
}
func indexRune(s []byte, r rune) {
if i := bytes.IndexRune(s, r); i == -1 {
log.Printf("Rune %d does NOT appear in %s", r, s)
} else {
log.Printf("Rune %d appears at index %d in %s", r, i, s)
}
}
func lastIndex(s, sep []byte) {
if i := bytes.LastIndex(s, sep); i == -1 {
log.Printf("%s does NOT appear in %s", sep, s)
} else {
log.Printf("%s appears last at index %d in %s", sep, i, s)
}
}
func lastIndexAny(s []byte, chars string) {
if i := bytes.LastIndexAny(s, chars); i == -1 {
log.Printf("No unicode characters in %q appear in %s", chars, s)
} else {
log.Printf("A unicode character in %q appears last at index %d in %s", chars, i, s)
}
}
func lastIndexFunc(s []byte, f func(rune) bool) {
if i := bytes.LastIndexFunc(s, f); i == -1 {
log.Printf("Something controlled by %#v does NOT appear in %s", f, s)
} else {
log.Printf("Something controlled by %#v appears at index %d in %s", f, i, s)
}
}
func main() {
golang := []byte("golang")
haskell := []byte("haskell")
lang := []byte("lang")
gos := []byte("go")
contains(golang, lang)
contains(golang, haskell)
count(golang, lang)
count(haskell, []byte("l"))
hasPrefix(golang, gos)
hasPrefix(haskell, gos)
hasSuffix(golang, lang)
hasSuffix(haskell, lang)
index(golang, lang)
index(golang, gos)
index(haskell, lang)
indexAny(golang, "lang")
indexAny(haskell, "lang")
indexAny(haskell, "go")
indexByte(golang, 'h')
indexByte(golang, 'l')
indexByte(haskell, 'l')
g := rune('g')
indexFunc(golang, func(r rune) bool { return r == g })
indexFunc(haskell, func(r rune) bool { return r == g })
indexRune(golang, rune('o'))
indexRune(haskell, rune('l'))
lastIndex(golang, []byte("g"))
lastIndex(haskell, []byte("l"))
lastIndexAny(golang, "abcdefg")
lastIndexAny(haskell, "lmnop")
lastIndexFunc(golang, func(r rune) bool { return r == g })
lastIndexFunc(haskell, func(r rune) bool { return r == g })
}