diff options
author | Timothy Arceri <[email protected]> | 2017-08-09 13:34:08 +1000 |
---|---|---|
committer | Timothy Arceri <[email protected]> | 2017-08-11 10:43:31 +1000 |
commit | 26f4657c3f075045527c4568e8cbbaaa5d0b08e4 (patch) | |
tree | f42b99121a226131b49c12424497b529f424f448 /src/util/ralloc.c | |
parent | 53320e25b425b10478891c695ca9a3c0477abd22 (diff) |
util/ralloc: add ralloc_str_append() helper
This function differs from ralloc_strcat() and ralloc_strncat()
in that it does not do any strlen() calls which can become
costly on large strings.
Reviewed-by: Thomas Helland <[email protected]>
Diffstat (limited to 'src/util/ralloc.c')
-rw-r--r-- | src/util/ralloc.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/util/ralloc.c b/src/util/ralloc.c index 821ee72fe8d..bf46439df4e 100644 --- a/src/util/ralloc.c +++ b/src/util/ralloc.c @@ -403,6 +403,25 @@ ralloc_strncat(char **dest, const char *str, size_t n) return cat(dest, str, strnlen(str, n)); } +bool +ralloc_str_append(char **dest, const char *str, + size_t existing_length, size_t str_size) +{ + char *both; + assert(dest != NULL && *dest != NULL); + + both = resize(*dest, existing_length + str_size + 1); + if (unlikely(both == NULL)) + return false; + + memcpy(both + existing_length, str, str_size); + both[existing_length + str_size] = '\0'; + + *dest = both; + + return true; +} + char * ralloc_asprintf(const void *ctx, const char *fmt, ...) { |