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 #ifndef UCX_STRING_H
38 #define UCX_STRING_H
39
40 #include "common.h"
41 #include "allocator.h"
42
43
44
45
46 struct cx_mutstr_s {
47
48
49
50
51
52 char *ptr;
53
54 size_t length;
55 };
56
57
58
59
60 typedef struct cx_mutstr_s cxmutstr;
61
62
63
64
65 struct cx_string_s {
66
67
68
69
70
71 char const *ptr;
72
73 size_t length;
74 };
75
76
77
78
79 typedef struct cx_string_s cxstring;
80
81
82
83
84 struct cx_strtok_ctx_s {
85
86
87
88 cxstring str;
89
90
91
92 cxstring delim;
93
94
95
96 cxstring
const *delim_more;
97
98
99
100 size_t delim_more_count;
101
102
103
104 size_t pos;
105
106
107
108
109
110
111
112 size_t delim_pos;
113
114
115
116 size_t next_pos;
117
118
119
120 size_t found;
121
122
123
124 size_t limit;
125 };
126
127
128
129
130 typedef struct cx_strtok_ctx_s CxStrtokCtx;
131
132 #ifdef __cplusplus
133 extern "C" {
134
135
136
137
138
139
140 #define CX_STR(literal) cxstring{literal,
sizeof(literal) -
1}
141
142 #else
143
144
145
146
147
148
149
150
151 #define CX_STR(literal) (cxstring){literal,
sizeof(literal) -
1}
152
153 #endif
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 __attribute__((__warn_unused_result__, __nonnull__))
172 cxmutstr cx_mutstr(
char *cstring);
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 __attribute__((__warn_unused_result__))
191 cxmutstr cx_mutstrn(
192 char *cstring,
193 size_t length
194 );
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 __attribute__((__warn_unused_result__, __nonnull__))
212 cxstring cx_str(
char const *cstring);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 __attribute__((__warn_unused_result__))
232 cxstring cx_strn(
233 char const *cstring,
234 size_t length
235 );
236
237
238
239
240
241
242
243
244
245
246
247 __attribute__((__warn_unused_result__))
248 cxstring cx_strcast(cxmutstr str);
249
250
251
252
253
254
255
256
257
258
259
260
261 __attribute__((__nonnull__))
262 void cx_strfree(cxmutstr *str);
263
264
265
266
267
268
269
270
271
272
273
274
275
276 __attribute__((__nonnull__))
277 void cx_strfree_a(
278 CxAllocator
const *alloc,
279 cxmutstr *str
280 );
281
282
283
284
285
286
287
288
289
290
291
292 __attribute__((__warn_unused_result__))
293 size_t cx_strlen(
294 size_t count,
295 ...
296 );
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 __attribute__((__warn_unused_result__, __nonnull__))
317 cxmutstr cx_strcat_ma(
318 CxAllocator
const *alloc,
319 cxmutstr str,
320 size_t count,
321 ...
322 );
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338 #define cx_strcat_a(alloc, count, ...) \
339 cx_strcat_ma(alloc, cx_mutstrn(
NULL,
0), count,
__VA_ARGS__)
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 #define cx_strcat(count, ...) \
355 cx_strcat_ma(cxDefaultAllocator, cx_mutstrn(
NULL,
0), count,
__VA_ARGS__)
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374 #define cx_strcat_m(str, count, ...) \
375 cx_strcat_ma(cxDefaultAllocator, str, count,
__VA_ARGS__)
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392 __attribute__((__warn_unused_result__))
393 cxstring cx_strsubs(
394 cxstring string,
395 size_t start
396 );
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417 __attribute__((__warn_unused_result__))
418 cxstring cx_strsubsl(
419 cxstring string,
420 size_t start,
421 size_t length
422 );
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439 __attribute__((__warn_unused_result__))
440 cxmutstr cx_strsubs_m(
441 cxmutstr string,
442 size_t start
443 );
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464 __attribute__((__warn_unused_result__))
465 cxmutstr cx_strsubsl_m(
466 cxmutstr string,
467 size_t start,
468 size_t length
469 );
470
471
472
473
474
475
476
477
478
479
480
481
482
483 __attribute__((__warn_unused_result__))
484 cxstring cx_strchr(
485 cxstring string,
486 int chr
487 );
488
489
490
491
492
493
494
495
496
497
498
499
500
501 __attribute__((__warn_unused_result__))
502 cxmutstr cx_strchr_m(
503 cxmutstr string,
504 int chr
505 );
506
507
508
509
510
511
512
513
514
515
516
517
518
519 __attribute__((__warn_unused_result__))
520 cxstring cx_strrchr(
521 cxstring string,
522 int chr
523 );
524
525
526
527
528
529
530
531
532
533
534
535
536
537 __attribute__((__warn_unused_result__))
538 cxmutstr cx_strrchr_m(
539 cxmutstr string,
540 int chr
541 );
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559 __attribute__((__warn_unused_result__))
560 cxstring cx_strstr(
561 cxstring haystack,
562 cxstring needle
563 );
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581 __attribute__((__warn_unused_result__))
582 cxmutstr cx_strstr_m(
583 cxmutstr haystack,
584 cxstring needle
585 );
586
587
588
589
590
591
592
593
594
595
596
597
598
599 __attribute__((__warn_unused_result__, __nonnull__))
600 size_t cx_strsplit(
601 cxstring string,
602 cxstring delim,
603 size_t limit,
604 cxstring *output
605 );
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 __attribute__((__warn_unused_result__, __nonnull__))
627 size_t cx_strsplit_a(
628 CxAllocator
const *allocator,
629 cxstring string,
630 cxstring delim,
631 size_t limit,
632 cxstring **output
633 );
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648 __attribute__((__warn_unused_result__, __nonnull__))
649 size_t cx_strsplit_m(
650 cxmutstr string,
651 cxstring delim,
652 size_t limit,
653 cxmutstr *output
654 );
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675 __attribute__((__warn_unused_result__, __nonnull__))
676 size_t cx_strsplit_ma(
677 CxAllocator
const *allocator,
678 cxmutstr string,
679 cxstring delim,
680 size_t limit,
681 cxmutstr **output
682 );
683
684
685
686
687
688
689
690
691
692 __attribute__((__warn_unused_result__))
693 int cx_strcmp(
694 cxstring s1,
695 cxstring s2
696 );
697
698
699
700
701
702
703
704
705
706 __attribute__((__warn_unused_result__))
707 int cx_strcasecmp(
708 cxstring s1,
709 cxstring s2
710 );
711
712
713
714
715
716
717
718
719
720
721
722 __attribute__((__warn_unused_result__, __nonnull__))
723 int cx_strcmp_p(
724 void const *s1,
725 void const *s2
726 );
727
728
729
730
731
732
733
734
735
736
737
738 __attribute__((__warn_unused_result__, __nonnull__))
739 int cx_strcasecmp_p(
740 void const *s1,
741 void const *s2
742 );
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757 __attribute__((__warn_unused_result__, __nonnull__))
758 cxmutstr cx_strdup_a(
759 CxAllocator
const *allocator,
760 cxstring string
761 );
762
763
764
765
766
767
768
769
770
771
772
773
774
775 #define cx_strdup(string) cx_strdup_a(cxDefaultAllocator, string)
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790 #define cx_strdup_ma(allocator, string) cx_strdup_a(allocator, cx_strcast(string))
791
792
793
794
795
796
797
798
799
800
801
802
803
804 #define cx_strdup_m(string) cx_strdup_a(cxDefaultAllocator, cx_strcast(string))
805
806
807
808
809
810
811
812
813
814
815 __attribute__((__warn_unused_result__))
816 cxstring cx_strtrim(cxstring string);
817
818
819
820
821
822
823
824
825
826
827 __attribute__((__warn_unused_result__))
828 cxmutstr cx_strtrim_m(cxmutstr string);
829
830
831
832
833
834
835
836
837
838 __attribute__((__warn_unused_result__))
839 bool cx_strprefix(
840 cxstring string,
841 cxstring prefix
842 );
843
844
845
846
847
848
849
850
851
852 __attribute__((__warn_unused_result__))
853 bool cx_strsuffix(
854 cxstring string,
855 cxstring suffix
856 );
857
858
859
860
861
862
863
864
865
866 __attribute__((__warn_unused_result__))
867 bool cx_strcaseprefix(
868 cxstring string,
869 cxstring prefix
870 );
871
872
873
874
875
876
877
878
879
880 __attribute__((__warn_unused_result__))
881 bool cx_strcasesuffix(
882 cxstring string,
883 cxstring suffix
884 );
885
886
887
888
889
890
891
892
893
894 void cx_strlower(cxmutstr string);
895
896
897
898
899
900
901
902
903
904 void cx_strupper(cxmutstr string);
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925 __attribute__((__warn_unused_result__, __nonnull__))
926 cxmutstr cx_strreplacen_a(
927 CxAllocator
const *allocator,
928 cxstring str,
929 cxstring pattern,
930 cxstring replacement,
931 size_t replmax
932 );
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952 #define cx_strreplacen(str, pattern, replacement, replmax) \
953 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement, replmax)
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972 #define cx_strreplace_a(allocator, str, pattern, replacement) \
973 cx_strreplacen_a(allocator, str, pattern, replacement,
SIZE_MAX)
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992 #define cx_strreplace(str, pattern, replacement) \
993 cx_strreplacen_a(cxDefaultAllocator, str, pattern, replacement,
SIZE_MAX)
994
995
996
997
998
999
1000
1001
1002
1003 __attribute__((__warn_unused_result__))
1004 CxStrtokCtx cx_strtok(
1005 cxstring str,
1006 cxstring delim,
1007 size_t limit
1008 );
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 __attribute__((__warn_unused_result__))
1019 CxStrtokCtx cx_strtok_m(
1020 cxmutstr str,
1021 cxstring delim,
1022 size_t limit
1023 );
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035 __attribute__((__warn_unused_result__, __nonnull__))
1036 bool cx_strtok_next(
1037 CxStrtokCtx *ctx,
1038 cxstring *token
1039 );
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053 __attribute__((__warn_unused_result__, __nonnull__))
1054 bool cx_strtok_next_m(
1055 CxStrtokCtx *ctx,
1056 cxmutstr *token
1057 );
1058
1059
1060
1061
1062
1063
1064
1065
1066 __attribute__((__nonnull__))
1067 void cx_strtok_delim(
1068 CxStrtokCtx *ctx,
1069 cxstring
const *delim,
1070 size_t count
1071 );
1072
1073
1074 #ifdef __cplusplus
1075 }
1076 #endif
1077
1078 #endif
1079