-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
236 lines (215 loc) · 5.73 KB
/
string.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package golib
import (
"cmp"
"encoding/hex"
"fmt"
"slices"
"strconv"
"strings"
"unicode/utf8"
"golang.org/x/text/cases"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
// PushOntoStringArray adds str(s) to arr if missing
func PushOntoStringArray(arr []string, strs ...string) (arr1 []string) {
arr1 = arr
for _, str := range strs {
if StrInArray(str, arr1) {
continue
}
arr1 = append(arr1, str)
}
return arr1
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// CutStrInArray cuts each string inside the given array to the
// given length and adds the suffix is the string was cut.
// It also replaces newlines with ⏎ in the string
func CutStrInArray(arr []string, l int, suffix string) (arr2 []string) {
arr2 = []string{}
for _, s := range arr {
s2 := []string{}
for _, p := range strings.Split(s, "\n") {
s2 = append(s2, strings.Trim(p, " "))
}
s = strings.Join(s2, "⏎")
arr2 = append(arr2, CutStr(s, l, suffix))
}
return arr2
}
// CutStr cuts s if is longer than "len"
// When a string is cut, the suffix is added
func CutStr(s string, l int, suffix string) string {
if len(s) > l {
return s[0:l] + suffix
}
return s
}
// CutRunes works like CutStr but counts runes not bytes
func CutRunes(s string, l int, suffix string) string {
r := []rune(s)
if len(r) > l {
return string(r[0:l]) + suffix
}
return s
}
// PadStr returns string s filled to a length of padWidth. If s is longer than pw
// string will be cut to the length. If padWidth <= 0 s will be returned unchanged.
// The counting of characters is rune based. So "Ä" counts as one.
func PadStr(s string, padWidth int) string {
if padWidth <= 0 {
return s
}
l := utf8.RuneCountInString(s)
switch {
case l == padWidth:
return s
case l < padWidth:
return s + strings.Repeat(" ", padWidth-l)
default:
return string([]rune(s)[0:padWidth])
}
}
func StrInArray(str string, arr []string) bool {
for _, item := range arr {
if item == str {
return true
}
}
return false
}
// ArrayContainsStrs returns true if arr contains any of the passed
// strings
func ArrayContainsStrs(arr []string, strs ...string) bool {
for _, str := range strs {
if StrInArray(str, arr) {
return true
}
}
return false
}
func ToString(i interface{}) string {
if i == nil {
return ""
}
switch v := i.(type) {
case int64:
return strconv.FormatInt(v, 10)
case *int64:
return strconv.FormatInt(*v, 10)
case string:
return v
default:
return fmt.Sprintf("%v", i)
}
}
// ReplaceEndless replaces old to new in s as long
// as the string is getting shorter.
// This function can be used to remove double // from an URL path.
func ReplaceEndless(s, old, new string) string {
for {
ns := strings.ReplaceAll(s, old, new)
if len(ns) >= len(s) {
return s
}
s = ns
}
}
// ToAnySlice converts a slice of []T to []any
func ToAnySlice[T any](list []T) []any {
in := make([]any, len(list))
for idx, s0 := range list {
in[idx] = s0
}
return in
}
// AnyToStrSlice converts a slice of string to slice of interface
func AnyToStrSlice[T any](list []T) []string {
sn := make([]string, len(list))
for idx, i0 := range list {
sn[idx] = fmt.Sprintf("%v", i0)
}
return sn
}
// FoldStr uses ICU folding on s
func FoldStr(s string) string {
c := cases.Fold()
return c.String(s)
}
// SortStr returns a sortable hex string for s in language lang. To
// sort, the collate.IgnoreWidth, collate.IgnoreCase are set. Strings are normalized
// to NFC.
func SortStr(lang language.Tag, s string, opts ...collate.Option) string {
opts = append(opts, collate.IgnoreWidth, collate.IgnoreCase)
cl := collate.New(lang, opts...)
buf := new(collate.Buffer)
return hex.EncodeToString(cl.KeyFromString(buf, s))
}
// Split string s into byte chunks of a max size of chunkSize Each string
// returned has a maximum length of byteChunkSize The split is UTF-8 safe.
// If byteChunkSize is 0, no chunking is performed.
func StringByteChunks(s string, byteChunkSize int) (chunks []string) {
if len(s) == 0 {
return nil
}
if byteChunkSize == 0 || byteChunkSize >= len(s) {
return []string{s}
}
chunks = []string{}
chars := []byte{}
chunkOffset := 0
for idx, charRune := range s {
char := string(charRune)
if idx-chunkOffset+len(char) > byteChunkSize {
// last char caused an overflow, add
// to the previous last
chunks = append(chunks, string(chars))
chars = []byte{}
chunkOffset = idx
}
chars = append(chars, []byte(char)...)
}
chunks = append(chunks, string(chars))
return chunks
}
// ToValidUTF8 checks and converts a string to valid UTF-8, replacing invalid characters.
// It iterates over the string, and for each rune that is identified as invalid (RuneError),
// it replaces it with the specified 'replacement' rune.
func ToValidUTF8(s string, replacement rune) string {
if utf8.ValidString(s) {
return s // The string is already valid UTF-8.
}
// Build a new string with invalid runes replaced.
var builder strings.Builder
for _, r := range s {
if r == utf8.RuneError {
builder.WriteRune(replacement)
} else {
builder.WriteRune(r)
}
}
return builder.String()
}
// DebugValues takes a slice of T and returns and ordered list each item
// rendered in a comma separated list. If the list is longer than length bytes,
// the rest of the slice is omitted and the output ends in ... With length <= 0,
// the whole slice is rendered
func DebugValues[T cmp.Ordered](list []T, length int) string {
slices.Sort(list)
sb := strings.Builder{}
sb.WriteRune('[')
for idx, item := range list {
s := fmt.Sprintf("%v", item)
if idx > 0 {
sb.WriteRune(',')
}
if length > 0 && len(s)+sb.Len() > length {
sb.WriteString("...")
break
}
sb.WriteString(s)
}
sb.WriteString(fmt.Sprintf("] %d", len(list)))
return sb.String()
}