Tue, 08 May 2018 12:49:56 +0200
makes sstrcat and sstrnlen scstr_t compatible
src/string.c | file | annotate | diff | comparison | revisions | |
src/ucx/string.h | file | annotate | diff | comparison | revisions |
--- a/src/string.c Sun Apr 01 09:51:01 2018 +0200 +++ b/src/string.c Tue May 08 12:49:56 2018 +0200 @@ -65,13 +65,14 @@ } -size_t sstrnlen(size_t n, sstr_t s, ...) { +size_t ucx_strnlen(size_t n, ...) { va_list ap; - size_t size = s.length; - va_start(ap, s); + va_start(ap, n); + + size_t size = 0; - for (size_t i = 1 ; i < n ; i++) { - sstr_t str = va_arg(ap, sstr_t); + for (size_t i = 0 ; i < n ; i++) { + scstr_t str = va_arg(ap, scstr_t); if(((size_t)-1) - str.length < size) { size = 0; break; @@ -86,8 +87,7 @@ static sstr_t sstrvcat_a( UcxAllocator *a, size_t count, - sstr_t s1, - sstr_t s2, + scstr_t s1, va_list ap) { sstr_t str; str.ptr = NULL; @@ -96,11 +96,13 @@ return str; } + scstr_t s2 = va_arg (ap, scstr_t); + if(((size_t)-1) - s1.length < s2.length) { return str; } - sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t)); + scstr_t *strings = (scstr_t*) calloc(count, sizeof(scstr_t)); if(!strings) { return str; } @@ -111,7 +113,7 @@ size_t slen = s1.length + s2.length; int error = 0; for (size_t i=2;i<count;i++) { - sstr_t s = va_arg (ap, sstr_t); + scstr_t s = va_arg (ap, scstr_t); strings[i] = s; if(((size_t)-1) - s.length < slen) { error = 1; @@ -136,7 +138,7 @@ // concatenate strings size_t pos = 0; for (size_t i=0;i<count;i++) { - sstr_t s = strings[i]; + scstr_t s = strings[i]; memcpy(str.ptr + pos, s.ptr, s.length); pos += s.length; } @@ -148,18 +150,18 @@ return str; } -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) { +sstr_t ucx_strcat(size_t count, scstr_t s1, ...) { va_list ap; - va_start(ap, s2); - sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap); + va_start(ap, s1); + sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, ap); va_end(ap); return s; } -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) { +sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...) { va_list ap; - va_start(ap, s2); - sstr_t s = sstrvcat_a(a, count, s1, s2, ap); + va_start(ap, s1); + sstr_t s = sstrvcat_a(a, count, s1, ap); va_end(ap); return s; }
--- a/src/ucx/string.h Sun Apr 01 09:51:01 2018 +0200 +++ b/src/ucx/string.h Tue May 08 12:49:56 2018 +0200 @@ -76,6 +76,7 @@ const char *ptr; size_t length; } scstr_t; + #ifdef __cplusplus } #endif @@ -168,7 +169,9 @@ * @param ... all other strings * @return the cumulated length of all strings */ -size_t sstrnlen(size_t count, sstr_t string, ...); +size_t ucx_strnlen(size_t count, ...); + +#define sstrnlen(count, ...) ucx_strnlen(count, __VA_ARGS__) /** * Concatenates two or more strings. @@ -185,7 +188,9 @@ * @param ... all remaining strings * @return the concatenated string */ -sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...); +sstr_t ucx_strcat(size_t count, scstr_t s1, ...); + +#define sstrcat(count, s1, ...) ucx_strcat(count, SCSTR(s1), __VA_ARGS__) /** * Concatenates two or more strings using a UcxAllocator. @@ -199,8 +204,9 @@ * @param ... all remaining strings * @return the concatenated string */ -sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...); +sstr_t ucx_strcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...); +#define sstrcat_a(count, s1, ...) ucx_strcat_a(count, SCSTR(s1), __VA_ARGS__) /** * Returns a substring starting at the specified location.