-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
44 lines (39 loc) · 947 Bytes
/
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
package generic
import (
"strings"
)
func JoinToString[T any](list []T, sep string, stringer func(T) string) string {
var out strings.Builder
var length = len(list)
var lastIndex = length - 1
for index := 0; index < length; index++ {
out.WriteString(stringer(list[index]))
if index < lastIndex {
out.WriteString(sep)
}
}
return out.String()
}
func TrimSpace(s *string) {
*s = strings.TrimSpace(*s)
}
// SharedPrefix Extracts the shared prefix from two strings.
// Generated by ChatGPT
func SharedPrefix(str1, str2 string) string {
len1, len2 := len(str1), len(str2)
i := 0
for i < len1 && i < len2 && str1[i] == str2[i] {
i++
}
return str1[:i]
}
// SharedSuffix extracts the shared suffix from two strings.
// Generated by ChatGPT
func SharedSuffix(str1, str2 string) string {
len1, len2 := len(str1), len(str2)
i := 0
for i < len1 && i < len2 && str1[len1-i-1] == str2[len2-i-1] {
i++
}
return str1[len1-i:]
}