diff options
| author | 2023-01-08 10:39:11 +0100 | |
|---|---|---|
| committer | 2023-01-08 10:39:11 +0100 | |
| commit | da7e57f4cc016c254899c9438de1cbc9871a93bc (patch) | |
| tree | cec0c0bd2ac110bc0ad7d420c656e93d2937e0e4 /xs.h | |
| parent | Updated dependencies. (diff) | |
| download | penes-snac2-da7e57f4cc016c254899c9438de1cbc9871a93bc.tar.gz penes-snac2-da7e57f4cc016c254899c9438de1cbc9871a93bc.tar.xz penes-snac2-da7e57f4cc016c254899c9438de1cbc9871a93bc.zip | |
Backport from xs.
Diffstat (limited to 'xs.h')
| -rw-r--r-- | xs.h | 28 |
1 files changed, 22 insertions, 6 deletions
| @@ -128,6 +128,10 @@ void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char | |||
| 128 | 128 | ||
| 129 | fclose(f); | 129 | fclose(f); |
| 130 | } | 130 | } |
| 131 | #else | ||
| 132 | (void)file; | ||
| 133 | (void)line; | ||
| 134 | (void)func; | ||
| 131 | #endif | 135 | #endif |
| 132 | 136 | ||
| 133 | return ndata; | 137 | return ndata; |
| @@ -669,26 +673,38 @@ int xs_list_in(char *list, const char *val) | |||
| 669 | d_char *xs_join(char *list, const char *sep) | 673 | d_char *xs_join(char *list, const char *sep) |
| 670 | /* joins a list into a string */ | 674 | /* joins a list into a string */ |
| 671 | { | 675 | { |
| 672 | d_char *s; | 676 | d_char *s = NULL; |
| 673 | char *v; | 677 | char *v; |
| 674 | int c = 0; | 678 | int c = 0; |
| 675 | 679 | int offset = 0; | |
| 676 | s = xs_str_new(NULL); | 680 | int ssz = strlen(sep); |
| 677 | 681 | ||
| 678 | while (xs_list_iter(&list, &v)) { | 682 | while (xs_list_iter(&list, &v)) { |
| 679 | /* refuse to join non-string values */ | 683 | /* refuse to join non-string values */ |
| 680 | if (xs_type(v) == XSTYPE_STRING) { | 684 | if (xs_type(v) == XSTYPE_STRING) { |
| 685 | int sz; | ||
| 686 | |||
| 681 | /* add the separator */ | 687 | /* add the separator */ |
| 682 | if (c != 0) | 688 | if (c != 0) { |
| 683 | s = xs_str_cat(s, sep); | 689 | s = xs_realloc(s, offset + ssz); |
| 690 | memcpy(s + offset, sep, ssz); | ||
| 691 | offset += ssz; | ||
| 692 | } | ||
| 684 | 693 | ||
| 685 | /* add the element */ | 694 | /* add the element */ |
| 686 | s = xs_str_cat(s, v); | 695 | sz = strlen(v); |
| 696 | s = xs_realloc(s, offset + sz); | ||
| 697 | memcpy(s + offset, v, sz); | ||
| 698 | offset += sz; | ||
| 687 | 699 | ||
| 688 | c++; | 700 | c++; |
| 689 | } | 701 | } |
| 690 | } | 702 | } |
| 691 | 703 | ||
| 704 | /* null-terminate */ | ||
| 705 | s = xs_realloc(s, _xs_blk_size(offset + 1)); | ||
| 706 | s[offset] = '\0'; | ||
| 707 | |||
| 692 | return s; | 708 | return s; |
| 693 | } | 709 | } |
| 694 | 710 | ||