-
Notifications
You must be signed in to change notification settings - Fork 1
/
lite_string.c
1831 lines (1678 loc) · 63.5 KB
/
lite_string.c
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
// Copyright (c) 2024 Ian Duncan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lite_string.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifndef __has_include
#define __has_include(x) 0 // Compatibility with non-GNU compilers
#endif // !__has_include
#ifndef __has_builtin
#define __has_builtin(x) 0 // For compilers that do not support __has_builtin
#endif // !__has_builtin
#define HAS_STRNCASECMP 1
#if _MSC_VER || _WIN32 || _WIN64 || WIN32
#define strncasecmp _strnicmp // Windows equivalent
#elif _GNU_SOURCE || _DEFAULT_SOURCE
// strncasecmp is available as a GNU extension
#elif __has_include(<strings.h>) || _POSIX_C_SOURCE >= 200112L
#include <strings.h> // POSIX strncasecmp is available
#else
// strncasecmp is not available, use a custom implementation
#undef HAS_STRNCASECMP
#define HAS_STRNCASECMP 0
#include <ctype.h> // For tolower()
#endif // _MSC_VER || _WIN32 || _WIN64 || WIN32
#if __STDC_VERSION__ >= 202311L // C23 or later, use the new attributes if available
#if HAS_ATTRIBUTE(always_inline)
#define LITE_ATTR_ALWAYS_INLINE [[gnu::always_inline]]
#else
#define LITE_ATTR_ALWAYS_INLINE
#endif // HAS_ATTRIBUTE(gnu::always_inline)
#if HAS_C_ATTRIBUTE(maybe_unused)
#define LITE_ATTR_MAYBE_UNUSED [[__maybe_unused__]]
#elif HAS_ATTRIBUTE(unused)
#define LITE_ATTR_MAYBE_UNUSED [[gnu::unused]]
#else
#define LITE_ATTR_MAYBE_UNUSED
#endif // HAS_C_ATTRIBUTE(maybe_unused)
#else
#if HAS_ATTRIBUTE(__always_inline__)
#define LITE_ATTR_ALWAYS_INLINE __attribute__((__always_inline__))
#else
#define LITE_ATTR_ALWAYS_INLINE
#endif // HAS_ATTRIBUTE(__always_inline__)
#if HAS_ATTRIBUTE(__unused__)
#define LITE_ATTR_MAYBE_UNUSED __attribute__((__unused__))
#else
#define LITE_ATTR_MAYBE_UNUSED
#endif // HAS_ATTRIBUTE(__unused__)
#endif // __STDC_VERSION__ >= 202311L
/**
* @brief A simple emulation of a C++ string in C.
*
* The data is stored as a pointer to a dynamically allocated array of characters.\n
* The capacity represents the total number of characters that the string can hold without needing to be resized.\n
* When the size reaches the capacity, the string is resized to a larger capacity to accommodate more characters.
*/
struct lite_string {
char *data; ///< A pointer to the character data.
size_t size; ///< The number of characters in the string, not including the null character.
size_t capacity; ///< The total number of characters that the string can hold.
};
/**
* @brief Creates a new string with an initial capacity of 16.
*
* @return A pointer to the newly created string, or nullptr if memory allocation failed.
* @note The returned pointer must be freed by the caller, using \p string_free
*/
LITE_ATTR_NODISCARD LITE_ATTR_HOT lite_string *string_new(void) {
lite_string *s = (lite_string *) malloc(sizeof(lite_string));
if (s) {
if ((s->data = (char *) calloc(16, sizeof(char)))) {
s->size = 0;
s->capacity = 16;
return s;
}
// If memory allocation failed, free the string
free(s);
}
return nullptr;
}
/**
* @brief Creates a new string and initializes it with a C-string.
*
* @param cstr A pointer to the C-string that will be used to initialize the new string.
* @return A pointer to the newly created string, or nullptr if the memory allocation failed
* or the C-string is nullptr.
* @note The returned pointer must be freed by the caller, using the \p string_free() function.
*/
LITE_ATTR_NODISCARD LITE_ATTR_HOT lite_string *string_new_cstr(const char *const restrict cstr) {
lite_string *s = string_new();
if (cstr && s) {
if (!string_append_cstr(s, cstr)) {
string_free(s);
return nullptr;
}
}
return s;
}
/**
* @brief Frees the memory used by a string.
*
* If the input pointer is nullptr, the function does nothing.
*
* @param s A pointer to the string to be freed.
*/
LITE_ATTR_HOT void string_free(lite_string *const restrict s) {
if (s) {
if (s->data) {
free(s->data);
s->data = nullptr;
}
s->size = 0;
s->capacity = 0;
free(s);
}
}
/**
* @brief Computes the smallest power of 2 greater than or equal to the input integer.
*
* @param x The input integer.
* @return The smallest power of 2 greater than or equal to the input integer.
*
* @note This function is for internal use only, and should not be called directly by the user.
*/
LITE_ATTR_UNSEQUENCED LITE_ATTR_ALWAYS_INLINE static inline size_t lite_clp2_(size_t x) {
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
/**
* @brief Resizes the string to the given size.
*
* @param s A pointer to the string to be resized.
* @param size The new size for the string.
* @return False if resizing was necessary but failed, or if the string is invalid. True otherwise.
*
* @note This function only resizes the string if it needs to be resized.\n
* Resizing is needed if the new size is greater than the current capacity
* (which should be greater than 16).\n
* The new size is rounded up to the next power of 2 to improve performance.
*/
LITE_ATTR_HOT bool string_reserve(lite_string *const restrict s, size_t size) {
if (s) {
// Space for the null terminator
++size;
// Round up the new size to the next power of 2
size = lite_clp2_(size);
if (size < 16) size = 16;
// Reallocate the memory
if (size > s->capacity - 1) {
void *temp = realloc(s->data, size * sizeof(char));
if (temp == nullptr) return false;
// Update the string's data pointer
s->data = (char *) temp;
// Set the new capacity to the new size, and initialize the new memory to zero
s->capacity = size;
memset(s->data + s->size, '\0', s->capacity - s->size);
}
return true;
}
return false;
}
/**
* @brief Inserts a specified number of characters from a C-string into a string at a specified index.
*
* @param s A pointer to the string where the characters will be inserted.
* @param cstr The C-string from which the characters will be copied.
* @param index The index at which the characters will be inserted.
* @param count The number of characters to be copied from the C-string to the string.
* @return true if the characters were successfully inserted, false otherwise.
*
* @note If the index is equal to the size of the string and the string is empty,
* the function will append the characters from the C-string to the string.
*/
bool string_insert_cstr_range(lite_string *const restrict s, const char *const restrict cstr,
const size_t index, const size_t count) {
if (s && cstr) {
if (!count) return true;
if (count <= strlen(cstr)) {
// The index must be within the bounds of the string
if (index < s->size) {
// Resize the string if necessary
if (s->size + count >= s->capacity - 1) {
if (!string_reserve(s, s->size + count)) return false;
}
// Move the characters after the index to make room for the C-string
memmove(s->data + (index + count) * sizeof(char), s->data + index * sizeof(char),
(s->size - index) * sizeof(char));
// Copy the C-string into the string
memcpy(s->data + index * sizeof(char), cstr, count * sizeof(char));
s->size += count;
return true;
}
if (index == s->size) return string_append_cstr_range(s, cstr, count);
}
}
return false;
}
/**
* @brief Inserts a C-string into a string at a specified index.
*
* @param s A pointer to the string where the C-string will be inserted.
* @param index The index at which the C-string will be inserted.
* @param cstr The C-string to be inserted.
* @return true if the C-string was successfully inserted, false otherwise.
*/
bool string_insert_cstr(lite_string *const restrict s, const char *restrict cstr, const size_t index) {
return cstr && string_insert_cstr_range(s, cstr, index, strlen(cstr));
}
/**
* @brief Appends a character to the end of a string.
*
* @param s A pointer to the string where the character will be appended.
* @param c The character to be appended.
* @return true if the character was successfully appended, false otherwise.
*
* @note If the string is full, it is resized to twice its current capacity.\n
* The character is not appended if it is the null character.
*/
bool string_push_back(lite_string *const restrict s, const char c) {
if (s && c != '\0') {
if (s->size >= s->capacity - 1) {
if (!string_reserve(s, s->capacity << 1)) return false;
}
s->data[s->size++] = c;
return true;
}
return false;
}
/**
* @brief Retrieves the character at a given index in the string.
*
* @param s A pointer to the string.
* @param index The index of the character to be retrieved.
* @return The character at the given index, or the null character
* if the index is out of bounds or the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE char string_at(const lite_string *const restrict s, const size_t index) {
if (s && index < s->size)
return s->data[index];
return '\0';
}
/**
* @brief Removes the last character from the string.
*
* If the string is valid and not empty, it replaces replaces the last character with the null character.
*
* @param s A pointer to the string.
*/
void string_pop_back(lite_string *const restrict s) {
if (s && s->size)
s->data[--s->size] = '\0';
}
/**
* @brief Checks if the string is empty.
*
* @param s A pointer to the string.
* @return true if the string is empty or invalid, false otherwise.
*/
LITE_ATTR_REPRODUCIBLE bool string_empty(const lite_string *const restrict s) {
return s == nullptr || s->size == 0;
}
/**
* @brief Retrieves the last character of a string.
*
* @param s A pointer to the string.
* @return The last character of the string if the string is valid and not empty, or the null character otherwise.
*/
LITE_ATTR_REPRODUCIBLE char string_back(const lite_string *const restrict s) {
return s && s->size ? s->data[s->size - 1] : '\0';
}
/**
* @brief Retrieves the first character of a string.
*
* @param s A pointer to the string.
* @return The first character of the string if the string is valid and not empty, or the null character otherwise.
*/
LITE_ATTR_REPRODUCIBLE char string_front(const lite_string *const restrict s) {
return s && s->size ? s->data[0] : '\0';
}
/**
* @brief Erases a range of characters from a string.
*
* @param s A pointer to the string from which the characters will be removed.
* @param start The starting index of the range to be removed.
* @param count The number of characters to be removed.
* @return true if the characters were successfully removed, false otherwise.
*/
bool string_erase_range(lite_string *const restrict s, const size_t start, const size_t count) {
if (s && start < s->size) {
if (count == 0) return true;
// Check if the range is within the bounds of the string
#if __has_builtin(__builtin_add_overflow)
size_t end;
if (!__builtin_add_overflow(start, count, &end) && end <= s->size)
#else
if (count < s->size && start + count <= s->size)
#endif
{
// Copy the characters after the range to overwrite the characters to be removed
memmove(s->data + start * sizeof(char), s->data + (start + count) * sizeof(char),
(s->size - start - count) * sizeof(char));
s->size -= count;
// Fill the remaining space with null characters
memset(s->data + s->size, '\0', s->capacity - s->size);
return true;
}
}
return false;
}
/**
* @brief Removes the character at a given index in the string.
*
* @param s A pointer to the string.
* @param index The index of the character to be removed.
* @return true if the character was successfully removed, false otherwise.
*/
bool string_erase(lite_string *const restrict s, const size_t index) {
if (s && index < s->size) {
// Move the characters after the index to overwrite the character to be removed
memmove(s->data + index * sizeof(char), s->data + (index + 1) * sizeof(char), (s->size - index) * sizeof(char));
// Replace the last character with the null character
s->data[--s->size] = '\0';
return true;
}
return false;
}
/**
* @brief Compares two strings for equality.
*
* @param s1 A pointer to the first string.
* @param s2 A pointer to the second string.
* @return true if the strings are equal, false otherwise.
*/
LITE_ATTR_REPRODUCIBLE bool string_compare(const lite_string *const restrict s1, const lite_string *const restrict s2) {
if (s1 == nullptr || s2 == nullptr || s1->size != s2->size)
return false;
return memcmp(s1->data, s2->data, s1->size) == 0;
}
/**
* @brief Compares two strings for equality, ignoring case.
*
* @param s1 A pointer to the first string.
* @param s2 A pointer to the second string.
* @return true if the strings are equal (ignoring case), false otherwise.
*/
LITE_ATTR_REPRODUCIBLE bool string_case_compare(const lite_string *const restrict s1,
const lite_string *const restrict s2) {
if (s1 == nullptr || s2 == nullptr || s1->size != s2->size)
return false;
return strncasecmp(s1->data, s2->data, s1->size) == 0;
}
/**
* @brief Returns the length of a string.
*
* @param s A pointer to the string.
* @return The length of the string, or 0 if the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE LITE_ATTR_HOT size_t string_length(const lite_string *const restrict s) {
return s ? s->size : 0;
}
/**
* @brief Returns the size of a string.
*
* @param s A pointer to the string.
* @return The length of the string, or 0 if the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE LITE_ATTR_HOT size_t string_size(const lite_string *const restrict s) {
return string_length(s);
}
/**
* @brief Returns the capacity of the string.
*
* @param s A pointer to the string.
* @return The capacity of the string, or 0 if the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE size_t string_capacity(const lite_string *const restrict s) {
return s ? s->capacity : 0;
}
/**
* @brief Clears the string.
*
* @param s A pointer to the string.
*/
void string_clear(lite_string *const restrict s) {
if (s && s->size) {
memset(s->data, '\0', s->size);
s->size = 0;
}
}
/**
* @brief Inserts a new character at a given index in the string.
*
* @param s A pointer to the string where the character will be inserted.
* @param index The index at which the character will be inserted.
* @param c The character to be inserted.
* @return true if the character was successfully inserted, false otherwise.
*/
bool string_insert(lite_string *const restrict s, const size_t index, const char c) {
if (s && c != '\0') {
if (index < s->size) {
// Resize the string if necessary
if (s->size >= s->capacity - 1) {
if (!string_reserve(s, s->capacity << 1)) return false;
}
// Move the characters after the index to make room for the new character
memmove(s->data + (index + 1) * sizeof(char), s->data + index * sizeof(char),
(s->size - index) * sizeof(char));
// Insert the new character into the string
s->data[index] = c;
++s->size;
return true;
}
if (index == 0 && s->size == 0) return string_push_back(s, c);
}
return false;
}
/**
* @brief Inserts a specified number of characters from a substring into a string at a specified index.
*
* @param s A pointer to the string where the characters will be inserted.
* @param sub A pointer to the substring from which the characters will be copied.
* @param index The index at which the characters will be inserted.
* @param count The number of characters to be copied from the substring to the string.
* @return true if the characters were successfully inserted, false otherwise.
*
* @note If the index is equal to the size of the string and the string is empty,
* the function will append the characters from the substring to the string.
*/
bool
string_insert_range(lite_string *const restrict s, const lite_string *const restrict sub, const size_t index,
const size_t count) {
if (s && sub) {
if (!count) return true;
if (count <= sub->size) {
if (index < s->size) {
if (s->size + count >= s->capacity - 1) {
if (!string_reserve(s, s->size + count)) return false;
}
memmove(s->data + (index + count) * sizeof(char), s->data + index * sizeof(char),
(s->size - index) * sizeof(char));
memcpy(s->data + index * sizeof(char), sub->data, count * sizeof(char));
s->size += count;
return true;
}
if (index == 0 && s->size == 0) return string_append_range(s, sub, count);
}
}
return false;
}
/**
* @brief Modifies the character at a given index in the string.
*
* @param s A pointer to the string where the character will be modified.
* @param index The index of the character to be modified.
* @param c The new character.
*/
void string_set(const lite_string *const restrict s, const size_t index, const char c) {
if (s && c != '\0' && index < s->size)
s->data[index] = c;
}
/**
* @brief Inserts a string into another string at a specified index.
*
* @param s A pointer to the string where the new string will be inserted.
* @param sub A pointer to the string that will be inserted into the first string.
* @param index The position in the first string where the second string will be inserted.
* @return true if the string was successfully inserted, false otherwise.
*/
bool
string_insert_string(lite_string *const restrict s, const lite_string *const restrict sub, const size_t index) {
return sub && string_insert_range(s, sub, index, sub->size);
}
/**
* @brief Retrieves a substring from the string.
*
* @param s A pointer to the string from which the substring will be retrieved.
* @param start The start index of the substring.
* @param len The length of the substring.
* @return A pointer to the new string containing the substring, or nullptr if the substring could not be retrieved.
*
* @note The returned pointer must be freed by the caller, using \p string_free
*/
LITE_ATTR_NODISCARD lite_string *
string_substr(const lite_string *const restrict s, const size_t start, const size_t len) {
if (s) {
// The requested substring must be within the bounds of the string
if (len == 0 || start >= s->size || len > s->size || start + len - 1 > s->size) return nullptr;
// Create a new string to store the substring
lite_string *sub = string_new();
if (sub) {
// Resize the new string to the length of the substring
if (string_reserve(sub, len)) {
// Copy the substring into the new string
memcpy(sub->data, s->data + start, len);
sub->size = len;
return sub;
}
// If resizing failed, free the new string
string_free(sub);
}
}
return nullptr;
}
/**
* @brief Concatenates two strings.
*
* @param s1 A pointer to the first string.
* @param s2 A pointer to the second string.
* @return A pointer to the new string containing the concatenated strings,
* or nullptr if the strings could not be concatenated.
* @note The returned pointer must be freed by the caller, using \p string_free
*/
LITE_ATTR_NODISCARD lite_string *
string_concat(const lite_string *const restrict s1, const lite_string *const restrict s2) {
if (s1 && s2) {
lite_string *s = string_new();
if (s) {
// Resize the new string appropriately
if (string_reserve(s, s1->size + s2->size)) {
// Copy the input strings into the new string
memcpy(s->data, s1->data, s1->size * sizeof(char));
memcpy(s->data + s1->size * sizeof(char), s2->data, s2->size * sizeof(char));
s->size = s1->size + s2->size;
return s;
}
// If resizing failed, free the new string
string_free(s);
}
}
return nullptr;
}
/**
* @brief Appends a specified number of characters from one string to another.
*
* @param s1 A pointer to the string where the characters will be appended.
* @param s2 A pointer to the string from which the characters will be copied.
* @param count The number of characters to be copied from the second string to the first string.
* @return true if the characters were successfully appended, false otherwise.
*/
bool
string_append_range(lite_string *const restrict s1, const lite_string *const restrict s2, const size_t count) {
if (s1) {
if (count == 0) return true;
if (s2) {
if (count <= s2->size) {
if (string_reserve(s1, s1->size + count)) {
// Copy the characters from the second string into the first string
memcpy(s1->data + s1->size * sizeof(char), s2->data, count * sizeof(char));
s1->size += count;
return true;
}
}
}
}
return false;
}
/**
* @brief Appends a string to the end of another string.
*
* @param s1 A pointer to the string where the second string will be appended.
* @param s2 A pointer to the string that will be appended to the first string.
* @return true if the second string was successfully appended, false otherwise.
*/
bool string_append(lite_string *const restrict s1, const lite_string *const restrict s2) {
return s2 && string_append_range(s1, s2, s2->size);
}
/**
* @brief Appends a specified number of characters from a C-string to a string.
*
* @param s A pointer to the string where the characters will be appended.
* @param cstr The C-string from which the characters will be copied.
* @param count The number of characters to be copied from the C-string to the string.
* @return true if the characters were successfully appended, false otherwise.
*/
bool
string_append_cstr_range(lite_string *const restrict s, const char *const restrict cstr, const size_t count) {
if (s) {
if (count == 0) return true;
if (cstr) {
// Resize the string if necessary
if (count <= strlen(cstr)) {
if (string_reserve(s, s->size + count)) {
// Copy the C-string into the string
memcpy(s->data + s->size * sizeof(char), cstr, count * sizeof(char));
s->size += count;
return true;
}
}
}
}
return false;
}
/**
* @brief Appends a C-string to the end of a string.
*
* @param s A pointer to the string where the C-string will be appended.
* @param cstr The C-string to be appended to the string.
* @return true if the C-string was successfully appended, false otherwise.
*/
bool string_append_cstr(lite_string *const restrict s, const char *const restrict cstr) {
return string_append_cstr_range(s, cstr, strlen(cstr));
}
/**
* @brief Returns a pointer to the C-string representation of a string.
*
* @param s A pointer to the string.
* @return A pointer to the C-string representation of the string, or nullptr if the string is invalid.
* @note It is not recommended to modify the string's data directly. Use the provided functions instead.
*/
LITE_ATTR_HOT char *string_cstr(const lite_string *const restrict s) {
if (s) {
// Check if the string is null-terminated
if (s->data[s->size] != '\0')
s->data[s->size] = '\0';
return s->data;
}
return nullptr;
}
/**
* @brief Returns a pointer to the underlying character array holding the string data.
*
* @param s A pointer to the string.
* @return A pointer to the string's data, or a nullptr if the string is null.
* @note It is not recommended to modify the string's data directly. Use the provided functions instead.
* @note The returned pointer is not guaranteed to be null-terminated.
* Use \p string_cstr() to get a null-terminated C-string.
*/
LITE_ATTR_REPRODUCIBLE LITE_ATTR_HOT char *string_data(const lite_string *const restrict s) {
return s ? s->data : nullptr;
}
/**
* @brief Compares a string with a C-string for equality.
*
* @param s A pointer to the string.
* @param cstr The C-string to be compared with the string.
* @return true if the string and the C-string are equal, false otherwise.
*/
LITE_ATTR_REPRODUCIBLE bool string_compare_cstr(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
if (s->size == strlen(cstr))
return memcmp(s->data, cstr, s->size) == 0;
}
return false;
}
/**
* @brief Compares a string with a C-string for equality, ignoring case.
*
* @param s A pointer to the string.
* @param cstr The C-string to be compared with the string.
* @return true if the string and the C-string are equal (ignoring case), false otherwise.
*/
LITE_ATTR_REPRODUCIBLE bool
string_case_compare_cstr(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && cstr) {
if (s->size == strlen(cstr)) {
#if HAS_STRNCASECMP
return strncasecmp(s->data, cstr, s->size) == 0;
#else
for (size_t i = 0; i < s->size; ++i) {
if (tolower((unsigned char) s->data[i]) != tolower((unsigned char) cstr[i]))
return false;
}
return true;
#endif
}
}
return false;
}
/**
* @brief Copies the characters from a string to a buffer.
*
* @param s A pointer to the string.
* @param buf The buffer where the characters will be copied.
* @return true if the characters were successfully copied, false otherwise.
*/
bool string_copy_buffer(const lite_string *const restrict s, char *buf) {
if (s && buf && !string_empty(s)) {
// Copy the characters from the string to the buffer
memcpy(buf, s->data, s->size * sizeof(char));
// Append the null character to the end of the buffer
buf[s->size] = '\0';
return true;
}
return false;
}
/**
* @brief Copies the contents of one string to another.
*
* @param src A pointer to the source string.
* @param dest A pointer to the destination string.
* @return true if the contents of the source string were successfully copied to the destination string, false otherwise.
*/
bool string_copy(const lite_string *const restrict src, lite_string *const restrict dest) {
if (src && dest && src->data && dest->data) {
// Resize the destination string if necessary
if (src->size > dest->size)
if (!string_reserve(dest, src->size)) return false;
// Copy the characters from the source string to the destination string
memcpy(dest->data, src->data, src->size * sizeof(char));
dest->size = src->size;
return true;
}
return false;
}
/**
* @brief Swaps the contents of two strings.
*
* @param s1 A pointer to the first string.
* @param s2 A pointer to the second string.
* @return true if the contents of the strings were successfully swapped, false otherwise.
*/
bool string_swap(lite_string *const restrict s1, lite_string *const restrict s2) {
if (s1 && s2) {
const lite_string temp = *s1;
*s1 = *s2;
*s2 = temp;
return true;
}
return false;
}
/**
* @brief Finds the last occurrence of a character in a string.
*
* @param s A pointer to the string.
* @param c The character to be found.
* @return The index of the last occurrence of the character in the string,
* or \p lite_string_npos if the character was not found.
*/
LITE_ATTR_REPRODUCIBLE size_t string_find_last_of(const lite_string *const restrict s, const char c) {
if (s && s->size && c != '\0') {
#if defined(_GNU_SOURCE) && !(defined(_WIN32) || defined(WIN32) || _MSC_VER)
const char *found = (const char *) memrchr(s->data, c, s->size);
if (found) return found - s->data;
#else
for (size_t i = s->size; i > 0; --i) {
if (s->data[i - 1] == c)
return i - 1;
}
#endif
}
return lite_string_npos;
}
/**
* @brief Finds the last occurrence of a character that does not match the specified character in a string.
*
* @param s A pointer to the string.
* @param c The character to be compared against.
* @return The index of the last occurrence of a character that does not match the specified character in the string,
* or \p lite_string_npos if all characters match or the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE size_t string_find_last_not_of(const lite_string *const restrict s, const char c) {
if (s && s->size && c != '\0') {
for (size_t i = s->size; i > 0; --i) {
if (s->data[i - 1] != c)
return i - 1;
}
}
return lite_string_npos;
}
/**
* @brief Finds the first occurrence of a character in a string, starting from a specified index.
*
* @param s A pointer to the string.
* @param c The character to be found.
* @param start The index from which the search will start.
* @return The index of the first occurrence of the character in the string,
* or \p lite_string_npos if the character was not found.
*/
LITE_ATTR_REPRODUCIBLE size_t
string_find_first_from(const lite_string *const restrict s, const char c, const size_t start) {
if (s && s->size && c != '\0' && start < s->size) {
const char *found = (const char *) memchr(s->data + start, c, s->size - start);
if (found) return found - s->data;
}
return lite_string_npos;
}
/**
* @brief Finds the first occurrence of a character in a string.
*
* @param s A pointer to the string.
* @param c The character to be found.
* @return The index of the first occurrence of the character in the string,
* or \p lite_string_npos if the character was not found.
*/
LITE_ATTR_REPRODUCIBLE size_t string_find_first_of(const lite_string *const restrict s, const char c) {
return string_find_first_from(s, c, 0);
}
/**
* @brief Finds the first occurrence of a character that does not match the specified character in a string.
*
* @param s A pointer to the string.
* @param c The character to be compared against.
* @return The index of the first occurrence of a character that does not match the specified character in the string,
* or \p lite_string_npos if all characters match or the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE size_t string_find_first_not_of(const lite_string *const restrict s, const char c) {
if (s && s->size && c != '\0') {
for (size_t i = 0; i < s->size; ++i) {
if (s->data[i] != c)
return i;
}
}
return lite_string_npos;
}
/**
* @brief Finds the first occurrence of any character from a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be found.
* @return The index of the first occurrence of any character from the C-string in the string,
* or \p lite_string_npos if no character was found.
*/
LITE_ATTR_REPRODUCIBLE size_t
string_find_first_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && s->size && cstr) {
const size_t len = strlen(cstr);
if (len) {
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};
// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char) cstr[i]] = true;
// Find the first occurrence of any character from the C-string in the string
for (size_t i = 0; i < s->size; ++i) {
if (lookup[(unsigned char) s->data[i]])
return i;
}
}
}
return lite_string_npos;
}
/**
* @brief Finds the first occurrence of any character not present in a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be compared against.
* @return The index of the first occurrence of any character not present in the C-string in the string,
* or \p lite_string_npos if all characters match or the string is invalid.
*/
LITE_ATTR_REPRODUCIBLE size_t
string_find_first_not_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && s->size && cstr) {
const size_t len = strlen(cstr);
if (len) {
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};
// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char) cstr[i]] = true;
// Find the first occurrence of any character not present in the C-string in the string
for (size_t i = 0; i < s->size; ++i) {
if (!lookup[(unsigned char) s->data[i]])
return i;
}
}
}
return lite_string_npos;
}
/**
* @brief Finds the last occurrence of any character from a given C-string in a string.
*
* @param s A pointer to the string.
* @param cstr The C-string containing the characters to be found.
* @return The index of the last occurrence of any character from the C-string in the string,
* or \p lite_string_npos if no character was found.
*/
LITE_ATTR_REPRODUCIBLE size_t
string_find_last_of_chars(const lite_string *const restrict s, const char *const restrict cstr) {
if (s && s->size && cstr) {
const size_t len = strlen(cstr);
if (len) {
// Create a lookup table for the characters in the C-string
bool lookup[256] = {false};
// Set the corresponding index to true for each character in the C-string
for (size_t i = 0; i < len; ++i)
lookup[(unsigned char) cstr[i]] = true;
// Find the last occurrence of any character from the C-string in the string
for (size_t i = s->size; i > 0; --i) {
if (lookup[(unsigned char) s->data[i - 1]])
return i - 1;
}
}
}
return lite_string_npos;
}
/**
* @brief Finds the last occurrence of any character not present in a given C-string in a string.
*
* @param s A pointer to the string.