-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathstrbuf.c
820 lines (705 loc) · 19.4 KB
/
strbuf.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
/*
*/
#include <stdint.h>
#include <ctype.h>
#include <limits.h>
#include "strview.h"
#include "strbuf.h"
#ifdef STRBUF_PROVIDE_PRINTF
#include <stdio.h>
#endif
#ifdef STRBUF_PROVIDE_PRNF
#include "prnf.h"
#endif
#ifdef STRBUF_DEFAULT_ALLOCATOR_STDLIB
#include <stdlib.h>
#endif
#ifdef STRBUF_ASSERT_DEFAULT_ALLOCATOR_STDLIB
#include <assert.h>
#endif
//********************************************************************************************************
// Local defines
//********************************************************************************************************
// #include <stdio.h>
// #define DBG(_fmtarg, ...) printf("%s:%.4i - "_fmtarg"\n" , __FILE__, __LINE__ ,##__VA_ARGS__)
//********************************************************************************************************
// Private prototypes
//********************************************************************************************************
static strbuf_t* create_buf(int initial_capacity, strbuf_allocator_t allocator);
static strview_t buffer_vcat(strbuf_t** buf_ptr, int n_args, va_list va);
static void insert_strview_into_buf(strbuf_t** buf_ptr, int index, strview_t str);
static void destroy_buf(strbuf_t** buf_ptr);
static void change_buf_capacity(strbuf_t** buf_ptr, int new_capacity);
static void assign_strview_to_buf(strbuf_t** buf_ptr, strview_t str);
static void append_char_to_buf(strbuf_t** strbuf, char c);
static int round_up_capacity(int capacity);
static strview_t strview_of_buf(strbuf_t* buf);
static bool buf_contains_str(strbuf_t* buf, strview_t str);
static bool buf_is_dynamic(strbuf_t* buf);
static void empty_buf(strbuf_t* buf);
static bool add_will_overflow_int(int a, int b);
static bool view_contains_char(strview_t view, char c);
#ifdef STRBUF_PROVIDE_PRNF
static void char_handler_for_prnf(void* dst, char c);
#endif
#ifdef STRBUF_DEFAULT_ALLOCATOR_STDLIB
static void* allocfunc_stdlib(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size);
#endif
//********************************************************************************************************
// Public functions
//********************************************************************************************************
#ifdef STRBUF_DEFAULT_ALLOCATOR_STDLIB
static void* allocfunc_stdlib(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size)
{
(void)this_allocator;
void* result = NULL;
if(size == 0)
free(ptr_to_free);
else
result = realloc(ptr_to_free, size);
#ifdef STRBUF_ASSERT_DEFAULT_ALLOCATOR_STDLIB
assert(size==0 || result);
#endif
return result;
}
strbuf_allocator_t strbuf_default_allocator = (strbuf_allocator_t){.allocator=allocfunc_stdlib, .app_data=NULL};
#else
#pragma weak strbuf_default_allocator
strbuf_allocator_t strbuf_default_allocator = (strbuf_allocator_t){.allocator=NULL, .app_data=NULL};
#endif
strbuf_t* strbuf_create_empty(size_t initial_capacity, strbuf_allocator_t* allocator)
{
strbuf_t* result;
if(!allocator)
allocator = &strbuf_default_allocator;
if(allocator->allocator && initial_capacity <= INT_MAX)
result = create_buf((int)initial_capacity, *allocator);
else
result = NULL;
return result;
}
strbuf_t* strbuf_create_init(strview_t initial_content, strbuf_allocator_t* allocator)
{
strbuf_t* result;
if(!allocator)
allocator = &strbuf_default_allocator;
if(allocator->allocator)
{
result = create_buf(initial_content.size, *allocator);
insert_strview_into_buf(&result, 0, initial_content);
}
else
result = NULL;
return result;
}
strbuf_t* strbuf_create_fixed(void* addr, size_t addr_size)
{
strbuf_t* result = NULL;
intptr_t alignment_mask;
size_t capacity;
if(addr_size > sizeof(strbuf_t) && addr)
{
capacity = addr_size - sizeof(strbuf_t) - 1;
//check alignment
alignment_mask = sizeof(void*)-1;
alignment_mask &= (intptr_t)addr;
if(alignment_mask == 0)
{
result = addr;
result->allocator.app_data = NULL;
result->allocator.allocator = NULL;
result->capacity = capacity <= INT_MAX ? capacity:INT_MAX;
result->size = 0;
empty_buf(result);
};
};
return result;
}
// concatenate a number of str's this can include the buffer itself, strbuf.str for appending
strview_t _strbuf_cat(strbuf_t** buf_ptr, int n_args, ...)
{
va_list va;
va_start(va, n_args);
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
str = buffer_vcat(buf_ptr, n_args, va);
va_end(va);
return str;
}
strview_t strbuf_vcat(strbuf_t** buf_ptr, int n_args, va_list va)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
str = buffer_vcat(buf_ptr, n_args, va);
return str;
}
#ifdef STRBUF_PROVIDE_PRINTF
strview_t strbuf_printf(strbuf_t** buf_ptr, const char* format, ...)
{
va_list va;
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
va_start(va, format);
str = strbuf_vprintf(buf_ptr, format, va);
va_end(va);
};
return str;
}
strview_t strbuf_vprintf(strbuf_t** buf_ptr, const char* format, va_list va)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
empty_buf(*buf_ptr);
str = strbuf_append_vprintf(buf_ptr, format, va);
};
return str;
}
strview_t strbuf_append_printf(strbuf_t** buf_ptr, const char* format, ...)
{
va_list va;
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
va_start(va, format);
str = strbuf_append_vprintf(buf_ptr, format, va);
va_end(va);
};
return str;
}
strview_t strbuf_append_vprintf(strbuf_t** buf_ptr, const char* format, va_list va)
{
int size;
int append_size;
bool failed;
strbuf_t* buf;
strview_t str = STRVIEW_INVALID;
va_list vb;
if(buf_ptr && *buf_ptr)
{
va_copy(vb, va);
buf = *buf_ptr;
size = buf->size;
append_size = vsnprintf(NULL, 0, format, va);
failed = add_will_overflow_int(size, append_size);
if(!failed)
{
size += append_size;
if(buf_is_dynamic(buf) && size > buf->capacity)
change_buf_capacity(&buf, round_up_capacity(size));
failed = size > buf->capacity;
};
if(!failed)
buf->size += vsnprintf(&buf->cstr[buf->size], buf->capacity - buf->size + 1, format, vb);
else
empty_buf(buf);
str = strbuf_view(&buf);
*buf_ptr = buf;
va_end(vb);
};
return str;
}
#endif
#ifdef STRBUF_PROVIDE_PRNF
strview_t strbuf_prnf(strbuf_t** buf_ptr, const char* format, ...)
{
va_list va;
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
va_start(va, format);
str = strbuf_vprnf(buf_ptr, format, va);
va_end(va);;
};
return str;
}
strview_t strbuf_vprnf(strbuf_t** buf_ptr, const char* format, va_list va)
{
strbuf_t* buf;
strview_t str = STRVIEW_INVALID;
int char_count;
if(buf_ptr && *buf_ptr)
{
buf = *buf_ptr;
empty_buf(buf);
char_count = vfptrprnf(char_handler_for_prnf, &buf, format, va);
if(char_count > buf->size)
empty_buf(buf);
str = strbuf_view(&buf);
*buf_ptr = buf;
};
return str;
}
strview_t strbuf_append_prnf(strbuf_t** buf_ptr, const char* format, ...)
{
va_list va;
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
va_start(va, format);
str = strbuf_append_vprnf(buf_ptr, format, va);
va_end(va);;
};
return str;
}
strview_t strbuf_append_vprnf(strbuf_t** buf_ptr, const char* format, va_list va)
{
strbuf_t* buf;
strview_t str = STRVIEW_INVALID;
int char_count;
if(buf_ptr && *buf_ptr)
{
buf = *buf_ptr;
char_count = buf->size;
char_count += vfptrprnf(char_handler_for_prnf, &buf, format, va);
if(char_count > buf->size || char_count < 0)
empty_buf(buf);
str = strbuf_view(&buf);
*buf_ptr = buf;
};
return str;
}
#endif
strview_t strbuf_view(strbuf_t** buf_ptr)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
str = strview_of_buf(*buf_ptr);
return str;
}
strview_t strbuf_append_char(strbuf_t** buf_ptr, char c)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
append_char_to_buf(buf_ptr, c);
str = strview_of_buf(*buf_ptr);
};
return str;
}
// reduce allocation size to the minimum possible
strview_t strbuf_shrink(strbuf_t** buf_ptr)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
if(buf_is_dynamic(*buf_ptr))
change_buf_capacity(buf_ptr, (*buf_ptr)->size);
str = strview_of_buf(*buf_ptr);
};
return str;
}
// increase allocation size to support a capacity of at least min_size
strview_t strbuf_grow(strbuf_t** buf_ptr, int min_size)
{
strview_t str = STRVIEW_INVALID;
if(buf_ptr && *buf_ptr)
{
if(buf_is_dynamic(*buf_ptr))
{
if(min_size > (*buf_ptr)->capacity)
change_buf_capacity(buf_ptr, min_size);
str = strview_of_buf(*buf_ptr);
};
};
return str;
}
void strbuf_destroy(strbuf_t** buf_ptr)
{
if(buf_ptr)
{
if(*buf_ptr && buf_is_dynamic(*buf_ptr))
destroy_buf(buf_ptr);
*buf_ptr = NULL;
};
}
char* strbuf_to_cstr(strbuf_t** buf_ptr)
{
int len;
char* str = NULL;
strbuf_allocator_t allocator;
if(buf_ptr && *buf_ptr)
{
len = (*buf_ptr)->size;
if(buf_is_dynamic(*buf_ptr))
{
allocator = (*buf_ptr)->allocator;
str = (void*)(*buf_ptr);
memmove(str, (*buf_ptr)->cstr, len);
str = allocator.allocator(&allocator, str, len+1);
}
else
str = (*buf_ptr)->cstr;
str[len] = 0;
*buf_ptr = NULL;
};
return str;
}
strview_t strbuf_assign(strbuf_t** buf_ptr, strview_t str)
{
strbuf_t* buf = NULL;
bool failed;
if(buf_ptr && *buf_ptr)
{
buf = *buf_ptr;
failed = !strview_is_valid(str);
if(!failed)
{
if(str.size > buf->capacity && buf_is_dynamic(buf))
change_buf_capacity(&buf, round_up_capacity(str.size));
failed = str.size > buf->capacity;
};
if(!failed)
{
memmove(buf->cstr, str.data, (size_t)str.size);
buf->size = str.size;
buf->cstr[buf->size] = 0;
}
else
empty_buf(buf);
*buf_ptr = buf;
};
return strview_of_buf(buf);
}
strview_t strbuf_append_strview(strbuf_t** buf_ptr, strview_t str)
{
if(buf_ptr && *buf_ptr)
insert_strview_into_buf(buf_ptr, (*buf_ptr)->size, str);
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_append_cstr(strbuf_t** buf_ptr, const char* str)
{
return strbuf_append_strview(buf_ptr, cstr(str));
}
strview_t strbuf_append_using(strbuf_t** buf_ptr, int (*strbuf_fetcher)(void* dst, int dst_size, void* fetcher_vars), void* fetch_vars)
{
strbuf_t* buf;
int available_space;
int bytes_appended;
bool fetch_fault = true;
if(buf_ptr && *buf_ptr)
{
buf = *buf_ptr;
available_space = buf->capacity - buf->size;
bytes_appended = strbuf_fetcher(&buf->cstr[buf->size], available_space, fetch_vars);
fetch_fault = bytes_appended < 0 || bytes_appended > available_space;
if(fetch_fault)
empty_buf(buf);
else
{
buf->size += bytes_appended;
buf->cstr[buf->size] = 0;
};
*buf_ptr = buf;
};
return fetch_fault ? STRVIEW_INVALID : strview_of_buf(*buf_ptr);
}
strview_t strbuf_prepend_strview(strbuf_t** buf_ptr, strview_t str)
{
if(buf_ptr && *buf_ptr)
insert_strview_into_buf(buf_ptr, 0, str);
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_prepend_cstr(strbuf_t** buf_ptr, const char* str)
{
return strbuf_prepend_strview(buf_ptr, cstr(str));
}
strview_t strbuf_insert_at_index_strview(strbuf_t** buf_ptr, int index, strview_t str)
{
if(buf_ptr && *buf_ptr)
insert_strview_into_buf(buf_ptr, index, str);
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_insert_at_index_cstr(strbuf_t** buf_ptr, int index, const char* str)
{
return strbuf_insert_at_index_strview(buf_ptr, index, cstr(str));
}
strview_t strbuf_insert_before_strview(strbuf_t** buf_ptr, strview_t dst, strview_t src)
{
strbuf_t* buf;
if(buf_ptr && *buf_ptr)
{
buf = *buf_ptr;
if(buf->cstr <= dst.data && dst.data <= &buf->cstr[buf->size])
insert_strview_into_buf(&buf, dst.data - buf->cstr, src);
*buf_ptr = buf;
};
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_insert_before_cstr(strbuf_t** buf_ptr, strview_t dst, const char* src)
{
return strbuf_insert_before_strview(buf_ptr, dst, cstr(src));
}
strview_t strbuf_insert_after_strview(strbuf_t** buf_ptr, strview_t dst, strview_t src)
{
strbuf_t* buf;
const char* dst_ptr;
if(buf_ptr && *buf_ptr && strview_is_valid(dst))
{
buf = *buf_ptr;
dst_ptr = &dst.data[dst.size];
if(buf->cstr <= dst_ptr && dst_ptr <= &buf->cstr[buf->size])
insert_strview_into_buf(&buf, dst_ptr - buf->cstr, src);
*buf_ptr = buf;
};
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_insert_after_cstr(strbuf_t** buf_ptr, strview_t dst, const char* src)
{
return strbuf_insert_after_strview(buf_ptr, dst, cstr(src));
}
strview_t strbuf_strip_strview(strbuf_t** buf_ptr, strview_t stripchars)
{
strbuf_t* buf;
char* ptr;
int count;
if(buf_ptr && *buf_ptr && strview_is_valid(stripchars))
{
buf = *buf_ptr;
count = buf->size;
ptr = buf->cstr;
while(count)
{
if(view_contains_char(stripchars, *ptr))
{
memmove(ptr, ptr+1, count);
buf->size--;
}
else
ptr++;
count--;
};
*buf_ptr = buf;
};
return buf_ptr ? strview_of_buf(*buf_ptr) : STRVIEW_INVALID;
}
strview_t strbuf_strip_cstr(strbuf_t** buf_ptr, const char* stripchars)
{
return strbuf_strip_strview(buf_ptr, cstr(stripchars));
}
//********************************************************************************************************
// Private functions
//********************************************************************************************************
static strbuf_t* create_buf(int initial_capacity, strbuf_allocator_t allocator)
{
strbuf_t* buf = NULL;
if(initial_capacity <= INT_MAX)
{
buf = allocator.allocator(&allocator, NULL, sizeof(strbuf_t)+initial_capacity+1);
buf->capacity = initial_capacity;
buf->allocator = allocator;
empty_buf(buf);
};
return buf;
}
static strview_t strview_of_buf(strbuf_t* buf)
{
strview_t str = STRVIEW_INVALID;
if(buf)
{
str.data = buf->cstr;
str.size = buf->size;
};
return str;
}
static strview_t buffer_vcat(strbuf_t** buf_ptr, int n_args, va_list va)
{
strview_t str;
int size_needed = 0;
bool tmp_buf_needed = false;
int i = 0;
bool failed = false;
strbuf_t* dst_buf = *buf_ptr;
strbuf_t* build_buf;
va_list vb;
va_copy(vb, va);
while(i++ != n_args)
{
str = va_arg(va, strview_t);
failed |= add_will_overflow_int(size_needed, str.size);
size_needed += str.size;
tmp_buf_needed |= buf_contains_str(dst_buf, str);
};
if(!failed)
{
if(tmp_buf_needed && buf_is_dynamic(dst_buf))
build_buf = create_buf(size_needed, dst_buf->allocator);
else
{
if(buf_is_dynamic(dst_buf) && dst_buf->capacity < size_needed)
change_buf_capacity(&dst_buf, round_up_capacity(size_needed));
build_buf = dst_buf;
empty_buf(build_buf);
};
if(buf_is_dynamic(build_buf) || !tmp_buf_needed)
{
if(build_buf->capacity >= size_needed)
{
i = 0;
while(i++ != n_args)
insert_strview_into_buf(&build_buf, build_buf->size, va_arg(vb, strview_t));
};
};
if(tmp_buf_needed && buf_is_dynamic(build_buf))
{
assign_strview_to_buf(&dst_buf, strview_of_buf(build_buf));
destroy_buf(&build_buf);
};
}
else
empty_buf(dst_buf);
*buf_ptr = dst_buf;
va_end(vb);
return strview_of_buf(dst_buf);
}
static void insert_strview_into_buf(strbuf_t** buf_ptr, int index, strview_t str)
{
strbuf_t* buf = *buf_ptr;
bool src_in_dst = buf_contains_str(buf, str);
size_t src_offset = str.data - buf->cstr;
strview_t strview_part_left_behind = STRVIEW_INVALID;
strview_t strview_part_shifted;
char* move_src;
char* move_dst;
bool failed;
if(index > buf->size)
index = buf->size;
if(index < 0)
index += buf->size;
if(index < 0)
index = 0;
failed = add_will_overflow_int(buf->size, str.size);
if(!failed)
{
if(buf_is_dynamic(buf) && buf->capacity < buf->size + str.size)
change_buf_capacity(&buf, round_up_capacity(buf->size + str.size));
if(src_in_dst && buf != *buf_ptr)
str.data = buf->cstr + src_offset;
failed = buf->capacity < buf->size + str.size;
};
if(!failed)
{
strview_part_shifted = str;
move_src = &buf->cstr[index];
move_dst = &buf->cstr[index+str.size];
if(str.size)
{
memmove(move_dst, move_src, buf->size-index);
if(src_in_dst)
{
if(move_src > str.data)
strview_part_left_behind = strview_split_index(&strview_part_shifted, move_src - str.data);
strview_part_shifted.data += move_dst-move_src;
};
};
buf->size += str.size;
if(strview_part_left_behind.size)
memcpy(move_src, strview_part_left_behind.data, strview_part_left_behind.size);
move_src += strview_part_left_behind.size;
if(strview_part_shifted.size)
memcpy(move_src, strview_part_shifted.data, strview_part_shifted.size);
buf->cstr[buf->size] = 0;
}
else
empty_buf(buf);
*buf_ptr = buf;
}
static void destroy_buf(strbuf_t** buf_ptr)
{
strbuf_t* buf = *buf_ptr;
if(buf_is_dynamic(buf))
buf->allocator.allocator(&buf->allocator, buf, 0);
*buf_ptr = NULL;
}
static void change_buf_capacity(strbuf_t** buf_ptr, int new_capacity)
{
strbuf_t* buf = *buf_ptr;
if(buf_is_dynamic(buf))
{
if(new_capacity < buf->size)
new_capacity = buf->size;
if(new_capacity != buf->capacity)
{
buf = buf->allocator.allocator(&buf->allocator, buf, sizeof(strbuf_t)+new_capacity+1);
buf->capacity = new_capacity;
};
};
*buf_ptr = buf;
}
static void assign_strview_to_buf(strbuf_t** buf_ptr, strview_t str)
{
empty_buf(*buf_ptr);
insert_strview_into_buf(buf_ptr, 0, str);
}
static void append_char_to_buf(strbuf_t** buf_ptr, char c)
{
strbuf_t* buf = *buf_ptr;
bool failed = add_will_overflow_int(buf->size, 1);
if(!failed)
{
if(buf_is_dynamic(buf) && buf->size+1 > buf->capacity)
change_buf_capacity(&buf, round_up_capacity(buf->size + 1));
failed = buf->capacity < buf->size+1;
};
if(!failed)
{
buf->cstr[buf->size] = c;
buf->size++;
buf->cstr[buf->size] = 0;
}
else
empty_buf(buf);
*buf_ptr = buf;
}
static int round_up_capacity(int capacity)
{
int remainder = capacity % STRBUF_CAPACITY_GROW_STEP;
if(remainder)
{
if(!add_will_overflow_int(capacity, STRBUF_CAPACITY_GROW_STEP-remainder))
capacity += STRBUF_CAPACITY_GROW_STEP-remainder;
else
capacity = INT_MAX;
};
return capacity;
}
static bool buf_contains_str(strbuf_t* buf, strview_t str)
{
return &buf->cstr[0] <= str.data && str.data < &buf->cstr[buf->size];
}
static bool buf_is_dynamic(strbuf_t* buf)
{
return !!(buf->allocator.allocator);
}
static void empty_buf(strbuf_t* buf)
{
buf->size = 0;
buf->cstr[0] = 0;
}
static bool add_will_overflow_int(int a, int b)
{
int c = a;
c += b;
return ((a < 0) == (b < 0) && (a < 0) != (c < 0));
}
static bool view_contains_char(strview_t view, char c)
{
bool retval = false;
if(strview_is_valid(view))
{
while(!retval && view.size)
{
retval |= (*view.data == c);
view.data++;
view.size--;
};
};
return retval;
}
#ifdef STRBUF_PROVIDE_PRNF
static void char_handler_for_prnf(void* dst, char c)
{
append_char_to_buf((strbuf_t**)dst, c);
}
#endif