[RFC PATCH 18/28] cli: lil: Remove duplicate function bodies

Sean Anderson
2021-07-01T06:19:46Z

lil_append_val is just lil_append_string with the string and length taken
from a struct lil_value. Use lil_append_stringh_len to implement both. Do
the same for lil_clone_value.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
---

common/cli_lil.c | 51 ++----------------------------------------------
1 file changed, 2 insertions(+), 49 deletions(-)

diff --git a/common/cli_lil.c b/common/cli_lil.c
index 6c05531441..5875fbd46b 100644
--- a/common/cli_lil.c
+++ b/common/cli_lil.c
@@ -369,39 +369,7 @@ static struct lil_value *alloc_value(const char *str)

struct lil_value *lil_clone_value(struct lil_value *src)
{
- struct lil_value *val;
-
- if (!src)
- return NULL;
-
- if (IS_ENABLED(CONFIG_LIL_POOLS))
- val = alloc_from_pool();
- else
- val = calloc(1, sizeof(struct lil_value));
- if (!val)
- return NULL;
-
- val->l = src->l;
- if (src->l) {
- if (IS_ENABLED(CONFIG_LIL_POOLS)) {
- ensure_capacity(val, val->l + 1);
- } else {
- val->d = malloc(val->l + 1);
- if (!val->d) {
- free(val);
- return NULL;
- }
- }
- memcpy(val->d, src->d, val->l + 1);
- } else {
- if (IS_ENABLED(CONFIG_LIL_POOLS)) {
- ensure_capacity(val, 1);
- val->d[0] = '\0';
- } else {
- val->d = NULL;
- }
- }
- return val;
+ return alloc_value_len(src->d, src->l);
}

int lil_append_char(struct lil_value *val, char ch)
@@ -450,22 +418,7 @@ int lil_append_string(struct lil_value *val, const char *s)

int lil_append_val(struct lil_value *val, struct lil_value *v)
{
- if (!v || !v->l)
- return 1;
-
- if (IS_ENABLED(CONFIG_LIL_POOLS)) {
- ensure_capacity(val, val->l + v->l + 1);
- memcpy(val->d + val->l, v->d, v->l + 1);
- } else {
- char *new = realloc(val->d, val->l + v->l + 1);
-
- if (!new)
- return 0;
- memcpy(new + val->l, v->d, v->l + 1);
- val->d = new;
- }
- val->l += v->l;
- return 1;
+ return lil_append_string_len(val, v->d, v->l);
}

void lil_free_value(struct lil_value *val)
--
2.32.0