您的位置:首页 > 数据库 > Redis

【redis源码分析】动态字符串--sds

2014-05-18 21:36 691 查看
#ifndef __SDS_H
#define __SDS_H

#define SDS_MAX_PREALLOC (1024*1024)

#include <sys/types.h>
#include <stdarg.h>

// sds 类型
typedef char *sds;

// sdshdr 结构,注意这个结构体的大小只有4+4,最后的buf是不占空间的
struct sdshdr {

// buf 已占用长度,相当于strlen(buf);
int len;

// buf 剩余可用长度
int free;

// 实际保存字符串数据的地方
// 利用c99(C99 specification 6.7.2.1.16)中引入的 flexible array member,通过buf来引用sdshdr后面的地址,
// 详情google "flexible array member"
//柔性数组成员,这里是标准写法,如果写成char[0],有些编译器是不通过的
char buf[];
};

/*
* 返回 sds buf 的已占用长度
*/
static inline size_t sdslen(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->len;
}

/*
* 返回 sds buf 的可用长度
*/
static inline size_t sdsavail(const sds s) {
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->free;
}
//创建一个指定长度的sds,如果指定了init,会将init的内容复制到新的sds中,
sds sdsnewlen(const void *init, size_t initlen);
//根据init字符串创建一个新的sds
sds sdsnew(const char *init);
//创建一个只包含空字符串 "" 的 sds(只包含‘\0’)
sds sdsempty();
//返回字符串的长度
size_t sdslen(const sds s);
//复制一个字符串
sds sdsdup(const sds s);
//释放sds,如果为NULL则什么也不做
void sdsfree(sds s);
//返回sds buf中的可用长度
size_t sdsavail(const sds s);
//将 sds 的 buf 扩展至给定长度,无内容部分用 \0 来填充
sds sdsgrowzero(sds s, size_t len);
//根据长度进行字符串拼接
sds sdscatlen(sds s, const void *t, size_t len);
//字符串拼接,其实是调用了sdscatlen(s, t, strlen(t))
sds sdscat(sds s, const char *t);
//拼接两个 sds ,其实是调用了sdscatlen(s, t, sdslen(t))
sds sdscatsds(sds s, const sds t);
//依据长度做字符串copy
sds sdscpylen(sds s, const char *t, size_t len);
//字符串copy
sds sdscpy(sds s, const char *t);

sds sdscatvprintf(sds s, const char *fmt, va_list ap);
#ifdef __GNUC__
sds sdscatprintf(sds s, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#else
sds sdscatprintf(sds s, const char *fmt, ...);
#endif
//裁切字符串的开头和尾部的空格,制表,回车符(由cset指定)
sds sdstrim(sds s, const char *cset);
//将sds裁剪为[start,end]之间的字符串
sds sdsrange(sds s, int start, int end);
//更新给定sds的len和free字段,但前提是之前的sds中的len和free必须是一致正确的,这个函数是用了strlen计算了实际的长度
void sdsupdatelen(sds s);
//清除给定 sds buf 中的内容,让它只包含一个 \0 终结符
void sdsclear(sds s);
//字符串比较
int sdscmp(const sds s1, const sds s2);
//依据sep将s分割,返回 一个二维数组
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
//释放由sdssplitlen函数解析的二维数组
void sdsfreesplitres(sds *tokens, int count);
//sds转换为小写
void sdstolower(sds s);
//sds转换为大写
void sdstoupper(sds s);
//
sds sdsfromlonglong(long long value);
//添加引用字符串,对特殊字符转义,字符串首尾添加引号,这个函数主要用来做参数解析,和下面的sdssplitargs配合使用
sds sdscatrepr(sds s, const char *p, size_t len);
//将由sdscatrepr函数处理的字符串解析成命令参数的形式
sds *sdssplitargs(const char *line, int *argc);
//释放由上面返回的参数的二维数组
void sdssplitargs_free(sds *argv, int argc);

sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);

/* Low level functions exposed to the user API */
//对 sds 的 buf 进行扩展,扩展后的可用长度不少于 addlen 。(如果之前sds的可用长度就大于addlen就什么也不做,否则按照newlen = (len+addlen)的两倍进行扩展)
sds sdsMakeRoomFor(sds s, size_t addlen);
//在 sds buf 的右端添加 incr 个位置(空位)。
void sdsIncrLen(sds s, int incr);
//将sds buf 内多余的空间释放
sds sdsRemoveFreeSpace(sds s);
//返回sds实际分配的空间,free+len
size_t sdsAllocSize(sds s);

