/* Destructively modify the sds string 's' to hold the specified binary * safe string pointed by 't' of length 'len' bytes. */ sds sdscpylen(sds s, const char *t, size_t len) { if (sdsalloc(s) < len) { s = sdsMakeRoomFor(s,len-sdslen(s)); if (s == NULL) return NULL; } memcpy(s, t, len); s[len] = '\0'; sdssetlen(s, len); return s; }
int main(void) { { struct sdshdr *sh; sds x = sdsempty(); test_cond("sdsempty() should be strlen 0", strlen(x) == 0 && sdslen(x) == 0 && memcmp(x,"\0",1) == 0); sdsfree(x); x = sdsalloc(NULL, 2); test_cond("Create a NULL string with reserved space 2 bytes", sdslen(x) == 0 && sdsavail(x) == 2); sdsfree(x); } test_report() return 0; }
/* Return the total size of the allocation of the specifed sds string, * including: * 1) The sds header before the pointer. * 2) The string. * 3) The free buffer at the end if any. * 4) The implicit null term. */ size_t sdsAllocSize(sds s) { size_t alloc = sdsalloc(s); return sdsHdrSize(s[-1])+alloc+1; }