2
0

sds.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* SDSLib, A C dynamic strings library
  2. *
  3. * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __SDS_H
  31. #define __SDS_H
  32. #define SDS_MAX_PREALLOC (1024*1024)
  33. #include <sys/types.h>
  34. #include <stdarg.h>
  35. typedef char *sds;
  36. struct sdshdr {
  37. int len;
  38. int free;
  39. char buf[];
  40. };
  41. static inline size_t sdslen(const sds s) {
  42. struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
  43. return sh->len;
  44. }
  45. static inline size_t sdsavail(const sds s) {
  46. struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
  47. return sh->free;
  48. }
  49. sds sdsnewlen(const void *init, size_t initlen);
  50. sds sdsnew(const char *init);
  51. sds sdsempty(void);
  52. size_t sdslen(const sds s);
  53. sds sdsdup(const sds s);
  54. void sdsfree(sds s);
  55. size_t sdsavail(const sds s);
  56. sds sdsgrowzero(sds s, size_t len);
  57. sds sdscatlen(sds s, const void *t, size_t len);
  58. sds sdscat(sds s, const char *t);
  59. sds sdscatsds(sds s, const sds t);
  60. sds sdscpylen(sds s, const char *t, size_t len);
  61. sds sdscpy(sds s, const char *t);
  62. sds sdscatvprintf(sds s, const char *fmt, va_list ap);
  63. #ifdef __GNUC__
  64. sds sdscatprintf(sds s, const char *fmt, ...)
  65. __attribute__((format(printf, 2, 3)));
  66. #else
  67. sds sdscatprintf(sds s, const char *fmt, ...);
  68. #endif
  69. sds sdstrim(sds s, const char *cset);
  70. void sdsrange(sds s, int start, int end);
  71. void sdsupdatelen(sds s);
  72. void sdsclear(sds s);
  73. int sdscmp(const sds s1, const sds s2);
  74. sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
  75. void sdsfreesplitres(sds *tokens, int count);
  76. void sdstolower(sds s);
  77. void sdstoupper(sds s);
  78. sds sdsfromlonglong(long long value);
  79. sds sdscatrepr(sds s, const char *p, size_t len);
  80. sds *sdssplitargs(const char *line, int *argc);
  81. sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
  82. sds sdsjoin(char **argv, int argc, char *sep);
  83. /* Low level functions exposed to the user API */
  84. sds sdsMakeRoomFor(sds s, size_t addlen);
  85. void sdsIncrLen(sds s, int incr);
  86. sds sdsRemoveFreeSpace(sds s);
  87. size_t sdsAllocSize(sds s);
  88. #endif