redis源码笔记-sds

系统 2604 0

sds和adlist一样,是redis的基础数据结构之一,是其为自身实现的字符串类型。A C dynamic strings library

sds.h

      
         1
      
      
        #ifndef __SDS_H

      
      
         2
      
      
        #define
      
       __SDS_H

      
         3
      
      
         4
      
      
        #define
      
       SDS_MAX_PREALLOC (1024*1024)       //字符串最大的预分配长度是1M

      
         5
      
      
         6
      
       #include <sys/types.h>

      
         7
      
       #include <stdarg.h>

      
         8
      
      
         9
      
       typedef 
      
        char
      
       *
      
        sds;          //sds本身被typedef为char*,是后续绝大部分函数的参数(之一)

      
      
        10
      
      
        11
      
      
        struct
      
      
         sdshdr {

      
      
        12
      
      
        int
      
      
         len;

      
      
        13
      
      
        int
      
      
         free;

      
      
        14
      
      
        char
      
      
         buf[];

      
      
        15
      
      
        };      //字符串的数据结构,记录了字符串长度、空闲字节,以及指向实际存储数据buf的指针

      
      
        16
      
      
        17
      
      
        static
      
       inline size_t sdslen(
      
        const
      
      
         sds s) {

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

      
      
        19
      
      
        return
      
       sh->
      
        len;

      
      
        20
      
      
        }       //尽管在不考虑typedef的前提下,但从函数参数来看,非引用/指针类型,但毕竟底层实现是char*,因此作者也将所有函数内不改变的参数标记为了const
        
//传的参数指向实际字符串,因此需要先取得数据结构头的位置,然后返回字符串长度;
21 22 static inline size_t sdsavail( const sds s) { 23 struct sdshdr *sh = ( void *)(s-( sizeof ( struct sdshdr))); 24 return sh-> free; 25 } //同上,返回的是空闲字节数 26 27 sds sdsnewlen( const void * init, size_t initlen); 28 sds sdsnew( const char * init); 29 sds sdsempty(); //三个初始化函数 30 size_t sdslen( const sds s); //上述内联函数的函数声明 31 sds sdsdup( const sds s); //字符串复制函数 32 void sdsfree(sds s); 33 size_t sdsavail(sds s); 34 sds sdsgrowzero(sds s, size_t len); 35 sds sdscatlen(sds s, void * t, size_t len); 36 sds sdscat(sds s, char * t); 37 sds sdscatsds(sds s, sds t); 38 sds sdscpylen(sds s, char * t, size_t len); 39 sds sdscpy(sds s, char * t); 40 41 sds sdscatvprintf(sds s, const char * fmt, va_list ap); 42 #ifdef __GNUC__ 43 sds sdscatprintf(sds s, const char * fmt, ...) 44 __attribute__((format(printf, 2 , 3 ))); 45 #else 46 sds sdscatprintf(sds s, const char * fmt, ...); 47 #endif //为啥这么写,没看懂。。非重点,暂且略过 48 49 sds sdstrim(sds s, const char * cset); 50 sds sdsrange(sds s, int start, int end); 51 void sdsupdatelen(sds s); 52 void sdsclear(sds s); 53 int sdscmp(sds s1, sds s2); 54 sds *sdssplitlen( char *s, int len, char *sep, int seplen, int * count); 55 void sdsfreesplitres(sds *tokens, int count); 56 void sdstolower(sds s); 57 void sdstoupper(sds s); 58 sds sdsfromlonglong( long long value); 59 sds sdscatrepr(sds s, char * p, size_t len); 60 sds *sdssplitargs( char *line, int * argc); 61 62 #endif
对libc的string.h库熟悉的同学,根据上述函数名也大概能猜出来函数的作用,就不一一写了,具体参见sds.c的实现部分。

sds.c

      
        看似很长,但绝大部分代码都很通俗易懂,大家不要恐惧。
        
