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