-
Notifications
You must be signed in to change notification settings - Fork 0
/
rapidstring.h
1544 lines (1379 loc) · 35.3 KB
/
rapidstring.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
/*
* TABLE OF CONTENTS
*
* 1. STRUCTURES & MACROS
* - Declarations: line 89
*
* 2. CONSTRUCTION & DESTRUCTION
* - Declarations: line 407
* - Definitions: line 1175
*
* 3. COPYING
* - Declarations: line 513
* - Definitions: line 1222
*
* 4. CAPACITY
* - Declarations: line 620
* - Definitions: line 1275
*
* 5. MODIFIERS
* - Declarations: line 759
* - Definitions: line 1340
*
* 6. HEAP OPERATIONS
* - Declarations: line 1068
* - Definitions: line 1512
*/
/**
* @file rapidstring.h
* @brief The single header of the rapidstring library.
*/
/**
* @mainpage rapidstring
* The documentation of the rapidstring library is grouped by modules. All
* functions are within a certain module, and a list of all modules may be seen
* [here](https://boyerjohn.github.io/rapidstring/modules.html).
*
* All functions are named based off what state the string must be. For example,
* `rs_heap_cat_n` may only be used to concatenate strings that are on the heap.
* This method is intended for internal use. However, if you know for a fact
* your string is larger than #RS_STACK_CAPACITY, you may use the heap methods
* directly to avoid a flag check and obtain a minor speedup. The same is true
* for `rs_stack_cat_n`, and all other stack or heap specific functions. Any
* functions without either infix simply means it can handle the string in any
* state. These are the functions intended for the average user. Furthermore,
* the latter functions will grow the size of the string when required, while
* the former functions require this to be done manually.
*
* There are no error codes returned from this library, as nothing ever fails.
* The only possible sources of failure are invalid arguments or a failed
* allocation. The former is undefined behavior (or assert failures in when
* `NDEBUG` is not defined) and the latter may be verified with `errno`. The
* simple truth is that nearly all applications brutally fail either way when
* memory runs out, and `rapidstring` takes advantage of this by never checking
* whether allocations succeed. If your application must handle allocation
* failures, you may set `errno` to `0` before calling a function that either
* intializes or grows the heap buffer, and then check `errno` after this call.
* All modern compilers will set `errno` if `malloc()` fails.
*
* @copyright Copyright © 2018 John Boyer.
* @author <a href="https://github.com/boyerjohn">John Boyer</a>
* @version 1.0.0
*/
#ifndef RAPIDSTRING_H_962AB5F800398A34
#define RAPIDSTRING_H_962AB5F800398A34
#include <assert.h> /* assert() */
#include <string.h> /* memcpy(), memmove(), memset() */
/*
* ===============================================================
*
* STRUCTURES & MACROS
*
* ===============================================================
*/
/**
* @defgroup structures Structures & macros
* Structure and macro definitions.
* @{
*/
/**
* @brief Major version of the rapidstring library.
*
* @since 1.0.0
*/
#define RS_VERSION_MAJOR 1
/**
* @brief Minor version of the rapidstring library.
*
* @since 1.0.0
*/
#define RS_VERSION_MINOR 0
/**
* @brief Patch version of the rapidstring library.
*
* @since 1.0.0
*/
#define RS_VERSION_PATCH 0
#ifndef RS_GROWTH_FACTOR
/**
* @brief String growth factor macro.
*
* A string's capacity will be increased by this factor every time it runs out
* of space. Redefine this macro depending on your application's needs.
*
* @since 1.0.0
*/
#define RS_GROWTH_FACTOR (2)
#endif
#ifndef RS_AVERAGE_SIZE
/**
* @brief Average string size macro.
*
* This macro is used to help the branch predictor. If you specify that your
* application's average string size is `8`, the branches will be optimized
* for stack strings. The same would occur for heap strings if a large size
* is provided.
*
* @since 1.0.0
*/
#define RS_AVERAGE_SIZE (20)
#endif
#if !defined(RS_MALLOC) && !defined(RS_REALLOC) && !defined(RS_FREE)
#include <stdlib.h> /* malloc(), realloc(), free() */
/**
* @brief Allocation macro.
*
* If this macro is redefined, RS_REALLOC() and RS_FREE() must be as well.
*
* @since 1.0.0
*/
#define RS_MALLOC malloc
/**
* @brief Reallocation macro.
*
* If this macro is redefined, RS_MALLOC() and RS_FREE() must be as well.
*
* @since 1.0.0
*/
#define RS_REALLOC realloc
/**
* @brief Deallocation macro.
*
* If this macro is redefined, RS_MALLOC() and RS_REALLOC() must be as well.
*
* @since 1.0.0
*/
#define RS_FREE free
#endif
#ifndef RS_HEAP_FLAG
/**
* @brief Heap flag of a #rapidstring.
*
* @warning Intended for internal use.
*
* @since 1.0.0
*/
#define RS_HEAP_FLAG (0xFF)
#endif
#define RS_ASSERT_RS(s) \
do { \
assert(s != NULL); \
assert(s->heap.flag == RS_HEAP_FLAG || \
s->heap.flag <= RS_STACK_CAPACITY); \
} while (0)
#ifdef __GNUC__
#define RS_GCC_VERSION \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#else
#define RS_GCC_VERSION (0)
#endif
/* GCC version 2.96 required for branch prediction expectation. */
#if RS_GCC_VERSION > 29600
#define RS_EXPECT(expr, val) __builtin_expect(expr, val)
#else
#define RS_EXPECT(expr, val) (expr)
#endif
#define RS_LIKELY(expr) RS_EXPECT(expr, 1)
#define RS_UNLIKELY(expr) RS_EXPECT(expr, 0)
#ifdef __STDC_VERSION__
#define RS_C99 (__STDC_VERSION__ >= 199901L)
#define RS_C11 (__STDC_VERSION__ >= 201112L)
#else
#define RS_C99 (0)
#define RS_C11 (0)
#endif
#ifndef RS_API
#ifdef RS_NOINLINE
/* GCC version 3.1 required for the no inline attribute. */
#if RS_GCC_VERSION > 30100
#define RS_API static __attribute__((noinline))
#elif defined(_MSC_VER)
#define RS_API static __declspec(noinline)
#else
#define RS_API static
#endif
#elif RS_C99
#define RS_API static inline
#elif defined(__GNUC__)
#define RS_API static __inline__
#elif defined(_MSC_VER)
#define RS_API static __forceinline
#else
#define RS_API static
#endif
#endif
typedef struct {
void *pointer;
size_t size;
} rs_align_dummy;
#ifdef RS_STACK_CAPACITY
/*
* This must be manually calculated as this defintion is used in the rs_heap
* struct itself. A classic Catch-22 situation.
*/
enum { PRE_HEAP_ALIGN_SZ = sizeof(size_t) * 2 + sizeof(void *) - 1 };
#define RS_ALIGNMENT (RS_STACK_CAPACITY - PRE_HEAP_ALIGN_SZ)
#elif RS_C11
#define RS_ALIGNMENT (_Alignof(rs_align_dummy))
#elif defined(__GNUC__)
#define RS_ALIGNMENT (__alignof__(rs_align_dummy))
#elif defined(_MSC_VER)
#define RS_ALIGNMENT (__alignof(rs_align_dummy))
#else
/*
* No other way to find the alignment than assuming structs are aligned
* by the largest member.
*/
#define RS_ALIGNMENT \
(sizeof(void *) > sizeof(size_t) ? sizeof(void *) : sizeof(size_t))
#endif
/**
* @brief Struct that stores the heap data.
*
* Accessing packed data structures incurs a performance penalty, therefore the
* alignment will be used to allow for a larger stack string.
*
* @warning Intended for internal use.
*
* @since 1.0.0
*/
typedef struct {
/**
* @brief Buffer of a heap string.
*
* Allocated using RS_MALLOC() or RS_REALLOC(). This buffer may be
* manually freed by directly calling `RS_FREE(s->heap.buffer)`. Doing
* so will avoid the heap flag check.
*/
char *buffer;
/**
* @brief Number of characters in a heap string.
*
* The null terminator is not included.
*/
size_t size;
/**
* @brief Capacity of a heap string.
*
* The null terminator is not included.
*/
size_t capacity;
/**
* @brief Alignnment of a heap string.
*
* Ensures @a flag and @a left are stored in the same location.
*/
unsigned char align[RS_ALIGNMENT - 1];
/**
* @brief Flag of the rapidstring union.
*
* The following is used to store the state of the union as well as the
* remaining capacity of the stack string. They may be shared as the
* capacity of a stack string will always be smaller than
* #RS_HEAP_FLAG, which guarantees unambiguity between the two states.
*/
unsigned char flag;
} rs_heap;
#ifndef RS_STACK_CAPACITY
/**
* @brief Capacity of a stack string.
*
* @warning This may not be redefined to lower than 15 bytes on x86
* architectures or 31 bytes on x86_64 architectures. This is because the
* remaining size of a stack string must be aligned with the heap flag.
*
* @since 1.0.0
*/
#define RS_STACK_CAPACITY (sizeof(rs_heap) - 1)
#endif
/**
* @brief Struct that stores the stack data.
*
* @warning Intended for internal use.
*
* @since 1.0.0
*/
typedef struct {
/**
* @brief Buffer of a stack string.
*
* An array of characters the size of #RS_STACK_CAPACITY excluding the
* null terminator.
*/
char buffer[RS_STACK_CAPACITY];
/**
* @brief The capacity left in the buffer of a stack string.
*
* Once the stack string runs out of space, zero will be written to
* this member, effectively becoming the null terminator.
*/
unsigned char left;
} rs_stack;
/**
* @brief Union that stores a #rapidstring.
*
* Some API functions are prefixed with `rs_heap_x()` or `rs_stack_x()`. These
* functions are to be used only when a string is guaranteed to be in either
* state, as mismatching these functions results in undefined behavior. There
* will always be the `rs_x()` alternative which automatically handles the state
* of the string. These functions should be used if optimization isn't a
* necessity or if strings have highly variable sizes.
*
* @since 1.0.0
*/
typedef union {
/** @brief Stack state of the #rapidstring union. */
rs_stack stack;
/** @brief Heap state of the #rapidstring union. */
rs_heap heap;
} rapidstring;
/* Based off the average string size, allow for more efficient branching. */
enum { RS_HEAP_LIKELY_V = RS_AVERAGE_SIZE > RS_STACK_CAPACITY };
#define RS_HEAP_LIKELY(expr) RS_EXPECT(expr, RS_HEAP_LIKELY_V)
#define RS_STACK_LIKELY(expr) RS_EXPECT(expr, !RS_HEAP_LIKELY_V)
/**
* @brief Pass the string data and size to a function.
*
* Forwards the buffer and size of a string to the provided function.
* Retrieving both the buffer and the size of a string requires a flag check,
* which would result in an additional branch if not done manually.
*
* @param[in] f A function.
* @param[in,out] s An initialized string.
* @param[in] input The input to forward the function.
*
* @warning Intended for internal use.
*
* @since 1.0.0
*/
#define RS_DATA_SIZE(f, s, input) \
do { \
if (rs_is_heap(input)) \
f(s, input->heap.buffer, rs_heap_len(input)); \
else \
f(s, input->stack.buffer, rs_stack_len(input)); \
} while (0)
/** @} */
/*
* ===============================================================
*
* CONSTRUCTION & DESTRUCTION
*
* ===============================================================
*/
/**
* @defgroup construction Construction & destruction
* Functions that initialize and free strings.
* @{
*/
/**
* @brief Initializes a string.
*
* @param[out] s The string to initialize.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_init(rapidstring *s);
/**
* @brief Initializes a string with a character array.
*
* @param[out] s A string to initialize.
* @param[in] input The input used to initialize @a s.
*
* @note Identicle to rs_init_w_n() with `strlen()`.
*
* @allocation When the legnth of @a input is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_init_w(rapidstring *s, const char *input);
/**
* @brief Initializes a string with a character array.
*
* @param[out] s A string to initialize.
* @param[in] input The input used to initialize @a s.
* @param[in] n The length of the input.
*
* @allocation When @a n is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_init_w_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Initializes a string with an initial capacity.
*
* @param[out] s A string to initialize.
* @param[in] n The new initial capacity of @a s.
*
* @allocation When @a n is greater than #RS_STACK_CAPACITY.
*
* @since 1.0.0
*/
RS_API void rs_init_w_cap(rapidstring *s, size_t n);
/**
* @brief Initializes a string with another string.
*
* @param[out] s A string to initialize.
* @param[in] input The input string used to initialize @a s.
*
* @allocation When the length of @a input is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_init_w_rs(rapidstring *s, const rapidstring *input);
/**
* @brief Frees a string.
*
* The string is in an invalid state after freeing. You must call rs_init() if
* you wish to reuse the same string.
*
* A jump may be avoided by directly calling `RS_FREE(s->heap.buffer)` if the
* string is known to be on the heap.
*
* Calling this fuction is unnecessary if the string size is always smaller or
* equal to #RS_STACK_CAPACITY.
*
* @param[in] s The string to free.
*
* @allocation Never.
*
* @since 1.0.0
*/
RS_API void rs_free(rapidstring *s);
/** @} */
/*
* ===============================================================
*
* COPYING
*
* ===============================================================
*/
/**
* @defgroup copying Copying
* Functions that copy data into a string.
* @{
*/
/**
* @brief Copies characters to a stack string.
*
* Overwrites any existing data.
*
* @param[in,out] s An initialized stack string.
* @param[in] input The input to assign to @a s.
* @param[in] n The length of @a input.
*
* @warning The input length must be smaller or equal to #RS_STACK_CAPACITY. If
* this is inconvenient for your usage, use rs_cpy().
*
* @allocation Never.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_stack_cpy_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Copies characters to a heap string.
*
* Overwrites any existing data.
*
* @param[in,out] s An initialized heap string.
* @param[in] input The input to assign to @a s.
* @param[in] n The length of @a input.
*
* @warning The input length must be smaller or equal to the capacity of @a s.
* If this is inconvenient for your usage, use rs_cpy().
*
* @allocation Never.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_heap_cpy_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Copies characters to a string.
*
* Overwrites any existing data.
*
* @param[in,out] s An initialized string.
* @param[in] input The input to assign to @a s.
*
* @note Identicle to rs_cpy_n() with `strlen()`.
*
* @allocation When the length of @a input is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_cpy(rapidstring *s, const char *input);
/**
* @brief Copies characters to a string.
*
* Overwrites any existing data.
*
* @param[in,out] s An initialized string.
* @param[in] input The input to assign to @a s.
* @param[in] n The length of @a input.
*
* @allocation When @a n is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_cpy_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Copies characters from a string to another string.
*
* Overwrites any existing data.
*
* @param[in,out] s An initialized string.
* @param[in] input The input string used to initialize @a s.
*
* @allocation When the length of @a input is greater than #RS_STACK_CAPACITY.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_cpy_rs(rapidstring *s, const rapidstring *input);
/** @} */
/*
* ===============================================================
*
* CAPACITY
*
* ===============================================================
*/
/**
* @defgroup capacity Capacity
* Functions related to the capacity of a string.
* @{
*/
/**
* @brief Checks whether a string is empty.
*
* @param[in] s An initialized string.
* @returns `1` if the string is empty, `0` otherwise.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API unsigned char rs_empty(const rapidstring *s);
/**
* @brief Returns the length of a stack string.
*
* @param[in] s An initialized stack string.
* @returns The stack string length.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API size_t rs_stack_len(const rapidstring *s);
/**
* @brief Returns the length of a heap string.
*
* @param[in] s An initialized heap string.
* @returns The heap string length.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API size_t rs_heap_len(const rapidstring *s);
/**
* @brief Returns the length.
*
* @param[in] s An initialized string.
* @returns The string length.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API size_t rs_len(const rapidstring *s);
/**
* @brief Returns the capacity.
*
* @param[in] s An initialized string.
* @returns The string capacity.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API size_t rs_cap(const rapidstring *s);
/**
* @brief Reserves capacity.
*
* @param[in,out] s An initialized string.
* @param[in] n The capacity to reserve.
*
* @allocation Always.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_reserve(rapidstring *s, size_t n);
/**
* @brief Frees all unused memory.
*
* @param[in,out] s An intialized string.
*
* @allocation Never.
*
* @since 1.0.0
*/
RS_API void rs_shrink_to_fit(rapidstring *s);
/**
* @brief Checks whether a string is on the heap.
*
* @param[in] s An initialized string.
* @returns `1` if @a s is on the heap, `0` otherwise.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API unsigned char rs_is_heap(const rapidstring *s);
/**
* @brief Checks whether a string is on the stack.
*
* @param[in] s An initialized string.
* @returns `1` if @a s is on the stack, `0` otherwise.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API unsigned char rs_is_stack(const rapidstring *s);
/** @} */
/*
* ===============================================================
*
* MODIFIERS
*
* ===============================================================
*/
/**
* @defgroup modifiers Modifiers
* Modifier functions such as concatenation, stealing and resizing.
* @{
*/
/**
* @brief Access the buffer.
*
* @param[in] s An initialized string.
* @returns The buffer.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API char *rs_data(rapidstring *s);
/**
* @brief Access the readonly buffer.
*
* @param[in] s An initialized string.
* @returns The readonly buffer.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API const char *rs_data_c(const rapidstring *s);
/**
* @brief Concatenates characters to a stack string.
*
* @param[in,out] s An initialized stack string.
* @param[in] input The input to concatenate.
* @param[in] n The length of @a input.
*
* @warning The input length must be smaller or equal to the remaining capacity
* of @a s. If this is inconvenient for your usage, use rs_cat().
*
* @allocation Never.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_stack_cat_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Concatenates characters to a heap string.
*
* @param[in,out] s An initialized heap string.
* @param[in] input The input to concatenate.
* @param[in] n The length of @a input.
*
* @warning The input length must be smaller or equal to the remaining capacity
* of @a s. If this is inconvenient for your usage, use rs_cat().
*
* @allocation Never.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_heap_cat_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Concatenates characters to a string.
*
* @param[in,out] s An initialized string.
* @param[in] input The input to concatenate.
*
* @note Identicle to rs_cat_n() with `strlen()`.
*
* @allocation When the length of @a input is greater than the remaining
* capacity of @a s.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_cat(rapidstring *s, const char *input);
/**
* @brief Concatenates characters to a string.
*
* @param[in,out] s An initialized string.
* @param[in] input The input to concatenate.
* @param[in] n The length of @a input.
*
* @allocation When @a n is greater than the remaining capacity of @a s.
*
* @complexity Linear in @a n.
*
* @since 1.0.0
*/
RS_API void rs_cat_n(rapidstring *s, const char *input, size_t n);
/**
* @brief Concatenates a string to another string.
*
* @param[in,out] s An initialized string.
* @param[in] input The input to concatenate.
*
* @allocation When the length of @a input is greater than the remaining
* capacity of @a s.
*
* @complexity Linear in the length of @a input.
*
* @since 1.0.0
*/
RS_API void rs_cat_rs(rapidstring *s, const rapidstring *input);
/**
* @brief Steals a buffer allocated on the heap.
*
* The buffer must either be allocated with RS_MALLOC() or RS_REALLOC(), or it
* must be manually freed.
*
* @param[out] s A string.
* @param[in] buffer The buffer to steal.
* @param[in] cap The capacity of @a buffer.
* @param[in] size The size of @a buffer.
*
* @note A null terminator will be written to the last element of the buffer.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_steal(rapidstring *s, char *buffer, size_t cap, size_t size);
/**
* @brief Removes the specified characters from a stack string.
*
* @param[in,out] s An initialized stack string.
* @param[in] index The index of the first character to remove.
* @param[in] n The number of characters to remove.
*
* @allocation Never.
*
* @complexity Linear in the length of @a s minus @a index.
*
* @since 1.0.0
*/
RS_API void rs_stack_erase(rapidstring *s, size_t index, size_t n);
/**
* @brief Removes the specified characters from a heap string.
*
* @param[in,out] s An initialized heap string.
* @param[in] index The index of the first character to remove.
* @param[in] n The number of characters to remove.
*
* @allocation Never.
*
* @complexity Linear in the length of @a s minus @a index.
*
* @since 1.0.0
*/
RS_API void rs_heap_erase(rapidstring *s, size_t index, size_t n);
/**
* @brief Removes the specified characters from a string.
*
* @param[in,out] s An initialized string.
* @param[in] index The index of the first character to remove.
* @param[in] n The number of characters to remove.
*
* @note If you wish to decrease the capacity of the string and keep the size
* the same, use rs_shrink_to_fit().
*
* @allocation Never.
*
* @complexity Linear in the length of @a s minus @a index.
*
* @since 1.0.0
*/
RS_API void rs_erase(rapidstring *s, size_t index, size_t n);
/**
* @brief Removes all characters from a stack string.
*
* @param[out] s An initialized stack string.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_stack_clear(rapidstring *s);
/**
* @brief Removes all characters from a heap string.
*
* @param[in,out] s An initialized heap string.
*
* The string will remain on the heap after being cleared. The capacity will
* remain the same.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_heap_clear(rapidstring *s);
/**
* @brief Removes all characters from a string.
*
* Heap strings will remain on the heap after being cleared. The capacity will
* remain the same.
*
* @param[in,out] s An initialized string.
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_clear(rapidstring *s);
/**
* @brief Resizes a stack string.
*
* @param[out] s A string.
* @param[in] n The new size.
*
* @warning The new size must be smaller or equal to #RS_STACK_CAPACITY. If this
* is inconvenient for your usage, use rs_resize().
*
* @allocation Never.
*
* @complexity Constant.
*
* @since 1.0.0
*/
RS_API void rs_stack_resize(rapidstring *s, size_t n);
/**
* @brief Resizes a heap string.