-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_manipulation.go
94 lines (77 loc) · 1.67 KB
/
string_manipulation.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
package strman
import (
"strings"
)
const (
kebabDelimiter = "-"
snakeDelimiter = "_"
)
func isLower(b byte) bool {
return 'a' <= b && b <= 'z'
}
func isUpper(b byte) bool {
return 'A' <= b && b <= 'Z'
}
func isNum(b byte) bool {
return '0' <= b && b <= '9'
}
func isSymbol(b byte) bool {
switch b {
case ' ', '\t', '\n', '\r', '.', '_', '-':
return false
default:
return true
}
}
func Split(source string) []string {
p := splitter{src: source}
out := make([]string, 0, len(source)/3)
for {
next, done := p.next()
if done {
return out
}
out = append(out, next)
}
}
func ToDelimited(source, delimiter string) string {
return strings.Join(Split(source), delimiter)
}
func ToScreamingDelimited(source, delimiter string) string {
s := Split(source)
transform(s, strings.ToUpper, 0)
return strings.Join(s, delimiter)
}
func ToKebab(source string) string {
return ToDelimited(source, kebabDelimiter)
}
func ToScreamingKebab(source string) string {
return ToScreamingDelimited(source, kebabDelimiter)
}
func ToSnake(source string) string {
return ToDelimited(source, snakeDelimiter)
}
func ToScreamingSnake(source string) string {
return ToScreamingDelimited(source, snakeDelimiter)
}
func ToCamel(source string) string {
s := Split(source)
transform(s, title, 1)
return strings.Join(s, "")
}
func ToPascal(source string) string {
s := Split(source)
transform(s, title, 0)
return strings.Join(s, "")
}
func title(src string) string {
return strings.ToUpper(src[:1]) + src[1:]
}
func transform(src []string, transformFunc func(string) string, startIdx int) {
for i, v := range src {
if i < startIdx {
continue
}
src[i] = transformFunc(v)
}
}