#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "sds.h"
#include "zmalloc.h"

/*
* 创建一个指定长度的 sds
* 如果给定了初始化值 init 的话,那么将 init 复制到 sds 的 buf 当中
*
* T = O(N)
*/
sds sdsnewlen(const void *init, size_t initlen) {

struct sdshdr *sh;

// 有 init ?
// O(N)
if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
}

// 内存不足,分配失败
if (sh == NULL) return NULL;

sh->len = initlen;
sh->free = 0;

// 如果给定了 init 且 initlen 不为 0 的话
// 那么将 init 的内容复制至 sds buf
// O(N)
if (initlen && init)
memcpy(sh->buf, init, initlen);

// 加上终结符
sh->buf[initlen] = '\0';

// 返回 buf 而不是整个 sdshdr
return (char*)sh->buf;
}

/*
* 创建一个只包含空字符串 "" 的 sds
*
* T = O(N)
*/
sds sdsempty(void) {
// O(N)
return sdsnewlen("",0);
}

/*
* 根据给定初始化值 init ,创建 sds
* 如果 init 为 NULL ,那么创建一个 buf 内只包含 \0 终结符的 sds
*
* T = O(N)
*/
sds sdsnew(const char *init) {
size_t initlen = (init == NULL) ? 0 : strlen(init);
return sdsnewlen(init, initlen);
}

/*
* 复制给定 sds
*
* T = O(N)
*/
sds sdsdup(const sds s) {
return sdsnewlen(s, sdslen(s));
}

/*
* 释放 sds 所对应的 sdshdr 结构的内存。
*
* 如果 s 为 NULL ,那么不做动作。
*
* T = O(N)
*/
void sdsfree(sds s) {
if (s == NULL) return;
zfree(s-sizeof(struct sdshdr));
}

/*
* 更新给定 sds 所对应的 sdshdr 结构的 free 和 len 属性
*
* T = O(n)
*/
void sdsupdatelen(sds s) {

struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));

// 计算正确的 buf 长度
int reallen = strlen(s);

// 更新属性
sh->free += (sh->len-reallen);
sh->len = reallen;
}

/*
* 清除给定 sds buf 中的内容,让它只包含一个 \0 终结符
*
* T = O(1)
*/
void sdsclear(sds s) {

struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));

sh->free += sh->len;
sh->len = 0;
sh->buf[0] = '\0';
}

/* Enlarge the free space at the end of the sds string so that the caller
* is sure that after calling this function can overwrite up to addlen
* bytes after the end of the string, plus one more byte for nul term.
*
* Note: this does not change the *size* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
/*
* 对 sds 的 buf 进行扩展,扩展后的可用长度不少于 addlen 。(如果之前sds的可用长度就大于addlen就什么也不做,否则按照newlen = (len+addlen)的两倍进行扩展)
*
* T = O(N)
*/
sds sdsMakeRoomFor(
sds s,
size_t addlen   // 需要增加的空间长度
)
{
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s);
size_t len, newlen;

// 剩余空间可以满足需求,无须扩展
if (free >= addlen) return s;

sh = (void*) (s-(sizeof(struct sdshdr)));

// 目前 buf 长度
len = sdslen(s);
// 新 buf 长度
newlen = (len+addlen);
// 如果新 buf 长度小于 SDS_MAX_PREALLOC 长度
// 那么将 buf 的长度设为新 buf 长度的两倍
if (newlen < SDS_MAX_PREALLOC)
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC;

// 扩展长度
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);

if (newsh == NULL) return NULL;

newsh->free = newlen - len;

return newsh->buf;
}

/* Reallocate the sds string so that it has no free space at the end. The
* contained string remains not altered, but next concatenation operations
* will require a reallocation. */
/*
* 在不改动 sds buf 内容的情况下,将 buf 内多余的空间释放出去。
* 在对 sds 执行这个函数之后,下一次对这个 sds 的拼接操作必然需要一次内存分配。
*
* T = O(N)
*/
sds sdsRemoveFreeSpace(sds s) {

struct sdshdr *sh;

sh = (void*) (s-(sizeof(struct sdshdr)));

// 修改 buf 长度为 sh->len + 1
// 不保留任何多余空间
sh = zrealloc(sh, sizeof(struct sdshdr)+sh->len+1);

sh->free = 0;

return sh->buf;
}

