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
263
264
265
266
267
268
269
|
package strings
import "core:io"
import "core:unicode"
import "core:unicode/utf8"
to_valid_utf8 :: proc(s, replacement: string, allocator := context.allocator) -> string {
if len(s) == 0 {
return ""
}
b: Builder
init_builder(&b, 0, 0, allocator)
s := s
for c, i in s {
if c != utf8.RUNE_ERROR {
continue
}
_, w := utf8.decode_rune_in_string(s[i:])
if w == 1 {
grow_builder(&b, len(s) + len(replacement))
write_string(&b, s[:i])
s = s[i:]
break
}
}
if builder_cap(b) == 0 {
return clone(s, allocator)
}
invalid := false
for i := 0; i < len(s); /**/ {
c := s[i]
if c < utf8.RUNE_SELF {
i += 1
invalid = false
write_byte(&b, c)
continue
}
_, w := utf8.decode_rune_in_string(s[i:])
if w == 1 {
i += 1
if !invalid {
invalid = true
write_string(&b, replacement)
}
continue
}
invalid = false
write_string(&b, s[i:][:w])
i += w
}
return to_string(b)
}
to_lower :: proc(s: string, allocator := context.allocator) -> string {
b: Builder
init_builder(&b, 0, len(s), allocator)
for r in s {
write_rune_builder(&b, unicode.to_lower(r))
}
return to_string(b)
}
to_upper :: proc(s: string, allocator := context.allocator) -> string {
b: Builder
init_builder(&b, 0, len(s), allocator)
for r in s {
write_rune_builder(&b, unicode.to_upper(r))
}
return to_string(b)
}
is_delimiter :: proc(c: rune) -> bool {
return c == '-' || c == '_' || is_space(c)
}
is_separator :: proc(r: rune) -> bool {
if r <= 0x7f {
switch r {
case '0'..='9': return false
case 'a'..='z': return false
case 'A'..='Z': return false
case '_': return false
}
return true
}
// TODO(bill): unicode categories
// if unicode.is_letter(r) || unicode.is_digit(r) {
// return false;
// }
return unicode.is_space(r)
}
string_case_iterator :: proc(w: io.Writer, s: string, callback: proc(w: io.Writer, prev, curr, next: rune)) {
prev, curr: rune
for next in s {
if curr == 0 {
prev = curr
curr = next
continue
}
callback(w, prev, curr, next)
prev = curr
curr = next
}
if len(s) > 0 {
callback(w, prev, curr, 0)
}
}
to_lower_camel_case :: to_camel_case
to_camel_case :: proc(s: string, allocator := context.allocator) -> string {
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
if !is_delimiter(curr) {
if is_delimiter(prev) {
io.write_rune(w, unicode.to_upper(curr))
} else if unicode.is_lower(prev) {
io.write_rune(w, curr)
} else {
io.write_rune(w, unicode.to_lower(curr))
}
}
})
return to_string(b)
}
to_upper_camel_case :: to_pascal_case
to_pascal_case :: proc(s: string, allocator := context.allocator) -> string {
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
string_case_iterator(w, s, proc(w: io.Writer, prev, curr, next: rune) {
if !is_delimiter(curr) {
if is_delimiter(prev) || prev == 0 {
io.write_rune(w, unicode.to_upper(curr))
} else if unicode.is_lower(prev) {
io.write_rune(w, curr)
} else {
io.write_rune(w, unicode.to_lower(curr))
}
}
})
return to_string(b)
}
to_delimiter_case :: proc(s: string, delimiter: rune, all_upper_case: bool, allocator := context.allocator) -> string {
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
adjust_case := unicode.to_upper if all_upper_case else unicode.to_lower
prev, curr: rune
for next in s {
if is_delimiter(curr) {
if !is_delimiter(prev) {
io.write_rune(w, delimiter)
}
} else if unicode.is_upper(curr) {
if unicode.is_lower(prev) || (unicode.is_upper(prev) && unicode.is_lower(next)) {
io.write_rune(w, delimiter)
}
io.write_rune(w, adjust_case(curr))
} else if curr != 0 {
io.write_rune(w, adjust_case(curr))
}
prev = curr
curr = next
}
if len(s) > 0 {
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
io.write_rune(w, delimiter)
}
io.write_rune(w, adjust_case(curr))
}
return to_string(b)
}
to_snake_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '_', false, allocator)
}
to_screaming_snake_case :: to_upper_snake_case
to_upper_snake_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '_', true, allocator)
}
to_kebab_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '-', false, allocator)
}
to_upper_case :: proc(s: string, allocator := context.allocator) -> string {
return to_delimiter_case(s, '-', true, allocator)
}
to_ada_case :: proc(s: string, allocator := context.allocator) -> string {
delimiter :: '_'
s := s
s = trim_space(s)
b: Builder
init_builder(&b, 0, len(s), allocator)
w := to_writer(&b)
prev, curr: rune
for next in s {
if is_delimiter(curr) {
if !is_delimiter(prev) {
io.write_rune(w, delimiter)
}
} else if unicode.is_upper(curr) {
if unicode.is_lower(prev) || (unicode.is_upper(prev) && unicode.is_lower(next)) {
io.write_rune(w, delimiter)
}
io.write_rune(w, unicode.to_upper(curr))
} else if curr != 0 {
io.write_rune(w, unicode.to_lower(curr))
}
prev = curr
curr = next
}
if len(s) > 0 {
if unicode.is_upper(curr) && unicode.is_lower(prev) && prev != 0 {
io.write_rune(w, delimiter)
io.write_rune(w, unicode.to_upper(curr))
} else {
io.write_rune(w, unicode.to_lower(curr))
}
}
return to_string(b)
}
|