Redis 簡單動態字符串
1.介紹
Redis兼容傳統的C語言字符串類型,但沒有直接使用C語言的傳統的字符串(以’\0’結尾的字符數組)表示,而是自己構建了一種名爲簡單動態字符串(simple dynamic string,SDS)的對象。簡單動態字符串在Redis數據庫中應用很廣泛,例如:鍵值對在底層就是由SDS實現的。
在redis種,有一種數據類型叫string類型,而string類型簡單的說就是SDS實現的(簡單理解),先通過幾個命令來感受一下string類型:redis字符串類型命令的介紹
127.0.0.1:6379> SET str1 Redis //設置key:value = str1:Redis
OK
127.0.0.1:6379> GET str1 //獲取str1的value
"Redis"
127.0.0.1:6379> TYPE str1 //獲取key的存儲類型 string類型
string
127.0.0.1:6379> STRLEN str1 //str1的長度爲5字節
(integer) 5
2.SDS的定義
SDS定義在redis源碼根目錄下的sds.h/sdshdr
typedef char *sds;
//sds兼容傳統C風格字符串,所以起了個別名叫sds,並且可以存放sdshdr結構buf成員的地址
SDS也有一個表頭(header)用來存放sds的信息。
struct sdshdr {
int len; //buf中已佔用空間的長度
int free; //buf中剩餘可用空間的長度
char buf[]; //初始化sds分配的數據空間,而且是柔性數組(Flexible array member)
};
關於柔型數組可以看陳皓的一篇文章:C語言結構體裏的成員數組和指針
根據這個結構體,我們用圖大概表示一下str1,如下圖:
- len爲5,表示這個sds長度爲5字節。
- free爲2,表示這個sds還有2個字節未使用的空間。
- buf是一個char[]的數組,分配了(len+1+free)個字節的長度,前len個字節保存着’R’、’e’、’d’、’i’、’s’這5個字符,接下來的1個字節保存着’\0’,剩下的free個字節未使用。
3. SDS的優點
SDS本質上就是char *,因爲有了表頭sdshdr結構的存在,所以SDS比傳統C字符串在某些方面更加優秀,並且能夠兼容傳統C字符串。
3.1 兼容C的部分函數
因爲SDS兼容傳統的C字符串,採用以’\0’作爲結尾,所以SDS就能夠使用一部分
3.2 二進制安全(Binary Safe)
因爲傳統C字符串符合ASCII編碼,這種編碼的操作的特點就是:遇零則止 。即,當讀一個字符串時,只要遇到’\0’結尾,就認爲到達末尾,就忽略’\0’結尾以後的所有字符。因此,如果傳統字符串保存圖片,視頻等二進制文件,操作文件時就被截斷了。
而SDS表頭的buf被定義爲字節數組,因爲判斷是否到達字符串結尾的依據則是表頭的len成員,這意味着它可以存放任何二進制的數據和文本數據,包括’\0’,如下圖:
3.3 獲得字符串長度的操作複雜度爲O(1)
傳統的C字符串獲得長度時的做法:遍歷字符串的長度,遇零則止,複雜度爲O(n)。
而SDS表頭的len成員就保存着字符串長度,所以獲得字符串長度的操作複雜度爲O(1)。
3.4 杜絕緩衝區溢出
因爲SDS表頭的free成員記錄着buf字符數組中未使用空間的字節數,所以,在進行APPEND命令向字符串後追加字符串時,如果不夠用會先進行內存擴展,在進行追加。
總之,正是因爲表頭的存在,使得redis的字符串有這麼多優點。
4. SDS源碼剖析
4.1 SDS內存分配策略—空間預分配
空間預分配策略用於優化SDS的字符串增長操作。
- 如果對SDS進行修改後,SDS表頭的len成員小於1MB,那麼就會分配和len長度相同的未使用空間。free和len成員大小相等。
- 如果對SDS進行修改後,SDS的長度大於等於1MB,那麼就會分配1MB的未使用空間。
通過空間預分配策略,Redis可以減少連續執行字符串增長操作所需的內存重分配次數。
源代碼如下:
sds sdsMakeRoomFor(sds s, size_t addlen) { //對 sds 中 buf 的長度進行擴展
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s); //獲得s的未使用空間長度
size_t len, newlen;
//free的長度夠用不用擴展直接返回
if (free >= addlen) return s;
//free長度不夠用,需要擴展
len = sdslen(s); //獲得s字符串的長度
sh = (void*) (s-(sizeof(struct sdshdr))); //獲取表頭地址
newlen = (len+addlen); //擴展後的新長度
//空間預分配
//#define SDS_MAX_PREALLOC (1024*1024)
//預先分配內存的最大長度爲 1MB
if (newlen < SDS_MAX_PREALLOC) //新長度小於“最大預分配長度”,就直接將擴展的新長度乘2
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC; //新長度大於“最大預分配長度”,就在加上一個“最大預分配長度”
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); //獲得新的擴展空間的地址
if (newsh == NULL) return NULL;
newsh->free = newlen - len; //更新新空間的未使用的空間free
return newsh->buf;
}
4.2 SDS內存釋放策略—惰性空間釋放
惰性空間釋放用於優化SDS的字符串縮短操作。
- 當要縮短SDS保存的字符串時,程序並不立即使用內存充分配來回收縮短後多出來的字節,而是使用表頭的free成員將這些字節記錄起來,並等待將來使用。
源代碼如下:
void sdsclear(sds s) { //重置sds的buf空間,懶惰釋放
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
sh->free += sh->len; //表頭free成員+已使用空間的長度len = 新的free
sh->len = 0; //已使用空間變爲0
sh->buf[0] = '\0'; //字符串置空
}
4.3 Redis源碼註釋
在sds.h文件中,有兩個static inline的函數,分別是sdslen和sdsavail函數,你可以把它認爲是一個static的函數,加上了inline的屬性。而inline關鍵字僅僅是建議編譯器做內聯展開處理,而不是強制。
在sds.c中,幾乎所有的函數所傳的參數都是sds類型,而非表頭sdshdr的地址,但是使用了通過sds指針運算從而求得表頭的地址的技巧,因爲sds是指向sdshdr結構buf成員的。通過sds.h/sdslen函數,來分析:
這裏的關鍵就是sds類型是指向sdshdr結構buf成員。
- struct sdshdr結構共有三個變量,其中sds指向的buf成員是一個柔性數組,它僅僅起到佔位符的作用,並不佔用該結構體的大小,因此sizeof(sizeof(struct sdshdr))大小爲8字節。
- 由於一個SDS類型的內存是通過動態內存分配的,所以它的內存在堆區,堆由下往上增長,因此sds指針減區sizeof(struct sdshdr)的大小就得到了表頭的地址,然後就可以通過”->”訪問表頭的成員。如下圖:
static inline size_t sdslen(const sds s) { //計算buf中字符串的長度 struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr))); //s指針地址減去結構體大小就是結構體的地址 return sh->len; }
通過這種技巧,將表頭結構隱藏起來,只對外公開sds類型。
以下源碼註釋可以訪問放在這裏:sds.c和sds.h源碼註釋
- sds.h文件註釋
#ifndef __SDS_H
#define __SDS_H
#define SDS_MAX_PREALLOC (1024*1024) //預先分配內存的最大長度爲1MB
#include <sys/types.h>
#include <stdarg.h>
typedef char *sds; //sds兼容傳統C風格字符串,所以起了個別名叫sds,並且可以存放sdshdr結構buf成員的地址
struct sdshdr {
unsigned int len; //buf中已佔用空間的長度
unsigned int free; //buf中剩餘可用空間的長度
char buf[]; //初始化sds分配的數據空間,而且是柔性數組(Flexible array member)
};
static inline size_t sdslen(const sds s) { //計算buf中字符串的長度
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->len;
}
static inline size_t sdsavail(const sds s) { //計算buf中的未使用空間的長度
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
return sh->free;
}
sds sdsnewlen(const void *init, size_t initlen); //創建一個長度爲initlen的字符串,並保存init字符串中的值
sds sdsnew(const char *init); //創建一個默認長度的字符串
sds sdsempty(void); //建立一個只有表頭,字符串爲空"\0"的sds
size_t sdslen(const sds s); //計算buf中字符串的長度
sds sdsdup(const sds s); //拷貝一份s的副本
void sdsfree(sds s); //釋放s字符串和表頭
size_t sdsavail(const sds s); //計算buf中的未使用空間的長度
sds sdsgrowzero(sds s, size_t len); //將sds擴展制定長度並賦值爲0
sds sdscatlen(sds s, const void *t, size_t len); //將字符串t追加到s表頭的buf末尾,追加len個字節
sds sdscat(sds s, const char *t); //將t字符串拼接到s的末尾
sds sdscatsds(sds s, const sds t); //將sds追加到s末尾
sds sdscpylen(sds s, const char *t, size_t len); //將字符串t覆蓋到s表頭的buf中,拷貝len個字節
sds sdscpy(sds s, const char *t); //將字符串覆蓋到s表頭的buf中
sds sdscatvprintf(sds s, const char *fmt, va_list ap); //打印函數,被 sdscatprintf 所調用
#ifdef __GNUC__
sds sdscatprintf(sds s, const char *fmt, ...) //打印任意數量個字符串,並將這些字符串追加到給定 sds 的末尾
__attribute__((format(printf, 2, 3)));
#else
sds sdscatprintf(sds s, const char *fmt, ...); //打印任意數量個字符串,並將這些字符串追加到給定 sds 的末尾
#endif
sds sdscatfmt(sds s, char const *fmt, ...); //格式化打印多個字符串,並將這些字符串追加到給定 sds 的末尾
sds sdstrim(sds s, const char *cset); //去除sds中包含有 cset字符串出現字符 的字符
void sdsrange(sds s, int start, int end); //根據start和end區間截取字符串
void sdsupdatelen(sds s); //更新字符串s的長度
void sdsclear(sds s); //將字符串重置保存空間,懶惰釋放
int sdscmp(const sds s1, const sds s2); //比較兩個sds的大小,相等返回0
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); //使用長度爲seplen的sep分隔符對長度爲len的s進行分割,返回一個sds數組的地址,*count被設置爲數組元素數量
void sdsfreesplitres(sds *tokens, int count); //釋放tokens中的count個sds元素
void sdstolower(sds s); //將sds字符串所有字符轉換爲小寫
void sdstoupper(sds s); //將sds字符串所有字符轉換爲大寫
sds sdsfromlonglong(long long value); //根據long long value創建一個SDS
sds sdscatrepr(sds s, const char *p, size_t len); //將長度爲len的字符串p以帶引號""的格式追加到s末尾
sds *sdssplitargs(const char *line, int *argc); //參數拆分,主要用於 config.c 中對配置文件進行分析。
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); //將s中所有在 from 中的字符串,替換成 to 中的字符串
sds sdsjoin(char **argv, int argc, char *sep); //以分隔符連接字符串子數組構成新的字符串
/* Low level functions exposed to the user API */
sds sdsMakeRoomFor(sds s, size_t addlen); //對 sds 中 buf 的長度進行擴展
void sdsIncrLen(sds s, int incr); //根據incr的正負,移動字符串末尾的'\0'標誌
sds sdsRemoveFreeSpace(sds s); //回收sds中的未使用空間
size_t sdsAllocSize(sds s); //獲得sds所有分配的空間
#endif
- sds.c文件註釋
/* Create a new sds string with the content specified by the 'init' pointer
* and 'initlen'.
* If NULL is used for 'init' the string is initialized with zero bytes.
*
* The string is always null-termined (all the sds strings are, always) so
* even if you create an sds string with:
*
* mystring = sdsnewlen("abc",3);
*
* You can print the string with printf() as there is an implicit \0 at the
* end of the string. However the string is binary safe and can contain
* \0 characters in the middle, as the length is stored in the sds header. */
sds sdsnewlen(const void *init, size_t initlen) { //創建一個長度爲initlen的字符串,並保存init字符串中的值
struct sdshdr *sh;
if (init) {
sh = zmalloc(sizeof(struct sdshdr)+initlen+1); //申請空間:表頭+initlen長度+'\0'
} else {
sh = zcalloc(sizeof(struct sdshdr)+initlen+1); //如果init爲空,則將申請的空間初始化爲0
}
if (sh == NULL) return NULL;
sh->len = initlen; //設置表頭的len成員
sh->free = 0; //設置free,新的sds不預留任何空間
if (initlen && init)
memcpy(sh->buf, init, initlen); //將指定的字符串init拷貝到表頭的buf中
sh->buf[initlen] = '\0'; //以'\0'結尾
return (char*)sh->buf;
}
/* Create an empty (zero length) sds string. Even in this case the string
* always has an implicit null term. */
sds sdsempty(void) { //建立一個只有表頭,字符串爲空"\0"的sds
return sdsnewlen("",0);
}
/* Create a new sds string starting from a null terminated C string. */
sds sdsnew(const char *init) { //根據字符串init,創建一個與init一樣長度字符串的sds(表頭+buf)
size_t initlen = (init == NULL) ? 0 : strlen(init);
return sdsnewlen(init, initlen);
}
/* Duplicate an sds string. */
sds sdsdup(const sds s) { //拷貝一份s的副本
return sdsnewlen(s, sdslen(s));
}
/* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) { //釋放s字符串和表頭
if (s == NULL) return;
zfree(s-sizeof(struct sdshdr));
}
/* Set the sds string length to the length as obtained with strlen(), so
* considering as content only up to the first null term character.
*
* This function is useful when the sds string is hacked manually in some
* way, like in the following example:
*
* s = sdsnew("foobar");
* s[2] = '\0';
* sdsupdatelen(s);
* printf("%d\n", sdslen(s));
*
* The output will be "2", but if we comment out the call to sdsupdatelen()
* the output will be "6" as the string was modified but the logical length
* remains 6 bytes. */
void sdsupdatelen(sds s) { //更新字符串s的長度
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
int reallen = strlen(s);
sh->free += (sh->len-reallen);
sh->len = reallen;
}
/* Modify an sds string in-place to make it empty (zero length).
* However all the existing buffer is not discarded but set as free space
* so that next append operations will not require allocations up to the
* number of bytes previously available. */
void sdsclear(sds s) { //將字符串重置保存空間,懶惰釋放
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
sh->free += sh->len; //表頭free成員+已使用空間的長度len = 新的free
sh->len = 0; //已使用空間變爲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 *length* of the sds string as returned
* by sdslen(), but only the free buffer space we have. */
sds sdsMakeRoomFor(sds s, size_t addlen) { //對 sds 中 buf 的長度進行擴展
struct sdshdr *sh, *newsh;
size_t free = sdsavail(s); //獲得s的未使用空間長度
size_t len, newlen;
if (free >= addlen) return s; //free的長度夠用不用擴展直接返回
//free長度不夠用,需要擴展
len = sdslen(s); //獲得s字符串的長度
sh = (void*) (s-(sizeof(struct sdshdr)));
newlen = (len+addlen); //擴展後的新長度
if (newlen < SDS_MAX_PREALLOC) //新長度小於“最大預分配長度”,就直接將擴展的新長度乘2
newlen *= 2;
else
newlen += SDS_MAX_PREALLOC; //新長度大於“最大預分配長度”,就在加上一個“最大預分配長度”
newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); //獲得新的擴展空間的地址
if (newsh == NULL) return NULL;
newsh->free = newlen - len; //更新新空間的未使用的空間free
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.
*
* After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdsRemoveFreeSpace(sds s) { //回收sds中的未使用空間
struct sdshdr *sh;
sh = (void*) (s-(sizeof(struct sdshdr))); //獲得s表頭的地址
sh = zrealloc(sh, sizeof(struct sdshdr)+sh->len+1); //只分配表頭len成員大小的空間
sh->free = 0; //更新free成員
return sh->buf;
}
/* 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) { //獲得sds所分配的空間
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
return sizeof(*sh)+sh->len+sh->free+1; //表頭空間+len+free+'\0'
}
/* Increment the sds length and decrements the left free space at the
* end of the string according 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.
*
* Usage example:
*
* Using sdsIncrLen() and sdsMakeRoomFor() it is possible to mount the
* following schema, to cat bytes coming from the kernel to the end of an
* sds string 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, nread);
*/
void sdsIncrLen(sds s, int incr) { //根據incr的正負,移動字符串末尾的'\0'標誌
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
if (incr >= 0)
assert(sh->free >= (unsigned int)incr); //保證free的空間大於等於要擴展的空間,否則直接終止程序
else
assert(sh->len >= (unsigned int)(-incr)); //保證len的空間長度大於incr絕對值的長度
sh->len += incr; //更新len、free和'\0'的位置
sh->free -= incr;
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.
*
* if the specified length is smaller than the current length, no operation
* is performed. */
sds sdsgrowzero(sds s, size_t len) { //將sds擴展制定長度並賦值爲0
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
size_t totlen, curlen = sh->len;
if (len <= curlen) return s;
s = sdsMakeRoomFor(s,len-curlen); //擴展字符串sds
if (s == NULL) return NULL;
/* Make sure added region doesn't contain garbage */
sh = (void*)(s-(sizeof(struct sdshdr))); //獲得表頭地址
memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */ //用0來填充擴展的空間
//更新表頭
totlen = sh->len+sh->free; //總長度
sh->len = len; //使用空間的長度
sh->free = totlen-sh->len; //總長度-使用空間的長度 = 爲使用空間的長度
return s;
}
/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
* end of the specified sds string 's'.
*
* After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscatlen(sds s, const void *t, size_t len) { //將字符串t追加到s表頭的buf末尾,追加len個字節
struct sdshdr *sh;
size_t curlen = sdslen(s); //原有的長度
s = sdsMakeRoomFor(s,len); //擴展空間
if (s == NULL) return NULL;
sh = (void*) (s-(sizeof(struct sdshdr)));
memcpy(s+curlen, t, len); //字符串拼接
//更新屬性
sh->len = curlen+len;
sh->free = sh->free-len;
s[curlen+len] = '\0';
return s;
}
/* Append the specified null termianted C string to the sds string 's'.
*
* After the call, the passed sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscat(sds s, const char *t) { //將t字符串拼接到s的末尾
return sdscatlen(s, t, strlen(t));
}
/* Append the specified sds 't' to the existing sds 's'.
*
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscatsds(sds s, const sds t) { //將sds追加到s末尾
return sdscatlen(s, t, sdslen(t));
}
/* 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) { //將字符串t覆蓋到s表頭的buf中,拷貝len個字節
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
size_t totlen = sh->free+sh->len; //獲得總長度
if (totlen < len) { //總長度小於len
s = sdsMakeRoomFor(s,len-sh->len); //擴展l空間
if (s == NULL) return NULL;
sh = (void*) (s-(sizeof(struct sdshdr)));
totlen = sh->free+sh->len; //更新總長度
}
memcpy(s, t, len); //拷貝字符串t覆蓋s原有的字符串
//更新表頭
s[len] = '\0';
sh->len = len;
sh->free = totlen-len;
return s;
}
/* Like sdscpylen() but 't' must be a null-termined string so that the length
* of the string is obtained with strlen(). */
sds sdscpy(sds s, const char *t) { //將字符串覆蓋到s表頭的buf中
return sdscpylen(s, t, strlen(t));
}
/* Helper for sdscatlonglong() doing the actual number -> string
* conversion. 's' must point to a string with room for at least
* SDS_LLSTR_SIZE bytes.
*
* The function returns the length of the null-terminated string
* representation stored at 's'. */
#define SDS_LLSTR_SIZE 21
int sdsll2str(char *s, long long value) { //將一個long long類型的value轉化爲字符串,返回字符串長度
char *p, aux;
unsigned long long v;
size_t l;
/* Generate the string representation, this method produces
* an reversed string. */
v = (value < 0) ? -value : value;
p = s;
do {
*p++ = '0'+(v%10);
v /= 10;
} while(v);
if (value < 0) *p++ = '-';
/* Compute length and add null term. */
l = p-s;
*p = '\0';
/* Reverse the string. */
p--;
while(s < p) {
aux = *s;
*s = *p;
*p = aux;
s++;
p--;
}
return l;
}
/* Identical sdsll2str(), but for unsigned long long type. */
int sdsull2str(char *s, unsigned long long v) {//將一個unsigned long long類型的value轉化爲字符串,返回字符串長度
char *p, aux;
size_t l;
/* Generate the string representation, this method produces
* an reversed string. */
p = s;
do {
*p++ = '0'+(v%10);
v /= 10;
} while(v);
/* Compute length and add null term. */
l = p-s;
*p = '\0';
/* Reverse the string. */
p--;
while(s < p) {
aux = *s;
*s = *p;
*p = aux;
s++;
p--;
}
return l;
}
/* Create an sds string from a long long value. It is much faster than:
*
* sdscatprintf(sdsempty(),"%lld\n", value);
*/
sds sdsfromlonglong(long long value) { //根據long long value創建一個SDS
char buf[SDS_LLSTR_SIZE];
int len = sdsll2str(buf,value); //返回裝換成字符串的長度
return sdsnewlen(buf,len); //創建SDS
}
/* Like sdscatprintf() but gets va_list instead of being variadic. */
sds sdscatvprintf(sds s, const char *fmt, va_list ap) { //打印函數,被 sdscatprintf 所調用
va_list cpy;
char staticbuf[1024], *buf = staticbuf, *t;
size_t buflen = strlen(fmt)*2;
/* We try to start using a static buffer for speed.
* If not possible we revert to heap allocation. */
if (buflen > sizeof(staticbuf)) {
buf = zmalloc(buflen);
if (buf == NULL) return NULL;
} else {
buflen = sizeof(staticbuf);
}
/* Try with buffers two times bigger every time we fail to
* fit the string in the current buffer size. */
while(1) {
buf[buflen-2] = '\0';
va_copy(cpy,ap);
vsnprintf(buf, buflen, fmt, cpy);
va_end(cpy);
if (buf[buflen-2] != '\0') {
if (buf != staticbuf) zfree(buf);
buflen *= 2;
buf = zmalloc(buflen);
if (buf == NULL) return NULL;
continue;
}
break;
}
/* Finally concat the obtained string to the SDS string and return it. */
t = sdscat(s, buf);
if (buf != staticbuf) zfree(buf);
return t;
}
/* Append to the sds string 's' a string obtained using printf-alike format
* specifier.
*
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call.
*
* Example:
*
* s = sdsnew("Sum is: ");
* s = sdscatprintf(s,"%d+%d = %d",a,b,a+b).
*
* Often you need to create a string from scratch with the printf-alike
* format. When this is the need, just use sdsempty() as the target string:
*
* s = sdscatprintf(sdsempty(), "... your format ...", args);
*/
sds sdscatprintf(sds s, const char *fmt, ...) { //打印任意數量個字符串,並將這些字符串追加到給定 sds 的末尾
va_list ap;
char *t;
va_start(ap, fmt);
t = sdscatvprintf(s,fmt,ap);
va_end(ap);
return t;
}
/* This function is similar to sdscatprintf, but much faster as it does
* not rely on sprintf() family functions implemented by the libc that
* are often very slow. Moreover directly handling the sds string as
* new data is concatenated provides a performance improvement.
*
* However this function only handles an incompatible subset of printf-alike
* format specifiers:
*
* %s - C String
* %S - SDS string
* %i - signed int
* %I - 64 bit signed integer (long long, int64_t)
* %u - unsigned int
* %U - 64 bit unsigned integer (unsigned long long, uint64_t)
* %% - Verbatim "%" character.
*/
sds sdscatfmt(sds s, char const *fmt, ...) { //格式化打印多個字符串,並將這些字符串追加到給定 sds 的末尾
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
size_t initlen = sdslen(s);
const char *f = fmt;
int i;
va_list ap;
va_start(ap,fmt);
f = fmt; /* Next format specifier byte to process. */
i = initlen; /* Position of the next byte to write to dest str. */
while(*f) {
char next, *str;
unsigned int l;
long long num;
unsigned long long unum;
/* Make sure there is always space for at least 1 char. */
if (sh->free == 0) {
s = sdsMakeRoomFor(s,1);
sh = (void*) (s-(sizeof(struct sdshdr)));
}
switch(*f) {
case '%':
next = *(f+1);
f++;
switch(next) {
case 's':
case 'S':
str = va_arg(ap,char*);
l = (next == 's') ? strlen(str) : sdslen(str);
if (sh->free < l) {
s = sdsMakeRoomFor(s,l);
sh = (void*) (s-(sizeof(struct sdshdr)));
}
memcpy(s+i,str,l);
sh->len += l;
sh->free -= l;
i += l;
break;
case 'i':
case 'I':
if (next == 'i')
num = va_arg(ap,int);
else
num = va_arg(ap,long long);
{
char buf[SDS_LLSTR_SIZE];
l = sdsll2str(buf,num);
if (sh->free < l) {
s = sdsMakeRoomFor(s,l);
sh = (void*) (s-(sizeof(struct sdshdr)));
}
memcpy(s+i,buf,l);
sh->len += l;
sh->free -= l;
i += l;
}
break;
case 'u':
case 'U':
if (next == 'u')
unum = va_arg(ap,unsigned int);
else
unum = va_arg(ap,unsigned long long);
{
char buf[SDS_LLSTR_SIZE];
l = sdsull2str(buf,unum);
if (sh->free < l) {
s = sdsMakeRoomFor(s,l);
sh = (void*) (s-(sizeof(struct sdshdr)));
}
memcpy(s+i,buf,l);
sh->len += l;
sh->free -= l;
i += l;
}
break;
default: /* Handle %% and generally %<unknown>. */
s[i++] = next;
sh->len += 1;
sh->free -= 1;
break;
}
break;
default:
s[i++] = *f;
sh->len += 1;
sh->free -= 1;
break;
}
f++;
}
va_end(ap);
/* Add null-term */
s[i] = '\0';
return s;
}
/* Remove the part of the string from left and from right composed just of
* contiguous characters found in 'cset', that is a null terminted C string.
*
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call.
*
* Example:
*
* s = sdsnew("AA...AA.a.aa.aHelloWorld :::");
* s = sdstrim(s,"Aa. :");
* printf("%s\n", s);
*
* Output will be just "Hello World".
*/
sds sdstrim(sds s, const char *cset) { //去除sds中包含有 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字符的位置,成功返回第一次出現的位置
while(sp <= end && strchr(cset, *sp)) sp++; //從左開始修剪,sp爲目標串的左邊界
while(ep > start && strchr(cset, *ep)) ep--; //從右開始修剪,ep爲目標串的右邊界
len = (sp > ep) ? 0 : ((ep-sp)+1); //目標串的長度
if (sh->buf != sp) memmove(sh->buf, sp, len); //將字符串的位置前移到buf開頭
//更新表頭
sh->buf[len] = '\0';
sh->free = sh->free+(sh->len-len);
sh->len = len;
return s;
}
/* Turn the string into a smaller (or equal) string containing only the
* substring specified by the 'start' and 'end' indexes.
*
* start and end can be negative, where -1 means the last character of the
* string, -2 the penultimate character, and so forth.
*
* The interval is inclusive, so the start and end characters will be part
* of the resulting string.
*
* The string is modified in-place.
*
* Example:
*
* s = sdsnew("Hello World");
* sdsrange(s,1,-1); => "ello World"
*/
void sdsrange(sds s, int start, int end) { //根據start和end區間截取字符串
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
size_t newlen, len = sdslen(s); //獲得源串的長度
//1,start或end爲負數的情況,轉化爲非負數
if (len == 0) return;
if (start < 0) { //如果start小於0,則從字符串尾部往前開始算起始位置
start = len+start;
if (start < 0) start = 0;
}
if (end < 0) { //如果start小於0,則從字符串尾部往前開始算結束位置
end = len+end;
if (end < 0) end = 0;
}
//2,start和end爲非負數的情況
newlen = (start > end) ? 0 : (end-start)+1; //截取後字符串的長度
if (newlen != 0) {
if (start >= (signed)len) { //起始位置大於字符串的長度,被截取的字符串長度爲0
newlen = 0;
} else if (end >= (signed)len) { //結束位置大於字符串的長度,只截取到字符串的末尾
end = len-1;
newlen = (start > end) ? 0 : (end-start)+1; //更新截取後字符串的長度
}
} else {
start = 0;
}
//通過以上兩種情況計算出start和end位置,用memmove函數截取
if (start && newlen) memmove(sh->buf, sh->buf+start, newlen); //截取後,移動到buf的前面
//更新表頭
sh->buf[newlen] = 0;
sh->free = sh->free+(sh->len-newlen);
sh->len = newlen;
}
/* Apply tolower() to every character of the sds string 's'. */
void sdstolower(sds s) { //將sds字符串所有字符轉換爲小寫
int len = sdslen(s), j;
for (j = 0; j < len; j++) s[j] = tolower(s[j]); //tolower()函數將大寫字符轉換成小寫
}
/* Apply toupper() to every character of the sds string 's'. */
void sdstoupper(sds s) { //將sds字符串所有字符轉換爲大寫
int len = sdslen(s), j;
for (j = 0; j < len; j++) s[j] = toupper(s[j]); //toupper()函數將小字符轉換成大寫
}
/* Compare two sds strings s1 and s2 with memcmp().
*
* Return value:
*
* positive if s1 > s2.
* negative if s1 < s2.
* 0 if s1 and s2 are exactly the same binary string.
*
* If two strings share exactly the same prefix, but one of the two has
* additional characters, the longer string is considered to be greater than
* the smaller one. */
int sdscmp(const sds s1, const sds s2) { //比較兩個sds的大小,相等返回0
size_t l1, l2, minlen;
int cmp;
l1 = sdslen(s1);
l2 = sdslen(s2);
minlen = (l1 < l2) ? l1 : l2; //
cmp = memcmp(s1,s2,minlen); //memcmp()函數比較s1和s2內存地址的前len個字節,返回s1減s2的差值
if (cmp == 0) return l1-l2; //s1長度大於s2返回正差值,否則返回負差值
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.
*/
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) { //使用長度爲seplen的sep分隔符對長度爲len的s進行分割,返回一個sds數組的地址,*count被設置爲數組元素數量
int elements = 0, slots = 5, start = 0, j;
sds *tokens;
if (seplen < 1 || len < 0) return NULL;
tokens = zmalloc(sizeof(sds)*slots); //默認sds長度爲5個
if (tokens == NULL) return NULL;
if (len == 0) { //len爲0,count設爲0,不分割
*count = 0;
return tokens;
}
for (j = 0; j < (len-(seplen-1)); j++) { //sep和s對比(len-(seplen-1))次,
/* make sure there is room for the next element and the final one */
//如果當前字符串數組數量少於當前已存在數組+2個的時候,動態添加
if (slots < elements+2) {
sds *newtokens;
slots *= 2;
newtokens = zrealloc(tokens,sizeof(sds)*slots);
//擴展內存失敗,goto語句釋放內存
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); //將s分割的一段生成爲SDS然後放到預先的tokens數組裏
if (tokens[elements] == NULL) goto cleanup; //創建SDS失敗要釋放tokens數組
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; //創建SDS失敗要釋放tokens數組
elements++;
*count = elements;
return tokens;
cleanup: //釋放內存
{
int i;
for (i = 0; i < elements; i++) sdsfree(tokens[i]);
zfree(tokens);
*count = 0;
return NULL;
}
}
/* Free the result returned by sdssplitlen(), or do nothing if 'tokens' is NULL. */
void sdsfreesplitres(sds *tokens, int count) { //釋放tokens中的count個sds元素
if (!tokens) return;
while(count--)
sdsfree(tokens[count]);
zfree(tokens);
}
/* Append to the sds string "s" an escaped string representation where
* all the non-printable characters (tested with isprint()) are turned into
* escapes in the form "\n\r\a...." or "\x<hex-number>".
*
* After the call, the modified sds string is no longer valid and all the
* references must be substituted with the new pointer returned by the call. */
sds sdscatrepr(sds s, const char *p, size_t len) { //將長度爲len的字符串p以帶引號""的格式追加到s末尾
s = sdscatlen(s,"\"",1); //追加左引號,\是轉義字符
while(len--) {
switch(*p) {
case '\\':
case '"':
s = sdscatprintf(s,"\\%c",*p);
break;
case '\n': s = sdscatlen(s,"\\n",2); break; // "\\n"是兩個字符"\n"
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. */
int is_hex_digit(char c) { //如果c是十六進制符號中的一個返回正數
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F');
}
/* Helper function for sdssplitargs() that converts a hex digit into an
* integer from 0 to 15 */
int hex_digit_to_int(char c) { //16進制字符轉換爲10進制整數
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 free the resulting array of sds strings with
* sdsfreesplitres().
*
* Note that sdscatrepr() is able to convert back a string into
* a quoted string in the same format sdssplitargs() is able to parse.
*
* The function returns the allocated tokens on success, even when the
* input string is empty, or NULL if the input contains unbalanced
* quotes or closed quotes followed by non space characters
* as in: "foo"bar or "foo'
* 這個函數主要用於 config.c 中對配置文件進行分析。
* 例子:
* sds *arr = sdssplitargs("timeout 10086\r\nport 123321\r\n");
* 會得出
* arr[0] = "timeout"
* arr[1] = "10086"
* arr[2] = "port"
* arr[3] = "123321"
*/
sds *sdssplitargs(const char *line, int *argc) { //參數拆分,主要用於 config.c 中對配置文件進行分析。
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 {
/* Even on empty input string return something not NULL. */
if (vector == NULL) vector = zmalloc(sizeof(void*));
return vector;
}
}
err:
while((*argc)--)
sdsfree(vector[*argc]);
zfree(vector);
if (current) sdsfree(current);
*argc = 0;
return NULL;
}
/* Modify the string substituting all the occurrences of the set of
* characters specified 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. */
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) { //將s中所有在 from 中的字符串,替換成 to 中的字符串
size_t j, i, l = sdslen(s);
for (j = 0; j < l; j++) { //遍歷s
for (i = 0; i < setlen; i++) { //遍歷映射
if (s[j] == from[i]) { //替換字符串
s[j] = to[i];
break;
}
}
}
return s;
}
/* Join an array of C strings using the specified separator (also a C string).
* Returns the result as an sds string. */
sds sdsjoin(char **argv, int argc, char *sep) { //以分隔符連接字符串子數組構成新的字符串
sds join = sdsempty(); //創建一個空sds
int j;
for (j = 0; j < argc; j++) {
join = sdscat(join, argv[j]);
if (j != argc-1) join = sdscat(join,sep); //以*sep鏈接
}
return join;
}
參考書籍:《Redis設計與實現》——黃健翔