/*
* 计算给定 sds buf 的内存长度(包括已使用和未使用的)
*
* T = O(1)
*/
size_t sdsAllocSize(sds s) {

struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));

return sizeof(*sh)+sh->len+sh->free+1;
}

/* Increment the sds length and decrements the left free space at the
* end of the string accordingly to 'incr'. Also set the null term
* in the new end of the string.
*
* This function is used in order to fix the string length after the
* user calls sdsMakeRoomFor(), writes something after the end of
* the current string, and finally needs to set the new length.
*
* Note: it is possible to use a negative increment in order to
* right-trim the string.
*
* Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the
* following schema to cat bytes coming from the kerenl to the end of an
* sds string new things without copying into an intermediate buffer:
*
* oldlen = sdslen(s);
* s = sdsMakeRoomFor(s, BUFFER_SIZE);
* nread = read(fd, s+oldlen, BUFFER_SIZE);
* ... check for nread <= 0 and handle it ...
* sdsIncrLen(s, nhread);
*/
/*
* 在 sds buf 的右端添加 incr 个位置。
* 如果 incr 是负数时,可以作为 right-trim 使用
*
* incr 为正数的例子:
* hdr = sdshdr {
*          len = 10;
*          free = 10;
*          buf = "hello moto\0";
*       }
* sdsIncrLen(hdr->buf, 2);
* hdr = sdshdr {
*          len = 12;
*          free = 8;
*          buf = "hello moto\0[ ]\0";
*       }
* 这里的 [ ] 表示一个任意内容的 byte 。
*
* incr 为负数的例子:
* hdr = sdshdr {
*          len = 10;
*          free = 10;
*          buf = "hello moto\0";
*       }
* sdsIncrLen(hdr->buf, -2);
* hdr = sdshdr {
*          len = 8;
*          free = 12;
*          buf = "hello mo\0";
*       }
*
* T = O(1)
*/
void sdsIncrLen(sds s, int incr) {
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));

assert(sh->free >= incr);
sh->len += incr;
sh->free -= incr;
assert(sh->free >= 0);

s[sh->len] = '\0';
}

/* Grow the sds to have the specified length. Bytes that were not part of
* the original length of the sds will be set to zero. */
/*
* 将 sds 的 buf 扩展至给定长度,无内容部分用 \0 来填充
*
* T = O(N)
*/
sds sdsgrowzero(sds s, size_t len) {

struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));

size_t totlen, curlen = sh->len;

// 现有长度比给定长度要大,无须扩展
if (len <= curlen) return s;

// 扩展 sds
s = sdsMakeRoomFor(s,len-curlen);
if (s == NULL) return NULL;

/* Make sure added region doesn't contain garbage */
// 使用 \0 来填充空位,确保空位中不包含垃圾数据
sh = (void*)(s-(sizeof(struct sdshdr)));
memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */

// 更新 len 和 free 属性
totlen = sh->len + sh->free;
sh->len = len;
sh->free = totlen - sh->len;

return s;
}

/*
* 按长度 len 扩展 sds ,并将 t 拼接到 sds 的末尾
*根据长度进行字符串拼接
* T = O(N)
*/
sds sdscatlen(sds s, const void *t, size_t len) {

struct sdshdr *sh;

size_t curlen = sdslen(s);

// O(N)
s = sdsMakeRoomFor(s,len);
if (s == NULL) return NULL;

// 复制
// O(N)
memcpy(s+curlen, t, len);

// 更新 len 和 free 属性
// O(1)
sh = (void*) (s-(sizeof(struct sdshdr)));
sh->len = curlen+len;
sh->free = sh->free-len;

// 终结符
// O(1)
s[curlen+len] = '\0';

return s;
}

/*
* 将一个 char 数组拼接到 sds 末尾
*
* T = O(N)
*/
sds sdscat(sds s, const char *t) {
return sdscatlen(s, t, strlen(t));
}

/*
* 拼接两个 sds
*
* T = O(N)
*/
sds sdscatsds(sds s, const sds t) {
return sdscatlen(s, t, sdslen(t));
}

