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