blob: 788a7a6824e0b4f77b0b3a64c6e3528615b2e915 (
plain)
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
|
package longestcommon
import "strings"
//TrimPrefix removes the longest common prefix from all provided strings
func TrimPrefix(strs []string) {
p := Prefix(strs)
if p == "" {
return
}
for i, s := range strs {
strs[i] = strings.TrimPrefix(s, p)
}
}
//TrimSuffix removes the longest common suffix from all provided strings
func TrimSuffix(strs []string) {
p := Suffix(strs)
if p == "" {
return
}
for i, s := range strs {
strs[i] = strings.TrimSuffix(s, p)
}
}
//Prefix returns the longest common prefix of the provided strings
func Prefix(strs []string) string {
return longestCommonXfix(strs, true)
}
//Suffix returns the longest common suffix of the provided strings
func Suffix(strs []string) string {
return longestCommonXfix(strs, false)
}
func longestCommonXfix(strs []string, pre bool) string {
//short-circuit empty list
if len(strs) == 0 {
return ""
}
xfix := strs[0]
//short-circuit single-element list
if len(strs) == 1 {
return xfix
}
//compare first to rest
for _, str := range strs[1:] {
xfixl := len(xfix)
strl := len(str)
//short-circuit empty strings
if xfixl == 0 || strl == 0 {
return ""
}
//maximum possible length
maxl := xfixl
if strl < maxl {
maxl = strl
}
//compare letters
if pre {
//prefix, iterate left to right
for i := 0; i < maxl; i++ {
if xfix[i] != str[i] {
xfix = xfix[:i]
break
}
}
} else {
//suffix, iternate right to left
for i := 0; i < maxl; i++ {
xi := xfixl - i - 1
si := strl - i - 1
if xfix[xi] != str[si] {
xfix = xfix[xi+1:]
break
}
}
}
}
return xfix
}
|