Repository: valkey-io/valkey
File: src/sds.c (C)

Code before:

            /* add the token to the vector */
            if ((size_t)*argc == cap) {
                cap = cap == 0 ? 8 : (cap * 2);
                vector = s_realloc(vector, sizeof(sds) * cap);
            }
            vector[(*argc)++] = current;
            current = NULL;
        } else {
            while ((*argc)--) sdsfree(vector[*argc]);
            s_free(vector);
            *argc = 0;
            return NULL;
        }
    }
    /* Even on empty input string return something not NULL. */
    if (vector == NULL) vector = s_malloc(sizeof(void *));
    return vector;
}

sds *sdssplitargs(const char *line, int *argc) {
    return sdsnsplitargs_internal(line, NULL, argc);
}

sds *sdsnsplitargs(const char *line, int len, int *argc) {
    const char *end = len < 0 ? NULL : line + len;
    return sdsnsplitargs_internal(line, end, argc);
}

/* Modify the string substituting all the occurrences of the set of
 * characters specified in the 'from' string to the corresponding character
 * in the 'to' array.
 *
 * For instance: sdsmapchars(mystring, "ho", "01", 2)
 * will have the effect of turning the string "hello" into "0ell1".
 *
 * The function returns the sds string pointer, that is always the same
 * as the input pointer since no resize is needed. */
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
    size_t j, i, l = sdslen(s);

    for (j = 0; j < l; j++) {
        for (i = 0; i < setlen; i++) {
            if (s[j] == from[i]) {
                s[j] = to[i];
                break;
            }
        }
    }
    return s;
}

Review comment (suggestion):
So as we now have the internal implementation we can just do (right?):

```suggestion
sds *sdsnsplitargs(const char *line, size_t len, int *argc) {
    const char *end = line + len;
```

Code after:

            /* add the token to the vector */
            if ((size_t)*argc == cap) {
                cap = cap == 0 ? 8 : (cap * 2);
                vector = s_realloc(vector, sizeof(sds) * cap);
            }
            vector[(*argc)++] = current;
            current = NULL;
        } else {
            while ((*argc)--) sdsfree(vector[*argc]);
            s_free(vector);
            *argc = 0;
            return NULL;
        }
    }
    /* Even on empty input string return something not NULL. */
    if (vector == NULL) vector = s_malloc(sizeof(void *));
    return vector;
}

sds *sdssplitargs(const char *line, int *argc) {
    return sdsnsplitargs_internal(line, NULL, argc);
}

sds *sdsnsplitargs(const char *line, size_t len, int *argc) {
    const char *end = line + len;
    return sdsnsplitargs_internal(line, end, argc);
}

/* Modify the string substituting all the occurrences of the set of
 * characters specified in the 'from' string to the corresponding character
 * in the 'to' array.
 *
 * For instance: sdsmapchars(mystring, "ho", "01", 2)
 * will have the effect of turning the string "hello" into "0ell1".
 *
 * The function returns the sds string pointer, that is always the same
 * as the input pointer since no resize is needed. */
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
    size_t j, i, l = sdslen(s);

    for (j = 0; j < l; j++) {
        for (i = 0; i < setlen; i++) {
            if (s[j] == from[i]) {
                s[j] = to[i];
                break;
            }
        }
    }
    return s;
}