-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspacer.go
136 lines (116 loc) · 2.93 KB
/
spacer.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
// Copyright (c) 2021 startdusk. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package strnaming
import "strings"
// Spacer defines a spacer struct
type Spacer struct {
delimiter byte
screaming bool
prefix string
ignores []byte
cache map[string]string
}
// WithIgnore ignore special char eg $
func (c *Spacer) WithIgnore(ignores ...byte) *Spacer {
for _, ignore := range ignores {
if c.containsIgnore(ignore) {
continue
}
c.ignores = append(c.ignores, ignore)
}
return c
}
// WithScreaming convert all char for upper
func (c *Spacer) WithScreaming(screaming bool) *Spacer {
c.screaming = screaming
return c
}
// WithCache set cache
func (c *Spacer) WithCache(key, value string) *Spacer {
if key == "" || value == "" {
return c
}
if len(c.cache) == 0 {
c.cache = make(map[string]string)
}
c.cache[key] = value
return c
}
// WithPrefix set prefix
func (c *Spacer) WithPrefix(prefix string) *Spacer {
prefix = strings.TrimSpace(prefix)
if prefix != "" {
c.prefix = prefix
}
return c
}
// Convert to spacer string
func (c *Spacer) Convert(str string) string {
str = strings.TrimSpace(str)
if str == "" {
return str
}
if len(c.cache) > 0 {
if a, ok := c.cache[str]; ok {
return a
}
}
return c.convert(str)
}
func (c *Spacer) convert(str string) string {
var builder strings.Builder
// Normally, most underscore named strings have 1 to 2 separators, so 2 is added here
builder.Grow(len(str) + 2)
var prev byte
var prevUpper bool
for index, sl := 0, len(str); index < sl; index++ {
cur := str[index]
curUpper, curLower, curNum := isUpper(cur), isLower(cur), isNumber(cur)
if c.screaming && curLower {
cur = toUpper(cur)
} else if !c.screaming && curUpper {
cur = toLower(cur)
}
if next, ok := nextVal(index, str); ok && !c.containsIgnore(prev) {
nextUpper, nextLower, nextNum := isUpper(next), isLower(next), isNumber(next)
if (curUpper && (nextLower || nextNum)) || (curLower && (nextUpper || nextNum)) || (curNum && (nextUpper || nextLower)) {
if prevUpper && curUpper && nextLower {
builder.WriteByte(c.delimiter)
}
builder.WriteByte(cur)
if curLower || curNum || nextNum {
builder.WriteByte(c.delimiter)
}
prev, prevUpper = cur, curUpper
continue
}
}
if !c.containsIgnore(cur) && !curUpper && !curLower && !curNum {
builder.WriteByte(c.delimiter)
} else {
builder.WriteByte(cur)
}
prev, prevUpper = cur, curUpper
}
res := builder.String()
if c.prefix != "" {
return c.prefix + string(c.delimiter) + res
}
return res
}
func (c *Spacer) containsIgnore(ignore byte) bool {
if ignore == 0 || len(c.ignores) == 0 {
return false
}
return strings.ContainsAny(string(ignore), string(c.ignores))
}
func nextVal(index int, str string) (byte, bool) {
var b byte
next := index + 1
if next < len(str) {
b = str[next]
return b, true
}
return b, false
}