-
Notifications
You must be signed in to change notification settings - Fork 8
/
suffix.go
54 lines (49 loc) · 1.33 KB
/
suffix.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
package stringsutil
import "strings"
func SuffixStrip(s, suffix string) string {
if ReverseIndex(s, suffix) == 0 {
return s[:len(s)-len(suffix)]
}
return s
}
func SuffixReplace(s, oldSuffix, newSuffix string) string {
if len(oldSuffix) == 0 {
return s + newSuffix
} else if ReverseIndex(s, oldSuffix) == 0 {
return SuffixStrip(s, oldSuffix) + newSuffix
}
return s
}
func SuffixParse(s, wantSuffix string) (fullstring, prefix, suffix string) {
fullstring = s
if len(suffix) > len(fullstring) {
return
}
lastIndex := strings.LastIndex(s, wantSuffix)
if lastIndex > 0 && lastIndex == len(s)-len(wantSuffix) {
prefix = fullstring[:len(s)-len(wantSuffix)]
suffix = wantSuffix
return
}
return
}
func SuffixMap(inputs, suffixes []string) (prefixes []string, matches map[string]string, nonmatches []string) {
matches = map[string]string{}
for _, input := range inputs {
gotMatch := false
for _, suffix := range suffixes {
gotFull, gotPrefix, gotSuffix := SuffixParse(input, suffix)
if suffix == gotSuffix {
matches[gotSuffix] = gotFull
prefixes = append(prefixes, gotPrefix)
gotMatch = true
}
}
if !gotMatch {
nonmatches = append(nonmatches, input)
}
}
prefixes = SliceCondenseSpace(prefixes, true, true)
nonmatches = SliceCondenseSpace(nonmatches, true, true)
return prefixes, matches, nonmatches
}