1
#define SDS_ABORT_ON_OOM //在内存不够时的默认动作是终止进程(abort) 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include < string .h> 6 #include <ctype.h> 7 #include " sds.h " 8 #include " zmalloc.h " //同样用na个简单封装的内存分配库 9 10 static void sdsOomAbort( void ) { 11 fprintf(stderr, " SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n " ); 12 abort(); 13 } //abort前作的唯一的善后工作是告诉大家自己是由于在SDS时内存不够挂掉的 14 15 sds sdsnewlen( const void * init, size_t initlen) { 16 struct sdshdr * sh; 17 18 sh = zmalloc( sizeof ( struct sdshdr)+initlen+ 1 ); //所分配的长度是头的长度sizeof(struct sdshdr)+字符串长度initlen+‘\0’的一个字节
//注意这里的sizeof(struct sdshdr)长度为8,剩下的都直接存在了sh->buf上
19 #ifdef SDS_ABORT_ON_OOM 20 if (sh == NULL) sdsOomAbort(); 21 #else 22 if (sh == NULL) return NULL; //如果没有定义SDS_ABORT_ON_OOM,简单的返回NULL 23 #endif 24 sh->len = initlen; 25 sh->free = 0 ; //创建字符串时free的长度是0 26 if (initlen) { 27 if (init) memcpy(sh-> buf, init, initlen); //若参数init不等于NULL,则拷贝initlen长度给sh->buf 28 else memset(sh->buf, 0 ,initlen); //否则这块缓冲区被赋值为0 29 } 30 sh->buf[initlen] = ' \0 ' ; //用'\0'为字符串结尾 31 return ( char *)sh-> buf; //返回的是实际字符串的指针 32 } 33 34 sds sdsempty( void ) { 35 return sdsnewlen( "" , 0 ); //注意""字符串常量并不代表空,但0表示空了 36 } 37 38 sds sdsnew( const char * init) { 39 size_t initlen = (init == NULL) ? 0 : strlen(init); 40 return sdsnewlen(init, initlen); 41 } 42 43 sds sdsdup( const sds s) { 44 return sdsnewlen(s, sdslen(s)); 45 } //dup复制函数也是调用newlen实现的 46 47 void sdsfree(sds s) { 48 if (s == NULL) return ; 49 zfree(s- sizeof ( struct sdshdr)); 50 } //释放链接要先找到sds的header 51 52 void sdsupdatelen(sds s) { 53 struct sdshdr *sh = ( void *) (s-( sizeof ( struct sdshdr))); 54 int reallen = strlen(s); 55 sh->free += (sh->len- reallen); 56 sh->len = reallen; 57 } //更新长度是更新header的len和free两个字段 58 59 void sdsclear(sds s) { 60 struct sdshdr *sh = ( void *) (s-( sizeof ( struct sdshdr))); 61 sh->free += sh-> len; 62 sh->len = 0 ; 63 sh->buf[ 0 ] = ' \0 ' ; 64 } //将所有的len都给free,但并未释放内存空间 65 66 static sds sdsMakeRoomFor(sds s, size_t addlen) { 67 struct sdshdr *sh, * newsh; 68 size_t free = sdsavail(s); 69 size_t len, newlen; 70 71 if (free >= addlen) return s; //如果剩余空间仍足够,则直接将传进来的参数s返回 72 len = sdslen(s); 73 sh = ( void *) (s-( sizeof ( struct sdshdr))); 74 newlen = (len+ addlen); 75 if (newlen < SDS_MAX_PREALLOC) 76 newlen *= 2 ; 77 else 78 newlen += SDS_MAX_PREALLOC; //在为newlen增加了addlen这么长的空余空间的同时,也在这次操作中额外申请了内存空间,当newlen小于系统定义的单次最大分配内存时,额外分配的长度是newlen,负责多分配SDS_MAX_PREALLOC这么大的空间。熟悉std::vector实现的同学对这种分配机制一定似曾相识。这里基于这样一种假设,这次make room的sds,也很有可能在不久的将来再次make room,所以预先分配了空间 79 newsh = zrealloc(sh, sizeof ( struct sdshdr)+newlen+ 1 ); //realloc保证原来分配空间的数据会拷贝过去,但执行此操作后,sh指针就失效了 80 #ifdef SDS_ABORT_ON_OOM 81 if (newsh == NULL) sdsOomAbort(); 82 #else 83 if (newsh == NULL) return NULL; 84 #endif 85 86 newsh->free = newlen - len; 87 return newsh-> buf; 88 } 89 90 /* Grow the sds to have the specified length. Bytes that were not part of 91 * the original length of the sds will be set to zero. */ 92 sds sdsgrowzero(sds s, size_t len) { //len是一个to 参数,表示增长到 93 struct sdshdr *sh = ( void *)(s-( sizeof ( struct sdshdr))); 94 size_t totlen, curlen = sh-> len; 95 96 if (len <= curlen) return s; //len比原sds还短,则直接返回 97 s = sdsMakeRoomFor(s,len- curlen); 98 if (s == NULL) return NULL; //只有可能在不abort时,才会返回NULL 99 100 /* Make sure added region doesn't contain garbage */ 101 sh = ( void *)(s-( sizeof ( struct sdshdr))); 102 memset(s+curlen, 0 ,(len-curlen+ 1 )); /* also set trailing \0 byte */ //将len长度的字段都赋成0了,包括最有的'\0'也赋成0了 103 totlen = sh->len+sh-> free; 104 sh->len = len; 105 sh->free = totlen-sh-> len; 106 return s; //经过这个函数调用后,sds长度变成了len,只是有可能之前不足len长度的字符串被填成了0 107 } 108 109 sds sdscatlen(sds s, void * t, size_t len) { 110 struct sdshdr * sh; 111 size_t curlen = sdslen(s); 112 113 s = sdsMakeRoomFor(s,len); 114 if (s == NULL) return NULL; 115 sh = ( void *) (s-( sizeof ( struct sdshdr))); 116 memcpy(s+ curlen, t, len); 117 sh->len = curlen+ len; 118 sh->free = sh->free- len; 119 s[curlen+len] = ' \0 ' ; 120 return s; 121 } 122 123 sds sdscat(sds s, char * t) { 124 return sdscatlen(s, t, strlen(t)); 125 } 126 127 sds sdscatsds(sds s, sds t) { 128 return sdscatlen(s, t, sdslen(t)); 129 } 130 131 sds sdscpylen(sds s, char * t, size_t len) { 132 struct sdshdr *sh = ( void *) (s-( sizeof ( struct sdshdr))); 133 size_t totlen = sh->free+sh-> len; 134 135 if (totlen < len) { 136 s = sdsMakeRoomFor(s,len-sh-> len); 137 if (s == NULL) return NULL; 138 sh = ( void *) (s-( sizeof ( struct sdshdr))); 139 totlen = sh->free+sh-> len; 140 } 141 memcpy(s, t, len); 142 s[len] = ' \0 ' ; 143 sh->len = len; 144 sh->free = totlen- len; 145 return s; 146 } 147 148 sds sdscpy(sds s, char * t) { 149 return sdscpylen(s, t, strlen(t)); 150 } 151
下面这个函数的实现很有意思,因为类printf操作,事先并不知道源串长度,因此采用了一种类似启发式的方法,从16bytes开始,按照倍数递增的方法来猜测长度,猜测长度时,是在倒数第二个字节处打了个标记('\0'),然后判断操作前后是否保持一致来判断是否空间够的。好好看看while循环,其实挺有意思的。 152 sds sdscatvprintf(sds s, const char * fmt, va_list ap) { 153 va_list cpy; 154 char *buf, * t; 155 size_t buflen = 16 ; 156 157 while ( 1 ) { 158 buf = zmalloc(buflen); 159 #ifdef SDS_ABORT_ON_OOM 160 if (buf == NULL) sdsOomAbort(); 161 #else 162 if (buf == NULL) return NULL; 163 #endif 164 buf[buflen- 2 ] = ' \0 ' ; 165 va_copy(cpy,ap); 166 vsnprintf(buf, buflen, fmt, cpy); 167 if (buf[buflen- 2 ] != ' \0 ' ) { 168 zfree(buf); 169 buflen *= 2 ; 170 continue ; 171 } 172 break ; 173 } 174 t = sdscat(s, buf); 175 zfree(buf); 176 return t; 177 } 178 179 sds sdscatprintf(sds s, const char * fmt, ...) { 180 va_list ap; 181 char * t; 182 va_start(ap, fmt); 183 t = sdscatvprintf(s,fmt,ap); 184 va_end(ap); 185 return t; 186 } 187 188 sds sdstrim(sds s, const char * cset) { 189 struct sdshdr *sh = ( void *) (s-( sizeof ( struct sdshdr))); 190 char *start, *end, *sp, * ep; //sp == start pointer, ep == end pointer, sp,ep 是移动的,start end是静止的 191 size_t len; 192 193 sp = start = s; 194 ep = end = s+sdslen(s)- 1 ; //最后一个有意义的字节,非'\0' 195 while (sp <= end && strchr(cset, *sp)) sp++ ; 196 while (ep > start && strchr(cset, *ep)) ep-- ; //将头尾出现在字符串cset中的字符都删干净, *sp和*ep都是字符而非字符指针

197 len = (sp > ep) ? 0 : ((ep-sp)+ 1 ); 198 if (sh->buf != sp) memmove(sh-> buf, sp, len); //因为sp和sh->buf指向的字符串有可能重叠,所以用了memmove 199 sh->buf[len] = ' \0 ' ; 200 sh->free = sh->free+(sh->len- len); 201 sh->len = len; 202 return s; //删除特定空间后,并不释放内存,只是增大sh->free 203 } 204 205 sds sdsrange(sds s, int start, int end) { 206 struct sdshdr *sh = ( void *) (s-( sizeof ( struct sdshdr))); 207 size_t newlen, len = sdslen(s); 208 209 if (len == 0 ) return s; 210 if (start < 0 ) { 211 start = len+ start; 212 if (start < 0 ) start = 0 ; 213 } 214 if (end < 0 ) { 215 end = len+ end; 216 if (end < 0 ) end = 0 ; 217 } //处理start和end为负值的情况 218 newlen = (start > end) ? 0 : (end-start)+ 1 ; 219 if (newlen != 0 ) { 220 if (start >= (signed)len) { 221 newlen = 0 ; 222 } else if (end >= (signed)len) { 223 end = len- 1 ; 224 newlen = (start > end) ? 0 : (end-start)+ 1 ; 225 } 226 } else { 227 start = 0 ; 228 } 229 if (start && newlen) memmove(sh->buf, sh->buf+ start, newlen); //注意这个函数是在传进来的字符串上做操作的 230 sh->buf[newlen] = 0 ; 231 sh->free = sh->free+(sh->len- newlen); 232 sh->len = newlen; 233 return s; 234 } 235 236 void sdstolower(sds s) { 237 int len = sdslen(s), j; 238 239 for (j = 0; j < len; j++) s[j] = tolower(s[j]); 240 } //逐个字符变成小写 241 242 void sdstoupper(sds s) { 243 int len = sdslen(s), j; 244 245 for (j = 0; j < len; j++) s[j] = toupper(s[j]); 246 } //逐个字符变成大写 247 248 int sdscmp(sds s1, sds s2) { 249 size_t l1, l2, minlen; 250 int cmp; 251 252 l1 = sdslen(s1); 253 l2 = sdslen(s2); 254 minlen = (l1 < l2) ? l1 : l2; 255 cmp = memcmp(s1,s2,minlen); 256 if (cmp == 0) return l1-l2; 257 return cmp; 258 } //字符串比较,没啥好说的,只关注len,不关注free 259 260 /* Split 's' with separator in 'sep'. An array 261 * of sds strings is returned. *count will be set 262 * by reference to the number of tokens returned. 263 * 264 * On out of memory, zero length string, zero length 265 * separator, NULL is returned. 266 * 267 * Note that 'sep' is able to split a string using 268 * a multi-character separator. For example 269 * sdssplit("foo_-_bar","_-_"); will return two 270 * elements "foo" and "bar". 271 * 272 * This version of the function is binary-safe but 273 * requires length arguments. sdssplit() is just the 274 * same function but for zero-terminated strings. //其实作者根本就懒到没有在额外提供sdssplit,任何场景下都使用binary-safe的函数总是好的 275 */ 276 sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { 277 int elements = 0, slots = 5, start = 0, j; 278 sds *tokens; 279 280 if (seplen < 1 || len < 0) return NULL; 281 282 tokens = zmalloc(sizeof(sds)*slots); //先假设有5个tokens供返回。在这个函数里返回值叫tokens,但在后续的sdssplitargs里返回值被起名为vector,而且类型一个是sds*,一个是char**,虽说是一样的,但其实还是改的一致比较好。按道理,一个这样的不算大(关键是逻辑简单)的源文件,作者没必要交给两个不同的人写啊 283 #ifdef SDS_ABORT_ON_OOM 284 if (tokens == NULL) sdsOomAbort(); 285 #else 286 if (tokens == NULL) return NULL; 287 #endif 288 289 if (len == 0) { 290 *count = 0; 291 return tokens; 292 } 293 for (j = 0; j < (len-(seplen-1)); j++) { 294 /* make sure there is room for the next element and the final one */ 295 if (slots < elements+2) { 296 sds *newtokens; 297 298 slots *= 2; 299 newtokens = zrealloc(tokens,sizeof(sds)*slots); 300 if (newtokens == NULL) { 301 #ifdef SDS_ABORT_ON_OOM 302 sdsOomAbort(); 303 #else 304 goto cleanup; 305 #endif 306 } 307 tokens = newtokens; 308 } 309 /* search the separator */ 310 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { 311 tokens[elements] = sdsnewlen(s+start,j-start); 312 if (tokens[elements] == NULL) { 313 #ifdef SDS_ABORT_ON_OOM 314 sdsOomAbort(); 315 #else 316 goto cleanup; 317 #endif 318 } 319 elements++; 320 start = j+seplen; 321 j = j+seplen-1; /* skip the separator */ 322 } 323 } 324 /* Add the final element. We are sure there is room in the tokens array. */ 325 tokens[elements] = sdsnewlen(s+start,len-start); 326 if (tokens[elements] == NULL) { 327 #ifdef SDS_ABORT_ON_OOM 328 sdsOomAbort(); 329 #else 330 goto cleanup; 331 #endif 332 } 333 elements++; 334 *count = elements; 335 return tokens; 336 337 #ifndef SDS_ABORT_ON_OOM 338 cleanup: 339 { 340 int i; 341 for (i = 0; i < elements; i++) sdsfree(tokens[i]); 342 zfree(tokens); 343 return NULL; 344 } 345 #endif 346 } 347 348 void sdsfreesplitres(sds *tokens, int count) { 349 if (!tokens) return; 350 while(count--) 351 sdsfree(tokens[count]); 352 zfree(tokens); 353 } 354 355 sds sdsfromlonglong( long long value) { 356 char buf[ 32 ], * p; 357 unsigned long long v; 358 359 v = (value < 0 ) ? - value : value; 360 p = buf+ 31 ; /* point to the last character */ 361 do { 362 *p-- = ' 0 ' +(v% 10 ); 363 v /= 10 ; 364 } while (v); 365 if (value < 0 ) *p-- = ' - ' ; 366 p++ ; 367 return sdsnewlen(p, 32 -(p- buf)); 368 } //将long long转成字符串,没啥好说的 369 370 sds sdscatrepr(sds s, char * p, size_t len) { 371 s = sdscatlen(s, " \" " , 1 ); 372 while (len-- ) { 373 switch (* p) { 374 case ' \\ ' : 375 case ' " ' : 376 s = sdscatprintf(s, " \\%c " ,* p); 377 break ; 378 case ' \n ' : s = sdscatlen(s, " \\n " , 2 ); break ; 379 case ' \r ' : s = sdscatlen(s, " \\r " , 2 ); break ; 380 case ' \t ' : s = sdscatlen(s, " \\t " , 2 ); break ; 381 case ' \a ' : s = sdscatlen(s, " \\a " , 2 ); break ; 382 case ' \b ' : s = sdscatlen(s, " \\b " , 2 ); break ; 383 default : 384 if (isprint(* p)) 385 s = sdscatprintf(s, " %c " ,* p); 386 else 387 s = sdscatprintf(s, " \\x%02x " ,(unsigned char )* p); 388 break ; 389 } 390 p++ ; 391 } 392 return sdscatlen(s, " \" " , 1 ); 393 } //这个函数将参数p原封不动(做过处理的,是计算机认为合法的)保存在一个sds里,用于协议交换 394 395 /* Helper function for sdssplitargs() that returns non zero if 'c' 396 * is a valid hex digit. */ 397 int is_hex_digit( char c) { 398 return (c >= ' 0 ' && c <= ' 9 ' ) || (c >= ' a ' && c <= ' f ' ) || 399 (c >= ' A ' && c <= ' F ' ); 400 } 401 402 /* Helper function for sdssplitargs() that converts an hex digit into an 403 * integer from 0 to 15 */ 404 int hex_digit_to_int( char c) { 405 switch (c) { 406 case ' 0 ' : return 0 ; 407 case ' 1 ' : return 1 ; 408 case ' 2 ' : return 2 ; 409 case ' 3 ' : return 3 ; 410 case ' 4 ' : return 4 ; 411 case ' 5 ' : return 5 ; 412 case ' 6 ' : return 6 ; 413 case ' 7 ' : return 7 ; 414 case ' 8 ' : return 8 ; 415 case ' 9 ' : return 9 ; 416 case ' a ' : case ' A ' : return 10 ; 417 case ' b ' : case ' B ' : return 11 ; 418 case ' c ' : case ' C ' : return 12 ; 419 case ' d ' : case ' D ' : return 13 ; 420 case ' e ' : case ' E ' : return 14 ; 421 case ' f ' : case ' F ' : return 15 ; 422 default : return 0 ; 423 } 424 } 425 426 /* Split a line into arguments, where every argument can be in the 427 * following programming-language REPL-alike form: 428 * 429 * foo bar "newline are supported\n" and "\xff\x00otherstuff" 430 * 431 * The number of arguments is stored into *argc, and an array 432 * of sds is returned. The caller should sdsfree() all the returned 433 * strings and finally zfree() the array itself. 434 * 435 * Note that sdscatrepr() is able to convert back a string into 436 * a quoted string in the same format sdssplitargs() is able to parse. 437 */ 438 sds *sdssplitargs( char *line, int * argc) { 439 char *p = line; 440 char *current = NULL; 441 char **vector = NULL; 442 443 *argc = 0 ; 444 while ( 1 ) { 445 /* skip blanks */ 446 while (*p && isspace(*p)) p++ ; 447 if (* p) { 448 /* get a token */ 449 int inq= 0 ; /* set to 1 if we are in "quotes" */ 450 int insq= 0 ; /* set to 1 if we are in 'single quotes' */ 451 int done= 0 ; 452 453 if (current == NULL) current = sdsempty(); 454 while (! done) { 455 if (inq) { 456 if (*p == ' \\ ' && *(p+ 1 ) == ' x ' && 457 is_hex_digit(*(p+ 2 )) && 458 is_hex_digit(*(p+ 3 ))) 459 { 460 unsigned char byte ; 461 462 byte = (hex_digit_to_int(*(p+ 2 ))* 16 )+ 463 hex_digit_to_int(*(p+ 3 )); 464 current = sdscatlen(current,( char *)& byte , 1 ); 465 p += 3 ; 466 } else if (*p == ' \\ ' && *(p+ 1 )) { 467 char c; 468 469 p++ ; 470 switch (* p) { 471 case ' n ' : c = ' \n ' ; break ; 472 case ' r ' : c = ' \r ' ; break ; 473 case ' t ' : c = ' \t ' ; break ; 474 case ' b ' : c = ' \b ' ; break ; 475 case ' a ' : c = ' \a ' ; break ; 476 default : c = *p; break ; 477 } 478 current = sdscatlen(current,&c, 1 ); 479 } else if (*p == ' " ' ) { 480 /* closing quote must be followed by a space or 481 * nothing at all. */ 482 if (*(p+ 1 ) && !isspace(*(p+ 1 ))) goto err; 483 done= 1 ; 484 } else if (!* p) { 485 /* unterminated quotes */ 486 goto err; 487 } else { 488 current = sdscatlen(current,p, 1 ); 489 } 490 } else if (insq) { 491 if (*p == ' \\ ' && *(p+ 1 ) == ' \' ' ) { 492 p++ ; 493 current = sdscatlen(current, " ' " , 1 ); 494 } else if (*p == ' \' ' ) { 495 /* closing quote must be followed by a space or 496 * nothing at all. */ 497 if (*(p+ 1 ) && !isspace(*(p+ 1 ))) goto err; 498 done= 1 ; 499 } else if (!* p) { 500 /* unterminated quotes */ 501 goto err; 502 } else { 503 current = sdscatlen(current,p, 1 ); 504 } 505 } else { 506 switch (* p) { 507 case ' ' : 508 case ' \n ' : 509 case ' \r ' : 510 case ' \t ' : 511 case ' \0 ' : 512 done= 1 ; 513 break ; 514 case ' " ' : 515 inq= 1 ; 516 break ; 517 case ' \' ' : 518 insq= 1 ; 519 break ; 520 default : 521 current = sdscatlen(current,p, 1 ); 522 break ; 523 } 524 } 525 if (*p) p++ ; 526 } 527 /* add the token to the vector */ 528 vector = zrealloc(vector,((*argc)+ 1 )* sizeof ( char * )); 529 vector[*argc] = current; 530 (*argc)++ ; 531 current = NULL; 532 } else { 533 return vector; 534 } 535 } 536 537 err: 538 while ((*argc)-- ) 539 sdsfree(vector[* argc]); 540 zfree(vector); 541 if (current) sdsfree(current); 542 return NULL; 543 } //解析参数,仔细看肯定能看懂的,但不好加注释。。 544 545 #ifdef SDS_TEST_MAIN 546 #include <stdio.h> 547 #include "testhelp.h" //testhelp是redis自带的一个tiny的做单元测试的框架,下篇日志里介绍 548 549 int main(void) { 550 { 551 sds x = sdsnew("foo"), y; 552 553 test_cond("Create a string and obtain the length", 554 sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0) 555 556 sdsfree(x); 557 x = sdsnewlen("foo",2); 558 test_cond("Create a string with specified length", 559 sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0) 560 561 x = sdscat(x,"bar"); 562 test_cond("Strings concatenation", 563 sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0); 564 565 x = sdscpy(x,"a"); 566 test_cond("sdscpy() against an originally longer string", 567 sdslen(x) == 1 && memcmp(x,"a\0",2) == 0) 568 569 x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk"); 570 test_cond("sdscpy() against an originally shorter string", 571 sdslen(x) == 33 && 572 memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0) //^_^疑似笔误,不过测试还是可以通过的 573 574 sdsfree(x); 575 x = sdscatprintf(sdsempty(),"%d",123); 576 test_cond("sdscatprintf() seems working in the base case", 577 sdslen(x) == 3 && memcmp(x,"123\0",4) ==0) 578 579 sdsfree(x); 580 x = sdstrim(sdsnew("xxciaoyyy"),"xy"); 581 test_cond("sdstrim() correctly trims characters", 582 sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0) 583 584 y = sdsrange(sdsdup(x),1,1); 585 test_cond("sdsrange(...,1,1)", 586 sdslen(y) == 1 && memcmp(y,"i\0",2) == 0) 587 588 sdsfree(y); 589 y = sdsrange(sdsdup(x),1,-1); 590 test_cond("sdsrange(...,1,-1)", 591 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) 592 593 sdsfree(y); 594 y = sdsrange(sdsdup(x),-2,-1); 595 test_cond("sdsrange(...,-2,-1)", 596 sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0) 597 598 sdsfree(y); 599 y = sdsrange(sdsdup(x),2,1); 600 test_cond("sdsrange(...,2,1)", 601 sdslen(y) == 0 && memcmp(y,"\0",1) == 0) 602 603 sdsfree(y); 604 y = sdsrange(sdsdup(x),1,100); 605 test_cond("sdsrange(...,1,100)", 606 sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0) 607 608 sdsfree(y); 609 y = sdsrange(sdsdup(x),100,100); 610 test_cond("sdsrange(...,100,100)", 611 sdslen(y) == 0 && memcmp(y,"\0",1) == 0) 612 613 sdsfree(y); 614 sdsfree(x); 615 x = sdsnew("foo"); 616 y = sdsnew("foa"); 617 test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0) 618 619 sdsfree(y); 620 sdsfree(x); 621 x = sdsnew("bar"); 622 y = sdsnew("bar"); 623 test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0) 624 625 sdsfree(y); 626 sdsfree(x); 627 x = sdsnew("aar"); 628 y = sdsnew("bar"); 629 test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0) 630 } 631 test_report() 632 } 633 #endif

redis源码笔记-sds


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论