]> git.saurik.com Git - redis.git/blame - sds.c
ZLEN renamed ZCARD for consistency with SCARD
[redis.git] / sds.c
CommitLineData
ed9b544e 1/* SDSLib, A C dynamic strings library
2 *
3 * Copyright (c) 2006-2009, 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
56906eef 31#define SDS_ABORT_ON_OOM
32
ed9b544e 33#include "sds.h"
34#include <stdio.h>
35#include <stdlib.h>
36#include <stdarg.h>
37#include <string.h>
38#include <ctype.h>
39#include "zmalloc.h"
40
41static void sdsOomAbort(void) {
42 fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
43 abort();
44}
45
46sds sdsnewlen(const void *init, size_t initlen) {
47 struct sdshdr *sh;
48
f1017b3f 49 sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
50#ifdef SDS_ABORT_ON_OOM
51 if (sh == NULL) sdsOomAbort();
52#else
53 if (sh == NULL) return NULL;
54#endif
55 sh->len = initlen;
ed9b544e 56 sh->free = 0;
57 if (initlen) {
58 if (init) memcpy(sh->buf, init, initlen);
59 else memset(sh->buf,0,initlen);
60 }
61 sh->buf[initlen] = '\0';
62 return (char*)sh->buf;
63}
64
65sds sdsempty(void) {
66 return sdsnewlen("",0);
67}
68
69sds sdsnew(const char *init) {
70 size_t initlen = (init == NULL) ? 0 : strlen(init);
71 return sdsnewlen(init, initlen);
72}
73
74size_t sdslen(const sds s) {
75 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
f1017b3f 76 return sh->len;
ed9b544e 77}
78
79sds sdsdup(const sds s) {
80 return sdsnewlen(s, sdslen(s));
81}
82
83void sdsfree(sds s) {
84 if (s == NULL) return;
f1017b3f 85 zfree(s-sizeof(struct sdshdr));
ed9b544e 86}
87
88size_t sdsavail(sds s) {
89 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
90 return sh->free;
91}
92
93void sdsupdatelen(sds s) {
94 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
95 int reallen = strlen(s);
f1017b3f 96 sh->free += (sh->len-reallen);
97 sh->len = reallen;
ed9b544e 98}
99
100static sds sdsMakeRoomFor(sds s, size_t addlen) {
101 struct sdshdr *sh, *newsh;
102 size_t free = sdsavail(s);
f1017b3f 103 size_t len, newlen;
ed9b544e 104
f1017b3f 105 if (free >= addlen) return s;
ed9b544e 106 len = sdslen(s);
107 sh = (void*) (s-(sizeof(struct sdshdr)));
f1017b3f 108 newlen = (len+addlen)*2;
109 newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1);
ed9b544e 110#ifdef SDS_ABORT_ON_OOM
f1017b3f 111 if (newsh == NULL) sdsOomAbort();
ed9b544e 112#else
f1017b3f 113 if (newsh == NULL) return NULL;
ed9b544e 114#endif
f1017b3f 115
116 newsh->free = newlen - len;
ed9b544e 117 return newsh->buf;
118}
119
120sds sdscatlen(sds s, void *t, size_t len) {
f1017b3f 121 struct sdshdr *sh;
ed9b544e 122 size_t curlen = sdslen(s);
123
124 s = sdsMakeRoomFor(s,len);
125 if (s == NULL) return NULL;
f1017b3f 126 sh = (void*) (s-(sizeof(struct sdshdr)));
ed9b544e 127 memcpy(s+curlen, t, len);
f1017b3f 128 sh->len = curlen+len;
129 sh->free = sh->free-len;
ed9b544e 130 s[curlen+len] = '\0';
131 return s;
132}
133
134sds sdscat(sds s, char *t) {
135 return sdscatlen(s, t, strlen(t));
136}
137
138sds sdscpylen(sds s, char *t, size_t len) {
139 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
f1017b3f 140 size_t totlen = sh->free+sh->len;
ed9b544e 141
142 if (totlen < len) {
143 s = sdsMakeRoomFor(s,len-totlen);
144 if (s == NULL) return NULL;
f1017b3f 145 sh = (void*) (s-(sizeof(struct sdshdr)));
146 totlen = sh->free+sh->len;
ed9b544e 147 }
148 memcpy(s, t, len);
149 s[len] = '\0';
f1017b3f 150 sh->len = len;
151 sh->free = totlen-len;
ed9b544e 152 return s;
153}
154
155sds sdscpy(sds s, char *t) {
156 return sdscpylen(s, t, strlen(t));
157}
158
159sds sdscatprintf(sds s, const char *fmt, ...) {
160 va_list ap;
161 char *buf, *t;
162 size_t buflen = 32;
163
164 while(1) {
165 buf = zmalloc(buflen);
166#ifdef SDS_ABORT_ON_OOM
167 if (buf == NULL) sdsOomAbort();
168#else
169 if (buf == NULL) return NULL;
170#endif
171 buf[buflen-2] = '\0';
172 va_start(ap, fmt);
173 vsnprintf(buf, buflen, fmt, ap);
174 va_end(ap);
175 if (buf[buflen-2] != '\0') {
176 zfree(buf);
177 buflen *= 2;
178 continue;
179 }
180 break;
181 }
182 t = sdscat(s, buf);
183 zfree(buf);
184 return t;
185}
186
187sds sdstrim(sds s, const char *cset) {
188 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
189 char *start, *end, *sp, *ep;
190 size_t len;
191
192 sp = start = s;
193 ep = end = s+sdslen(s)-1;
194 while(sp <= end && strchr(cset, *sp)) sp++;
195 while(ep > start && strchr(cset, *ep)) ep--;
196 len = (sp > ep) ? 0 : ((ep-sp)+1);
197 if (sh->buf != sp) memmove(sh->buf, sp, len);
198 sh->buf[len] = '\0';
f1017b3f 199 sh->free = sh->free+(sh->len-len);
200 sh->len = len;
ed9b544e 201 return s;
202}
203
204sds sdsrange(sds s, long start, long end) {
205 struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
206 size_t newlen, len = sdslen(s);
207
208 if (len == 0) return s;
209 if (start < 0) {
210 start = len+start;
211 if (start < 0) start = 0;
212 }
213 if (end < 0) {
214 end = len+end;
215 if (end < 0) end = 0;
216 }
217 newlen = (start > end) ? 0 : (end-start)+1;
218 if (newlen != 0) {
219 if (start >= (signed)len) start = len-1;
220 if (end >= (signed)len) end = len-1;
221 newlen = (start > end) ? 0 : (end-start)+1;
222 } else {
223 start = 0;
224 }
225 if (start != 0) memmove(sh->buf, sh->buf+start, newlen);
226 sh->buf[newlen] = 0;
f1017b3f 227 sh->free = sh->free+(sh->len-newlen);
228 sh->len = newlen;
ed9b544e 229 return s;
230}
231
232void sdstolower(sds s) {
233 int len = sdslen(s), j;
234
235 for (j = 0; j < len; j++) s[j] = tolower(s[j]);
236}
237
238void sdstoupper(sds s) {
239 int len = sdslen(s), j;
240
241 for (j = 0; j < len; j++) s[j] = toupper(s[j]);
242}
243
244int sdscmp(sds s1, sds s2) {
245 size_t l1, l2, minlen;
246 int cmp;
247
248 l1 = sdslen(s1);
249 l2 = sdslen(s2);
250 minlen = (l1 < l2) ? l1 : l2;
251 cmp = memcmp(s1,s2,minlen);
252 if (cmp == 0) return l1-l2;
253 return cmp;
254}
255
256/* Split 's' with separator in 'sep'. An array
257 * of sds strings is returned. *count will be set
258 * by reference to the number of tokens returned.
259 *
260 * On out of memory, zero length string, zero length
261 * separator, NULL is returned.
262 *
263 * Note that 'sep' is able to split a string using
264 * a multi-character separator. For example
265 * sdssplit("foo_-_bar","_-_"); will return two
266 * elements "foo" and "bar".
267 *
268 * This version of the function is binary-safe but
269 * requires length arguments. sdssplit() is just the
270 * same function but for zero-terminated strings.
271 */
272sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
273 int elements = 0, slots = 5, start = 0, j;
274
275 sds *tokens = zmalloc(sizeof(sds)*slots);
276#ifdef SDS_ABORT_ON_OOM
277 if (tokens == NULL) sdsOomAbort();
278#endif
279 if (seplen < 1 || len < 0 || tokens == NULL) return NULL;
280 for (j = 0; j < (len-(seplen-1)); j++) {
281 /* make sure there is room for the next element and the final one */
282 if (slots < elements+2) {
a4d1ba9a 283 sds *newtokens;
284
ed9b544e 285 slots *= 2;
a4d1ba9a 286 newtokens = zrealloc(tokens,sizeof(sds)*slots);
ed9b544e 287 if (newtokens == NULL) {
288#ifdef SDS_ABORT_ON_OOM
289 sdsOomAbort();
290#else
291 goto cleanup;
292#endif
293 }
294 tokens = newtokens;
295 }
296 /* search the separator */
297 if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
298 tokens[elements] = sdsnewlen(s+start,j-start);
299 if (tokens[elements] == NULL) {
300#ifdef SDS_ABORT_ON_OOM
301 sdsOomAbort();
302#else
303 goto cleanup;
304#endif
305 }
306 elements++;
307 start = j+seplen;
308 j = j+seplen-1; /* skip the separator */
309 }
310 }
311 /* Add the final element. We are sure there is room in the tokens array. */
312 tokens[elements] = sdsnewlen(s+start,len-start);
313 if (tokens[elements] == NULL) {
314#ifdef SDS_ABORT_ON_OOM
315 sdsOomAbort();
316#else
317 goto cleanup;
318#endif
319 }
320 elements++;
321 *count = elements;
322 return tokens;
323
324#ifndef SDS_ABORT_ON_OOM
325cleanup:
326 {
327 int i;
328 for (i = 0; i < elements; i++) sdsfree(tokens[i]);
329 zfree(tokens);
330 return NULL;
331 }
332#endif
333}