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

Code before:
/* Create a new sds string starting from a null terminated C string. */
sds sdsnew(const char *init) {
    size_t initlen = (init == NULL) ? 0 : strlen(init);
    return sdsnewlen(init, initlen);
}

/* Duplicate an sds string. */
sds sdsdup(const sds s) {
    return sdsnewlen(s, sdslen(s));
}

/*
 * This method returns the minimum amount of bytes required to store the sds (header + data + NULL terminator).
 */
static inline size_t sdsminlen(sds s) {
    return sdslen(s) + sdsHdrSize(s[-1]) + 1;
}

/* This method copies the sds `s` into `buf` which is the target character buffer. */
size_t sdscopytobuffer(unsigned char *buf, size_t buf_len, sds s, uint8_t *hdr_size) {
    size_t reqd_keylen = sdsminlen(s);
    if (buf == NULL) {
        return reqd_keylen;
    }
    assert(buf_len >= reqd_keylen);
    memcpy(buf, sdsAllocPtr(s), reqd_keylen);
    *hdr_size = sdsHdrSize(s[-1]);
    return reqd_keylen;
}

/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) {
    if (s == NULL) return;
    s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s));
}

/* Set the sds string length to the length as obtained with strlen(), so
 * considering as content only up to the first null term character.
 *
 * This function is useful when the sds string is hacked manually in some
 * way, like in the following example:
 *
 * s = sdsnew("foobar");
 * s[2] = '\0';
 * sdsupdatelen(s);
 * printf("%d\n", sdslen(s));
 *
 * The output will be "2", but if we comment out the call to sdsupdatelen()
 * the output will be "6" as the string was modified but the logical length
 * remains 6 bytes. */
void sdsupdatelen(sds s) {

Review comment (suggestion):
```suggestion
    size_t required_keylen = sdsminlen(s);
    if (buf == NULL) {
        return required_keylen;
    }
    assert(buf_len >= required_keylen);
    memcpy(buf, sdsAllocPtr(s), required_keylen);
```
I don't think we should use abbreviations unless they are consistently used. 

Code after:
/* Create a new sds string starting from a null terminated C string. */
sds sdsnew(const char *init) {
    size_t initlen = (init == NULL) ? 0 : strlen(init);
    return sdsnewlen(init, initlen);
}

/* Duplicate an sds string. */
sds sdsdup(const sds s) {
    return sdsnewlen(s, sdslen(s));
}

/*
 * This method returns the minimum amount of bytes required to store the sds (header + data + NULL terminator).
 */
static inline size_t sdsminlen(sds s) {
    return sdslen(s) + sdsHdrSize(s[-1]) + 1;
}

/* This method copies the sds `s` into `buf` which is the target character buffer. */
size_t sdscopytobuffer(unsigned char *buf, size_t buf_len, sds s, uint8_t *hdr_size) {
    size_t required_keylen = sdsminlen(s);
    if (buf == NULL) {
        return required_keylen;
    }
    assert(buf_len >= required_keylen);
    memcpy(buf, sdsAllocPtr(s), required_keylen);
    *hdr_size = sdsHdrSize(s[-1]);
    return required_keylen;
}

/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) {
    if (s == NULL) return;
    s_free_with_size(sdsAllocPtr(s), sdsAllocSize(s));
}

/* Set the sds string length to the length as obtained with strlen(), so
 * considering as content only up to the first null term character.
 *
 * This function is useful when the sds string is hacked manually in some
 * way, like in the following example:
 *
 * s = sdsnew("foobar");
 * s[2] = '\0';
 * sdsupdatelen(s);
 * printf("%d\n", sdslen(s));
 *
 * The output will be "2", but if we comment out the call to sdsupdatelen()
 * the output will be "6" as the string was modified but the logical length
 * remains 6 bytes. */
void sdsupdatelen(sds s) {