-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_string.h
More file actions
2590 lines (2141 loc) · 77.6 KB
/
dynamic_string.h
File metadata and controls
2590 lines (2141 loc) · 77.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file dynamic_string.h
* @brief Modern, efficient, single-file string library for C
* @version 0.3.1
* @date Aug 29, 2025
*
* @details
* A modern, efficient, single-file string library for C featuring:
* - **Reference counting** with automatic memory management
* - **Immutable strings** for safety and sharing
* - **Copy-on-write StringBuilder** for efficient construction
* - **Unicode support** with UTF-8 storage and codepoint iteration
* - **Zero dependencies** - just drop in the .h file
* - **Direct C compatibility** - ds_string works with all C functions
*
* @section usage_section Usage
* @code{.c}
* #define DS_IMPLEMENTATION
* #include "dynamic_string.h"
*
* int main() {
* ds_string greeting = ds_new("Hello");
* ds_string full = ds_append(greeting, " World!");
* printf("%s\n", full); // Direct usage - no ds_cstr() needed!
* ds_release(&greeting);
* ds_release(&full);
* return 0;
* }
* @endcode
*
* @section license_section License
* Dual licensed under your choice of:
* - MIT License
* - The Unlicense (public domain)
*/
#ifndef DYNAMIC_STRING_H
#define DYNAMIC_STRING_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
// Configuration macros (user can override before including)
#ifndef DS_MALLOC
#define DS_MALLOC malloc
#endif
#ifndef DS_REALLOC
#define DS_REALLOC realloc
#endif
#ifndef DS_FREE
#define DS_FREE free
#endif
#ifndef DS_ASSERT
#include <assert.h>
#define DS_ASSERT assert
#endif
/**
* @brief Enable atomic reference counting (default: 0)
* @note Requires C11 and stdatomic.h support
* @note Only reference counting is atomic/thread-safe, not string operations
* @warning String modifications still require external synchronization
*/
#ifndef DS_ATOMIC_REFCOUNT
#define DS_ATOMIC_REFCOUNT 0
#endif
// API macros
#ifdef DS_STATIC
#define DS_DEF static
#else
#define DS_DEF extern
#endif
/* Check C11 support for atomic operations */
#if DS_ATOMIC_REFCOUNT && __STDC_VERSION__ < 201112L
#error "DS_ATOMIC_REFCOUNT requires C11 or later for atomic support (compile with -std=c11 or later)"
#endif
/* atomic operations */
#if DS_ATOMIC_REFCOUNT
#include <stdatomic.h>
#define DS_ATOMIC_SIZE_T _Atomic size_t
#define DS_ATOMIC_FETCH_ADD(ptr, val) atomic_fetch_add(ptr, val)
#define DS_ATOMIC_FETCH_SUB(ptr, val) atomic_fetch_sub(ptr, val)
#define DS_ATOMIC_LOAD(ptr) atomic_load(ptr)
#define DS_ATOMIC_STORE(ptr, val) atomic_store(ptr, val)
#else
#define DS_ATOMIC_SIZE_T size_t
#define DS_ATOMIC_FETCH_ADD(ptr, val) (*(ptr) += (val), *(ptr) - (val))
#define DS_ATOMIC_FETCH_SUB(ptr, val) (*(ptr) -= (val), *(ptr) + (val))
#define DS_ATOMIC_LOAD(ptr) (*(ptr))
#define DS_ATOMIC_STORE(ptr, val) (*(ptr) = (val))
#endif
#ifdef __cplusplus
extern "C" {
#endif
// ============================================================================
// INTERFACE
// ============================================================================
/**
* @brief String handle - points directly to null-terminated string data
*
* This is a char* that points directly to UTF-8 string data. Metadata
* (refcount, length) is stored at negative offsets before the string data.
* This allows ds_string to be used directly with all C string functions.
*
* Memory layout: [refcount|length|string_data|\0]
* ^
* ds_string points here
*
* @note Use directly with printf, strcmp, fopen, etc. - no conversion needed!
* @warning NULL ds_string parameters cause assertion failures - all functions require valid strings
*/
typedef char* ds_string;
/**
* @defgroup core_functions Core String Functions
* @brief Basic string creation, retention, and release functions
* @{
*/
/**
* @brief Create a new string from a C string
* @param text Null-terminated C string to copy (must not be NULL)
* @return New ds_string instance, or NULL on failure
* @since 0.0.1
*
* @code
* ds_string greeting = ds_new("Hello World");
* printf("%s\n", greeting); // Direct usage with C functions
* ds_release(&greeting);
* @endcode
*
* @see ds_create_length() for creating strings with explicit length
* @see ds_release() for memory cleanup
*/
DS_DEF ds_string ds_new(const char* text);
/**
* @brief Create a string from a buffer with explicit length
* @param text Source buffer (may contain embedded nulls)
* @param length Number of bytes to copy from buffer
* @return New ds_string instance, or NULL on failure
*/
DS_DEF ds_string ds_create_length(const char* text, size_t length);
/**
* @brief Increment reference count and return shared handle
* @param str String to retain (must not be NULL)
* @return New handle to the same string data
*/
DS_DEF ds_string ds_retain(ds_string str);
/**
* @brief Decrement reference count and free memory if last reference
* @param str Pointer to string handle to release (may be NULL)
*/
DS_DEF void ds_release(ds_string* str);
/** @} */
// String operations (return new strings - immutable)
/**
* @brief Append text to a string
* @param str Source string (must not be NULL)
* @param text Text to append (must not be NULL)
* @return New string with appended text, or NULL on failure
*/
DS_DEF ds_string ds_append(ds_string str, const char* text);
/**
* @brief Append a Unicode codepoint to a string
* @param str Source string (may be NULL)
* @param codepoint Unicode codepoint to append (invalid codepoints become U+FFFD)
* @return New string with appended text, retained original if text is NULL/empty, or NULL if allocation fails
*/
DS_DEF ds_string ds_append_char(ds_string str, uint32_t codepoint);
/**
* @brief Prepend text to the beginning of a string
* @param str Source string (may be NULL)
* @param text Text to prepend (may be NULL)
* @return New string with prepended text, or NULL on failure
*/
DS_DEF ds_string ds_prepend(ds_string str, const char* text);
/**
* @brief Insert text at a specific position in a string
* @param str Source string (may be NULL)
* @param index Byte position where to insert text (0-based)
* @param text Text to insert (may be NULL)
* @return New string with inserted text, or original string if index is invalid, or NULL on allocation failure
*/
DS_DEF ds_string ds_insert(ds_string str, size_t index, const char* text);
/**
* @brief Extract a substring from a string
* @param str Source string (may be NULL)
* @param start Starting byte position (0-based)
* @param len Number of bytes to include in substring
* @return New string containing the substring, or empty string if invalid range
*/
DS_DEF ds_string ds_substring(ds_string str, size_t start, size_t len);
// String concatenation
/**
* @brief Concatenate two strings
* @param a First string (may be NULL)
* @param b Second string (may be NULL)
* @return New string containing a + b, or NULL if both inputs are null
*/
DS_DEF ds_string ds_concat(ds_string a, ds_string b);
/**
* @brief Join multiple strings with a separator
* @param strings Array of ds_string to join (may contain NULL entries)
* @param count Number of strings in the array
* @param separator Separator to insert between strings (may be NULL)
* @return New string with all strings joined, or empty string if count is 0
*/
DS_DEF ds_string ds_join(ds_string* strings, size_t count, const char* separator);
// Utility functions (read-only)
/**
* @brief Get the length of a string in bytes
* @param str String to measure (must not be NULL)
* @return Length in bytes
*/
DS_DEF size_t ds_length(ds_string str);
/**
* @brief Compare two strings lexicographically
* @param a First string (must not be NULL)
* @param b Second string (must not be NULL)
* @return <0 if a < b, 0 if a == b, >0 if a > b
*/
DS_DEF int ds_compare(ds_string a, ds_string b);
/**
* @brief Compare two strings lexicographically (case-insensitive)
* @param a First string (may be NULL)
* @param b Second string (may be NULL)
* @return <0 if a < b, 0 if a == b, >0 if a > b
*/
DS_DEF int ds_compare_ignore_case(ds_string a, ds_string b);
/**
* @brief Calculate hash value for string
* @param str String to hash (may be NULL)
* @return Hash value (0 if str is NULL)
* @note Uses FNV-1a hash algorithm
*/
DS_DEF size_t ds_hash(ds_string str);
/**
* @brief Find the first occurrence of a substring
* @param str String to search in (may be NULL)
* @param needle Substring to search for (may be NULL)
* @return Index of first occurrence, or -1 if not found
*/
DS_DEF int ds_find(ds_string str, const char* needle);
/**
* @brief Find the last occurrence of a substring
* @param str String to search in (may be NULL)
* @param needle Substring to search for (may be NULL)
* @return Index of last occurrence, or -1 if not found
*/
DS_DEF int ds_find_last(ds_string str, const char* needle);
/**
* @brief Check if string contains a substring
* @param str String to search in (may be NULL)
* @param needle Substring to search for (may be NULL)
* @return 1 if found, 0 otherwise
*/
DS_DEF int ds_contains(ds_string str, const char* needle);
/**
* @brief Check if string starts with a prefix
* @param str String to check (may be NULL)
* @param prefix Prefix to look for (may be NULL)
* @return 1 if str starts with prefix, 0 otherwise
*/
DS_DEF int ds_starts_with(ds_string str, const char* prefix);
/**
* @brief Check if string ends with a suffix
* @param str String to check (may be NULL)
* @param suffix Suffix to look for (may be NULL)
* @return 1 if str ends with suffix, 0 otherwise
*/
DS_DEF int ds_ends_with(ds_string str, const char* suffix);
// String transformation functions
/**
* @brief Remove whitespace from both ends of a string
* @param str String to trim (may be NULL)
* @return New string with whitespace removed, or retained original if no trimming needed
* @since 0.0.2
*
* @code
* ds_string padded = ds_new(" hello world ");
* ds_string clean = ds_trim(padded);
* printf("'%s'\n", clean); // 'hello world'
* ds_release(&padded);
* ds_release(&clean);
* @endcode
*
* @see ds_trim_left() for trimming only leading whitespace
* @see ds_trim_right() for trimming only trailing whitespace
* @performance O(n) where n is string length
*/
DS_DEF ds_string ds_trim(ds_string str);
/**
* @brief Remove whitespace from the beginning of a string
* @param str String to trim (may be NULL)
* @return New string with leading whitespace removed, or retained original if no trimming needed
*/
DS_DEF ds_string ds_trim_left(ds_string str);
/**
* @brief Remove whitespace from the end of a string
* @param str String to trim (may be NULL)
* @return New string with trailing whitespace removed, or retained original if no trimming needed
*/
DS_DEF ds_string ds_trim_right(ds_string str);
// String replacement and manipulation
/**
* @brief Replace the first occurrence of a substring
* @param str Source string (may be NULL)
* @param old Substring to replace (may be NULL)
* @param new Replacement text (may be NULL)
* @return New string with first occurrence replaced, or retained original if no match found
*/
DS_DEF ds_string ds_replace(ds_string str, const char* old, const char* new);
/**
* @brief Replace all occurrences of a substring
* @param str Source string (may be NULL)
* @param old Substring to replace (may be NULL)
* @param new Replacement text (may be NULL)
* @return New string with all occurrences replaced, or retained original if no matches found
*/
DS_DEF ds_string ds_replace_all(ds_string str, const char* old, const char* new);
// Case transformation
/**
* @brief Convert string to uppercase
* @param str String to convert (may be NULL)
* @return New string in uppercase, or retained original if empty/NULL
*/
DS_DEF ds_string ds_to_upper(ds_string str);
/**
* @brief Convert string to lowercase
* @param str String to convert (may be NULL)
* @return New string in lowercase, or retained original if empty/NULL
*/
DS_DEF ds_string ds_to_lower(ds_string str);
// Utility transformations
/**
* @brief Repeat a string multiple times
* @param str String to repeat (may be NULL)
* @param times Number of repetitions
* @return New string with content repeated, or empty string if times is 0
*/
DS_DEF ds_string ds_repeat(ds_string str, size_t times);
/**
* @brief Truncate string to maximum length with optional ellipsis
* @param str String to truncate (may be NULL)
* @param max_length Maximum length in bytes (not including ellipsis)
* @param ellipsis Ellipsis string to append if truncated (may be NULL)
* @return New string truncated to max_length, or retained original if already short enough
*/
DS_DEF ds_string ds_truncate(ds_string str, size_t max_length, const char* ellipsis);
/**
* @brief Reverse a string (Unicode-aware)
* @param str String to reverse (may be NULL)
* @return New string with characters in reverse order, preserving Unicode codepoints
*/
DS_DEF ds_string ds_reverse(ds_string str);
/**
* @brief Pad string on the left to reach specified width
* @param str String to pad (may be NULL)
* @param width Target width in characters
* @param pad Character to use for padding
* @return New string padded to width, or retained original if already wide enough
*/
DS_DEF ds_string ds_pad_left(ds_string str, size_t width, char pad);
/**
* @brief Pad string on the right to reach specified width
* @param str String to pad (may be NULL)
* @param width Target width in characters
* @param pad Character to use for padding
* @return New string padded to width, or retained original if already wide enough
*/
DS_DEF ds_string ds_pad_right(ds_string str, size_t width, char pad);
// String splitting
/**
* @brief Split string into array by delimiter
* @param str String to split (may be NULL)
* @param delimiter Delimiter to split on (may be NULL)
* @param count Output parameter for number of parts (may be NULL)
* @return Allocated array of ds_string parts, or NULL on failure
* @since 0.0.2
*
* @warning Caller MUST call ds_free_split_result() to free the returned array
* @note Empty delimiter splits string into individual characters
* @note Consecutive delimiters create empty string parts
*
* @code
* ds_string text = ds_new("apple,banana,cherry");
* size_t count;
* ds_string* parts = ds_split(text, ",", &count);
*
* for (size_t i = 0; i < count; i++) {
* printf("Part %zu: %s\n", i, parts[i]);
* }
*
* ds_free_split_result(parts, count); // REQUIRED!
* ds_release(&text);
* @endcode
*
* @see ds_free_split_result() for proper cleanup
* @see ds_join() for the reverse operation
* @performance O(n) where n is string length
*/
DS_DEF ds_string* ds_split(ds_string str, const char* delimiter, size_t* count);
/**
* @brief Free the result array from ds_split()
* @param array Array returned by ds_split() (may be NULL)
* @param count Number of elements in array
*/
DS_DEF void ds_free_split_result(ds_string* array, size_t count);
// String formatting
/**
* @brief Create formatted string using printf-style format specifiers
* @param fmt Format string (may be NULL)
* @param ... Arguments for format specifiers
* @return New formatted string, or NULL if fmt is NULL or formatting fails
*/
DS_DEF ds_string ds_format(const char* fmt, ...);
/**
* @brief Create formatted string using printf-style format specifiers (va_list version)
* @param fmt Format string (may be NULL)
* @param args Variable argument list
* @return New formatted string, or NULL if fmt is NULL or formatting fails
*/
DS_DEF ds_string ds_format_v(const char* fmt, va_list args);
/**
* @brief Escape string for JSON
* @param str String to escape (may be NULL)
* @return New escaped string suitable for JSON, or NULL if str is NULL
*/
DS_DEF ds_string ds_escape_json(ds_string str);
/**
* @brief Unescape JSON string
* @param str JSON string to unescape (may be NULL)
* @return New unescaped string, or NULL if str is NULL or invalid JSON
*/
DS_DEF ds_string ds_unescape_json(ds_string str);
// Reference count inspection
/**
* @brief Get the current reference count of a string
* @param str String to inspect (may be NULL)
* @return Reference count, or 0 if str is NULL
*/
DS_DEF size_t ds_refcount(ds_string str);
/**
* @brief Check if a string has multiple references
* @param str String to check (may be NULL)
* @return 1 if shared (refcount > 1), 0 otherwise
*/
DS_DEF int ds_is_shared(ds_string str);
/**
* @brief Check if a string is empty
* @param str String to check (may be NULL)
* @return 1 if string is NULL or has zero length, 0 otherwise
*/
DS_DEF int ds_is_empty(ds_string str);
// Unicode codepoint iteration (Rust-style)
/**
* @brief Unicode codepoint iterator for UTF-8 strings
*/
typedef struct {
const char* data;
size_t pos; // Current byte position
size_t end; // End byte position
} ds_codepoint_iter;
/**
* @brief Create an iterator for Unicode codepoints in a string
* @param str String to iterate over (may be NULL)
* @return Iterator positioned at start of string
*/
DS_DEF ds_codepoint_iter ds_codepoints(ds_string str);
/**
* @brief Get the next Unicode codepoint from iterator
* @param iter Iterator to advance (must not be NULL)
* @return Next codepoint, or 0 if at end
*/
DS_DEF uint32_t ds_iter_next(ds_codepoint_iter* iter);
/**
* @brief Check if iterator has more codepoints
* @param iter Iterator to check (may be NULL)
* @return 1 if more codepoints available, 0 otherwise
*/
DS_DEF int ds_iter_has_next(const ds_codepoint_iter* iter);
// Unicode utility functions
/**
* @brief Count the number of Unicode codepoints in a string
* @param str String to count (may be NULL)
* @return Number of codepoints, or 0 if str is NULL
*/
DS_DEF size_t ds_codepoint_length(ds_string str);
/**
* @brief Get Unicode codepoint at specific index
* @param str String to access (may be NULL)
* @param index Codepoint index (0-based)
* @return Codepoint at index, or 0 if index is out of bounds
*/
DS_DEF uint32_t ds_codepoint_at(ds_string str, size_t index);
// Convenience macros for common operations
#define ds_empty() ds_new("")
#define ds_from_literal(lit) ds_new(lit)
// ============================================================================
// STRINGBUILDER - Mutable builder for efficient string construction
// ============================================================================
typedef struct ds_builder_struct {
ds_string data; // Points to string data (same layout as ds_string)
size_t capacity; // Capacity for growth (length is in metadata)
#ifdef DS_ATOMIC_REFCOUNT
_Atomic size_t refcount; // Atomic reference count
#else
size_t refcount; // Reference count
#endif
} *ds_builder;
/**
* @defgroup builder_core StringBuilder Core Functions
* @brief Core StringBuilder creation, management and basic operations
* @{
*/
/**
* @brief Create a new StringBuilder with default capacity
* @return New StringBuilder instance, NULL on allocation failure
* @since 0.3.0
*
* Creates a new StringBuilder with the default initial capacity.
* The StringBuilder must be released with ds_builder_release().
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello");
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_create_with_capacity() for custom capacity
* @see ds_builder_release() for cleanup
*/
DS_DEF ds_builder ds_builder_create(void);
/**
* @brief Create a new StringBuilder with specified capacity
* @param capacity Initial capacity in bytes (minimum of 16 bytes)
* @return New StringBuilder instance, NULL on allocation failure
* @since 0.3.0
*
* Creates a new StringBuilder with the specified initial capacity.
* If capacity is 0, uses the default initial capacity.
*
* @code
* ds_builder sb = ds_builder_create_with_capacity(100);
* // StringBuilder can hold 100 bytes before reallocation
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_create() for default capacity
*/
DS_DEF ds_builder ds_builder_create_with_capacity(size_t capacity);
/**
* @brief Increment the reference count of a StringBuilder
* @param sb StringBuilder to retain (must not be NULL)
* @return The same StringBuilder instance
* @since 0.3.0
*
* Increases the reference count, allowing the StringBuilder to be
* safely shared between multiple owners.
*
* @code
* ds_builder original = ds_builder_create();
* ds_builder shared = ds_builder_retain(original);
* // Both original and shared point to the same StringBuilder
* ds_builder_release(&original);
* ds_builder_release(&shared);
* @endcode
*
* @see ds_builder_release() for decrementing reference count
*/
DS_DEF ds_builder ds_builder_retain(ds_builder sb);
/**
* @brief Decrement reference count and release StringBuilder when it reaches zero
* @param sb Pointer to StringBuilder (set to NULL after release)
* @since 0.3.0
*
* Decrements the reference count and frees the StringBuilder when the
* count reaches zero. The pointer is set to NULL after release.
* Safe to call with NULL or already-released StringBuilder.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_release(&sb);
* // sb is now NULL
* ds_builder_release(&sb); // Safe - does nothing
* @endcode
*
* @see ds_builder_retain() for incrementing reference count
*/
DS_DEF void ds_builder_release(ds_builder* sb);
/**
* @brief Append null-terminated text to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param text Text to append (must not be NULL)
* @return 1 on success, 0 on failure
* @since 0.3.0
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello");
* ds_builder_append(sb, " World");
* // sb now contains "Hello World"
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_append_length() for partial string appending
* @see ds_builder_append_string() for appending ds_string
*/
DS_DEF int ds_builder_append(ds_builder sb, const char* text);
/**
* @brief Append a Unicode codepoint to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param codepoint Unicode codepoint to append
* @return 1 on success, 0 on failure
* @since 0.3.0
*
* Appends the codepoint as UTF-8 encoded bytes. Invalid codepoints
* (>= 0x110000) are replaced with the Unicode replacement character.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_char(sb, 0x1F30D); // 🌍 emoji
* ds_builder_append_char(sb, 0x41); // 'A'
* ds_builder_release(&sb);
* @endcode
*
* @see ds_append_char() for immutable string version
*/
DS_DEF int ds_builder_append_char(ds_builder sb, uint32_t codepoint);
/**
* @brief Append a ds_string to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param str ds_string to append (must not be NULL)
* @return 1 on success, 0 on failure
* @since 0.3.0
*
* @code
* ds_builder sb = ds_builder_create();
* ds_string greeting = ds_new("Hello");
* ds_builder_append_string(sb, greeting);
* ds_builder_append(sb, " World");
* ds_release(&greeting);
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_append() for C string appending
*/
DS_DEF int ds_builder_append_string(ds_builder sb, ds_string str);
/**
* @brief Insert text at specific position in StringBuilder
* @param sb StringBuilder to modify (must not be NULL)
* @param index Position to insert at (0-based)
* @param text Text to insert (must not be NULL)
* @return 1 on success, 0 on failure
* @since 0.3.0
*
* Inserts text at the specified position. If index is beyond the string
* length, the text is appended at the end.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello World");
* ds_builder_insert(sb, 6, "Beautiful ");
* // sb now contains "Hello Beautiful World"
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_append() for appending at end
* @see ds_builder_prepend() for inserting at beginning
*/
DS_DEF int ds_builder_insert(ds_builder sb, size_t index, const char* text);
/**
* @brief Clear all content from StringBuilder
* @param sb StringBuilder to clear (must not be NULL)
* @since 0.3.0
*
* Removes all content from the StringBuilder but preserves the allocated
* capacity for reuse.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello World");
* ds_builder_clear(sb);
* // sb is now empty but capacity is preserved
* ds_builder_append(sb, "New content");
* ds_builder_release(&sb);
* @endcode
*/
DS_DEF void ds_builder_clear(ds_builder sb);
/** @} */
/**
* @defgroup builder_formatting StringBuilder Formatting Functions
* @brief Functions for formatted text appending
* @{
*/
/**
* @brief Append formatted text to StringBuilder using printf-style formatting
* @param sb StringBuilder to append to (must not be NULL)
* @param fmt Format string with printf-style specifiers (must not be NULL)
* @param ... Arguments for format specifiers
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_format(sb, "User: %s, Age: %d", "Alice", 25);
* // sb now contains "User: Alice, Age: 25"
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_append_format_v() for va_list version
* @see ds_format() for creating formatted immutable strings
*/
DS_DEF int ds_builder_append_format(ds_builder sb, const char* fmt, ...);
/**
* @brief Append formatted text to StringBuilder using va_list
* @param sb StringBuilder to append to (must not be NULL)
* @param fmt Format string with printf-style specifiers (must not be NULL)
* @param args Variable argument list
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* This is the va_list version of ds_builder_append_format(), useful for
* creating wrapper functions that accept variable arguments.
*
* @see ds_builder_append_format() for the variadic version
*/
DS_DEF int ds_builder_append_format_v(ds_builder sb, const char* fmt, va_list args);
/** @} */
/**
* @defgroup builder_numeric StringBuilder Numeric Functions
* @brief Functions for appending numeric values
* @{
*/
/**
* @brief Append an integer value to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param value Integer value to append
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_int(sb, -42);
* // sb now contains "-42"
* ds_builder_release(&sb);
* @endcode
*/
DS_DEF int ds_builder_append_int(ds_builder sb, int value);
/**
* @brief Append an unsigned integer value to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param value Unsigned integer value to append
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_uint(sb, 42u);
* // sb now contains "42"
* ds_builder_release(&sb);
* @endcode
*/
DS_DEF int ds_builder_append_uint(ds_builder sb, unsigned int value);
/**
* @brief Append a long integer value to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param value Long integer value to append
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_long(sb, -123456789L);
* // sb now contains "-123456789"
* ds_builder_release(&sb);
* @endcode
*/
DS_DEF int ds_builder_append_long(ds_builder sb, long value);
/**
* @brief Append a double value to StringBuilder with specified precision
* @param sb StringBuilder to append to (must not be NULL)
* @param value Double value to append
* @param precision Number of decimal places (negative values default to 6)
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_double(sb, 3.14159, 2);
* // sb now contains "3.14"
* ds_builder_append_double(sb, 2.71828, -1); // Uses default precision (6)
* // sb now contains "3.142.718280"
* ds_builder_release(&sb);
* @endcode
*/
DS_DEF int ds_builder_append_double(ds_builder sb, double value, int precision);
/** @} */
/**
* @defgroup builder_buffer StringBuilder Buffer Operations
* @brief Functions for buffer-based string operations
* @{
*/
/**
* @brief Append a specific number of bytes from text to StringBuilder
* @param sb StringBuilder to append to (must not be NULL)
* @param text Source text buffer (must not be NULL)
* @param length Number of bytes to append from text
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* This function allows appending a portion of a string, which is useful
* for working with buffers or when you don't want the entire string.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append_length(sb, "Hello World", 5);
* // sb now contains "Hello"
* ds_builder_release(&sb);
* @endcode
*
* @see ds_builder_append() for null-terminated string appending
*/
DS_DEF int ds_builder_append_length(ds_builder sb, const char* text, size_t length);
/**
* @brief Prepend text to the beginning of StringBuilder
* @param sb StringBuilder to prepend to (must not be NULL)
* @param text Text to prepend (must not be NULL)
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "World");
* ds_builder_prepend(sb, "Hello ");
* // sb now contains "Hello World"
* ds_builder_release(&sb);
* @endcode
*
* @note This operation requires moving existing content, so it's less efficient
* than append operations for large strings
*/
DS_DEF int ds_builder_prepend(ds_builder sb, const char* text);
/**
* @brief Replace a range of characters in StringBuilder with new text
* @param sb StringBuilder to modify (must not be NULL)
* @param start Starting position of range to replace (0-based)
* @param end Ending position of range to replace (exclusive, 0-based)
* @param replacement Text to insert in place of the range (must not be NULL)
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* Replaces characters from position `start` up to (but not including) position `end`
* with the replacement text. The replacement can be shorter, same length, or longer
* than the original range.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello World");
* ds_builder_replace_range(sb, 6, 11, "Universe"); // Replace "World"
* // sb now contains "Hello Universe"
* ds_builder_release(&sb);
* @endcode
*
* @note If start > end, the values are swapped
* @note Positions beyond string length are clamped to string length
*/
DS_DEF int ds_builder_replace_range(ds_builder sb, size_t start, size_t end, const char* replacement);
/** @} */
/**
* @defgroup builder_manipulation StringBuilder Content Manipulation
* @brief Functions for modifying StringBuilder content
* @{
*/
/**
* @brief Remove a range of characters from StringBuilder
* @param sb StringBuilder to modify (must not be NULL)
* @param start Starting position of range to remove (0-based)
* @param length Number of characters to remove
* @return 1 on success, 0 on failure
* @since 0.3.1
*
* Removes `length` characters starting from position `start`. If the range
* extends beyond the string, only characters up to the end are removed.
*
* @code
* ds_builder sb = ds_builder_create();
* ds_builder_append(sb, "Hello Beautiful World");
* ds_builder_remove_range(sb, 6, 10); // Remove "Beautiful "
* // sb now contains "Hello World"
* ds_builder_release(&sb);
* @endcode
*
* @note If start is beyond string length, no characters are removed
* @note If length is 0, no characters are removed
*/
DS_DEF int ds_builder_remove_range(ds_builder sb, size_t start, size_t length);
/** @} */
/**
* @defgroup builder_conversion StringBuilder Conversion Functions
* @brief Functions for converting StringBuilder to immutable strings
* @{
*/