-
-
Notifications
You must be signed in to change notification settings - Fork 818
/
strings.go
59 lines (51 loc) · 1.07 KB
/
strings.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
package utils
import (
"strings"
"github.com/navidrome/navidrome/conf"
)
func NoArticle(name string) string {
articles := strings.Split(conf.Server.IgnoredArticles, " ")
for _, a := range articles {
n := strings.TrimPrefix(name, a+" ")
if n != name {
return n
}
}
return name
}
func BreakUpStringSlice(items []string, chunkSize int) [][]string {
numTracks := len(items)
var chunks [][]string
for i := 0; i < numTracks; i += chunkSize {
end := i + chunkSize
if end > numTracks {
end = numTracks
}
chunks = append(chunks, items[i:end])
}
return chunks
}
func RangeByChunks(items []string, chunkSize int, cb func([]string) error) error {
chunks := BreakUpStringSlice(items, chunkSize)
for _, chunk := range chunks {
err := cb(chunk)
if err != nil {
return err
}
}
return nil
}
func LongestCommonPrefix(list []string) string {
if len(list) == 0 {
return ""
}
for l := 0; l < len(list[0]); l++ {
c := list[0][l]
for i := 1; i < len(list); i++ {
if l >= len(list[i]) || list[i][l] != c {
return list[i][0:l]
}
}
}
return list[0]
}