Backport from xs.

This commit is contained in:
default 2023-01-08 10:39:11 +01:00
parent 631f16aea8
commit da7e57f4cc
4 changed files with 25 additions and 9 deletions

28
xs.h
View File

@ -128,6 +128,10 @@ void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char
fclose(f); fclose(f);
} }
#else
(void)file;
(void)line;
(void)func;
#endif #endif
return ndata; return ndata;
@ -669,26 +673,38 @@ int xs_list_in(char *list, const char *val)
d_char *xs_join(char *list, const char *sep) d_char *xs_join(char *list, const char *sep)
/* joins a list into a string */ /* joins a list into a string */
{ {
d_char *s; d_char *s = NULL;
char *v; char *v;
int c = 0; int c = 0;
int offset = 0;
s = xs_str_new(NULL); int ssz = strlen(sep);
while (xs_list_iter(&list, &v)) { while (xs_list_iter(&list, &v)) {
/* refuse to join non-string values */ /* refuse to join non-string values */
if (xs_type(v) == XSTYPE_STRING) { if (xs_type(v) == XSTYPE_STRING) {
int sz;
/* add the separator */ /* add the separator */
if (c != 0) if (c != 0) {
s = xs_str_cat(s, sep); s = xs_realloc(s, offset + ssz);
memcpy(s + offset, sep, ssz);
offset += ssz;
}
/* add the element */ /* add the element */
s = xs_str_cat(s, v); sz = strlen(v);
s = xs_realloc(s, offset + sz);
memcpy(s + offset, v, sz);
offset += sz;
c++; c++;
} }
} }
/* null-terminate */
s = xs_realloc(s, _xs_blk_size(offset + 1));
s[offset] = '\0';
return s; return s;
} }

View File

@ -71,7 +71,7 @@ static int _post_callback(char *buffer, size_t size,
int sz = pd->size - pd->offset; int sz = pd->size - pd->offset;
/* if it's still bigger than the provided space, trim */ /* if it's still bigger than the provided space, trim */
if (sz > size * nitems) if (sz > (int) (size * nitems))
sz = size * nitems; sz = size * nitems;
memcpy(buffer, pd->data + pd->offset, sz); memcpy(buffer, pd->data + pd->offset, sz);

View File

@ -21,7 +21,7 @@ d_char *xs_glob_n(const char *spec, int basename, int reverse, int max)
if (glob(spec, 0, NULL, &globbuf) == 0) { if (glob(spec, 0, NULL, &globbuf) == 0) {
int n; int n;
if (max > globbuf.gl_pathc) if (max > (int) globbuf.gl_pathc)
max = globbuf.gl_pathc; max = globbuf.gl_pathc;
for (n = 0; n < max; n++) { for (n = 0; n < max; n++) {

View File

@ -1 +1 @@
/* ae969c59232f4907369f10b84796456388fcf109 */ /* a04f6c8482d42d4d972f5190ecbad5c0509531b4 */