-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchablestringslice.go
113 lines (95 loc) · 2.74 KB
/
searchablestringslice.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
package generics
import (
"fmt"
"strings"
"golang.org/x/exp/slices"
)
// function designed to append a string to the string slice.
func (ss SearchableStringSlice) Append(target string) (newslice SearchableStringSlice) {
return append(ss, target)
}
// function designed to append a char to the string slice.
func (ss SearchableStringSlice) AppendChar(target rune) (newslice SearchableStringSlice) {
return append(ss, fmt.Sprintf("%c", target))
}
// function designed to clear the slice and return an
// empty slice.
func (ss SearchableStringSlice) Clear() SearchableStringSlice {
return []string{}
}
// function designed to combine two SearchableStringSlice.
// this will append the entire target slice to the one it
// it being combined with.
func (ss SearchableStringSlice) Combine(target SearchableStringSlice) (newslice SearchableStringSlice) {
switch {
case target.Length() < 1:
newslice = ss
case ss.Length() < 1:
newslice = target
default:
var current string
newslice = ss
for _, current = range ss {
newslice = newslice.Append(current)
}
}
return newslice
}
// function designed to search through a searchablestring
// and determine if the target string is in it.
func (ss SearchableStringSlice) In(target string) (err error) {
var current string
var found bool
// loop over each character in the string and compare
// it to the target char. if a match is found, set the
// found flag and break the loop.
for _, current = range ss {
if current == target {
found = true
break
}
}
// if the char was not found within the string, return
// an error stating as much.
if !found {
return fmt.Errorf("%s not found in %s", target, ss)
}
return nil
}
// function designed to find the index of a given string.
func (ss SearchableStringSlice) IndexOf(target string) (idx int, err error) {
// make sure the target is in the string before
// looping through to grab the index.
if err = ss.In(target); err != nil {
return -1, err
}
var currentIdx int
var currentChr string
for currentIdx, currentChr = range ss {
if currentChr == target {
idx = currentIdx
break
}
}
return idx, nil
}
// function designed to return the length of the array/slice.
// this will loop throught the slice and iterate a counter,
// returning the number of elements present in the slice.
func (ss SearchableStringSlice) Length() (count int) {
for range ss {
count++
}
return count
}
// function designed to sort the SearchableStringSlice
// alphabetically.
func (ss SearchableStringSlice) Sort() SearchableStringSlice {
slices.Sort(ss)
return ss
}
// function designed to return the string representation
// of the SearchableStringSlice.
func (ss SearchableStringSlice) String() string {
return fmt.Sprintf("[%s]", strings.Join(ss, ","))
}