首页 > 代码库 > SDS(Simple Dynamic String)

SDS(Simple Dynamic String)

 

 

+--------+-------------------------------+-----------+| Header | Binary safe C alike string... | Null term |+--------+-------------------------------+-----------+         |         Pointer returned to the user.

 

1、如果字符串长度小于2^5(32),类型SDS_TYPE_5;

2、如果字符串大于2^5(32),小于2^8(256),类型SDS_TYPE_8;

3、如果字符串大于2^8(256),小于2^16(65536),类型SDS_TYPE_16;

4、如果字符串大于2^16(65536),小于2^32(4294967296),类型SDS_TYPE_32;

5、如果大于2^32(4294967296),类型SDS_TYPE_64;

static inline char sdsReqType(size_t string_size) {    if (string_size < 1<<5)        return SDS_TYPE_5;    if (string_size < 1<<8)        return SDS_TYPE_8;    if (string_size < 1<<16)        return SDS_TYPE_16;    if (string_size < 1ll<<32)        return SDS_TYPE_32;    return SDS_TYPE_64;}

 

 

 计算动态字符长度,

static inline size_t sdslen(const sds s) {    unsigned char flags = s[-1];    switch(flags&SDS_TYPE_MASK) {        case SDS_TYPE_5:            return SDS_TYPE_5_LEN(flags);        case SDS_TYPE_8:            return SDS_HDR(8,s)->len;        case SDS_TYPE_16:            return SDS_HDR(16,s)->len;        case SDS_TYPE_32:            return SDS_HDR(32,s)->len;        case SDS_TYPE_64:            return SDS_HDR(64,s)->len;    }    return 0;}

 

 

 

下面代码是计算SDS头部的长度,

static inline int sdsHdrSize(char type) {    switch(type&SDS_TYPE_MASK) {        case SDS_TYPE_5:            return sizeof(struct sdshdr5);        case SDS_TYPE_8:            return sizeof(struct sdshdr8);        case SDS_TYPE_16:            return sizeof(struct sdshdr16);        case SDS_TYPE_32:            return sizeof(struct sdshdr32);        case SDS_TYPE_64:            return sizeof(struct sdshdr64);    }    return 0;}

 

SDS(Simple Dynamic String)