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