-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrap.c
More file actions
2389 lines (2076 loc) · 54.4 KB
/
strap.c
File metadata and controls
2389 lines (2076 loc) · 54.4 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
#include "strap.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(_MSC_VER)
# include <intrin.h>
#endif
#if defined(__SSE2__) || (defined(_MSC_VER) && (defined(_M_X64) || defined(_M_AMD64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 2)))
# define STRAP_HAVE_SSE2 1
# include <emmintrin.h>
#else
# define STRAP_HAVE_SSE2 0
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__linux__)
# define STRAP_HAVE_TM_GMTOFF 1
#else
# define STRAP_HAVE_TM_GMTOFF 0
#endif
#if defined(__APPLE__) || defined(__FreeBSD__)
# include <xlocale.h>
#endif
#if defined(_MSC_VER)
# define STRAP_THREAD_LOCAL __declspec(thread)
#else
# define STRAP_THREAD_LOCAL _Thread_local
#endif
static STRAP_THREAD_LOCAL strap_error_t strap_global_error = STRAP_OK;
static void strap_set_error(strap_error_t err)
{
strap_global_error = err;
}
strap_error_t strap_last_error(void)
{
return strap_global_error;
}
const char *strap_error_string(strap_error_t err)
{
switch (err)
{
case STRAP_OK:
return "no error";
case STRAP_ERR_INVALID_ARGUMENT:
return "invalid argument";
case STRAP_ERR_ALLOC:
return "allocation failed";
case STRAP_ERR_IO:
return "I/O error";
case STRAP_ERR_OVERFLOW:
return "size overflow";
default:
return "unknown error";
}
}
void strap_clear_error(void)
{
strap_global_error = STRAP_OK;
}
static int strap_check_add_overflow(size_t a, size_t b)
{
return b > 0 && a > SIZE_MAX - b;
}
static int strap_check_mul_overflow(size_t a, size_t b)
{
return (a != 0 && b > SIZE_MAX / a);
}
#define STRAP_LINE_BUFFER_DEFAULT_CAPACITY 256
static unsigned char strap_ascii_tolower(unsigned char ch)
{
if (ch >= 'A' && ch <= 'Z')
return (unsigned char)(ch + ('a' - 'A'));
return ch;
}
static void strap_split_free_partial(char **tokens, size_t count)
{
if (!tokens)
return;
for (size_t i = 0; i < count; ++i)
free(tokens[i]);
free(tokens);
}
static int strap_split_reserve(char ***tokens_ptr, size_t *capacity, size_t needed, size_t max_tokens)
{
if (needed <= *capacity)
return 0;
size_t new_capacity = *capacity;
if (max_tokens > 0)
{
if (needed > max_tokens)
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return -1;
}
new_capacity = max_tokens;
}
else
{
if (new_capacity == 0)
new_capacity = 4;
while (new_capacity < needed)
{
if (new_capacity > SIZE_MAX / 2)
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return -1;
}
new_capacity *= 2;
}
}
if (strap_check_add_overflow(new_capacity, 1) ||
strap_check_mul_overflow(new_capacity + 1, sizeof(char *)))
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return -1;
}
char **resized = realloc(*tokens_ptr, (new_capacity + 1) * sizeof(char *));
if (!resized)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return -1;
}
*tokens_ptr = resized;
*capacity = new_capacity;
return 0;
}
#if STRAP_HAVE_SSE2
static unsigned strap_ctz16(unsigned mask)
{
# if defined(_MSC_VER)
unsigned long idx;
_BitScanForward(&idx, mask);
return (unsigned)idx;
# else
return (unsigned)__builtin_ctz(mask);
# endif
}
static unsigned strap_highest_bit_index16(unsigned mask)
{
# if defined(_MSC_VER)
unsigned long idx;
_BitScanReverse(&idx, mask);
return (unsigned)idx;
# else
return 31U - (unsigned)__builtin_clz(mask);
# endif
}
static size_t strap_trim_leading_ascii_simd(const unsigned char *s, size_t len)
{
const __m128i zero = _mm_setzero_si128();
const __m128i space_limit = _mm_set1_epi8(' ');
size_t offset = 0;
while (offset + 16 <= len)
{
__m128i chunk = _mm_loadu_si128((const __m128i *)(s + offset));
int non_ascii = _mm_movemask_epi8(_mm_cmplt_epi8(chunk, zero));
if (non_ascii)
return SIZE_MAX;
__m128i above_space = _mm_cmpgt_epi8(chunk, space_limit);
int stop_mask = _mm_movemask_epi8(above_space);
if (stop_mask)
{
unsigned idx = strap_ctz16(stop_mask);
return offset + idx;
}
offset += 16;
}
while (offset < len)
{
unsigned char c = s[offset];
if (c > 0x7F)
return SIZE_MAX;
if (c > ' ')
return offset;
++offset;
}
return len;
}
static size_t strap_trim_trailing_ascii_simd(const unsigned char *s, size_t len)
{
if (len == 0)
return 0;
const __m128i zero = _mm_setzero_si128();
const __m128i space_limit = _mm_set1_epi8(' ');
size_t offset = len;
while (offset >= 16)
{
size_t chunk_start = offset - 16;
__m128i chunk = _mm_loadu_si128((const __m128i *)(s + chunk_start));
int non_ascii = _mm_movemask_epi8(_mm_cmplt_epi8(chunk, zero));
if (non_ascii)
return SIZE_MAX;
__m128i above_space = _mm_cmpgt_epi8(chunk, space_limit);
int mask = _mm_movemask_epi8(above_space);
if (mask)
{
unsigned idx = strap_highest_bit_index16(mask);
return chunk_start + idx + 1;
}
offset -= 16;
}
while (offset > 0)
{
unsigned char c = s[offset - 1];
if (c > 0x7F)
return SIZE_MAX;
if (c > ' ')
return offset;
--offset;
}
return 0;
}
#else
static size_t strap_trim_leading_ascii_simd(const unsigned char *s, size_t len)
{
(void)s;
(void)len;
return SIZE_MAX;
}
static size_t strap_trim_trailing_ascii_simd(const unsigned char *s, size_t len)
{
(void)s;
(void)len;
return SIZE_MAX;
}
#endif
static void strap_copy_bytes(char *dst, const char *src, size_t len)
{
if (!dst || !src || len == 0)
return;
#if STRAP_HAVE_SSE2
size_t i = 0;
for (; i + 16 <= len; i += 16)
{
__m128i chunk = _mm_loadu_si128((const __m128i *)(src + i));
_mm_storeu_si128((__m128i *)(dst + i), chunk);
}
size_t remaining = len - i;
if (remaining > 0)
memcpy(dst + i, src + i, remaining);
#else
memcpy(dst, src, len);
#endif
}
static int strap_line_buffer_reserve(strap_line_buffer_t *buffer, size_t needed)
{
if (buffer->capacity >= needed)
return 0;
size_t new_capacity = buffer->capacity;
if (new_capacity == 0)
new_capacity = STRAP_LINE_BUFFER_DEFAULT_CAPACITY;
while (new_capacity < needed)
{
if (new_capacity > SIZE_MAX / 2)
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return -1;
}
new_capacity *= 2;
}
char *resized = realloc(buffer->data, new_capacity);
if (!resized)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return -1;
}
buffer->data = resized;
buffer->capacity = new_capacity;
return 0;
}
/* --------------------------------------------------------------------- */
/* Arena allocator internals */
struct strap_arena_block
{
struct strap_arena_block *next;
size_t capacity;
size_t used;
unsigned char data[];
};
struct strap_arena
{
struct strap_arena_block *head;
size_t block_size;
};
static size_t strap_align_size(size_t value)
{
const size_t alignment = sizeof(void *);
size_t remainder = value % alignment;
if (remainder == 0)
return value;
if (strap_check_add_overflow(value, alignment - remainder))
return SIZE_MAX;
return value + (alignment - remainder);
}
static struct strap_arena_block *strap_arena_new_block(size_t capacity)
{
if (capacity == 0)
capacity = 4096;
size_t header_size = sizeof(struct strap_arena_block);
if (strap_check_add_overflow(header_size, capacity))
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
size_t total = header_size + capacity;
struct strap_arena_block *block = malloc(total);
if (!block)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
block->next = NULL;
block->capacity = capacity;
block->used = 0;
return block;
}
/* --------------------------------------------------------------------- */
/* Locale helpers */
typedef enum
{
STRAP_LOCALE_NONE,
STRAP_LOCALE_HANDLE,
STRAP_LOCALE_GLOBAL
} strap_locale_kind_t;
typedef struct
{
strap_locale_kind_t kind;
#if defined(_WIN32)
_locale_t handle;
#else
locale_t handle;
#endif
char *saved_global;
} strap_locale_ctx;
static int strap_locale_enter(const char *locale_name, strap_locale_ctx *ctx)
{
ctx->kind = STRAP_LOCALE_NONE;
ctx->saved_global = NULL;
if (!locale_name || locale_name[0] == '\0')
return 0;
#if defined(_WIN32)
ctx->handle = _create_locale(LC_ALL, locale_name);
if (!ctx->handle)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return -1;
}
ctx->kind = STRAP_LOCALE_HANDLE;
return 0;
#elif defined(LC_ALL_MASK)
ctx->handle = newlocale(LC_ALL_MASK, locale_name, (locale_t)0);
if (!ctx->handle)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return -1;
}
ctx->kind = STRAP_LOCALE_HANDLE;
return 0;
#else
const char *current = setlocale(LC_ALL, NULL);
if (!current)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return -1;
}
ctx->saved_global = strdup(current);
if (!ctx->saved_global)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return -1;
}
if (!setlocale(LC_ALL, locale_name))
{
free(ctx->saved_global);
ctx->saved_global = NULL;
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return -1;
}
ctx->kind = STRAP_LOCALE_GLOBAL;
return 0;
#endif
}
static void strap_locale_exit(strap_locale_ctx *ctx)
{
if (!ctx)
return;
switch (ctx->kind)
{
case STRAP_LOCALE_NONE:
break;
case STRAP_LOCALE_HANDLE:
#if defined(_WIN32)
if (ctx->handle)
_free_locale(ctx->handle);
#else
if (ctx->handle)
freelocale(ctx->handle);
#endif
break;
case STRAP_LOCALE_GLOBAL:
if (ctx->saved_global)
setlocale(LC_ALL, ctx->saved_global);
free(ctx->saved_global);
ctx->saved_global = NULL;
break;
}
ctx->kind = STRAP_LOCALE_NONE;
}
static int strap_tolower_locale_ctx(int ch, const strap_locale_ctx *ctx)
{
unsigned char c = (unsigned char)ch;
switch (ctx->kind)
{
case STRAP_LOCALE_HANDLE:
#if defined(_WIN32)
return _tolower_l(c, ctx->handle);
#else
return tolower_l(c, ctx->handle);
#endif
case STRAP_LOCALE_GLOBAL:
case STRAP_LOCALE_NONE:
default:
return tolower(c);
}
}
static int strap_toupper_locale_ctx(int ch, const strap_locale_ctx *ctx)
{
unsigned char c = (unsigned char)ch;
switch (ctx->kind)
{
case STRAP_LOCALE_HANDLE:
#if defined(_WIN32)
return _toupper_l(c, ctx->handle);
#else
return toupper_l(c, ctx->handle);
#endif
case STRAP_LOCALE_GLOBAL:
case STRAP_LOCALE_NONE:
default:
return toupper(c);
}
}
/* Safe reading */
void strap_line_buffer_init(strap_line_buffer_t *buffer)
{
if (!buffer)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return;
}
buffer->data = NULL;
buffer->capacity = 0;
strap_clear_error();
}
void strap_line_buffer_free(strap_line_buffer_t *buffer)
{
if (!buffer)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return;
}
free(buffer->data);
buffer->data = NULL;
buffer->capacity = 0;
}
char *strap_line_buffer_read(FILE *f, strap_line_buffer_t *buffer)
{
if (!f || !buffer)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
size_t len = 0;
if (buffer->capacity == 0)
{
if (strap_line_buffer_reserve(buffer, STRAP_LINE_BUFFER_DEFAULT_CAPACITY) != 0)
return NULL;
}
else
{
buffer->data[0] = '\0';
}
for (;;)
{
if (buffer->capacity - len <= 1)
{
size_t target = len + STRAP_LINE_BUFFER_DEFAULT_CAPACITY;
if (strap_line_buffer_reserve(buffer, target) != 0)
return NULL;
}
if (!fgets(buffer->data + len, (int)(buffer->capacity - len), f))
break;
size_t chunk_len = strlen(buffer->data + len);
char *newline = memchr(buffer->data + len, '\n', chunk_len);
if (newline)
{
*newline = '\0';
strap_clear_error();
return buffer->data;
}
len += chunk_len;
}
if (len > 0 && buffer->data)
{
strap_clear_error();
return buffer->data;
}
if (ferror(f))
{
errno = EIO;
strap_set_error(STRAP_ERR_IO);
}
else
{
strap_clear_error();
}
return NULL;
}
char *afgets(FILE *f)
{
if (!f)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
strap_line_buffer_t buffer;
strap_line_buffer_init(&buffer);
char *inplace = strap_line_buffer_read(f, &buffer);
if (!inplace)
{
strap_line_buffer_free(&buffer);
return NULL;
}
char *result = strdup(inplace);
strap_line_buffer_free(&buffer);
if (!result)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
strap_clear_error();
return result;
}
char *afread(FILE *f, size_t *out_len)
{
if (!f)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
const size_t chunk = 4096;
size_t capacity = chunk;
size_t len = 0;
char *buffer = malloc(capacity + 1);
if (!buffer)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
for (;;)
{
if (len == capacity)
{
if (strap_check_add_overflow(capacity, chunk))
{
free(buffer);
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
size_t new_capacity = capacity + chunk;
char *tmp = realloc(buffer, new_capacity + 1);
if (!tmp)
{
free(buffer);
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
buffer = tmp;
capacity = new_capacity;
}
size_t to_read = capacity - len;
size_t n = fread(buffer + len, 1, to_read, f);
len += n;
if (n < to_read)
{
if (feof(f))
break;
if (ferror(f))
{
free(buffer);
errno = EIO;
strap_set_error(STRAP_ERR_IO);
return NULL;
}
}
}
buffer[len] = '\0';
char *shrunk = realloc(buffer, len + 1);
if (shrunk)
buffer = shrunk;
if (out_len)
*out_len = len;
strap_clear_error();
return buffer;
}
/* Arena allocator */
strap_arena_t *strap_arena_create(size_t block_size)
{
strap_arena_t *arena = malloc(sizeof(*arena));
if (!arena)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
if (block_size == 0)
block_size = 4096;
arena->head = NULL;
arena->block_size = block_size;
strap_clear_error();
return arena;
}
void strap_arena_destroy(strap_arena_t *arena)
{
if (!arena)
return;
struct strap_arena_block *block = arena->head;
while (block)
{
struct strap_arena_block *next = block->next;
free(block);
block = next;
}
free(arena);
}
void strap_arena_clear(strap_arena_t *arena)
{
if (!arena)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return;
}
for (struct strap_arena_block *block = arena->head; block; block = block->next)
block->used = 0;
strap_clear_error();
}
void *strap_arena_alloc(strap_arena_t *arena, size_t size)
{
if (!arena || size == 0)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
size_t aligned = strap_align_size(size);
if (aligned == SIZE_MAX)
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
struct strap_arena_block *block = arena->head;
if (!block || block->used + aligned > block->capacity)
{
size_t block_capacity = arena->block_size;
if (aligned > block_capacity)
block_capacity = aligned;
struct strap_arena_block *new_block = strap_arena_new_block(block_capacity);
if (!new_block)
return NULL;
new_block->next = arena->head;
arena->head = new_block;
block = new_block;
}
void *memory = block->data + block->used;
block->used += aligned;
strap_clear_error();
return memory;
}
char *strap_arena_strdup(strap_arena_t *arena, const char *s)
{
if (!arena || !s)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
size_t len = strlen(s);
if (strap_check_add_overflow(len, 1))
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
char *dst = strap_arena_alloc(arena, len + 1);
if (!dst)
return NULL;
memcpy(dst, s, len + 1);
strap_clear_error();
return dst;
}
char *strap_arena_strndup(strap_arena_t *arena, const char *s, size_t n)
{
if (!arena || !s)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
if (strap_check_add_overflow(n, 1))
{
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
char *dst = strap_arena_alloc(arena, n + 1);
if (!dst)
return NULL;
memcpy(dst, s, n);
dst[n] = '\0';
strap_clear_error();
return dst;
}
/* String manipulation */
static char *strjoin_impl(strap_arena_t *arena, const char **parts, size_t nparts, const char *sep)
{
if (!parts || nparts == 0)
{
if (arena)
{
char *empty = strap_arena_alloc(arena, 1);
if (!empty)
return NULL;
empty[0] = '\0';
strap_clear_error();
return empty;
}
char *empty = strdup("");
if (!empty)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
}
else
{
strap_clear_error();
}
return empty;
}
size_t sep_len = sep ? strlen(sep) : 0;
size_t *lengths = malloc(nparts * sizeof(size_t));
if (!lengths)
{
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
size_t total_len = 1; /* null terminator */
for (size_t i = 0; i < nparts; ++i)
{
size_t part_len = parts[i] ? strlen(parts[i]) : 0;
lengths[i] = part_len;
if (strap_check_add_overflow(total_len, part_len))
{
free(lengths);
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
total_len += part_len;
if (i + 1 < nparts && sep_len > 0)
{
if (strap_check_add_overflow(total_len, sep_len))
{
free(lengths);
errno = EOVERFLOW;
strap_set_error(STRAP_ERR_OVERFLOW);
return NULL;
}
total_len += sep_len;
}
}
char *result;
if (arena)
{
result = strap_arena_alloc(arena, total_len);
if (!result)
{
free(lengths);
return NULL;
}
}
else
{
result = malloc(total_len);
if (!result)
{
free(lengths);
errno = ENOMEM;
strap_set_error(STRAP_ERR_ALLOC);
return NULL;
}
}
char *write_ptr = result;
for (size_t i = 0; i < nparts; ++i)
{
if (i > 0 && sep_len > 0)
{
strap_copy_bytes(write_ptr, sep, sep_len);
write_ptr += sep_len;
}
size_t part_len = lengths[i];
if (part_len > 0 && parts[i])
{
strap_copy_bytes(write_ptr, parts[i], part_len);
write_ptr += part_len;
}
}
*write_ptr = '\0';
free(lengths);
strap_clear_error();
return result;
}
char *strjoin(const char **parts, size_t nparts, const char *sep)
{
return strjoin_impl(NULL, parts, nparts, sep);
}
char *strjoin_arena(strap_arena_t *arena, const char **parts, size_t nparts, const char *sep)
{
if (!arena)
{
errno = EINVAL;
strap_set_error(STRAP_ERR_INVALID_ARGUMENT);
return NULL;
}
return strjoin_impl(arena, parts, nparts, sep);
}
char *strjoin_va(const char *sep, ...)
{
va_list args;
va_start(args, sep);
size_t count = 0;