forked from c3lang/c3c
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c3
More file actions
1206 lines (1051 loc) · 28.6 KB
/
Copy pathstring.c3
File metadata and controls
1206 lines (1051 loc) · 28.6 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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module std::core::string;
import std::io, std::ascii;
typedef String @if(!$defined(String)) = inline char[];
<*
ZString is a pointer to a zero terminated array of chars.
Use ZString when you need to interop with C zero terminated strings.
*>
typedef ZString = inline char*;
<*
WString is a pointer to a zero terminated array of Char16.
Depending on the platform, this may or may not correspond to wchar_t.
For Windows, wchar_t is generally 16 bits, on MacOS it is 32 bits.
However, for both MacOS and Linux, regular C strings (ZString)
will be UTF-8 encoded, so there is no need to use the wchar_t versions
of functions outside of encoding functions.
*>
typedef WString = inline Char16*;
<*
Char32 is a UTF32 codepoint
*>
alias Char32 = uint;
<*
Char16 is a UTF16 "character"
*>
alias Char16 = ushort;
<*
Common faults used with strings
*>
faultdef INVALID_UTF8, INVALID_UTF16, CONVERSION_FAILED,
EMPTY_STRING, NEGATIVE_VALUE, MALFORMED_INTEGER,
INTEGER_OVERFLOW, MALFORMED_FLOAT, FLOAT_OUT_OF_RANGE;
<*
Create a pointer to an UTF32 encoded string at compile time.
@param $string : "The string to encode"
*>
macro Char32* @wstring32(String $string) @builtin
{
return (Char32*)&&$$wstr32($string);
}
<*
Create a slice of an UTF32 encoded string at compile time.
@param $string : "The string to encode"
*>
macro Char32[] @char32(String $string) @builtin
{
return $$wstr32($string)[..^2];
}
<*
Create a WString (an UTF16 encoded string) at compile time.
@param $string : "The string to encode"
*>
macro WString @wstring(String $string) @builtin
{
return (WString)&&$$wstr16($string);
}
<*
Create a slice of an UTF32 encoded string at compile time.
@param $string : "The string to encode"
*>
macro Char16[] @char16(String $string) @builtin
{
return $$wstr16($string)[..^2];
}
macro String @sprintf(String $format, ...) @builtin @const
{
return $$sprintf($format, $vasplat);
}
<*
Return a temporary ZString created using the formatting function.
@param [in] fmt : `The formatting string`
*>
fn ZString tformat_zstr(String fmt, args...) @format(0)
{
DString str = dstring::temp_with_capacity(fmt.len + args.len * 8);
str.appendf(fmt, ...args);
return str.zstr_view();
}
<*
Return a new String created using the formatting function.
@param [inout] allocator : `The allocator to use`
@param [in] fmt : `The formatting string`
*>
fn String format(Allocator allocator, String fmt, args...) @format(1) => @pool()
{
DString str = dstring::temp_with_capacity(fmt.len + args.len * 8);
str.appendf(fmt, ...args);
return str.copy_str(allocator);
}
<*
Return a new String created using the formatting function.
@param [inout] buffer : `The buffer to use`
@param [in] fmt : `The formatting string`
*>
fn String bformat(char[] buffer, String fmt, args...) @format(1)
{
DString str = dstring::new_with_capacity(allocator::wrap(buffer), fmt.len + args.len * 8);
str.appendf(fmt, ...args);
return str.str_view();
}
<*
Return a temporary String created using the formatting function.
@param [in] fmt : `The formatting string`
*>
fn String tformat(String fmt, args...) @format(0)
{
DString str = dstring::temp_with_capacity(fmt.len + args.len * 8);
str.appendf(fmt, ...args);
return str.str_view();
}
<*
Check if a character is in a set.
@param c : `the character to check`
@param [in] set : `String containing the characters`
@pure
@return `True if a character is in the set`
*>
macro bool char_in_set(char c, String set)
{
foreach (ch : set) if (ch == c) return true;
return false;
}
fn String join(Allocator allocator, String[] s, String joiner)
{
if (!s)
{
return (String)allocator::new_array(allocator, char, 2)[:0];
}
usz total_size = joiner.len * s.len;
foreach (String* &str : s)
{
total_size += str.len;
}
@pool()
{
DString res = dstring::temp_with_capacity(total_size);
res.append(s[0]);
foreach (String* &str : s[1..])
{
res.append(joiner);
res.append(*str);
}
return res.copy_str(allocator);
};
}
<*
Replace all instances of one substring with a different string.
@param [in] self
@param [in] needle : `The string to be replaced`
@param [in] new_str : `The replacement string`
@param [&inout] allocator : `The allocator to use for the String`
@return "The new string with the elements replaced"
*>
fn String String.replace(self, Allocator allocator, String needle, String new_str) @nodiscard
{
@pool()
{
String[] split = self.tsplit(needle);
return dstring::join(tmem, split, new_str).copy_str(allocator);
};
}
<*
Replace all instances of one substring with a different string, allocating the new string on the temp allocator.
@param [in] self
@param [in] needle : `The string to be replaced`
@param [in] new_str : `The replacement string`
@return "The new string with the elements replaced"
*>
fn String String.treplace(self, String needle, String new_str)
{
String[] split = self.tsplit(needle);
return dstring::join(tmem, split, new_str).str_view();
}
<*
Remove characters from the front and end of a string.
@param [in] self : `The string to trim`
@param [in] to_trim : `The set of characters to trim, defaults to whitespace`
@pure
@return `a substring of the string passed in`
*>
fn String String.trim(self, String to_trim = " \n\t\r\f\v")
{
return self.trim_left(to_trim).trim_right(to_trim);
}
<*
Remove characters from the front and end of a string.
@param [in] self : `The string to trim`
@param to_trim : `The set of characters to trim, defaults to whitespace`
@pure
@return `a substring of the string passed in`
*>
fn String String.trim_charset(self, AsciiCharset to_trim = ascii::WHITESPACE_SET)
{
usz start = 0;
usz len = self.len;
while (start < len && to_trim.contains(self[start])) start++;
while (len > start && to_trim.contains(self[len - 1])) len--;
return self[start..len - 1];
}
<*
Remove characters from the front of a string.
@param [in] self : `The string to trim`
@param [in] to_trim : `The set of characters to trim, defaults to whitespace`
@pure
@return `a substring of the string passed in`
*>
fn String String.trim_left(self, String to_trim = " \n\t\r\f\v")
{
usz start = 0;
usz len = self.len;
while (start < len && char_in_set(self[start], to_trim)) start++;
if (start == len) return self[:0];
return self[start..];
}
<*
Remove characters from the end of a string.
@param [in] self : `The string to trim`
@param [in] to_trim : `The set of characters to trim, defaults to whitespace`
@pure
@return `a substring of the string passed in`
*>
fn String String.trim_right(self, String to_trim = " \n\t\r\f\v")
{
usz len = self.len;
while (len > 0 && char_in_set(self[len - 1], to_trim)) len--;
return self[:len];
}
<*
Check if the String starts with the prefix.
@param [in] self
@param [in] prefix
@pure
@return `'true' if the string starts with the prefix`
*>
fn bool String.starts_with(self, String prefix)
{
if (prefix.len > self.len) return false;
if (!prefix.len) return true;
return self[:prefix.len] == prefix;
}
<*
Check if the String ends with the suffix.
@param [in] self
@param [in] suffix
@pure
@return `'true' if the string ends with the suffix`
*>
fn bool String.ends_with(self, String suffix)
{
if (suffix.len > self.len) return false;
if (!suffix.len) return true;
return self[^suffix.len..] == suffix;
}
<*
Strip the front of the string if the prefix exists.
@param [in] self
@param [in] prefix
@pure
@return `the substring with the prefix removed`
*>
fn String String.strip(self, String prefix)
{
if (!prefix.len || !self.starts_with(prefix)) return self;
return self[prefix.len..];
}
<*
Strip the end of the string if the suffix exists.
@param [in] self
@param [in] suffix
@pure
@return `the substring with the suffix removed`
*>
fn String String.strip_end(self, String suffix)
{
if (!suffix.len || !self.ends_with(suffix)) return self;
// Note that this is the safe way if we want to support zero length.
return self[:(self.len - suffix.len)];
}
<*
Split a string into parts, e.g "a|b|c" split with "|" yields { "a", "b", "c" }
@param [in] self
@param [in] delimiter
@param max : "Max number of elements, 0 means no limit, defaults to 0"
@param skip_empty : "True to skip empty elements"
@param [&inout] allocator : "The allocator to use for the String[]"
@require delimiter.len > 0 : "The delimiter must be at least 1 character long"
@ensure return.len > 0 || skip_empty
*>
fn String[] String.split(self, Allocator allocator, String delimiter, usz max = 0, bool skip_empty = false)
{
usz capacity = 16;
usz i = 0;
String* holder = allocator::alloc_array(allocator, String, capacity);
bool no_more = false;
while (!no_more)
{
usz? index = i == max - 1 ? NOT_FOUND? : self.index_of(delimiter);
String res @noinit;
if (try index)
{
res = self[:index];
self = self[index + delimiter.len..];
}
else
{
res = self;
no_more = true;
}
if (!res.len && skip_empty)
{
continue;
}
if (i == capacity)
{
capacity *= 2;
holder = allocator::realloc(allocator, holder, String.sizeof * capacity);
}
holder[i++] = res;
}
return holder[:i];
}
<*
This function is identical to String.split, but implicitly uses the
temporary allocator.
@param [in] s
@param [in] delimiter
@param max : "Max number of elements, 0 means no limit, defaults to 0"
@param skip_empty : "True to skip empty elements"
*>
fn String[] String.tsplit(s, String delimiter, usz max = 0, bool skip_empty = false) => s.split(tmem, delimiter, max, skip_empty) @inline;
faultdef BUFFER_EXCEEDED;
<*
Split a string into parts, e.g "a|b|c" split with "|" yields { "a", "b", "c" }
@param [in] s
@param [in] delimiter
@param [inout] buffer
@param max : "Max number of elements, 0 means no limit, defaults to 0"
@require delimiter.len > 0 : "The delimiter must be at least 1 character long"
@ensure return.len > 0 || skip_empty
@return? BUFFER_EXCEEDED : `If there are more elements than would fit the buffer`
*>
fn String[]? String.split_to_buffer(s, String delimiter, String[] buffer, usz max = 0, bool skip_empty = false)
{
usz max_capacity = buffer.len;
usz i = 0;
bool no_more = false;
while (!no_more)
{
usz? index = i == max - 1 ? NOT_FOUND? : s.index_of(delimiter);
String res @noinit;
if (try index)
{
res = s[:index];
s = s[index + delimiter.len..];
}
else
{
res = s;
no_more = true;
}
if (!res.len && skip_empty)
{
continue;
}
if (i == max_capacity)
{
return BUFFER_EXCEEDED?;
}
buffer[i++] = res;
}
return buffer[:i];
}
<*
Check if a substring is found in the string.
@param [in] s
@param [in] substr : "The string to look for."
@pure
@return "true if the string contains the substring, false otherwise"
*>
fn bool String.contains(s, String substr)
{
return @ok(s.index_of(substr));
}
<*
Check if a character is found in the string.
@param [in] s
@param character : "The character to look for."
@pure
@return "true if the string contains the character, false otherwise"
*>
fn bool String.contains_char(s, char character)
{
return @ok(s.index_of_char(character));
}
<*
Check how many non-overlapping instances of a substring there is.
If the substring has zero length, the number of matches is zero.
@param [in] self : "The string to search"
@param [in] substr : "The string to look for."
@pure
@return "The number of times matched"
*>
fn usz String.count(self, String substr)
{
usz count = 0;
usz needed = substr.len;
if (needed == 0) return 0;
char first = substr[0];
while OUTER: (self.len >= needed)
{
foreach (i, c: self[..^needed])
{
if (c == first && self[i : needed] == substr)
{
count++;
self = self[i + needed..];
continue OUTER;
}
}
break;
}
return count;
}
<*
Find the index of the first incidence of a string.
@param [in] self
@param character : "The character to look for"
@pure
@ensure return < self.len
@return "the index of the character"
@return? NOT_FOUND : "if the character cannot be found"
*>
fn usz? String.index_of_char(self, char character)
{
foreach (i, c : self)
{
if (c == character) return i;
}
return NOT_FOUND?;
}
<*
Find the index of the first incidence of a one of the chars.
@param [in] self
@param [in] characters : "The characters to look for"
@pure
@ensure return < self.len
@return "the index of the character"
@return? NOT_FOUND : "if the character cannot be found"
*>
fn usz? String.index_of_chars(String self, char[] characters)
{
foreach (i, c : self)
{
foreach (j, pin : characters)
{
if (c == pin) return i;
}
}
return NOT_FOUND?;
}
<*
Find the index of the first incidence of a character.
@param [in] self
@param character : "The character to look for"
@param start_index : "The index to start with, may exceed max index."
@pure
@ensure return < self.len
@return "the index of the character"
@return? NOT_FOUND : "if the character cannot be found starting from the start_index"
*>
fn usz? String.index_of_char_from(self, char character, usz start_index)
{
usz len = self.len;
if (len <= start_index) return NOT_FOUND?;
for (usz i = start_index; i < len; i++)
{
if (self[i] == character) return i;
}
return NOT_FOUND?;
}
<*
Find the index of the first incidence of a character starting from the end.
@param [in] self
@param character : "the character to find"
@pure
@ensure return < self.len
@return "the index of the character"
@return? NOT_FOUND : "if the character cannot be found"
*>
fn usz? String.rindex_of_char(self, char character)
{
foreach_r (i, c : self)
{
if (c == character) return i;
}
return NOT_FOUND?;
}
<*
Find the index of the first incidence of a string.
@param [in] self
@param [in] substr
@pure
@ensure return < self.len
@require substr.len > 0 : "The string must be len 1 or more"
@return "the index of the substring"
@return? NOT_FOUND : "if the substring cannot be found"
*>
fn usz? String.index_of(self, String substr)
{
usz needed = substr.len;
if (needed > 0 && self.len >= needed)
{
char first = substr[0];
foreach (i, c: self[..^needed])
{
if (c == first && self[i : needed] == substr) return i;
}
}
return NOT_FOUND?;
}
<*
Find the index of the last incidence of a string.
@param [in] self
@param [in] substr
@pure
@ensure return < self.len
@require substr.len > 0 : "The substring must be len 1 or more"
@return "the index of the substring"
@return? NOT_FOUND : "if the substring cannot be found"
*>
fn usz? String.rindex_of(self, String substr)
{
usz needed = substr.len;
if (needed > 0 && self.len >= needed)
{
char first = substr[0];
foreach_r (i, c: self[..^needed])
{
if (c == first && self[i : needed] == substr) return i;
}
}
return NOT_FOUND?;
}
fn bool ZString.eq(self, ZString other) @operator(==)
{
char* a = self;
char* b = other;
if (a == b) return true;
if (!a || !b) return false;
for (;; a++, b++)
{
char c = *a;
if (c != *b) return false;
if (!c) return true;
}
}
fn String ZString.str_view(self)
{
return (String)(self[:self.len()]);
}
fn usz ZString.char_len(str)
{
usz len = 0;
char* ptr = (char*)str;
while (char c = ptr++[0])
{
if (c & 0xC0 != 0x80) len++;
}
return len;
}
fn usz ZString.len(self)
{
usz len;
for (char* ptr = (char*)self; *ptr; ptr++) len++;
return len;
}
fn usz WString.len(self)
{
usz len;
for (Char16* ptr = (Char16*)self; *ptr; ptr++) len++;
return len;
}
fn ZString String.zstr_copy(self, Allocator allocator)
{
usz len = self.len;
char* str = allocator::malloc(allocator, len + 1);
mem::copy(str, self.ptr, len);
str[len] = 0;
return (ZString)str;
}
fn String String.concat(self, Allocator allocator, String s2)
{
usz full_len = self.len + s2.len;
char* str = allocator::malloc(allocator, full_len + 1);
usz self_len = self.len;
mem::copy(str, self.ptr, self_len);
mem::copy(str + self_len, s2.ptr, s2.len);
str[full_len] = 0;
return (String)str[:full_len];
}
fn String String.tconcat(self, String s2) => self.concat(tmem, s2);
fn ZString String.zstr_tcopy(self) => self.zstr_copy(tmem) @inline;
<*
Copy this string, by duplicating the string, always adding a zero byte
sentinel, so that it safely can be converted to a ZString by a
cast.
*>
fn String String.copy(self, Allocator allocator)
{
usz len = self.len;
char* str = allocator::malloc(allocator, len + 1);
mem::copy(str, self.ptr, len);
str[len] = 0;
return (String)str[:len];
}
fn void String.free(&self, Allocator allocator)
{
if (!self.ptr) return;
allocator::free(allocator, self.ptr);
*self = "";
}
fn String String.tcopy(self) => self.copy(tmem) @inline;
fn String ZString.copy(self, Allocator allocator)
{
return self.str_view().copy(allocator) @inline;
}
fn String ZString.tcopy(self)
{
return self.str_view().copy(tmem) @inline;
}
<*
Convert an UTF-8 string to UTF-16
@return "The UTF-16 string as a slice, allocated using the given allocator"
@return? INVALID_UTF8 : "If the string contained an invalid UTF-8 sequence"
*>
fn Char16[]? String.to_utf16(self, Allocator allocator)
{
usz len16 = conv::utf16len_for_utf8(self);
Char16* data = allocator::alloc_array_try(allocator, Char16, len16 + 1)!;
conv::utf8to16_unsafe(self, data)!;
data[len16] = 0;
return data[:len16];
}
fn Char16[]? String.to_temp_utf16(self) => self.to_utf16(tmem);
fn WString? String.to_wstring(self, Allocator allocator)
{
return (WString)self.to_utf16(allocator).ptr;
}
fn WString? String.to_temp_wstring(self) => self.to_wstring(tmem);
fn Char32[]? String.to_utf32(self, Allocator allocator)
{
usz codepoints = conv::utf8_codepoints(self);
Char32* data = allocator::alloc_array_try(allocator, Char32, codepoints + 1)!;
conv::utf8to32_unsafe(self, data)!;
data[codepoints] = 0;
return data[:codepoints];
}
fn Char32[]? String.to_temp_utf32(self) => self.to_utf32(tmem);
<*
Convert a string to ASCII lower case in place.
@param [inout] self
@pure
*>
fn void String.convert_to_lower(self)
{
foreach (&c : self) if (c.is_upper() @pure) *c += 'a' - 'A';
}
fn String String.to_lower_copy(self, Allocator allocator)
{
String copy = self.copy(allocator);
copy.convert_to_lower();
return copy;
}
fn String String.to_lower_tcopy(self)
{
return self.to_lower_copy(tmem);
}
<*
Convert a string to ASCII upper case.
@param [inout] self
@pure
*>
fn void String.convert_to_upper(self)
{
foreach (&c : self) if (c.is_lower() @pure) *c -= 'a' - 'A';
}
<*
Returns a string converted to ASCII upper case.
@param [in] self
@param [inout] allocator
@return `a new String converted to ASCII upper case.`
*>
fn String String.to_upper_copy(self, Allocator allocator)
{
String copy = self.copy(allocator);
copy.convert_to_upper();
return copy;
}
fn String String.capitalize_copy(self, Allocator allocator)
{
String s = self.copy(allocator);
if (s.len > 0 && s[0].is_lower())
{
s[0] &= (char)~0x20;
}
return s;
}
<*
Convert a string from `snake_case` to PascalCase.
@param [in] self
@return `"FooBar" from "foo_bar" the resulting pointer may safely be cast to ZString.`
*>
fn String String.snake_to_pascal_copy(self, Allocator allocator)
{
Splitter splitter = self.tokenize("_");
char[] new_string = allocator::alloc_array(allocator, char, self.len + 1);
usz index = 0;
while (try s = splitter.next())
{
assert(s.len > 0);
char c = s[0];
if (c.is_lower()) c = c.to_upper();
new_string[index++] = c;
s = s[1..];
new_string[index:s.len] = s[..];
index += s.len;
}
new_string[index] = 0;
return (String)new_string[:index];
}
<*
Movifies the current string from `snake_case` to PascalCase.
@param [inout] self
*>
fn void String.convert_snake_to_pascal(&self)
{
Splitter splitter = self.tokenize("_");
String new_string = *self;
usz index = 0;
while (try s = splitter.next())
{
assert(s.len > 0);
char c = s[0];
if (c.is_lower()) c = c.to_upper();
new_string[index++] = c;
s = s[1..];
new_string[index:s.len] = s[..];
index += s.len;
}
*self = new_string[:index];
}
<*
Convert a string from `PascalCase` to `snake_case`.
@param [in] self
@return `"foo_bar" from "FooBar" the resulting pointer may safely be cast to ZString.`
*>
fn String String.pascal_to_snake_copy(self, Allocator allocator) => @pool()
{
DString d;
d.init(tmem, (usz)(self.len * 1.5));
usz index = 0;
foreach (i, c : self)
{
if (c.is_upper())
{
if (i > 0 && ((self[i - 1].is_lower() || self[i - 1].is_digit()) || (i < self.len - 1 && self[i + 1].is_lower())))
{
d.append_char('_');
}
d.append_char(c.to_lower());
continue;
}
d.append_char(c);
}
return d.copy_str(allocator);
}
fn StringIterator String.iterator(self)
{
return { self, 0 };
}
<*
@param [in] self
@return `a temporary String converted to ASCII upper case.`
*>
fn String String.to_upper_tcopy(self)
{
return self.to_upper_copy(tmem);
}
fn String? from_utf32(Allocator allocator, Char32[] utf32)
{
usz len = conv::utf8len_for_utf32(utf32);
char* data = allocator::malloc_try(allocator, len + 1)!;
defer catch allocator::free(allocator, data);
conv::utf32to8_unsafe(utf32, data);
data[len] = 0;
return (String)data[:len];
}
fn String? from_utf16(Allocator allocator, Char16[] utf16)
{
usz len = conv::utf8len_for_utf16(utf16);
char* data = allocator::malloc_try(allocator, len + 1)!;
defer catch allocator::free(allocator, data);
conv::utf16to8_unsafe(utf16, data)!;
data[len] = 0;
return (String)data[:len];
}
fn String? from_wstring(Allocator allocator, WString wstring)
{
usz utf16_len;
while (wstring[utf16_len] != 0) utf16_len++;
Char16[] utf16 = wstring[:utf16_len];
return from_utf16(allocator, utf16);
}
fn String? tfrom_wstring(WString wstring) => from_wstring(tmem, wstring) @inline;
fn String? tfrom_utf16(Char16[] utf16) => from_utf16(tmem, utf16) @inline;
fn usz String.utf8_codepoints(s)
{
usz len = 0;
foreach (char c : s)
{
if (c & 0xC0 != 0x80) len++;
}
return len;
}
<*
Determine whether the current string actually points to a ZString-like string.
This is done by looking at the byte one step after the end of the string. If this
is zero, it is considered zero terminated.
This function can safely be used with data pointing to null. However, it will not
work correctly if the pointer is invalid, for example it is already freed.
*>
fn bool String.is_zstr(self) @deprecated("Unsafe, use copy instead")
{
return self.ptr && *(self.ptr + self.len) == 0;
}
<*
Return a pointer to the string *iff* it is a pointer
to a zero terminated string, otherwise return a temp allocated zstring copy.
This function is suitable if you are converting strings to ZString on the temp
allocator, but suspect that the String might actually already point to zero
terminated data.
The function looks one step beyond the end of the slice to determine this,
which means that if that data is then modified after this call, this function
might behave incorrectly.
For this reason, try to ensure that the resulting ZString is immediately used.
@ensure return[self.len] == 0
*>
fn ZString String.quick_zstr(self) @deprecated("Unsafe, use zstr_tcopy instead")
{
return self.zstr_tcopy();
}
<*
Convert a number to a given base. If the base is not given, then
it will be inferred from the number if the string starts with 0x 0o or 0b and the
base is given as 10.
Furthermore it will skip any spaces before and after the number.
@param $Type : "The type to convert to"
@param base : "The base to convert to"
@require base > 0 && base <= 16 : "Unsupported base"
@return? MALFORMED_INTEGER : "When the value has some illegal character"
@return? INTEGER_OVERFLOW : "If the value does not fit in the given type"
@return? EMPTY_STRING : "If the string was empty"
@return? NEGATIVE_VALUE : "If the type was unsigned, and the value had a - prefix"
*>