-
Notifications
You must be signed in to change notification settings - Fork 0
/
dstr.h
1235 lines (994 loc) · 33.3 KB
/
dstr.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
/*
SUMMARY:
A dynamic string library.
See end of file for license information.
Do this
#define DSTR_IMPLEMENATION
before you include this file in *one* C or C++ file to create the implementation.
strv.h should be defined before dstr.h
NOTES:
Various standard functions can be replaced such as:
#define DSTR_ASSERT(x) my_assert(x)
#define DSTR_MALLOC my_malloc
#define DSTR_FREE my_free
EXAMPLE:
#include "strv.h"
#include "dstr.h"
int main() {
dstr str;
dstr_init(&str)
dstr_append_str(&str, "Hello World");
printf("%s\n", str.data);
dstr_destroy(&str);
}
#define STRV_IMPLEMENTATION
#include "strv.h"
#define DSTR_IMPLEMENATION
#include "dstr.h"
*/
#ifndef RE_DSTR_H
#define RE_DSTR_H
#ifndef DSTR_API
#ifdef DSTR_STATIC
#define DSTR_API static
#else
#define DSTR_API extern
#endif
#endif
#ifndef DSTR_INTERNAL /* use for internal functions */
#define DSTR_INTERNAL static
#endif
#ifndef DSTR_MALLOC
#define DSTR_MALLOC malloc
#endif
#ifndef DSTR_FREE
#define DSTR_FREE free
#endif
#ifndef DSTR_MEMCPY
#define DSTR_MEMCPY memcpy
#endif
#ifndef DSTR_MEMMOVE
#define DSTR_MEMMOVE memmove
#endif
#ifndef DSTR_MEMCMP
#define DSTR_MEMCMP memcmp
#endif
#ifndef DSTR_MIN_ALLOC
#define DSTR_MIN_ALLOC 8
#endif
#define DSTR_MIN(a_, b_) ((a_) < (b_) ? (a_) : (b_))
#define DSTR_MAX(a_, b_) ((a_) < (b_) ? (b_) : (a_))
#ifndef DSTR_ASSERT
#define DSTR_ASSERT assert
#include <assert.h>
#endif
#ifndef DSTR_SIZE_T
#define DSTR_SIZE_T size_t
#endif
#ifndef DSTR_CHAR_T
#define DSTR_CHAR_T char
#endif
#include <string.h> /* strlen, memcpy, memmove, memset */
#include <stdlib.h> /* size_t malloc free */
#include <stdarg.h> /* ..., va_list */
#include <stdio.h>
#include <ctype.h> /* isspace */
#include <stddef.h> /* ptrdiff_t */
#ifdef __cplusplus
extern "C" {
#endif
typedef DSTR_SIZE_T dstr_size_t;
typedef DSTR_CHAR_T dstr_char_t;
typedef dstr_char_t* dstr_it;
typedef int dstr_bool;
static const dstr_size_t DSTR_NPOS = (dstr_size_t)-1;
/*-----------------------------------------------------------------------*/
/* dstr - API */
/*-----------------------------------------------------------------------*/
typedef struct dstr dstr;
struct dstr {
dstr_size_t size;
dstr_char_t* data;
dstr_size_t capacity; /* capacity is the number of char a string can hold, the null terminating char is not counted. */
dstr_size_t local_buffer_size; /* @TODO try if we can use capacity for this */
};
DSTR_API void dstr_init (dstr* s);
DSTR_API void dstr_destroy (dstr* s);
/* Non-owning reference with buffer. */
/* Another allocated buffer will be used if the capacity is reached */
/* NOTE: dstr_destroy should always be used to free the buffer in case the capacity is reached */
DSTR_API void dstr_init_from_local_buffer(dstr* s, dstr_size_t local_buffer_capacity);
/* Default constructor. Constructs empty string with dstr_init and return it. */
DSTR_API dstr dstr_make ();
/* Create an empty string with an initial capacity */
DSTR_API dstr dstr_make_reserve (dstr_size_t capacity);
/* Create a dstr from the data pointer and size */
DSTR_API dstr dstr_make_from (const dstr_char_t* data, dstr_size_t size);
/* overload of dstr_make_from */
DSTR_API dstr dstr_make_from_str (const dstr_char_t* str);
/* overload of dstr_make_from */
DSTR_API dstr dstr_make_from_char (dstr_char_t ch);
/* overload of dstr_make_from */
DSTR_API dstr dstr_make_from_strv (strv sv);
/* overload of dstr_make_from */
DSTR_API dstr dstr_make_from_dstr (const dstr* str);
/* Create with a char repeated N times */
DSTR_API dstr dstr_make_from_nchar (dstr_size_t count, dstr_char_t ch);
DSTR_API dstr dstr_make_from_fmt (const char* fmt, ...);
DSTR_API dstr dstr_make_from_vfmt (const char* fmt, va_list args);
DSTR_API strv dstr_to_strv(const dstr* s);
/* Use the strv counterpart */
DSTR_API int dstr_compare(const dstr* s, strv sv);
/* Use the strv counterpart */
DSTR_API int dstr_compare_str(const dstr* s, const dstr_char_t* str);
/* Use the strv counterpart */
DSTR_API int dstr_compare_dstr(const dstr* s, const dstr* str);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_equals(const dstr* s, strv sv);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_equals_str(const dstr* s, const dstr_char_t* other);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_equals_dstr(const dstr* s, const dstr* str);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_less_than(const dstr* s, strv sv);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_less_than_str(const dstr* s, const dstr_char_t* str);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_greater_than(const dstr* s, strv sv);
/* Use the strv counterpart */
DSTR_API dstr_bool dstr_greater_than_str(const dstr* s, const dstr_char_t* str);
/* Access character at index with bounds checking */
DSTR_API dstr_char_t dstr_at(const dstr* s, dstr_size_t index);
/* Returns a pointer to the first character of a string */
DSTR_API dstr_char_t* dstr_data(dstr* s);
/* Returns a c string */
DSTR_API dstr_char_t* dstr_c_str(dstr* s);
/* If new_cap is greater than the current capacity(), new storage is allocated, and capacity() is made equal or greater than new_cap. */
DSTR_API void dstr_reserve(dstr* s, dstr_size_t new_string_capacity);
/* Append data from data pointer and size */
DSTR_API void dstr_append (dstr* s, strv sv);
/* Overload of dstr_append */
DSTR_API void dstr_append_str (dstr* s, const dstr_char_t* str);
/* Overload of dstr_append */
DSTR_API void dstr_append_char (dstr* s, const dstr_char_t ch);
/* Overload of dstr_append */
DSTR_API void dstr_append_dstr (dstr* s, const dstr* dstr);
DSTR_API void dstr_append_nchar (dstr* s, dstr_size_t count, const dstr_char_t ch);
DSTR_API int dstr_append_fv (dstr* s, const char* fmt, va_list args);
DSTR_API int dstr_append_f (dstr* s, const char* fmt, ...);
/* append string at a certain index all content after index will be lost */
DSTR_API void dstr_append_from (dstr* s, int index, strv sv);
/* Overload of dstr_append_from */
DSTR_API void dstr_append_str_from (dstr* s, int index, const dstr_char_t* str);
/* Overload of dstr_append_from */
DSTR_API void dstr_append_char_from (dstr*s, int index, char ch);
/* Overload of dstr_append_from */
DSTR_API void dstr_append_dstr_from (dstr* s, int index, const dstr* str);
DSTR_API int dstr_append_from_fv (dstr* s, int index, const char* fmt, va_list args);
/* Equivalent to dstr_append_char */
DSTR_API void dstr_push_back(dstr* s, const dstr_char_t ch);
/* Replaces content with the content from a string defined the data pointer and the size */
DSTR_API void dstr_assign (dstr* s, strv sv);
/* Overload of dstr_assign */
DSTR_API void dstr_assign_str (dstr* s, const dstr_char_t* str);
/* Overload of dstr_assign */
DSTR_API void dstr_assign_char (dstr* s, dstr_char_t ch);
/* Overload of dstr_assign */
DSTR_API void dstr_assign_dstr (dstr* s, const dstr* str);
/* Overload of dstr_assign */
DSTR_API void dstr_assign_nchar (dstr* s, dstr_size_t count, dstr_char_t ch);
DSTR_API void dstr_assign_fv (dstr* s, const char* fmt, va_list args);
DSTR_API void dstr_assign_f (dstr* s, const char* fmt, ...);
DSTR_API void dstr_assign_fv_nogrow (dstr* s, const char* fmt, va_list args);
DSTR_API void dstr_assign_f_nogrow (dstr* s, const char* fmt, ...);
/* Reduces memory usage by freeing unused memory */
DSTR_API void dstr_shrink_to_fit(dstr* s);
DSTR_API int dstr_empty(const dstr* s);
DSTR_API dstr_size_t dstr_size(const dstr* s);
DSTR_API dstr_size_t dstr_length(const dstr* s);
DSTR_API dstr_size_t dstr_capacity(const dstr* s);
DSTR_API dstr_it dstr_begin(const dstr* s);
DSTR_API dstr_it dstr_end(const dstr* s);
DSTR_API dstr_it dstr_insert(dstr* s, const dstr_it index, strv sv);
DSTR_API dstr_it dstr_insert_str(dstr* s, const dstr_it index, const dstr_char_t* str);
DSTR_API dstr_it dstr_insert_char(dstr* s, const dstr_it index, const dstr_char_t value);
DSTR_API dstr_it dstr_insert_dstr(dstr* s, const dstr_it index, const dstr* str);
DSTR_API dstr_it dstr_erase(dstr* s, const dstr_it data, dstr_size_t size);
DSTR_API dstr_it dstr_erase_value(dstr* s, const dstr_it index);
DSTR_API dstr_it dstr_erase_at(dstr* s, dstr_size_t index);
/* Removes the last character from the string. */
DSTR_API void dstr_pop_back(dstr* s);
/* Same effect as resize(0). This does not free the allocated buffer. */
DSTR_API void dstr_clear(dstr* s);
/* Resizes the string to contain count characters. This does not free the allocated buffer. */
DSTR_API void dstr_resize (dstr* s, dstr_size_t size);
/* Resizes and fills extra spaces with 'ch' value */
DSTR_API void dstr_resize_fill (dstr* s, dstr_size_t size, dstr_char_t ch);
/* Replace the content between the range [index - (index + count)] with the content of 'replacing'. */
DSTR_API void dstr_replace_range (dstr* s, dstr_size_t index, dstr_size_t count, strv replacing);
DSTR_API void dstr_copy_to (const dstr* s, dstr* dest);
DSTR_API void dstr_swap (dstr* s, dstr* other);
/*-----------------------------------------------------------------------*/
/* dstr - Extended API */
/*-----------------------------------------------------------------------*/
DSTR_API strv dstr_trimmed (dstr* s);
DSTR_API void dstr_replace (dstr* s, strv to_replace, strv with);
DSTR_API void dstr_replace_dstr (dstr* s, const dstr* to_replaced, const dstr* with);
DSTR_API void dstr_replace_str (dstr* s, const dstr_char_t* to_replaced, const dstr_char_t* with);
#define DSTR_DEFINETYPE(TYPENAME, LOCAL_BUFFER_SIZE) \
typedef struct TYPENAME TYPENAME; \
struct TYPENAME { \
dstr dstr; \
char local_buffer[LOCAL_BUFFER_SIZE]; \
}; \
static inline void TYPENAME ## _init(struct TYPENAME* s) \
{ \
dstr_init_from_local_buffer(&s->dstr, LOCAL_BUFFER_SIZE); \
} \
static inline void TYPENAME ## _destroy(struct TYPENAME* s) \
{ \
dstr_destroy(&s->dstr); \
} \
static inline void TYPENAME ## _assign_fv(struct TYPENAME* s, const char* fmt, va_list args) \
{ \
dstr_assign_fv(&s->dstr, fmt, args); \
} \
static inline void TYPENAME ## _assign_f(struct TYPENAME* s, const char* fmt, ...) \
{ \
va_list args; \
va_start(args, fmt); \
dstr_assign_fv(&s->dstr, fmt, args); \
va_end(args); \
} \
static inline void TYPENAME ## _append_f(struct TYPENAME* s, const char* fmt, ...) \
{ \
va_list args; \
va_start(args, fmt); \
dstr_append_fv(&s->dstr, fmt, args); \
va_end(args); \
} \
static inline void TYPENAME ## _append_fv(struct TYPENAME* s, const char* fmt, va_list args) \
{ \
dstr_append_fv(&s->dstr, fmt, args); \
}
DSTR_DEFINETYPE(dstr16, 16);
DSTR_DEFINETYPE(dstr32, 32);
DSTR_DEFINETYPE(dstr64, 64);
DSTR_DEFINETYPE(dstr128, 128);
DSTR_DEFINETYPE(dstr256, 256);
DSTR_DEFINETYPE(dstr512, 512);
DSTR_DEFINETYPE(dstr1024, 1024);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* RE_DSTR_H */
/*-----------------------------------------------------------------------*/
/* dstr - Private */
/*-----------------------------------------------------------------------*/
DSTR_INTERNAL void dstr__reserve_no_preserve_data(dstr* s, dstr_size_t new_string_capacity);
DSTR_INTERNAL void dstr__reserve_internal(dstr* s, dstr_size_t new_string_capacity, dstr_bool preserve_data);
DSTR_INTERNAL int dstr__is_allocated (dstr* s);
DSTR_INTERNAL dstr_bool dstr__owns_local_buffer (dstr* s);
DSTR_INTERNAL dstr_char_t* dstr__get_local_buffer (dstr* s);
DSTR_INTERNAL dstr_bool dstr__is_using_local_buffer (dstr* s);
/*-----------------------------------------------------------------------*/
/* dstr - API Implementation */
/*-----------------------------------------------------------------------*/
#ifdef DSTR_IMPLEMENTATION
DSTR_INTERNAL dstr_size_t sizeof_nchar(int count) { return count * sizeof(dstr_char_t); }
/* Shared default value to ensure that s->data is always valid with a '\0' char when a dstr is initialized */
static dstr_char_t DSTR__DEFAULT_DATA[1] = { '\0' };
/* returns 150% of the capacity or use the DSTR_MIN_ALLOC value */
static int
dstr__get_new_capacity(dstr* s, dstr_size_t needed_size)
{
dstr_size_t minimum_size = DSTR_MAX(DSTR_MIN_ALLOC, s->capacity + (s->capacity / 2));
return DSTR_MAX(needed_size, minimum_size);
}
static void
dstr__grow_if_needed(dstr* s, dstr_size_t needed)
{
if (needed > s->capacity)
dstr_reserve(s, dstr__get_new_capacity(s, needed));
}
static void
dstr__grow_if_needed_discard(dstr* s, dstr_size_t needed)
{
if (needed > s->capacity)
dstr__reserve_no_preserve_data(s, dstr__get_new_capacity(s, needed));
}
DSTR_API void
dstr_init(dstr* s)
{
s->size = 0;
s->data = DSTR__DEFAULT_DATA;
s->capacity = 0;
s->local_buffer_size = 0;
}
DSTR_API void
dstr_destroy(dstr* s)
{
/* dstr is initialized */
if (dstr__is_allocated(s))
{
DSTR_FREE(s->data);
}
if (s->local_buffer_size)
{
s->data = dstr__get_local_buffer(s);
s->size = 0;
s->data[s->size] = '\0';
s->capacity = s->local_buffer_size - 1;
}
else
{
dstr_init(s);
}
}
DSTR_API void
dstr_init_from_local_buffer(dstr* s, dstr_size_t local_buffer_size)
{
s->data = dstr__get_local_buffer(s);
s->size = 0;
s->data[s->size] = '\0';
/* capacity is the number of char a string can hold(null terminating char is not counted),
* so the capacity is equal to buffer_size - 1 in this case
*/
s->capacity = local_buffer_size - 1;
s->local_buffer_size = local_buffer_size;
}
DSTR_API dstr
dstr_make()
{
dstr result;
dstr_init(&result);
return result;
}
DSTR_API dstr
dstr_make_reserve(dstr_size_t capacity) {
dstr result;
dstr_init(&result);
if (capacity) {
dstr_reserve(&result, capacity);
}
return result;
}
DSTR_API dstr
dstr_make_from(const dstr_char_t* data, dstr_size_t size)
{
return dstr_make_from_strv(strv_make_from(data, size));
}
DSTR_API dstr
dstr_make_from_str(const dstr_char_t* str)
{
return dstr_make_from_strv(strv_make_from_str(str));
}
DSTR_API dstr
dstr_make_from_char(dstr_char_t ch)
{
return dstr_make_from(&ch, 1);
}
DSTR_API dstr
dstr_make_from_strv(strv sv)
{
dstr result;
dstr_init(&result);
dstr_assign(&result, sv);
return result;
}
DSTR_API dstr
dstr_make_from_dstr(const dstr* s)
{
return dstr_make_from_strv(dstr_to_strv(s));
}
DSTR_API dstr
dstr_make_from_nchar(dstr_size_t count, dstr_char_t ch)
{
dstr result;
dstr_init(&result);
dstr_assign_nchar(&result, count, ch);
return result;
}
DSTR_API dstr
dstr_make_from_fmt(const char* fmt, ...)
{
dstr result;
dstr_init(&result);
va_list args;
va_start(args, fmt);
dstr_append_fv(&result, fmt, args);
va_end(args);
return result;
}
DSTR_API dstr
dstr_make_from_vfmt(const char* fmt, va_list args)
{
dstr result;
dstr_init(&result);
dstr_append_fv(&result, fmt, args);
return result;
}
DSTR_API strv
dstr_to_strv(const dstr* s)
{
return strv_make_from(s->data, s->size);
}
DSTR_API int
dstr_compare(const dstr* s, strv sv)
{
return strv_compare(dstr_to_strv(s), sv);
}
DSTR_API int
dstr_compare_str(const dstr* s, const dstr_char_t* str)
{
return strv_compare_str(dstr_to_strv(s), str);
}
DSTR_API int
dstr_compare_dstr(const dstr* s, const dstr* other)
{
return strv_compare(dstr_to_strv(s), dstr_to_strv(other));
}
DSTR_API dstr_bool
dstr_equals(const dstr* s, strv sv)
{
return strv_equals(dstr_to_strv(s), sv);
}
DSTR_API dstr_bool
dstr_equals_str(const dstr* s, const dstr_char_t* str)
{
return strv_equals_str(dstr_to_strv(s), str);
}
DSTR_API dstr_bool
dstr_equals_dstr(const dstr* s, const dstr* other)
{
return strv_equals(dstr_to_strv(s), dstr_to_strv(other));
}
DSTR_API dstr_bool
dstr_less_than(const dstr* s, strv sv)
{
return strv_less_than(dstr_to_strv(s), sv);
}
DSTR_API dstr_bool
dstr_less_than_str(const dstr* s, const dstr_char_t* str)
{
return strv_less_than_str(dstr_to_strv(s), str);
}
DSTR_API dstr_bool
dstr_greater_than(const dstr* s, strv sv)
{
return strv_greater_than(dstr_to_strv(s), sv);
}
DSTR_API dstr_bool
dstr_greater_than_str(const dstr* s, const dstr_char_t* str)
{
return strv_greater_than_str(dstr_to_strv(s), str);
}
DSTR_API dstr_char_t
dstr_at(const dstr* s, dstr_size_t index)
{
return s->data[index];
}
DSTR_API dstr_char_t*
dstr_data(dstr* s)
{
return s->data;
}
DSTR_API dstr_char_t*
dstr_c_str(dstr* s)
{
return s->data;
}
DSTR_API void
dstr_reserve(dstr* s, dstr_size_t new_string_capacity)
{
dstr_bool preserve_data = (dstr_bool)1;
dstr__reserve_internal(s, new_string_capacity, preserve_data);
}
DSTR_API void
dstr_append(dstr* s, strv sv)
{
dstr_append_from(s, s->size, sv);
}
DSTR_API void
dstr_append_str(dstr* s, const dstr_char_t* str)
{
dstr_append(s, strv_make_from_str(str));
}
DSTR_API void
dstr_append_char(dstr* s, const dstr_char_t ch)
{
dstr_append_char_from(s, s->size, ch);
}
DSTR_API void
dstr_append_dstr(dstr* s, const dstr* other)
{
dstr_append(s, dstr_to_strv(other));
}
DSTR_API void
dstr_append_nchar(dstr* s, dstr_size_t count, const dstr_char_t ch)
{
dstr_size_t capacity_needed = s->size + count;
dstr__grow_if_needed(s, capacity_needed);
dstr_char_t* first = s->data + s->size;
dstr_char_t* last = first + count;
for (; first != last; ++first) {
*first = ch;
}
s->size += count;
s->data[s->size] = '\0';
}
DSTR_API int
dstr_append_fv(dstr* s, const char* fmt, va_list args)
{
return dstr_append_from_fv(s, s->size, fmt, args);
}
DSTR_API int
dstr_append_f(dstr* s, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
int len = dstr_append_fv(s, fmt, args);
va_end(args);
return len;
}
DSTR_API void
dstr_append_from(dstr* s, int index, strv sv)
{
dstr__grow_if_needed(s, index + sv.size);
DSTR_MEMCPY(s->data + index, sv.data, sizeof_nchar(sv.size));
s->size = index + sv.size;
s->data[s->size] = '\0';
}
DSTR_API void
dstr_append_str_from(dstr* s, int index, const dstr_char_t* str)
{
dstr_append_from(s, index, strv_make_from_str(str));
}
DSTR_API void
dstr_append_char_from(dstr*s, int index, char c)
{
dstr_append_from(s, index, strv_make_from(&c, 1));
}
DSTR_API void
dstr_append_dstr_from(dstr* s, int index, const dstr* other)
{
dstr_append_from(s, index, dstr_to_strv(other));
}
DSTR_API int
dstr_append_from_fv(dstr* s, int index, const char* fmt, va_list args)
{
va_list args_copy;
va_copy(args_copy, args);
/* Caluclate necessary len */
int add_len = vsnprintf(NULL, 0, fmt, args_copy);
DSTR_ASSERT(add_len >= 0);
dstr__grow_if_needed(s, s->size + add_len + 1);
add_len = vsnprintf(s->data + index, add_len + 1, fmt, args);
s->size = index + add_len;
return add_len;
}
DSTR_API void
dstr_push_back(dstr* s, const dstr_char_t ch)
{
dstr_append_char(s, ch);
}
DSTR_API void
dstr_pop_back(dstr* s)
{
DSTR_ASSERT(s->size);
--(s->size);
s->data[s->size] = '\0';
}
DSTR_API void
dstr_assign(dstr* s, strv sv)
{
dstr_append_from(s, 0, sv);
}
DSTR_API void
dstr_assign_char(dstr* s, dstr_char_t ch)
{
dstr_assign(s, strv_make_from(&ch, 1));
}
DSTR_API void
dstr_assign_str(dstr* s, const dstr_char_t* str)
{
dstr_assign(s, strv_make_from_str(str));
}
DSTR_API void
dstr_assign_dstr(dstr* s, const dstr* other)
{
dstr_assign(s, dstr_to_strv(other));
}
DSTR_API void
dstr_assign_nchar(dstr* s, dstr_size_t count, dstr_char_t ch)
{
dstr_size_t size = count;
dstr_size_t capacity_needed = size;
dstr__grow_if_needed_discard(s, capacity_needed);
dstr_it first = s->data;
dstr_it last = s->data + count;
for (; first != last; ++first) {
*first = ch;
}
s->data[size] = '\0';
s->size = size;
}
DSTR_API void
dstr_assign_fv(dstr* s, const char* fmt, va_list args)
{
dstr_append_from_fv(s, 0, fmt, args);
}
DSTR_API void
dstr_assign_f(dstr* s, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
dstr_assign_fv(s, fmt, args);
va_end(args);
}
DSTR_API void
dstr_assign_fv_nogrow(dstr* s, const char* fmt, va_list args)
{
int size = vsnprintf(s->data, s->capacity + 1, fmt, args);
if (size == -1)
size = s->capacity;
s->size = size;
s->data[size] = 0;
}
DSTR_API void
dstr_assign_f_nogrow(dstr* s, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
dstr_assign_fv_nogrow(s, fmt, args);
va_end(args);
}
DSTR_API void
dstr_shrink_to_fit(dstr* s)
{
dstr_char_t * new_data;
dstr_size_t new_capacity;
if (dstr__owns_local_buffer(s)
&& s->size <= s->local_buffer_size - 1)
{
new_data = dstr__get_local_buffer(s);
new_capacity = s->local_buffer_size - 1; /* - 1 for '\0' char */
}
else
{
new_capacity = s->size;
new_data = (dstr_char_t*)DSTR_MALLOC(sizeof_nchar(s->size + 1)); /* +1 because we want to copy the '\0' */
}
DSTR_ASSERT(new_data);
DSTR_MEMCPY(new_data, s->data, sizeof_nchar(s->size + 1)); /* +1 because we want to copy the '\0' */
if (dstr__is_allocated(s))
DSTR_FREE(s->data);
s->data = new_data;
s->capacity = new_capacity;
}
DSTR_API int
dstr_empty(const dstr* s)
{
return !s->size;
}
DSTR_API dstr_size_t
dstr_size(const dstr* s)
{
return s->size;
}
DSTR_API dstr_size_t
dstr_length(const dstr* s)
{
return s->size;
}
DSTR_API dstr_size_t
dstr_capacity(const dstr* s)
{
return s->capacity;
}
DSTR_API dstr_it
dstr_begin(const dstr* s)
{
return strv_begin(dstr_to_strv(s));
}
DSTR_API dstr_it
dstr_end(const dstr* s)
{
return strv_end(dstr_to_strv(s));
}
DSTR_API dstr_it
dstr_insert(dstr* s, const dstr_it index, strv sv)
{
DSTR_ASSERT(index >= dstr_begin(s) && index <= dstr_end(s));
const dstr_size_t count = sv.size;
const ptrdiff_t offset = index - s->data;
const dstr_size_t distance_from_index_to_end = (dstr_size_t)(dstr_end(s) - index);
const dstr_size_t capacity_needed = s->size + count;
dstr__grow_if_needed(s, capacity_needed);
/* There is data between index and end to move */
if (distance_from_index_to_end > 0)
{
DSTR_MEMMOVE(
s->data + offset + sizeof_nchar(count),
s->data + offset,
distance_from_index_to_end
);
}
DSTR_MEMCPY(s->data + offset, sv.data, sizeof_nchar(count));
s->size += count;
s->data[s->size] = '\0';
return s->data + offset;
}
DSTR_API dstr_it
dstr_insert_str(dstr* s, const dstr_it index, const dstr_char_t* str)
{
return dstr_insert(s, index, strv_make_from_str(str));
}
DSTR_API dstr_it
dstr_insert_char(dstr* s, const dstr_it index, const dstr_char_t ch)
{
return dstr_insert(s, index, strv_make_from(&ch, 1));
}
DSTR_API dstr_it
dstr_insert_dstr(dstr* s, const dstr_it index, const dstr* other)
{
return dstr_insert(s, index, dstr_to_strv(other));
}
DSTR_API dstr_it
dstr_erase_ref(dstr* s, const dstr_it first, dstr_size_t count)
{
const dstr_it last = first + count;
DSTR_ASSERT(first >= s->data && first <= dstr_end(s));
DSTR_ASSERT(last >= s->data && last <= dstr_end(s));
const dstr_size_t first_index = (dstr_size_t)(first - s->data);
const dstr_size_t last_index = (dstr_size_t)(last - s->data);
const dstr_size_t count_to_move = (s->size - last_index) + 1; /* +1 for '\0' */
DSTR_MEMMOVE(s->data + first_index, s->data + last_index, sizeof_nchar(count_to_move));
s->size -= count;
return s->data + first_index;
}
DSTR_API dstr_it
dstr_erase(dstr* s, const dstr_it first, dstr_size_t size)
{
if (!size) return dstr_begin(s);
const dstr_it last = first + size;
DSTR_ASSERT(first >= dstr_begin(s) && first < dstr_end(s));
DSTR_ASSERT(last >= first && last <= dstr_end(s));
const dstr_size_t first_index = (dstr_size_t)(first - s->data);
const dstr_size_t last_index = (dstr_size_t)(last - s->data);
const dstr_size_t count_to_move = (dstr_size_t)(dstr_end(s) - last) + 1; /* +1 for '\0' */
DSTR_MEMMOVE(s->data + first_index, s->data + last_index, sizeof_nchar(count_to_move));
s->size -= size;
return s->data + first_index;
}
DSTR_API dstr_it
dstr_erase_value(dstr* s, const dstr_it index)
{
return dstr_erase(s, index, 1);
}
DSTR_API dstr_it
dstr_erase_at(dstr* s, dstr_size_t index)
{
dstr_char_t* value = s->data + index;
return dstr_erase_value(s, value);
}
DSTR_API void
dstr_clear(dstr* s)
{
dstr_resize(s, 0);
}
DSTR_API void
dstr_resize(dstr* s, dstr_size_t size)
{
if (s->size == size)
return;
dstr_size_t extra_count = 0;
if (size > s->capacity){
dstr__grow_if_needed(s, size);
extra_count = s->capacity - s->size;
} else if (size > s->size){
extra_count = size - s->size;
}
if (extra_count) {
memset(s->data + s->size, 0, sizeof_nchar(extra_count));
}
s->size = size;
s->data[s->size] = '\0';
}
DSTR_API void
dstr_resize_fill(dstr* s, dstr_size_t size, dstr_char_t ch)
{
if (!size)
{