/*
* 将一个 char 数组的前 len 个字节复制至 sds
* 如果 sds 的 buf 不足以容纳要复制的内容,
* 那么扩展 buf 的长度,让 buf 的长度大于等于 len 。
*
* T = O(N)
*/
sds sdscpylen(sds s, const char *t, size_t len) {

struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));

// 是否需要扩展 buf ?
size_t totlen = sh->free+sh->len;
if (totlen < len) {
// 扩展 buf 长度,让它的长度大于等于 len
// 具体的大小请参考 sdsMakeRoomFor 的注释
// T = O(N)
s = sdsMakeRoomFor(s,len-sh->len);
if (s == NULL) return NULL;
sh = (void*) (s-(sizeof(struct sdshdr)));
totlen = sh->free+sh->len;
}

// O(N)
memcpy(s, t, len);
s[len] = '\0';

sh->len = len;
sh->free = totlen-len;

return s;
}

/*
* 将一个 char 数组复制到 sds
*/
sds sdscpy(sds s, const char *t) {
return sdscpylen(s, t, strlen(t));
}
//字符串格式化
sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
va_list cpy;
char *buf, *t;
size_t buflen = 16;

while(1) {
buf = zmalloc(buflen);
if (buf == NULL) return NULL;
buf[buflen-2] = '\0';
va_copy(cpy,ap);
vsnprintf(buf, buflen, fmt, cpy);
if (buf[buflen-2] != '\0') {
zfree(buf);
buflen *= 2;
continue;
}
break;
}
t = sdscat(s, buf);
zfree(buf);
return t;
}
//字符串格式化
sds sdscatprintf(sds s, const char *fmt, ...) {
va_list ap;
char *t;
va_start(ap, fmt);
t = sdscatvprintf(s,fmt,ap);
va_end(ap);
return t;
}
//裁切字符串的开头和尾部的空格,制表,回车符(由cset指定,也可以是其他字符)
sds sdstrim(sds s, const char *cset) {
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
char *start, *end, *sp, *ep;
size_t len;

sp = start = s;
ep = end = s+sdslen(s)-1;
//处理开头,strchr(cset,*sp)判断*sp是否在cset中,也就是找到是否有特殊字符,指针前移
while(sp <= end && strchr(cset, *sp)) sp++;
//处理结尾
while(ep > start && strchr(cset, *ep)) ep--;
//求得现在的实际长度
len = (sp > ep) ? 0 : ((ep-sp)+1);
//移动字符串
if (sh->buf != sp) memmove(sh->buf, sp, len);
//重新修改sdshdr结构
sh->buf[len] = '\0';
sh->free = sh->free+(sh->len-len);
sh->len = len;
return s;
}
//将sds裁剪为[start,end]之间的字符串,如果是负数,则将负数加上sds的len,重新输出[start,end]
/*
如对于:”ciao“
sdsrange(1,1) -> i
sdsrange(1,-1) -> sdsrange(1,-1+4)->sdsrange(1,3) -> iao
sdsrange(-2,-1)-> sdsrange(-2+4,-1+4) -> sdsrange(2,3) ->ao
sdsrange(2,1) -> 不合法返回NULL

*/
sds sdsrange(sds s, int start, int end) {
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
size_t newlen, len = sdslen(s);

if (len == 0) return s;
if (start < 0) {
start = len+start;
if (start < 0) start = 0;
}
if (end < 0) {
end = len+end;
if (end < 0) end = 0;
}
newlen = (start > end) ? 0 : (end-start)+1;
if (newlen != 0) {
if (start >= (signed)len) {
newlen = 0;
} else if (end >= (signed)len) {
end = len-1;
newlen = (start > end) ? 0 : (end-start)+1;
}
} else {
start = 0;
}
if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
sh->buf[newlen] = 0;
sh->free = sh->free+(sh->len-newlen);
sh->len = newlen;
return s;
}
//sds转换为小写
void sdstolower(sds s) {
int len = sdslen(s), j;

for (j = 0; j < len; j++) s[j] = tolower(s[j]);
}

