From 4ff750609549a99f6b318ce600c42c90d4f2e480 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 20 Aug 2021 16:10:37 -0400 Subject: [PATCH] common: move append_str to string-utils MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Move the append_str function from filter-visitor-generate-bytecode.c to the string-utils lib, so that it can be re-used elsewhere. Change-Id: Ica09cb750ac7f3f2d6f3fb5fc786b683ceb6f79a Signed-off-by: Simon Marchi Signed-off-by: Jérémie Galarneau --- .../filter-visitor-generate-bytecode.cpp | 30 ++++--------------- src/common/string-utils/string-utils.cpp | 22 ++++++++++++++ src/common/string-utils/string-utils.h | 10 +++++++ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/common/filter/filter-visitor-generate-bytecode.cpp b/src/common/filter/filter-visitor-generate-bytecode.cpp index 7b418acdc..c7e1be782 100644 --- a/src/common/filter/filter-visitor-generate-bytecode.cpp +++ b/src/common/filter/filter-visitor-generate-bytecode.cpp @@ -19,6 +19,7 @@ #include "common/bytecode/bytecode.h" #include "common/compat/string.h" #include "common/macros.h" +#include "common/string-utils/string-utils.h" #include "filter-ast.h" #include "filter-ir.h" @@ -59,27 +60,6 @@ int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node) return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn)); } -static -int append_str(char **s, const char *append) -{ - char *old_str = *s; - char *new_str; - size_t oldlen = (old_str == NULL) ? 0 : strlen(old_str); - size_t appendlen = strlen(append); - - new_str = (char *) calloc(oldlen + appendlen + 1, 1); - if (!new_str) { - return -ENOMEM; - } - if (oldlen) { - strcpy(new_str, old_str); - } - strcat(new_str, append); - *s = new_str; - free(old_str); - return 0; -} - /* * 1: match * 0: no match @@ -97,14 +77,14 @@ int load_expression_legacy_match(const struct ir_load_expression *exp, switch (op->type) { case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: *op_type = BYTECODE_OP_GET_CONTEXT_REF; - if (append_str(symbol, "$ctx.")) { + if (strutils_append_str(symbol, "$ctx.")) { return -ENOMEM; } need_dot = false; break; case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: *op_type = BYTECODE_OP_GET_CONTEXT_REF; - if (append_str(symbol, "$app.")) { + if (strutils_append_str(symbol, "$app.")) { return -ENOMEM; } need_dot = false; @@ -130,10 +110,10 @@ int load_expression_legacy_match(const struct ir_load_expression *exp, case IR_LOAD_EXPRESSION_LOAD_FIELD: goto end; case IR_LOAD_EXPRESSION_GET_SYMBOL: - if (need_dot && append_str(symbol, ".")) { + if (need_dot && strutils_append_str(symbol, ".")) { return -ENOMEM; } - if (append_str(symbol, op->u.symbol)) { + if (strutils_append_str(symbol, op->u.symbol)) { return -ENOMEM; } break; diff --git a/src/common/string-utils/string-utils.cpp b/src/common/string-utils/string-utils.cpp index 3051644f3..8dcc6b2aa 100644 --- a/src/common/string-utils/string-utils.cpp +++ b/src/common/string-utils/string-utils.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include "string-utils.h" #include "../macros.h" @@ -373,3 +375,23 @@ size_t strutils_array_of_strings_len(char * const *array) return count; } + +int strutils_append_str(char **s, const char *append) +{ + char *old = *s; + char *new_str; + size_t oldlen = (old == NULL) ? 0 : strlen(old); + size_t appendlen = strlen(append); + + new_str = (char *) zmalloc(oldlen + appendlen + 1); + if (!new_str) { + return -ENOMEM; + } + if (oldlen) { + strcpy(new_str, old); + } + strcat(new_str, append); + *s = new_str; + free(old); + return 0; +} diff --git a/src/common/string-utils/string-utils.h b/src/common/string-utils/string-utils.h index 1cf31e9d4..8f47abbc3 100644 --- a/src/common/string-utils/string-utils.h +++ b/src/common/string-utils/string-utils.h @@ -27,4 +27,14 @@ void strutils_free_null_terminated_array_of_strings(char **array); size_t strutils_array_of_strings_len(char * const *array); +/* + * Append `append` to the malloc-end string `str`. + * + * On success, `str` is free'd (if not NULL) and assigned a new malloc-ed + * string. On failure, `str` is not modified. + * + * Return 0 on success, -ENOMEM on failure. + */ +int strutils_append_str(char **str, const char *append); + #endif /* _STRING_UTILS_H */ -- 2.34.1