diff options
author | Guillem Jover <guillem@debian.org> | 2018-03-16 04:17:16 +0100 |
---|---|---|
committer | Guillem Jover <guillem@debian.org> | 2018-03-26 15:30:18 +0200 |
commit | ac3134292ecb5ea5f203c3fa1376cbc093d35518 (patch) | |
tree | db1af11e903c56bfbbd71b2012fece681d12f73f /lib | |
parent | 738d1fdb056d3137f4d6291ac72386b0ced17769 (diff) | |
download | dpkg-ac3134292ecb5ea5f203c3fa1376cbc093d35518.tar.gz |
libdpkg: Add new str_concat() function
Diffstat (limited to 'lib')
-rw-r--r-- | lib/dpkg/libdpkg.map | 1 | ||||
-rw-r--r-- | lib/dpkg/string.c | 19 | ||||
-rw-r--r-- | lib/dpkg/string.h | 1 | ||||
-rw-r--r-- | lib/dpkg/t/t-string.c | 34 |
4 files changed, 54 insertions, 1 deletions
diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index 5e685c948..85b8a1fa6 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -90,6 +90,7 @@ LIBDPKG_PRIVATE { str_match_end; str_fnv_hash; + str_concat; str_fmt; str_escape_fmt; str_strip_quotes; diff --git a/lib/dpkg/string.c b/lib/dpkg/string.c index 220e2789f..ff97159f9 100644 --- a/lib/dpkg/string.c +++ b/lib/dpkg/string.c @@ -28,6 +28,25 @@ #include <dpkg/string.h> #include <dpkg/dpkg.h> +char * +str_concat(char *dst, ...) +{ + va_list args; + const char *src; + size_t len; + + va_start(args, dst); + while ((src = va_arg(args, const char *))) { + len = strlen(src); + memcpy(dst, src, len); + dst += len; + } + va_end(args); + *dst = '\0'; + + return dst; +} + /** * Match the end of a string. * diff --git a/lib/dpkg/string.h b/lib/dpkg/string.h index 927aeaee4..d0f6bd7ad 100644 --- a/lib/dpkg/string.h +++ b/lib/dpkg/string.h @@ -56,6 +56,7 @@ bool str_match_end(const char *str, const char *end); unsigned int str_fnv_hash(const char *str); +char *str_concat(char *dst, ...) DPKG_ATTR_SENTINEL; char *str_fmt(const char *fmt, ...) DPKG_ATTR_PRINTF(1); char *str_escape_fmt(char *dest, const char *src, size_t n); char *str_quote_meta(const char *src); diff --git a/lib/dpkg/t/t-string.c b/lib/dpkg/t/t-string.c index 038de84b0..1f5ee4cbe 100644 --- a/lib/dpkg/t/t-string.c +++ b/lib/dpkg/t/t-string.c @@ -77,6 +77,37 @@ test_str_fnv_hash(void) } static void +test_str_concat(void) +{ + char buf[1024], *str; + + memset(buf, 0, sizeof(buf)); + str = str_concat(buf, NULL); + test_pass(str == buf); + test_str(buf, ==, ""); + + memset(buf, 0, sizeof(buf)); + str = str_concat(buf, "aaa", NULL); + test_str(buf, ==, "aaa"); + test_pass(str == buf + 3); + + memset(buf, 0, sizeof(buf)); + str = str_concat(buf, "zzzz", "yy", "xxxx", NULL); + test_str(buf, ==, "zzzzyyxxxx"); + test_pass(str == buf + 10); + + memset(buf, 0, sizeof(buf)); + str = str_concat(buf, "1234", "", "5678", NULL); + test_str(buf, ==, "12345678"); + test_pass(str == buf + 8); + + memset(buf, ' ', sizeof(buf)); + str = str_concat(buf, "eol", NULL, "bom", NULL); + test_str(buf, ==, "eol"); + test_pass(str == buf + 3); +} + +static void test_str_fmt(void) { char *str; @@ -197,11 +228,12 @@ test_str_strip_quotes(void) TEST_ENTRY(test) { - test_plan(50); + test_plan(60); test_str_is_set(); test_str_match_end(); test_str_fnv_hash(); + test_str_concat(); test_str_fmt(); test_str_escape_fmt(); test_str_quote_meta(); |