...
1
2
3
4
5
6 package str
7
8 import (
9 "fmt"
10 "strings"
11 "unicode"
12 "unicode/utf8"
13 )
14
15
16
17 func StringList(args ...any) []string {
18 var x []string
19 for _, arg := range args {
20 switch arg := arg.(type) {
21 case []string:
22 x = append(x, arg...)
23 case string:
24 x = append(x, arg)
25 default:
26 panic("stringList: invalid argument of type " + fmt.Sprintf("%T", arg))
27 }
28 }
29 return x
30 }
31
32
33
34
35
36
37
38
39
40 func ToFold(s string) string {
41
42
43 for i := 0; i < len(s); i++ {
44 c := s[i]
45 if c >= utf8.RuneSelf || 'A' <= c && c <= 'Z' {
46 goto Slow
47 }
48 }
49 return s
50
51 Slow:
52 var b strings.Builder
53 for _, r := range s {
54
55
56
57 for {
58 r0 := r
59 r = unicode.SimpleFold(r0)
60 if r <= r0 {
61 break
62 }
63 }
64
65 if 'A' <= r && r <= 'Z' {
66 r += 'a' - 'A'
67 }
68 b.WriteRune(r)
69 }
70 return b.String()
71 }
72
73
74
75
76 func FoldDup(list []string) (string, string) {
77 clash := map[string]string{}
78 for _, s := range list {
79 fold := ToFold(s)
80 if t := clash[fold]; t != "" {
81 if s > t {
82 s, t = t, s
83 }
84 return s, t
85 }
86 clash[fold] = s
87 }
88 return "", ""
89 }
90
91
92 func Uniq(ss *[]string) {
93 if len(*ss) <= 1 {
94 return
95 }
96 uniq := (*ss)[:1]
97 for _, s := range *ss {
98 if s != uniq[len(uniq)-1] {
99 uniq = append(uniq, s)
100 }
101 }
102 *ss = uniq
103 }
104
View as plain text