/*
* 将给定 sds 中的字符全部转为大写
*/
void sdstoupper(sds s) {
int len = sdslen(s), j;

for (j = 0; j < len; j++) s[j] = toupper(s[j]);
}
/*
*	比较两个sds,实际上就是memcmp
*	当s1<s2时,返回值<0
*	当s1=s2时,返回值=0
*	当s1>s2时,返回值>0
*/
int sdscmp(const sds s1, const sds s2) {
size_t l1, l2, minlen;
int cmp;

l1 = sdslen(s1);
l2 = sdslen(s2);
minlen = (l1 < l2) ? l1 : l2;
cmp = memcmp(s1,s2,minlen);
if (cmp == 0) return l1-l2;
return cmp;
}

/* Split 's' with separator in 'sep'. An array
* of sds strings is returned. *count will be set
* by reference to the number of tokens returned.
*
* On out of memory, zero length string, zero length
* separator, NULL is returned.
*
* Note that 'sep' is able to split a string using
* a multi-character separator. For example
* sdssplit("foo_-_bar","_-_"); will return two
* elements "foo" and "bar".
*
* This version of the function is binary-safe but
* requires length arguments. sdssplit() is just the
* same function but for zero-terminated strings.
*/
//根据sep来分割字符串s,返回一个字符串数组,失败返回NULL,sep可以是多个字符(但是是作为一个整体)
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
int elements = 0, slots = 5, start = 0, j;
sds *tokens;

if (seplen < 1 || len < 0) return NULL;
//根据slot来分配空间
tokens = zmalloc(sizeof(sds)*slots);
if (tokens == NULL) return NULL;

if (len == 0) {
*count = 0;
return tokens;
}
for (j = 0; j < (len-(seplen-1)); j++) {
/* make sure there is room for the next element and the final one */
if (slots < elements+2) {
sds *newtokens;

slots *= 2;
newtokens = zrealloc(tokens,sizeof(sds)*slots);
if (newtokens == NULL) goto cleanup;
tokens = newtokens;
}
/* search the separator */
if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
//分配新空间,并且复制
tokens[elements] = sdsnewlen(s+start,j-start);
//如果返回时NULL说明上一步中,内存分配失败,需要进行清理
if (tokens[elements] == NULL) goto cleanup;
elements++;
start = j+seplen;
j = j+seplen-1; /* skip the separator */
}
}
/* Add the final element. We are sure there is room in the tokens array. */
//添加最后一个元素
tokens[elements] = sdsnewlen(s+start,len-start);
if (tokens[elements] == NULL) goto cleanup;
elements++;
*count = elements;
return tokens;

cleanup:
{
int i;
for (i = 0; i < elements; i++) sdsfree(tokens[i]);
zfree(tokens);
*count = 0;
return NULL;
}
}
//释放由sdssplitlen函数解析的二维数组,调用上一个函数以后需要适时的调用这个函数,否则会造成内存泄露
void sdsfreesplitres(sds *tokens, int count) {
if (!tokens) return;
while(count--)
//先释放没个slot中的内容
sdsfree(tokens[count]);
//释放全部slot本身的空间
zfree(tokens);
}
//将一个long long类型的整数转换为一个sds
sds sdsfromlonglong(long long value) {
char buf[32], *p;
unsigned long long v;

v = (value < 0) ? -value : value;
p = buf+31; /* point to the last character */
do {
*p-- = '0'+(v%10);
v /= 10;
} while(v);
if (value < 0) *p-- = '-';
p++;
//创建sds
return sdsnewlen(p,32-(p-buf));
}
//添加引用字符串,对特殊字符转义,字符串首尾添加引号,这个函数主要用来做参数解析,和下面的sdssplitargs配合使用
sds sdscatrepr(sds s, const char *p, size_t len) {
s = sdscatlen(s,"\"",1);
while(len--) {
switch(*p) {
//对于\和“进行转义,
case '\\':
case '"':
s = sdscatprintf(s,"\\%c",*p);
break;
//对于特殊的制表符和控制字符,进行字符化,之前的一个字符会变成两个字符
case '\n': s = sdscatlen(s,"\\n",2); break;
case '\r': s = sdscatlen(s,"\\r",2); break;
case '\t': s = sdscatlen(s,"\\t",2); break;
case '\a': s = sdscatlen(s,"\\a",2); break;
case '\b': s = sdscatlen(s,"\\b",2); break;
default:
if (isprint(*p))
//可打印字符直接拼接
s = sdscatprintf(s,"%c",*p);
else
//否则的话,两位十六进制输出
s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
break;
}
p++;
}
return sdscatlen(s,"\"",1);
}

