Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions sds.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ sds sdsnew(const char *init) {
return sdsnewlen(init, initlen);
}

/* Create a new binary sds from hexadecimal string representation.
* hexstr MUST be NULL terminated, of even length, with lower-case hex
* letters and may begin with '0x' */
sds sdsnewbin(const char* hexstr) {
size_t sz = strlen(hexstr);
if (sz % 2) return NULL;
sds val = sdsnewlen(SDS_NOINIT, sz / 2);
const char* pos = hexstr;
if (pos[0] == '0' && pos[1] == 'x') {
pos += 2;
sz -= 2;
}
for (size_t i = 0; i < sz / 2; i++) {
if (sscanf(pos, "%2hhx", &val[i]) != 1) {
sdsfree(val);
return NULL;
}
pos += 2;
}
return val;
}

/* Duplicate an sds string. */
sds sdsdup(const sds s) {
return sdsnewlen(s, sdslen(s));
Expand Down
1 change: 1 addition & 0 deletions sds.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ static inline void sdssetalloc(sds s, size_t newlen) {
sds sdsnewlen(const void *init, size_t initlen);
sds sdsnew(const char *init);
sds sdsempty(void);
sds sdsnewbin(const char* hexstr);
sds sdsdup(const sds s);
void sdsfree(sds s);
sds sdsgrowzero(sds s, size_t len);
Expand Down