-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcstring.h
1726 lines (1616 loc) · 107 KB
/
cstring.h
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
#ifndef HEADER_CSTRING_8C60CBA8_34A6_4EA3_94B1_A69444BA0B9C_1_3
#define HEADER_CSTRING_8C60CBA8_34A6_4EA3_94B1_A69444BA0B9C_1_3
/**
* @copyright Copyright (c) 2025 Steffen Illhardt,
* Licensed under the MIT license
* ( https://opensource.org/license/mit/ ).
* @brief cstring - Heap implemented using C library allocation functions.
* @file cstring.h
* @version 1.3
* @details
* The c-string library is based on the c-vector library,
* Copyright (c) 2015 Evan Teran,
* refer to: https://github.com/eteran/c-vector
*/
/* ======================== */
/* === PUBLIC INTERFACE === */
/* ======================== */
/**
* @defgroup cstring_api The cstring API
* @{
*/
/* ------------- */
/* --- types --- */
/**
* @brief cstring_string_type - The string type used in this library.
* @param type - The type of string to act on.
*/
#define cstring_string_type(type) type *
/**
* @brief cstring - Syntactic sugar to retrieve a string type.
* @param type - The type of string to act on.
*/
#define cstring(type) cstring_string_type(type)
/**
* @brief cstring_iterator - The iterator type used for a string.
* @param type - The type of iterator to act on.
*/
#define cstring_iterator(type) cstring_string_type(type)
/* ----------------------- */
/* --- cstring literal --- */
/**
* @brief cstring_literal - Generate a cstring object with static duration.
* @note The pointer references static read-only data which is constant at
* compile time. DO NOT FREE IT.
* @param name - A not yet used variable name for the cstring object.
* @param type - The type of string to act on.
* @param lit - A string literal used to create the cstring literal. The
* argument passed to this parameter cannot be a pointer!
*/
#define cstring_literal(name, type, lit) \
cstring_string_type(const type) name; \
do { \
static const struct _cstring_literal_tag_##name { \
const size_t size; \
const size_t capacity; \
void (*const unused)(void *); \
const type data[((sizeof(lit) + sizeof(size_t) - 1) / sizeof(size_t)) * sizeof(size_t) / sizeof(type)]; \
} _cstring_literal_container_##name = {(sizeof(lit) / sizeof(type)), (sizeof(lit) / sizeof(type)), NULL, lit}; \
name = &*_cstring_literal_container_##name.data; \
} while (0)
/* --------------------------------- */
/* --- construction, destruction --- */
/**
* @brief cstring_init - Allocate a new cstring with zero length.
* @details Also see `cstring_assign()`, `cstring_reserve()`,
* `cstring_push_back()`, `cstring_append()`, `cstring_resize()`.
* @param name - A not yet used variable name for the cstring variable to be
* declared and initialized.
* @param type - The type of string to act on.
* @return void
*/
#define cstring_init(name, type) \
cstring_string_type(type) name = NULL; \
do { \
cstring_grow_(name, ((64 - sizeof(cstring_metadata_t)) / sizeof(type))); \
(name)[0] = 0; \
cstring_set_ttl_siz_(name, 1); \
} while (0)
/**
* @brief cstring_assign - Assign a string to a cstring.
* @details Also see `cstring_init()`, `cstring_reserve()`,
* `cstring_push_back()`, `cstring_append()`, `cstring_resize()`.
* @param str - The cstring. Can be a NULL string. <br>
* If `str` refers to an existing cstring, the old content is
* overwritten.
* @param ptr - Pointer to the first character assigned to the cstring.
* @param count - Number of consecutive characters to be used.
* @return void
*/
#define cstring_assign(str, ptr, count) \
do { \
const void *const tmp_ptr__ = (const void *)(ptr); \
if ((str) && cstring_ttl_cap_(str) < (size_t)(count) + 1) { \
cstring_free(str); \
} \
if (!(str)) { \
cstring_grow_((str), (size_t)(count) + 1); \
} \
if (tmp_ptr__ && (count)) { \
cstring_clib_memcpy((str), (ptr), (size_t)(count) * sizeof(*(ptr))); \
cstring_set_ttl_siz_((str), (size_t)(count) + 1); \
(str)[(size_t)(count)] = 0; \
} else { \
cstring_set_ttl_siz_((str), 1); \
(str)[0] = 0; \
} \
} while (0)
/**
* @brief cstring_free - Free all memory associated with the cstring and set it
* to NULL.
* @param str - The cstring. Can be a NULL string.
* @return void
*/
#define cstring_free(str) \
do { \
if (str) { \
cstring_clib_free(cstring_string_to_base_(str)); \
(str) = NULL; \
} \
} while (0)
/* ---------------------- */
/* --- element access --- */
/**
* @brief cstring_at - Return the character at position `pos` in the cstring.
* @param str - The cstring.
* @param pos - Position of a character in the string.
* @return The ASCII value (as `int`) of the character at the specified position
* in the string. If the macro fails, -1 is returned.
*/
#define cstring_at(str, pos) \
((size_t)(pos) < cstring_size(str) ? (int)((size_t)(str)[(size_t)(pos)] & get_typemask_(str)) : -1)
/**
* @brief cstring_front - Return the first character in the cstring.
* @param str - The cstring.
* @return The ASCII value (as `int`) of the first character in the string. If
* the macro fails, -1 is returned.
*/
#define cstring_front(str) \
(cstring_size(str) ? (int)((size_t)(str)[0] & get_typemask_(str)) : -1)
/**
* @brief cstring_back - Return the last character in the cstring.
* @param str - The cstring.
* @return The ASCII value (as `int`) of the last character in the string. If
* the macro fails, -1 is returned.
*/
#define cstring_back(str) \
(cstring_size(str) ? (int)((size_t)(str)[cstring_size(str) - 1] & get_typemask_(str)) : -1)
/* ----------------- */
/* --- iterators --- */
/**
* @brief cstring_begin - Return an iterator to first character of the string.
* @param str - The cstring.
* @return A pointer to the first character (or NULL).
*/
#define cstring_begin(str) \
(str)
/**
* @brief cstring_end - Return an iterator to one past the last character of the
* string.
* @param str - The cstring.
* @return A pointer to one past the last character (or NULL).
*/
#define cstring_end(str) \
((str) ? (str) + cstring_size(str) : NULL)
/* ---------------- */
/* --- capacity --- */
/**
* @brief cstring_empty - Return 1 if the string is empty.
* @param str - The cstring. Can be a NULL string.
* @return 1 if `str` is NULL or empty, 0 if non-empty.
*/
#define cstring_empty(str) \
(cstring_size(str) == 0)
/**
* @brief cstring_size - Get the current length of the string.
* @param str - The cstring. Can be a NULL string.
* @return The length as a `size_t`, terminating null not counted. Zero if `str`
* is NULL.
*/
#define cstring_size(str) \
(cstring_ttl_siz_(str) ? cstring_ttl_siz_(str) - 1 : (size_t)0)
/**
* @brief cstring_length - Get the current length of the string.
* @param str - The cstring. Can be a NULL string.
* @return The length as a `size_t`, terminating null not counted. Zero if `str`
* is NULL.
*/
#define cstring_length(str) \
cstring_size(str)
/**
* @brief cstring_max_size - Get the maximum number of elements a string of the
* specified character type is able to hold.
* @details
* For clarity, this is like: <br>
* `((min(PTRDIFF_MAX, (SIZE_MAX / 2)) - sizeof(metadata)) / sizeof(type) - 1)`,
* with -1 for the string terminator that is not counted. Also, (SIZE_MAX / 2)
* because any `array + SIZE_MAX` would be bogus. <br>
* PTRDIFF_MAX and SIZE_MAX may not be defined in ancient C libraries. Hence the
* calculation in the macro. However, the value of the macro is a constant
* expression. It is supposed to be calculated at compile time.
* @note The resulting value is technically possible. However, typically
* allocations of such a big size will fail.
* @param type - The type of string to act on.
* @return The maximum number of elements the string is able to hold.
*/
#define cstring_max_size(type) \
(((((size_t)1) << ((sizeof(ptrdiff_t) < sizeof(size_t) ? sizeof(ptrdiff_t) : sizeof(size_t)) * CHAR_BIT - 1)) - 1 - sizeof(cstring_metadata_t)) / sizeof(type) - 1)
/**
* @brief cstring_reserve - Request that the string capacity be at least enough
* to contain `n` characters.
* @details If `n` is greater than the current string capacity, the function
* causes the container to reallocate its storage increasing its
* capacity to `n` (or greater). <br>
* Also see `cstring_init()`, `cstring_assign()`,
* `cstring_push_back()`, `cstring_append()`, `cstring_resize()`.
* @param str - The cstring. Can be a NULL string.
* @param n - Minimum capacity for the string.
* @return void
*/
#define cstring_reserve(str, n) \
do { \
const int is_new__ = ((str) == NULL); \
if (is_new__ || cstring_ttl_cap_(str) < (size_t)(n) + 1) { \
cstring_grow_((str), (size_t)(n) + 1); \
} \
if (is_new__) { \
cstring_set_ttl_siz_((str), 1); \
(str)[0] = 0; \
} \
} while (0)
/**
* @brief cstring_capacity - Get the current capacity of the string.
* @param str - The cstring. Can be a NULL string.
* @return The capacity as a `size_t`. Zero if `str` is NULL.
*/
#define cstring_capacity(str) \
(cstring_ttl_cap_(str) ? cstring_ttl_cap_(str) - 1 : (size_t)0)
/**
* @brief cstring_shrink_to_fit - Request the container to reduce its capacity
* to fit its size.
* @param str - The cstring.
* @return void
*/
#define cstring_shrink_to_fit(str) \
do { \
if (str) { \
const size_t cs_sz___ = cstring_ttl_siz_(str); \
cstring_grow_(str, cs_sz___); \
} \
} while (0)
/**
* @brief cstring_unsafe_set_size - Set the size property to the specified
* value and add the string terminator accordingly.
* @details Providing a cstring with sufficiently large capacity as buffer to
* external API is supported. However, the third party API cannot
* update the header data of a cstring. This function enables the user
* to manually update the size property in order to keep it usable in
* the cstring API.
* @note This function does not examine the string data to evaluate the
* credibility of the specified size. Furthermore, this function does not
* force the capacity of the cstring to grow. If the required size exceeds
* the current capacity, the function sets the size to meet the capacity.
* Consider the memory being corrupted by the API that updated the string
* data in this case.
* @param str - The cstring.
* @param size - The size to be applied.
* @return void
*/
#define cstring_unsafe_set_size(str, size) \
do { \
if (str) { \
const size_t cs_new_sz__ = cstring_capacity(str) < (size_t)(size) ? cstring_capacity(str) : (size_t)(size); \
cstring_set_ttl_siz_((str), cs_new_sz__ + 1); \
(str)[cs_new_sz__] = 0; \
} \
} while (0)
/* ----------------- */
/* --- modifiers --- */
/**
* @brief cstring_clear - Erase all of the characters in the string.
* @param str - The cstring.
* @return void
*/
#define cstring_clear(str) \
do { \
if (str) { \
cstring_set_ttl_siz_((str), 1); \
(str)[0] = 0; \
} \
} while (0)
/**
* @brief cstring_insert - Insert a string at position `pos` into the cstring.
* @param str - The cstring.
* @param pos - Position in the string where the new characters are inserted.
* @param ptr - Pointer to the first character inserted into the cstring.
* @param count - Number of consecutive characters to be used.
* @return void
*/
#define cstring_insert(str, pos, ptr, count) \
do { \
const size_t old_ttl_sz__ = cstring_ttl_siz_(str); \
if ((size_t)(pos) + 1 <= old_ttl_sz__) { \
const size_t new_ttl_sz__ = old_ttl_sz__ + (size_t)(count); \
if (cstring_ttl_cap_(str) < new_ttl_sz__) { \
cstring_grow_((str), new_ttl_sz__); \
} \
if ((size_t)(pos) < old_ttl_sz__ - 1) { \
cstring_clib_memmove((str) + (size_t)(pos) + (size_t)(count), (str) + (size_t)(pos), sizeof(*(str)) * (old_ttl_sz__ - 1 - (size_t)(pos))); \
} \
cstring_clib_memcpy((str) + (size_t)(pos), (ptr), (size_t)(count) * sizeof(*(ptr))); \
cstring_set_ttl_siz_((str), new_ttl_sz__); \
(str)[new_ttl_sz__ - 1] = 0; \
} \
} while (0)
/**
* @brief cstring_erase - Remove the characters beginning at offset `pos` from
* the cstring.
* @param str - The cstring.
* @param pos - Offset of the first character erased from the cstring.
* @param n - Number of consecutive characters to be erased.
* @return void
*/
#define cstring_erase(str, pos, n) \
do { \
if ((size_t)(pos) + 1 <= cstring_ttl_siz_(str)) { \
const size_t cs_count__ = (size_t)(pos) + (size_t)(n) >= cstring_size(str) ? cstring_size(str) - (size_t)(pos) : (size_t)(n); \
cstring_set_ttl_siz_((str), cstring_ttl_siz_(str) - cs_count__); \
cstring_clib_memmove((str) + (size_t)(pos), (str) + (size_t)(pos) + cs_count__, sizeof(*(str)) * (cstring_ttl_siz_(str) - (size_t)(pos))); \
} \
} while (0)
/**
* @brief cstring_push_back - Add a character to the end of the string.
* @details Also see `cstring_init()`, `cstring_assign()`, `cstring_reserve()`,
* `cstring_append()`, `cstring_resize()`.
* @param str - The cstring. Can be a NULL string.
* @param value - The character to add.
* @return void
*/
#define cstring_push_back(str, value) \
do { \
size_t new_ttl_siz___; \
if (!(str)) { \
cstring_grow_((str), 2); \
cstring_set_ttl_siz_((str), 1); \
} \
new_ttl_siz___ = cstring_ttl_siz_(str) + 1; \
if (cstring_ttl_cap_(str) < new_ttl_siz___) { \
cstring_grow_((str), new_ttl_siz___); \
} \
cstring_set_ttl_siz_((str), new_ttl_siz___); \
(str)[new_ttl_siz___ - 2] = (value); \
(str)[new_ttl_siz___ - 1] = 0; \
} while (0)
/**
* @brief cstring_pop_back - Remove the last character from the cstring.
* @param str - The cstring.
* @return void
*/
#define cstring_pop_back(str) \
do { \
const size_t s_sz__ = cstring_size(str); \
if (s_sz__) { \
cstring_set_ttl_siz_((str), s_sz__); \
(str)[s_sz__ - 1] = 0; \
} \
} while (0)
/**
* @brief cstring_append - Append a string at the end of the cstring.
* @details Also see `cstring_init()`, `cstring_assign()`, `cstring_reserve()`,
* `cstring_push_back()`, `cstring_resize()`.
* @param str - The cstring. Can be a NULL string.
* @param ptr - Pointer to the first character appended to the cstring.
* @param count - Number of consecutive characters to be used.
* @return void
*/
#define cstring_append(str, ptr, count) \
do { \
if (str) { \
cstring_insert((str), cstring_size(str), (ptr), (size_t)(count)); \
} else { \
cstring_assign((str), (ptr), (size_t)(count)); \
} \
} while (0)
/**
* @brief cstring_replace - Replace a substring beginning at position `pos` with
* another string.
* @param str - The cstring.
* @param pos - Offset of the first character replaced in the cstring.
* @param n - Number of consecutive characters to be replaced.
* @param ptr - Pointer to the first replacement character.
* @param count - Number of consecutive replacement characters to be used.
* @return void
*/
#define cstring_replace(str, pos, n, ptr, count) \
do { \
if ((ptrdiff_t)(count) >= 0 && (size_t)(pos) + 1 <= cstring_ttl_siz_(str)) { \
const size_t s_siz__ = cstring_size(str); \
const size_t repl_n__ = (size_t)(pos) + (size_t)(n) >= s_siz__ ? s_siz__ - (size_t)(pos) : (size_t)(n); \
const ptrdiff_t repl_diff__ = (ptrdiff_t)(count) - (ptrdiff_t)repl_n__; \
const size_t new_ttl_siz__ = (size_t)((ptrdiff_t)s_siz__ + repl_diff__) + 1; \
if (new_ttl_siz__ > cstring_ttl_cap_(str)) { \
cstring_grow_((str), new_ttl_siz__); \
} \
if (repl_diff__) { \
cstring_clib_memmove((str) + (size_t)(pos) + (count), (str) + (size_t)(pos) + repl_n__, (s_siz__ - repl_n__ - (size_t)(pos) + 1) * sizeof(*(str))); \
} \
cstring_clib_memcpy((str) + (size_t)(pos), (ptr), (size_t)(count) * sizeof(*(ptr))); \
cstring_set_ttl_siz_((str), new_ttl_siz__); \
(str)[new_ttl_siz__ - 1] = 0; \
} \
} while (0)
/**
* @brief cstring_copy - Copy a cstring.
* @param from - The original cstring.
* @param to - Destination to which the cstring is copied. Can be a NULL
* string. <br>
* If `to` refers to an existing cstring, the old content is
* overwritten.
* @return void
*/
#define cstring_copy(from, to) \
do { \
if (from) { \
const size_t from_ttl_sz__ = cstring_ttl_siz_(from); \
if ((to) && cstring_ttl_cap_(to) < from_ttl_sz__) { \
cstring_free(to); \
} \
if (!(to)) { \
cstring_grow_((to), from_ttl_sz__); \
} \
cstring_clib_memcpy((to), (from), from_ttl_sz__ * sizeof(*(from))); \
cstring_set_ttl_siz_((to), from_ttl_sz__); \
} else { \
cstring_clear(to); \
} \
} while (0)
/**
* @brief cstring_resize - Resize the container to contain `count` characters.
* @details Also see `cstring_init()`, `cstring_assign()`, `cstring_reserve()`,
* `cstring_push_back()`, `cstring_append()`.
* @param str - The cstring. Can be a NULL string.
* @param count - New size of the cstring.
* @param value - The value to initialize new characters with.
* @return void
*/
#define cstring_resize(str, count, value) \
do { \
const size_t cs_sz_count__ = (size_t)(count) + 1; \
size_t cs_sz__ = cstring_size(str); \
if (cs_sz_count__ > cs_sz__ + 1) { \
cstring_grow_((str), cs_sz_count__); \
do { \
(str)[cs_sz__++] = (value); \
} while (cs_sz__ < cs_sz_count__); \
} \
cstring_set_ttl_siz_((str), cs_sz_count__); \
(str)[(size_t)(count)] = 0; \
} while (0)
/**
* @brief cstring_swap - Exchange the content of the cstring by the content of
* another cstring of the same type.
* @param str - The original cstring. Can be a NULL string.
* @param other - The other cstring to swap content with. Can be a NULL string.
* @return void
*/
#define cstring_swap(str, other) \
do { \
void *cs_swap__ = (void *)str; \
str = other; \
other = cs_swap__; \
} while (0)
/**
* @brief cstring_trim - Remove contiguous occurrences of the specified
* character from the begin and/or the end of a cstring.
* @param str - The cstring.
* @param value - The character to be removed.
* @param mode - Flags specifying where the characters are removed. <br>
* 1 to remove leading characters <br>
* 2 to remove trailing characters <br>
* Their combination (1|2) results in trimming on both sides of
* the string.
* @return void
*/
#define cstring_trim(str, value, mode) \
do { \
size_t cs_end_i__ = cstring_size(str); \
if (cs_end_i__) { \
size_t cs_new_len__; \
const int mode_head__ = (int)((mode) & 1); \
const int mode_tail__ = (int)((mode) & 2); \
size_t cs_beg_i__ = 0; \
if (mode_tail__) { \
while (cs_end_i__ > cs_beg_i__) { \
if ((str)[--cs_end_i__] != (value)) { \
++cs_end_i__; \
break; \
} \
} \
} \
if (mode_head__) { \
while (cs_beg_i__ < cs_end_i__) { \
if ((str)[cs_beg_i__] != (value)) { \
break; \
} \
++cs_beg_i__; \
} \
} \
cs_new_len__ = cs_end_i__ - cs_beg_i__; \
if (cs_new_len__ && cs_beg_i__) { \
cstring_clib_memmove((str), (str) + cs_beg_i__, sizeof(*(str)) * cs_new_len__); \
} \
cstring_set_ttl_siz_((str), cs_new_len__ + 1); \
(str)[cs_new_len__] = 0; \
} \
} while (0)
/**
* @brief cstring_fix - Update the cstring to a fixed length by either padding
* or shortening.
* @param str - The cstring.
* @param length - New length of the cstring.
* @param value - Character used for the padding.
* @param mode - Flags specifying where the cstring is to be padded or
* shortened. <br>
* 1 at the begin of the cstring <br>
* 2 at the end of the cstring <br>
* Their combination (1|2) leads to a centered alignment.
* @return void
*/
#define cstring_fix(str, length, value, mode) \
do { \
if ((str) && (ptrdiff_t)(length) >= 0) { \
ptrdiff_t cs_diff__ = (ptrdiff_t)((size_t)(length) - (cstring_size(str))); \
if (cs_diff__) { \
const int mode_head___ = (int)((mode) & 1); \
const int mode_tail___ = (int)((mode) & 2); \
const ptrdiff_t head_len__ = mode_head___ && mode_tail___ ? cs_diff__ / 2 : (mode_head___ ? cs_diff__ : (ptrdiff_t)0); \
if (cs_diff__ < 0) { \
cstring_clib_memmove((str), (str) - (head_len__), (size_t)(length) * sizeof(*(str))); \
} else { \
if ((size_t)(length) + 1 > cstring_ttl_cap_(str)) { \
cstring_grow_((str), (size_t)(length) + 1); \
} \
if (mode_head___ && head_len__) { \
ptrdiff_t n__ = 0; \
cstring_clib_memmove((str) + head_len__, (str), cstring_size(str) * sizeof(*(str))); \
while (n__ < head_len__) { \
(str)[n__++] = (value); \
} \
} \
if (mode_tail___) { \
ptrdiff_t n__ = head_len__ + (ptrdiff_t)cstring_size(str); \
const ptrdiff_t e__ = (ptrdiff_t)(length); \
while (n__ < e__) { \
(str)[n__++] = (value); \
} \
} \
} \
cstring_set_ttl_siz_((str), (size_t)(length) + 1); \
(str)[(size_t)(length)] = L'\0'; \
} \
} \
} while (0)
/**
* @brief cstring_reverse - Reverse the character order in the cstring.
* @param str - The cstring.
* @return void
*/
#define cstring_reverse(str) \
do { \
const size_t cs_tmp_i__ = cstring_size(str); \
if (cs_tmp_i__ > 1) { \
size_t cs_end_i___ = cs_tmp_i__; \
size_t cs_beg_i___ = 0; \
while (cs_end_i___ > cs_beg_i___) { \
(str)[cs_tmp_i__] = (str)[--cs_end_i___]; \
(str)[cs_end_i___] = (str)[cs_beg_i___]; \
(str)[cs_beg_i___++] = (str)[cs_tmp_i__]; \
} \
(str)[cs_tmp_i__] = 0; \
} \
} while (0)
/* -------------- */
/* --- search --- */
/**
* @brief cstring_find - Find the first occurrence of the given substring.
* @details Implements the Rabin-Karp algorithm.
* @param str - The cstring.
* @param pos - Position at which to start the search, i.e. the found
* substring must not begin in a position preceding `pos`.
* Zero means that the whole `str` is searched.
* @param ptr - Pointer to the first character of the string to search
* for.
* @param count - Length of the string to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first character of the found substring or -1 if no
* such substring is found.
* @return void
*/
#define cstring_find(str, pos, ptr, count, ret_offset) \
do { \
const void *const tmp_ptr___ = (const void *)(ptr); \
const ptrdiff_t sub_size__ = (ptrdiff_t)(count), str_size__ = (ptrdiff_t)cstring_size(str) - (ptrdiff_t)(pos); \
if (!tmp_ptr___ || (ptrdiff_t)(pos) < 0 || sub_size__ > str_size__ || str_size__ <= 0 || sub_size__ <= 0) { \
(ret_offset) = (ptrdiff_t)-1; \
} else if (sub_size__ == str_size__) { \
int eq__; \
str_n_eq_((str) + (ptrdiff_t)(pos), (ptr), sub_size__, eq__); \
(ret_offset) = eq__ ? (ptrdiff_t)(pos) : (ptrdiff_t)-1; \
} else if (sub_size__ == 1) { \
find_first_char_(0, (str), (pos), (ptr), (ret_offset)); \
} else { \
ptrdiff_t cs_off__; \
const ptrdiff_t end__ = sub_size__ - 1, cs_diff__ = (ptrdiff_t)cstring_size(str) - sub_size__; \
size_t str_hash__ = 0, sub_hash__ = 0, hash_factor__ = 1; \
const size_t mask__ = get_typemask_(str); \
(ret_offset) = (ptrdiff_t)-1; \
for (cs_off__ = 0; cs_off__ < end__; ++cs_off__) { \
str_hash__ = (str_hash__ << 1) + ((size_t)(str)[cs_off__ + (ptrdiff_t)(pos)] & mask__); \
sub_hash__ = (sub_hash__ << 1) + ((size_t)(ptr)[cs_off__] & mask__); \
hash_factor__ <<= 1; \
} \
str_hash__ = (str_hash__ << 1) + ((size_t)(str)[cs_off__ + (ptrdiff_t)(pos)] & mask__); \
sub_hash__ = (sub_hash__ << 1) + ((size_t)(ptr)[cs_off__] & mask__); \
cs_off__ = (ptrdiff_t)(pos); \
do { \
if (sub_hash__ == str_hash__) { \
int eq__; \
str_n_eq_((str) + cs_off__, (ptr), sub_size__, eq__); \
if (eq__) { \
(ret_offset) = cs_off__; \
break; \
} \
} \
str_hash__ = \
((str_hash__ - hash_factor__ * ((size_t)(str)[cs_off__] & mask__)) << 1) + \
((size_t)(str)[cs_off__ + sub_size__] & mask__); \
} while (cs_off__++ < cs_diff__); \
} \
} while (0)
/**
* @brief cstring_rfind - Find the last occurrence of the given substring.
* @details Implements the Rabin-Karp algorithm.
* @param str - The cstring.
* @param pos - Position at which to start the search, proceeded from
* right to left. Hence the found substring cannot begin in
* a position following `pos`. -1 means that the whole `str`
* is searched.
* @param ptr - Pointer to the first character of the string to search
* for.
* @param count - Length of the string to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first character of the found substring or -1 if no
* such substring is found.
* @return void
*/
#define cstring_rfind(str, pos, ptr, count, ret_offset) \
do { \
const void *const tmp_ptr____ = (const void *)(ptr); \
const ptrdiff_t sub_size___ = (ptrdiff_t)(count); \
const ptrdiff_t str_size___ = ((ptrdiff_t)(pos) == -1 || (ptrdiff_t)(pos) + sub_size___ > (ptrdiff_t)cstring_size(str)) \
? (ptrdiff_t)cstring_size(str) \
: ((ptrdiff_t)(pos) + sub_size___); \
if (!tmp_ptr____ || (ptrdiff_t)(pos) < -1 || sub_size___ > str_size___ || !str_size___ || sub_size___ <= 0) { \
(ret_offset) = (ptrdiff_t)-1; \
} else if (sub_size___ == str_size___) { \
int eq___; \
str_n_eq_((str), (ptr), sub_size___, eq___); \
(ret_offset) = eq___ ? (ptrdiff_t)0 : (ptrdiff_t)-1; \
} else if (sub_size___ == 1) { \
find_last_char_(0, (str), (pos), (ptr), (ret_offset)); \
} else { \
ptrdiff_t cs_off___; \
const ptrdiff_t cs_diff___ = str_size___ - sub_size___; \
size_t str_hash___ = 0, sub_hash___ = 0, hash_factor___ = 1; \
const size_t mask___ = get_typemask_(str); \
(ret_offset) = (ptrdiff_t)-1; \
for (cs_off___ = sub_size___ - 1; cs_off___; --cs_off___) { \
str_hash___ = (str_hash___ << 1) + ((size_t)(str)[cs_off___ + cs_diff___] & mask___); \
sub_hash___ = (sub_hash___ << 1) + ((size_t)(ptr)[cs_off___] & mask___); \
hash_factor___ <<= 1; \
} \
str_hash___ = (str_hash___ << 1) + ((size_t)(str)[cs_off___ + cs_diff___] & mask___); \
sub_hash___ = (sub_hash___ << 1) + ((size_t)(ptr)[cs_off___] & mask___); \
cs_off___ = cs_diff___; \
do { \
if (sub_hash___ == str_hash___) { \
int eq___; \
str_n_eq_((str) + cs_off___, (ptr), sub_size___, eq___); \
if (eq___) { \
(ret_offset) = cs_off___; \
break; \
} \
} \
--cs_off___; \
str_hash___ = \
((str_hash___ - hash_factor___ * ((size_t)(str)[cs_off___ + sub_size___] & mask___)) << 1) + \
((size_t)(str)[cs_off___] & mask___); \
} while (cs_off___); \
} \
} while (0)
/**
* @brief cstring_find_first_of - Find the first character equal to one of the
* characters in the given character sequence.
* @param str - The cstring.
* @param pos - Position at which to begin searching.
* @param ptr - Pointer to the first character of the string identifying
* characters to search for.
* @param count - Length of the string of characters to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first found character or -1 if no such character
* is found.
* @return void
*/
#define cstring_find_first_of(str, pos, ptr, count, ret_offset) \
find_first_of_(0, str, pos, ptr, count, ret_offset)
/**
* @brief cstring_find_first_not_of - Find the first character equal to none of
* the characters in the given character sequence.
* @param str - The cstring.
* @param pos - Position at which to begin searching.
* @param ptr - Pointer to the first character of the string identifying
* characters to search for.
* @param count - Length of the string of characters to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first found character or -1 if no such character
* is found.
* @return void
*/
#define cstring_find_first_not_of(str, pos, ptr, count, ret_offset) \
find_first_of_(1, str, pos, ptr, count, ret_offset)
/**
* @brief cstring_find_last_of - Find the last character equal to one of the
* characters in the given character sequence.
* @param str - The cstring.
* @param pos - Position at which to begin searching. -1 means that the
* whole `str` is searched.
* @param ptr - Pointer to the first character of the string identifying
* characters to search for.
* @param count - Length of the string of characters to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first found character or -1 if no such character
* is found.
* @return void
*/
#define cstring_find_last_of(str, pos, ptr, count, ret_offset) \
find_last_of_(0, str, pos, ptr, count, ret_offset)
/**
* @brief cstring_find_last_not_of - Find the last character equal to none of
* the characters in the given character sequence.
* @param str - The cstring.
* @param pos - Position at which to begin searching. -1 means that the
* whole `str` is searched.
* @param ptr - Pointer to the first character of the string identifying
* characters to search for.
* @param count - Length of the string of characters to search for.
* @param ret_offset - Variable of type `ptrdiff_t` that receives the position
* of the first found character or -1 if no such character
* is found.
* @return void
*/
#define cstring_find_last_not_of(str, pos, ptr, count, ret_offset) \
find_last_of_(1, str, pos, ptr, count, ret_offset)
/* ------------------ */
/* --- operations --- */
/**
* @brief cstring_compare - Lexicographically compare two strings.
* @param str1 - The first cstring.
* @param str2 - The second cstring.
* @param ret_order - Variable of type `int` that receives the result of the
* comparison. <br>
* -1 if `str1` appears before `str2` <br>
* 0 if both strings compare equivalent <br>
* 1 if `str1` appears after `str2`
* @return void
*/
#define cstring_compare(str1, str2, ret_order) \
do { \
if ((str1) && (str2)) { \
const size_t min__ = cstring_size(str1) < cstring_size(str2) ? cstring_size(str1) : cstring_size(str2); \
size_t idx__ = 0; \
while (idx__ < min__ && (str1)[idx__] == (str2)[idx__]) { \
++idx__; \
} \
if ((str1)[idx__] == (str2)[idx__]) { \
(ret_order) = 0; \
} else { \
const size_t tp_mask__ = get_typemask_(str1); \
(ret_order) = ((size_t)(str1)[idx__] & tp_mask__) < ((size_t)(str2)[idx__] & tp_mask__) ? -1 : 1; \
} \
} \
} while (0)
/**
* @brief cstring_starts_with - Check if the string begins with the given prefix.
* @param str - The cstring.
* @param ptr - Pointer to the first character of the prefix.
* @param count - Length of the prefix.
* @param ret_found - Variable of type `int` that receives the result of the
* check. <br>
* 0 if the prefix was not found <br>
* 1 if the prefix was found
* @return void
*/
#define cstring_starts_with(str, ptr, count, ret_found) \
do { \
const void *const tmp_p__ = (const void *)(ptr); \
const ptrdiff_t sub_sz__ = (ptrdiff_t)(count); \
(ret_found) = 0; \
if (tmp_p__ && sub_sz__ > 0 && sub_sz__ <= (ptrdiff_t)cstring_size(str)) { \
str_n_eq_((str), (ptr), sub_sz__, (ret_found)); \
} \
} while (0)
/**
* @brief cstring_ends_with - Check if the string ends with the given suffix.
* @param str - The cstring.
* @param ptr - Pointer to the first character of the suffix.
* @param count - Length of the suffix.
* @param ret_found - Variable of type `int` that receives the result of the
* check. <br>
* 0 if the suffix was not found <br>
* 1 if the suffix was found
* @return void
*/
#define cstring_ends_with(str, ptr, count, ret_found) \
do { \
const void *const tmp_p___ = (const void *)(ptr); \
const ptrdiff_t sub_sz___ = (ptrdiff_t)(count); \
(ret_found) = 0; \
if (tmp_p___ && sub_sz___ > 0 && sub_sz___ <= (ptrdiff_t)cstring_size(str)) { \
str_n_eq_((str) + (ptrdiff_t)cstring_size(str) - sub_sz___, (ptr), sub_sz___, (ret_found)); \
} \
} while (0)
/**
* @brief cstring_ends_with - Check if the string contains the given substring.
* @param str - The cstring.
* @param ptr - Pointer to the first character of the substring.
* @param count - Length of the substring.
* @param ret_found - Variable of type `int` that receives the result of the
* check. <br>
* 0 if the substring was not found <br>
* 1 if the substring was found
* @return void
*/
#define cstring_contains(str, ptr, count, ret_found) \
do { \
ptrdiff_t off__; \
cstring_find((str), 0, (ptr), (count), off__); \
(ret_found) = off__ >= 0; \
} while (0)
/**
* @brief cstring_substring - Copy a part of a string.
* @param from - The source cstring.
* @param pos - Position in the source cstring where the substring begins.
* @param n - Number of consecutive characters copied to the substring.
* @param to - Destination to which the substring is copied. Can be a NULL
* string. <br>
* If `to` refers to an existing cstring, the old content is
* overwritten.
* @return void
*/
#define cstring_substring(from, pos, n, to) \
do { \
if ((from) && cstring_ttl_siz_(from) > (size_t)(pos)) { \
const size_t cs_count___ = (size_t)(pos) + (size_t)(n) >= cstring_size(from) ? cstring_size(from) - (size_t)(pos) : (size_t)(n); \
if ((to) && cstring_ttl_cap_(to) < cs_count___ + 1) { \
cstring_free(to); \
} \
if (!(to)) { \
cstring_grow_((to), cs_count___ + 1); \
} \
cstring_clib_memcpy((to), (from) + (size_t)(pos), cs_count___ * sizeof(*(from))); \
cstring_set_ttl_siz_((to), cs_count___ + 1); \
(to)[cs_count___] = 0; \
} else { \
cstring_clear(to); \
} \
} while (0)
/** @} */
/* ========================== */
/* === EXTENDED INTERFACE === */
/* ========================== */
/**
* @defgroup cstring_array_api The cstring_array API
* @{
*/
/* ------------------------- */
/* --- vector of cstring --- */
/**
* @brief cstring_array_type - The vector type of the data provided by
* `cstring_split()`.
* @param type - The character type of the strings in the vector.
*/
#define cstring_array_type(type) type **
/**
* @brief cstring_array - Syntactic sugar to retrieve a vector type.
* @param type - The character type of the strings in the vector.
*/
#define cstring_array(type) cstring_array_type(type)
/**
* @brief cstring_array_iterator - The iterator type used for a vector.
* @param type - The character type of the strings in the vector.
*/
#define cstring_array_iterator(type) cstring_array_type(type)
/**
* @brief cstring_split - Tokenize a cstring into a cstring_array vector.
* @param str - The cstring.
* @param max_tok - Maximum number of tokens to be created. -1 specifies that
* all tokens are created.
* @param ptr - Pointer to the first character of the delimiter string
* that separates the tokens in `str`.
* @param count - Number of consecutive characters to be used as delimiter.
* @param ret_array - Variable of cstring_array(type) that receives the created
* vector. Can be a NULL vector. <br>
* If `ret_array` refers to an existing vector, the old
* content is overwritten.
* @return void
*/
#define cstring_split(str, max_tok, ptr, count, ret_array) \
do { \
const void *const check__ = (const void *)(ptr); \
const ptrdiff_t search_sz__ = (ptrdiff_t)(count); \
cstring_array_clear(ret_array); \
if ((str) && (max_tok) && (ptrdiff_t)(max_tok) > -2 && check__ && search_sz__ > 0) { \
ptrdiff_t found__; \
ptrdiff_t begin__ = 0; \
size_t s_cnt__ = 0; \
const size_t tok_minus_one__ = (size_t)(max_tok) - (size_t)1; \
cstring_array_reserve((ret_array), 63); \
cstring_set_ttl_siz_((ret_array), 1); \
cstring_find((str), begin__, (ptr), search_sz__, found__); \
for (; found__ != -1 && s_cnt__ < tok_minus_one__; ++s_cnt__) { \
cstring_push_back((ret_array), NULL); \
cstring_assign((ret_array)[s_cnt__], (str) + begin__, found__ - begin__); \
begin__ = found__ + search_sz__; \
cstring_find((str), begin__, (ptr), search_sz__, found__); \
} \
cstring_push_back((ret_array), NULL); \
cstring_assign((ret_array)[s_cnt__], (str) + begin__, cstring_size(str) - (size_t)begin__); \