-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.go
More file actions
262 lines (212 loc) · 4.47 KB
/
Copy pathstr.go
File metadata and controls
262 lines (212 loc) · 4.47 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package typing
import (
"strconv"
"strings"
"unicode"
)
type Str string
func (s Str) Equals(other string) bool {
return string(s) == other
}
func (s Str) Len() int {
return len(s)
}
func (s Str) ToBytes() []byte {
return []byte(s)
}
func (s Str) ToString() string {
return string(s)
}
func (s Str) ToRunes() []rune {
return []rune(s)
}
func (s Str) ToInt() (int, error) {
return strconv.Atoi(string(s))
}
func (s Str) ToInt64() (int64, error) {
return strconv.ParseInt(string(s), 10, 64)
}
func (s Str) ToFloat64() (float64, error) {
return strconv.ParseFloat(string(s), 64)
}
func (s Str) ToBool() (bool, error) {
return strconv.ParseBool(string(s))
}
func (s Str) Upper() Str {
return Str(strings.ToUpper(string(s)))
}
func (s Str) Lower() Str {
return Str(strings.ToLower(string(s)))
}
func (s Str) Split(sep string) []Str {
return s.SplitN(sep, -1)
}
func (s Str) SplitN(sep string, n int) []Str {
ss := strings.SplitN(string(s), sep, n)
ret := make([]Str, len(ss))
for i, s_ := range ss {
ret[i] = Str(s_)
}
return ret
}
func (s Str) SplitLines() []Str {
return s.Split("\n")
}
func (s Str) RSplitN(sep string, n int) []Str {
if n <= 0 {
return s.Split(sep)
}
if sep == "" {
return []Str{s}
}
var res []Str
for n > 0 {
idx := s.LastIndex(sep)
if idx == -1 {
break
}
res = append([]Str{s[idx+len(sep):]}, res...)
s = s[:idx]
n--
}
res = append([]Str{s}, res...)
return res
}
func (s Str) Replace(old, new string) Str {
return s.ReplaceN(old, new, -1)
}
func (s Str) ReplaceN(old, new string, n int) Str {
return Str(strings.Replace(string(s), old, new, n))
}
func (s Str) Trim(cutset string) Str {
return Str(strings.Trim(string(s), cutset))
}
func (s Str) TrimLeft(cutset string) Str {
return Str(strings.TrimLeft(string(s), cutset))
}
func (s Str) TrimRight(cutset string) Str {
return Str(strings.TrimRight(string(s), cutset))
}
func (s Str) IsLower() bool {
hasLetter := false
for _, r := range s {
if unicode.IsLetter(r) {
hasLetter = true
if !unicode.IsLower(r) {
return false
}
}
}
return hasLetter
}
func (s Str) IsUpper() bool {
return s == s.Upper()
}
func (s Str) HasPrefix(prefix string) bool {
return strings.HasPrefix(string(s), prefix)
}
func (s Str) HasSuffix(suffix string) bool {
return strings.HasSuffix(string(s), suffix)
}
func (s Str) Contains(substr string) bool {
return s.Index(substr) >= 0
}
func (s Str) ContainsAny(chars string) bool {
return s.IndexAny(chars) >= 0
}
func (s Str) RemovePrefix(prefix string) Str {
return s.TrimLeft(prefix)
}
func (s Str) RemoveSuffix(suffix string) Str {
return s.TrimRight(suffix)
}
func (s Str) Repeat(count int) Str {
return Str(strings.Repeat(string(s), count))
}
func (s Str) Join(ss []string) Str {
return Str(strings.Join(ss, string(s)))
}
func (s Str) Index(sub string) int {
return strings.Index(string(s), sub)
}
func (s Str) IndexAny(chars string) int {
return strings.IndexAny(string(s), chars)
}
func (s Str) LastIndex(sub string) int {
return strings.LastIndex(string(s), sub)
}
func (s Str) SwapCase() Str {
var b strings.Builder
for _, r := range s {
switch {
case unicode.IsUpper(r):
b.WriteRune(unicode.ToLower(r))
case unicode.IsLower(r):
b.WriteRune(unicode.ToUpper(r))
default:
b.WriteRune(r)
}
}
return Str(b.String())
}
func tokenize(s string) []string {
var tokens []string
var buf []rune
flush := func() {
if len(buf) > 0 {
tokens = append(tokens, strings.ToLower(string(buf)))
buf = buf[:0]
}
}
runes := []rune(s)
for i, r := range runes {
// 1. 非字母数字,直接切
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
flush()
continue
}
// 2. 大小写边界
if unicode.IsUpper(r) {
if len(buf) > 0 {
prev := buf[len(buf)-1]
var next rune
if i+1 < len(runes) {
next = runes[i+1]
}
if unicode.IsLower(prev) ||
(next != 0 && unicode.IsLower(next)) {
flush()
}
}
}
// 3. 字母 ↔ 数字边界
if len(buf) > 0 {
prev := buf[len(buf)-1]
if unicode.IsDigit(r) != unicode.IsDigit(prev) {
flush()
}
}
buf = append(buf, r)
}
flush()
return tokens
}
func (s Str) SnakeCase() Str {
tokens := tokenize(string(s))
return Str(strings.Join(tokens, "_"))
}
func (s Str) CamelCase() Str {
tokens := tokenize(string(s))
if len(tokens) == 0 {
return ""
}
var b strings.Builder
for i, t := range tokens {
if i == 0 {
b.WriteString(t)
continue
}
b.WriteString(strings.ToTitle(t))
}
return Str(b.String())
}