redis-dynamic string

redis源码sds.c sdc.h实现动态字符串  (所有的sds字符串都是以NULL为结尾(NULL-termined))

结构体部分

typedef char *sds;//sds 指向真正的字符串的存放位置。字符串所在的地址前面是字符串长度len, 空闲长度free

struct sdshdr {
    int len;
    int free;
    char buf[];
};

lenfreebuf(sds指向的位置)

如果减少了len的值,则会相应的增加free的值。


函数部分

size_t sdslen(const sds s) //字符串长度(逻辑长度)

size_t sdsavail(const sds s);//空闲部分的长度。

sds sdsnewlen(const void *init, size_t initlen)//用init指定的内容&initlen指定的长度创建字符串。返回值为指向字符串的起始地址。

//如果init=NULL 字符串初始化为零字节(calloc分配内存空间并把每个字节初始化为0)。 这里创建的字符串,都会在字符串末尾隐含一个'\0'

//并且字符串的中间可以包含'\0',因为其字符串长度包含在len中。如:mystring = sdsnewlen("abc",3");

sds sdsempty(void) 创建空的字符串,并以null结束。

sds sdsnew(const char *init)// 如 init 为"abcd" Create a new sds string starting from a null termined C string.

sds sdsdup(const sds s)  复制一个sds 字符串

void sdsupdatelen(sds s) //以字符串中首次出现的NULL前的字符的长度为准。

/*s = sdsnew("foobar");
 * s[2] = '\0';
 * sdsupdatelen(s);
 * printf("%d\n", sdslen(s)) 

*/输出为2 。如果注释了sdsupdatelen(s);则为6.

void sdsclear(sds s)  //清空,并把空间作为空闲空间使用。

sds sdsMakeRoomFor(sds s, size_t addlen)//加大空闲空间。采用预分配方式。

sds sdsRemoveFreeSpace(sds s)//移除空闲空间

在调用这个函数,传递进去的sds字符串已经无效(这里用了realloc重新分配空间),sds字符串用这个函数的返回值。

size_t sdsAllocSize(sds s) 返回sds 字符串分配的总大小。包括sds 头 + string+ 空闲buff+隐含的null占用的一个字节。

void sdsIncrLen(sds s, int incr)  增加shshdr 字段中len为orglen + incr ,怎free为orgfree-incr。 同时也在字符串的新的末端加上'\0'

//该函数主要是用于在调用sdsMakeRoomFor之后,固定字符串的长度。 是用来在字符串 末端之后添加字符串之后,需修改sds的长度

incr<0 裁剪字符串。

Usage example:

 * oldlen = sdslen(s);
 * s = sdsMakeRoomFor(s, BUFFER_SIZE);
 * nread = read(fd, s+oldlen, BUFFER_SIZE);
 * ... check for nread <= 0 and handle it ...
 * sdsIncrLen(s, nread);


sds sdsgrowzero(sds s, size_t len) //len为修改之后总的字符串的长度。 如果len小于当前字符串的长度,不执行任何操作。新增加的空间的每个元素会被置为0

sds sdscatlen(sds s, const void *t, size_t len) //添加长度为len,由指针t指向的内容到sds 字符串s的末尾。

sds sdscat(sds s, const char *t)       //Append the specified null termianted C string to the sds string 's'.

sds sdscatsds(sds s, const sds t)  Append the specified sds 't' to the existing sds 's'.

sds sdscpylen(sds s, const char *t, size_t len)//  长度为len,由指针t指向的内容复制到sds 字符串

sds sdscpy(sds s, const char *t) //Like sdscpylen() but 't' must be a null-termined string so that the length of the string is obtained with strlen(). 


sds sdscatvprintf(sds s, const char *fmt, va_list ap)
sds sdscatprintf(sds s, const char *fmt, ...) 




void sdstolower(sds s) //  Apply tolower() to every character of the sds string 's'.
void sdstoupper(sds s)//Apply toupper() to every character of the sds string 's'. 
int sdscmp(const sds s1, const sds s2)//Compare two sds strings s1 and s2 with memcmp().
//strchr函数原型:extern char *strchr(const char *s,char c);查找字符串s中首次出现字符c的位置。
//该函数区分大小写。如果想要不区分大小写,请使用 stristr()。
sds sdstrim(sds s, const char *cset)//


void sdsrange(sds s, int start, int end)//end(-1开始) //从start(0开始)到倒数第-end个位置。包含start和end被包含在子字符串中
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) 
//Split 's' with separator in 'sep'. 返回的是sds数组,*count为分割后的字符串的数量
 //sep可以为多字符分割符号
void sdsfreesplitres(sds *tokens, int count) ///* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
sds sdsfromlonglong(long long value)//Create an sds string from a long long value. 它比 sdscatprintf(sdsempty(),"%lld\n", value)效率要快
sds sdscatrepr(sds s, const char *p, size_t len) //添加转义字符到sds 字符串中

sds *sdssplitargs(const char *line, int *argc)

sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen)//用to字符替换sds字符串中出现的from。

sds sdsjoin(char **argv, int argc, char *sep) //连接一个指定的字符串数组成字符串,用sep分隔符来进行分割。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值