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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
|
package strings
import "core:mem"
import "core:unicode/utf8"
clone :: proc(s: string, allocator := context.allocator) -> string {
c := make([]byte, len(s)+1, allocator);
copy(c, cast([]byte)s);
c[len(s)] = 0;
return string(c[:len(s)]);
}
clone_to_cstring :: proc(s: string, allocator := context.allocator) -> cstring {
c := make([]byte, len(s)+1, allocator);
copy(c, cast([]byte)s);
c[len(s)] = 0;
return cstring(&c[0]);
}
@(deprecated="Please use 'strings.clone'")
new_string :: proc(s: string, allocator := context.allocator) -> string {
c := make([]byte, len(s)+1, allocator);
copy(c, cast([]byte)s);
c[len(s)] = 0;
return string(c[:len(s)]);
}
@(deprecated="Please use 'strings.clone_to_cstring'")
new_cstring :: proc(s: string, allocator := context.allocator) -> cstring {
c := make([]byte, len(s)+1, allocator);
copy(c, cast([]byte)s);
c[len(s)] = 0;
return cstring(&c[0]);
}
@(deprecated="Please use a standard cast for cstring to string")
to_odin_string :: proc(str: cstring) -> string {
return string(str);
}
string_from_ptr :: proc(ptr: ^byte, len: int) -> string {
return transmute(string)mem.Raw_String{ptr, len};
}
compare :: proc(lhs, rhs: string) -> int {
return mem.compare(cast([]byte)lhs, cast([]byte)rhs);
}
contains_rune :: proc(s: string, r: rune) -> int {
for c, offset in s {
if c == r do return offset;
}
return -1;
}
contains :: proc(s, substr: string) -> bool {
return index(s, substr) >= 0;
}
contains_any :: proc(s, chars: string) -> bool {
return index_any(s, chars) >= 0;
}
rune_count :: proc(s: string) -> int {
return utf8.rune_count_in_string(s);
}
equal_fold :: proc(s, t: string) -> bool {
loop: for s != "" && t != "" {
sr, tr: rune;
if s[0] < utf8.RUNE_SELF {
sr, s = rune(s[0]), s[1:];
} else {
r, size := utf8.decode_rune_in_string(s);
sr, s = r, s[size:];
}
if t[0] < utf8.RUNE_SELF {
tr, t = rune(t[0]), t[1:];
} else {
r, size := utf8.decode_rune_in_string(t);
tr, t = r, t[size:];
}
if tr == sr { // easy case
continue loop;
}
if tr < sr {
tr, sr = sr, tr;
}
if tr < utf8.RUNE_SELF {
switch sr {
case 'A'..'Z':
if tr == (sr+'a')-'A' {
continue loop;
}
}
return false;
}
// TODO(bill): Unicode folding
return false;
}
return s == t;
}
has_prefix :: proc(s, prefix: string) -> bool {
return len(s) >= len(prefix) && s[0:len(prefix)] == prefix;
}
has_suffix :: proc(s, suffix: string) -> bool {
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix;
}
join :: proc(a: []string, sep: string, allocator := context.allocator) -> string {
if len(a) == 0 {
return "";
}
n := len(sep) * (len(a) - 1);
for s in a {
n += len(s);
}
b := make([]byte, n, allocator);
i := copy(b, cast([]byte)a[0]);
for s in a[1:] {
i += copy(b[i:], cast([]byte)sep);
i += copy(b[i:], cast([]byte)s);
}
return string(b);
}
concatenate :: proc(a: []string, allocator := context.allocator) -> string {
if len(a) == 0 {
return "";
}
n := 0;
for s in a {
n += len(s);
}
b := make([]byte, n, allocator);
i := 0;
for s in a {
i += copy(b[i:], cast([]byte)s);
}
return string(b);
}
index_byte :: proc(s: string, c: byte) -> int {
for i := 0; i < len(s); i += 1 {
if s[i] == c do return i;
}
return -1;
}
// Returns i1 if c is not present
last_index_byte :: proc(s: string, c: byte) -> int {
for i := len(s)-1; i >= 0; i -= 1 {
if s[i] == c do return i;
}
return -1;
}
index :: proc(s, substr: string) -> int {
n := len(substr);
switch {
case n == 0:
return 0;
case n == 1:
return index_byte(s, substr[0]);
case n == len(s):
if s == substr {
return 0;
}
return -1;
case n > len(s):
return -1;
}
for i := 0; i < len(s)-n+1; i += 1 {
x := s[i:i+n];
if x == substr {
return i;
}
}
return -1;
}
index_any :: proc(s, chars: string) -> int {
if chars == "" {
return -1;
}
// TODO(bill): Optimize
for r, i in s {
for c in chars {
if r == c {
return i;
}
}
}
return -1;
}
last_index_any :: proc(s, chars: string) -> int {
if chars == "" {
return -1;
}
for i := len(s); i > 0; {
r, w := utf8.decode_last_rune_in_string(s[:i]);
i -= w;
for c in chars {
if r == c {
return i;
}
}
}
return -1;
}
count :: proc(s, substr: string) -> int {
if len(substr) == 0 { // special case
return rune_count(s) + 1;
}
if len(substr) == 1 {
c := substr[0];
switch len(s) {
case 0:
return 0;
case 1:
return int(s[0] == c);
}
n := 0;
for i := 0; i < len(s); i += 1 {
if s[i] == c {
n += 1;
}
}
return n;
}
// TODO(bill): Use a non-brute for approach
n := 0;
for {
i := index(s, substr);
if i == -1 {
return n;
}
n += 1;
s = s[i+len(substr):];
}
return n;
}
repeat :: proc(s: string, count: int, allocator := context.allocator) -> string {
if count < 0 {
panic("strings: negative repeat count");
} else if count > 0 && (len(s)*count)/count != len(s) {
panic("strings: repeat count will cause an overflow");
}
b := make([]byte, len(s)*count, allocator);
i := copy(b, cast([]byte)s);
for i < len(b) { // 2^N trick to reduce the need to copy
copy(b[i:], b[:i]);
i *= 2;
}
return string(b);
}
replace_all :: proc(s, old, new: string, allocator := context.allocator) -> (output: string, was_allocation: bool) {
return replace(s, old, new, -1, allocator);
}
// if n < 0, no limit on the number of replacements
replace :: proc(s, old, new: string, n: int, allocator := context.allocator) -> (output: string, was_allocation: bool) {
if old == new || n == 0 {
was_allocation = false;
output = s;
return;
}
if m := count(s, old); m == 0 {
was_allocation = false;
output = s;
return;
} else if n < 0 || m < n {
n = m;
}
t := make([]byte, len(s) + n*(len(new) - len(old)), allocator);
was_allocation = true;
w := 0;
start := 0;
for i := 0; i < n; i += 1 {
j := start;
if len(old) == 0 {
if i > 0 {
_, width := utf8.decode_rune_in_string(s[start:]);
j += width;
}
} else {
j += index(s[start:], old);
}
w += copy(t[w:], cast([]byte)s[start:j]);
w += copy(t[w:], cast([]byte)new);
start = j + len(old);
}
w += copy(t[w:], cast([]byte)s[start:]);
output = string(t[0:w]);
return;
}
is_ascii_space :: proc(r: rune) -> bool {
switch r {
case '\t', '\n', '\v', '\f', '\r', ' ':
return true;
}
return false;
}
is_space :: proc(r: rune) -> bool {
if r < 0x2000 {
switch r {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xa0, 0x1680:
return true;
}
} else {
if r <= 0x200a {
return true;
}
switch r {
case 0x2028, 0x2029, 0x202f, 0x205f, 0x3000:
return true;
}
}
return false;
}
is_null :: proc(r: rune) -> bool {
return r == 0x0000;
}
index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
for r, i in s {
if p(r) == truth {
return i;
}
}
return -1;
}
index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
for r, i in s {
if p(state, r) == truth {
return i;
}
}
return -1;
}
last_index_proc :: proc(s: string, p: proc(rune) -> bool, truth := true) -> int {
// TODO(bill): Probably use Rabin-Karp Search
for i := len(s); i > 0; {
r, size := utf8.decode_last_rune_in_string(s[:i]);
i -= size;
if p(r) == truth {
return i;
}
}
return -1;
}
last_index_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr, truth := true) -> int {
// TODO(bill): Probably use Rabin-Karp Search
for i := len(s); i > 0; {
r, size := utf8.decode_last_rune_in_string(s[:i]);
i -= size;
if p(state, r) == truth {
return i;
}
}
return -1;
}
trim_left_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
i := index_proc(s, p, false);
if i == -1 {
return "";
}
return s[i:];
}
index_rune :: proc(s: string, r: rune) -> int {
switch {
case 0 <= r && r < utf8.RUNE_SELF:
return index_byte(s, byte(r));
case r == utf8.RUNE_ERROR:
for c, i in s {
if c == utf8.RUNE_ERROR {
return i;
}
}
return -1;
case !utf8.valid_rune(r):
return -1;
}
b, w := utf8.encode_rune(r);
return index(s, string(b[:w]));
}
trim_left_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
i := index_proc_with_state(s, p, state, false);
if i == -1 {
return "";
}
return s[i:];
}
trim_right_proc :: proc(s: string, p: proc(rune) -> bool) -> string {
i := last_index_proc(s, p, false);
if i >= 0 && s[i] >= utf8.RUNE_SELF {
_, w := utf8.decode_rune_in_string(s[i:]);
i += w;
} else {
i += 1;
}
return s[0:i];
}
trim_right_proc_with_state :: proc(s: string, p: proc(rawptr, rune) -> bool, state: rawptr) -> string {
i := last_index_proc_with_state(s, p, state, false);
if i >= 0 && s[i] >= utf8.RUNE_SELF {
_, w := utf8.decode_rune_in_string(s[i:]);
i += w;
} else {
i += 1;
}
return s[0:i];
}
is_in_cutset :: proc(state: rawptr, r: rune) -> bool {
if state == nil {
return false;
}
cutset := (^string)(state)^;
for c in cutset {
if r == c {
return true;
}
}
return false;
}
trim_left :: proc(s: string, cutset: string) -> string {
if s == "" || cutset == "" {
return s;
}
return trim_left_proc_with_state(s, is_in_cutset, &cutset);
}
trim_right :: proc(s: string, cutset: string) -> string {
if s == "" || cutset == "" {
return s;
}
return trim_right_proc_with_state(s, is_in_cutset, &cutset);
}
trim :: proc(s: string, cutset: string) -> string {
return trim_right(trim_left(s, cutset), cutset);
}
trim_left_space :: proc(s: string) -> string {
return trim_left_proc(s, is_space);
}
trim_right_space :: proc(s: string) -> string {
return trim_right_proc(s, is_space);
}
trim_space :: proc(s: string) -> string {
return trim_right_space(trim_left_space(s));
}
trim_left_null :: proc(s: string) -> string {
return trim_left_proc(s, is_null);
}
trim_right_null :: proc(s: string) -> string {
return trim_right_proc(s, is_null);
}
trim_null :: proc(s: string) -> string {
return trim_right_null(trim_left_null(s));
}
// scrub scruvs invalid utf-8 characters and replaces them with the replacement string
// Adjacent invalid bytes are only replaced once
scrub :: proc(str: string, replacement: string, allocator := context.allocator) -> string {
b := make_builder(allocator);;
grow_builder(&b, len(str));
has_error := false;
cursor := 0;
origin := str;
for len(str) > 0 {
r, w := utf8.decode_rune_in_string(str);
if r == utf8.RUNE_ERROR {
if !has_error {
has_error = true;
write_string(&b, origin[:cursor]);
}
} else if has_error {
has_error = false;
write_string(&b, replacement);
origin = origin[cursor:];
cursor = 0;
}
cursor += w;
str = str[w:];
}
return to_string(b);
}
reverse :: proc(str: string, allocator := context.allocator) -> string {
n := len(str);
buf := make([]byte, n);
i := 0;
for len(str) > 0 {
_, w := utf8.decode_rune_in_string(str);
copy(buf[i:], cast([]byte)str[:w]);
str = str[w:];
}
return string(buf);
}
expand_tabs :: proc(str: string, tab_size: int, allocator := context.allocator) -> string {
if tab_size <= 0 {
panic("tab size must be positive");
}
if str == "" {
return "";
}
b := make_builder(allocator);
column: int;
for len(str) > 0 {
r, w := utf8.decode_rune_in_string(str);
if r == '\t' {
expand := tab_size - column%tab_size;
for i := 0; i < expand; i += 1 {
write_byte(&b, ' ');
}
column += expand;
} else {
if r == '\n' {
column = 0;
} else {
column += w;
}
write_rune(&b, r);
}
str = str[w:];
}
return to_string(b);
}
partition :: proc(str, sep: string) -> (head, match, tail: string) {
i := index(str, sep);
if i == -1 {
head = str;
return;
}
head = str[:i];
match = str[i:i+len(sep)];
tail = str[i+len(sep):];
return;
}
center_justify :: centre_justify; // NOTE(bill): Because Americans exist
// centre_justify returns a string with a pad string at boths sides if the str's rune length is smaller than length
centre_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
n := rune_count(str);
if n >= length || pad == "" {
return clone(str, allocator);
}
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_pad_string(&b, pad, pad_len, remains/2);
write_string(&b, str);
write_pad_string(&b, pad, pad_len, (remains+1)/2);
return to_string(b);
}
// left_justify returns a string with a pad string at left side if the str's rune length is smaller than length
left_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
n := rune_count(str);
if n >= length || pad == "" {
return clone(str, allocator);
}
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_string(&b, str);
write_pad_string(&b, pad, pad_len, remains);
return to_string(b);
}
// right_justify returns a string with a pad string at right side if the str's rune length is smaller than length
right_justify :: proc(str: string, length: int, pad: string, allocator := context.allocator) -> string {
n := rune_count(str);
if n >= length || pad == "" {
return clone(str, allocator);
}
remains := length-1;
pad_len := rune_count(pad);
b := make_builder(allocator);
grow_builder(&b, len(str) + (remains/pad_len + 1)*len(pad));
write_pad_string(&b, pad, pad_len, remains);
write_string(&b, str);
return to_string(b);
}
@private
write_pad_string :: proc(b: ^Builder, pad: string, pad_len, remains: int) {
repeats := remains / pad_len;
for i := 0; i < repeats; i += 1 {
write_string(b, pad);
}
remains = remains % pad_len;
if remains != 0 do for i := 0; i < remains; i += 1 {
r, w := utf8.decode_rune_in_string(pad);
write_rune(b, r);
pad = pad[w:];
}
}
|