/* Helper function for sdssplitargs() that returns non zero if 'c'
* is a valid hex digit. */
//判断是否是16进制字符
int is_hex_digit(char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}

/* Helper function for sdssplitargs() that converts an hex digit into an
* integer from 0 to 15 */
//由十六进制字符转换为其对应的十进制数字
int hex_digit_to_int(char c) {
switch(c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default: return 0;
}
}

/* Split a line into arguments, where every argument can be in the
* following programming-language REPL-alike form:
*
* foo bar "newline are supported\n" and "\xff\x00otherstuff"
*
* The number of arguments is stored into *argc, and an array
* of sds is returned. The caller should sdsfree() all the returned
* strings and finally zfree() the array itself.
*
* Note that sdscatrepr() is able to convert back a string into
* a quoted string in the same format sdssplitargs() is able to parse.
*/
//将由sdscatrepr函数处理的字符串解析成命令参数的形式
sds *sdssplitargs(const char *line, int *argc) {
const char *p = line;
char *current = NULL;
char **vector = NULL;

*argc = 0;
while(1) {
/* skip blanks */
while(*p && isspace(*p)) p++;
if (*p) {
/* get a token */
int inq=0;  /* set to 1 if we are in "quotes" */
int insq=0; /* set to 1 if we are in 'single quotes' */
int done=0;

if (current == NULL) current = sdsempty();
while(!done) {
if (inq) {
if (*p == '\\' && *(p+1) == 'x' &&
is_hex_digit(*(p+2)) &&
is_hex_digit(*(p+3)))
{
unsigned char byte;

byte = (hex_digit_to_int(*(p+2))*16)+
hex_digit_to_int(*(p+3));
current = sdscatlen(current,(char*)&byte,1);
p += 3;
} else if (*p == '\\' && *(p+1)) {
char c;

p++;
switch(*p) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case 'a': c = '\a'; break;
default: c = *p; break;
}
current = sdscatlen(current,&c,1);
} else if (*p == '"') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) goto err;
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
} else {
current = sdscatlen(current,p,1);
}
} else if (insq) {
if (*p == '\\' && *(p+1) == '\'') {
p++;
current = sdscatlen(current,"'",1);
} else if (*p == '\'') {
/* closing quote must be followed by a space or
* nothing at all. */
if (*(p+1) && !isspace(*(p+1))) goto err;
done=1;
} else if (!*p) {
/* unterminated quotes */
goto err;
} else {
current = sdscatlen(current,p,1);
}
} else {
switch(*p) {
case ' ':
case '\n':
case '\r':
case '\t':
case '\0':
done=1;
break;
case '"':
inq=1;
break;
case '\'':
insq=1;
break;
default:
current = sdscatlen(current,p,1);
break;
}
}
if (*p) p++;
}
/* add the token to the vector */
vector = zrealloc(vector,((*argc)+1)*sizeof(char*));
vector[*argc] = current;
(*argc)++;
current = NULL;
} else {
return vector;
}
}

err:
while((*argc)--)
sdsfree(vector[*argc]);
zfree(vector);
if (current) sdsfree(current);
return NULL;
}
//释放由上面返回的参数的二维数组
void sdssplitargs_free(sds *argv, int argc) {
int j;

for (j = 0 ;j < argc; j++) sdsfree(argv[j]);
zfree(argv);
}

/* Modify the string substituting all the occurrences of the set of
* characters specifed in the 'from' string to the corresponding character
* in the 'to' array.
*
* For instance: sdsmapchars(mystring, "ho", "01", 2)
* will have the effect of turning the string "hello" into "0ell1".
*
* The function returns the sds string pointer, that is always the same
* as the input pointer since no resize is needed. */
//将s中出现在from的字符替换成to中对应的字符(下标一致即为对应)
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
size_t j, i, l = sdslen(s);

for (j = 0; j < l; j++) {
for (i = 0; i < setlen; i++) {
if (s[j] == from[i]) {
s[j] = to[i];
break;
}
}
}
return s;
}


总结:总体感觉redis的sds设计的很累赘,将char*,sds,sdshdr等结构没有统一起来,对外提供的接口也有点混乱。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  源码 c语言 redis