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
| 331 | * sdsIncrLen(s, nread); |
| 332 | */ |
| 333 | void sdsIncrLen(sds s, ssize_t incr) { |
| 334 | unsigned char flags = s[-1]; |
| 335 | size_t len; |
| 336 | switch(flags&SDS_TYPE_MASK) { |
| 337 | case SDS_TYPE_5: { |
| 338 | unsigned char *fp = ((unsigned char*)s)-1; |
| 339 | unsigned char oldlen = SDS_TYPE_5_LEN(flags); |
| 340 | assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr))); |
| 341 | *fp = SDS_TYPE_5 | ((oldlen+incr) << SDS_TYPE_BITS); |
| 342 | len = oldlen+incr; |
| 343 | break; |
| 344 | } |
| 345 | case SDS_TYPE_8: { |
| 346 | SDS_HDR_VAR(8,s); |
| 347 | assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); |
| 348 | len = (sh->len += incr); |
| 349 | break; |
| 350 | } |
| 351 | case SDS_TYPE_16: { |
| 352 | SDS_HDR_VAR(16,s); |
| 353 | assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); |
| 354 | len = (sh->len += incr); |
| 355 | break; |
| 356 | } |
| 357 | case SDS_TYPE_32: { |
| 358 | SDS_HDR_VAR(32,s); |
| 359 | assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr))); |
| 360 | len = (sh->len += incr); |
| 361 | break; |
| 362 | } |
| 363 | case SDS_TYPE_64: { |
| 364 | SDS_HDR_VAR(64,s); |
| 365 | assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr))); |
| 366 | len = (sh->len += incr); |
| 367 | break; |
| 368 | } |
| 369 | default: len = 0; /* Just to avoid compilation warnings. */ |
| 370 | } |
| 371 | s[len] = '\0'; |
| 372 | } |
| 373 | |
| 374 | /* Grow the sds to have the specified length. Bytes that were not part of |
| 375 | * the original length of the sds will be set to zero. |