]>
Commit | Line | Data |
---|---|---|
24f753a8 PN |
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 | ||
31 | #define SDS_ABORT_ON_OOM | |
32 | ||
33 | #include "sds.h" | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include <string.h> | |
37 | #include <ctype.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 = malloc(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 | free(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 = realloc(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, const 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, const 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-sh->len); | |
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 sdscatvprintf(sds s, const char *fmt, va_list ap) { | |
158 | va_list cpy; | |
159 | char *buf, *t; | |
160 | size_t buflen = 16; | |
161 | ||
162 | while(1) { | |
163 | buf = malloc(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_copy(cpy,ap); | |
171 | vsnprintf(buf, buflen, fmt, cpy); | |
172 | if (buf[buflen-2] != '\0') { | |
173 | free(buf); | |
174 | buflen *= 2; | |
175 | continue; | |
176 | } | |
177 | break; | |
178 | } | |
179 | t = sdscat(s, buf); | |
180 | free(buf); | |
181 | return t; | |
182 | } | |
183 | ||
184 | sds sdscatprintf(sds s, const char *fmt, ...) { | |
185 | va_list ap; | |
186 | char *t; | |
187 | va_start(ap, fmt); | |
188 | t = sdscatvprintf(s,fmt,ap); | |
189 | va_end(ap); | |
190 | return t; | |
191 | } | |
192 | ||
193 | sds sdstrim(sds s, const char *cset) { | |
194 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
195 | char *start, *end, *sp, *ep; | |
196 | size_t len; | |
197 | ||
198 | sp = start = s; | |
199 | ep = end = s+sdslen(s)-1; | |
200 | while(sp <= end && strchr(cset, *sp)) sp++; | |
201 | while(ep > start && strchr(cset, *ep)) ep--; | |
202 | len = (sp > ep) ? 0 : ((ep-sp)+1); | |
203 | if (sh->buf != sp) memmove(sh->buf, sp, len); | |
204 | sh->buf[len] = '\0'; | |
205 | sh->free = sh->free+(sh->len-len); | |
206 | sh->len = len; | |
207 | return s; | |
208 | } | |
209 | ||
210 | sds sdsrange(sds s, int start, int end) { | |
211 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
212 | size_t newlen, len = sdslen(s); | |
213 | ||
214 | if (len == 0) return s; | |
215 | if (start < 0) { | |
216 | start = len+start; | |
217 | if (start < 0) start = 0; | |
218 | } | |
219 | if (end < 0) { | |
220 | end = len+end; | |
221 | if (end < 0) end = 0; | |
222 | } | |
223 | newlen = (start > end) ? 0 : (end-start)+1; | |
224 | if (newlen != 0) { | |
225 | if (start >= (signed)len) start = len-1; | |
226 | if (end >= (signed)len) end = len-1; | |
227 | newlen = (start > end) ? 0 : (end-start)+1; | |
228 | } else { | |
229 | start = 0; | |
230 | } | |
231 | if (start != 0) memmove(sh->buf, sh->buf+start, newlen); | |
232 | sh->buf[newlen] = 0; | |
233 | sh->free = sh->free+(sh->len-newlen); | |
234 | sh->len = newlen; | |
235 | return s; | |
236 | } | |
237 | ||
238 | void sdstolower(sds s) { | |
239 | int len = sdslen(s), j; | |
240 | ||
241 | for (j = 0; j < len; j++) s[j] = tolower(s[j]); | |
242 | } | |
243 | ||
244 | void sdstoupper(sds s) { | |
245 | int len = sdslen(s), j; | |
246 | ||
247 | for (j = 0; j < len; j++) s[j] = toupper(s[j]); | |
248 | } | |
249 | ||
250 | int sdscmp(sds s1, sds s2) { | |
251 | size_t l1, l2, minlen; | |
252 | int cmp; | |
253 | ||
254 | l1 = sdslen(s1); | |
255 | l2 = sdslen(s2); | |
256 | minlen = (l1 < l2) ? l1 : l2; | |
257 | cmp = memcmp(s1,s2,minlen); | |
258 | if (cmp == 0) return l1-l2; | |
259 | return cmp; | |
260 | } | |
261 | ||
262 | /* Split 's' with separator in 'sep'. An array | |
263 | * of sds strings is returned. *count will be set | |
264 | * by reference to the number of tokens returned. | |
265 | * | |
266 | * On out of memory, zero length string, zero length | |
267 | * separator, NULL is returned. | |
268 | * | |
269 | * Note that 'sep' is able to split a string using | |
270 | * a multi-character separator. For example | |
271 | * sdssplit("foo_-_bar","_-_"); will return two | |
272 | * elements "foo" and "bar". | |
273 | * | |
274 | * This version of the function is binary-safe but | |
275 | * requires length arguments. sdssplit() is just the | |
276 | * same function but for zero-terminated strings. | |
277 | */ | |
278 | sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) { | |
279 | int elements = 0, slots = 5, start = 0, j; | |
280 | ||
281 | sds *tokens = malloc(sizeof(sds)*slots); | |
282 | #ifdef SDS_ABORT_ON_OOM | |
283 | if (tokens == NULL) sdsOomAbort(); | |
284 | #endif | |
285 | if (seplen < 1 || len < 0 || tokens == NULL) return NULL; | |
286 | if (len == 0) { | |
287 | *count = 0; | |
288 | return tokens; | |
289 | } | |
290 | for (j = 0; j < (len-(seplen-1)); j++) { | |
291 | /* make sure there is room for the next element and the final one */ | |
292 | if (slots < elements+2) { | |
293 | sds *newtokens; | |
294 | ||
295 | slots *= 2; | |
296 | newtokens = realloc(tokens,sizeof(sds)*slots); | |
297 | if (newtokens == NULL) { | |
298 | #ifdef SDS_ABORT_ON_OOM | |
299 | sdsOomAbort(); | |
300 | #else | |
301 | goto cleanup; | |
302 | #endif | |
303 | } | |
304 | tokens = newtokens; | |
305 | } | |
306 | /* search the separator */ | |
307 | if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { | |
308 | tokens[elements] = sdsnewlen(s+start,j-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 | start = j+seplen; | |
318 | j = j+seplen-1; /* skip the separator */ | |
319 | } | |
320 | } | |
321 | /* Add the final element. We are sure there is room in the tokens array. */ | |
322 | tokens[elements] = sdsnewlen(s+start,len-start); | |
323 | if (tokens[elements] == NULL) { | |
324 | #ifdef SDS_ABORT_ON_OOM | |
325 | sdsOomAbort(); | |
326 | #else | |
327 | goto cleanup; | |
328 | #endif | |
329 | } | |
330 | elements++; | |
331 | *count = elements; | |
332 | return tokens; | |
333 | ||
334 | #ifndef SDS_ABORT_ON_OOM | |
335 | cleanup: | |
336 | { | |
337 | int i; | |
338 | for (i = 0; i < elements; i++) sdsfree(tokens[i]); | |
339 | free(tokens); | |
340 | return NULL; | |
341 | } | |
342 | #endif | |
343 | } | |
344 | ||
345 | void sdsfreesplitres(sds *tokens, int count) { | |
346 | if (!tokens) return; | |
347 | while(count--) | |
348 | sdsfree(tokens[count]); | |
349 | free(tokens); | |
350 | } | |
351 | ||
352 | sds sdsfromlonglong(long long value) { | |
353 | char buf[32], *p; | |
354 | unsigned long long v; | |
355 | ||
356 | v = (value < 0) ? -value : value; | |
357 | p = buf+31; /* point to the last character */ | |
358 | do { | |
359 | *p-- = '0'+(v%10); | |
360 | v /= 10; | |
361 | } while(v); | |
362 | if (value < 0) *p-- = '-'; | |
363 | p++; | |
364 | return sdsnewlen(p,32-(p-buf)); | |
365 | } | |
366 | ||
367 | sds sdscatrepr(sds s, char *p, size_t len) { | |
368 | s = sdscatlen(s,"\"",1); | |
369 | while(len--) { | |
370 | switch(*p) { | |
371 | case '\\': | |
372 | case '"': | |
373 | s = sdscatprintf(s,"\\%c",*p); | |
374 | break; | |
375 | case '\n': s = sdscatlen(s,"\\n",1); break; | |
376 | case '\r': s = sdscatlen(s,"\\r",1); break; | |
377 | case '\t': s = sdscatlen(s,"\\t",1); break; | |
378 | case '\a': s = sdscatlen(s,"\\a",1); break; | |
379 | case '\b': s = sdscatlen(s,"\\b",1); break; | |
380 | default: | |
381 | if (isprint(*p)) | |
382 | s = sdscatprintf(s,"%c",*p); | |
383 | else | |
384 | s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); | |
385 | break; | |
386 | } | |
387 | p++; | |
388 | } | |
389 | return sdscatlen(s,"\"",1); | |
390 | } | |
391 | ||
392 | /* Split a line into arguments, where every argument can be in the | |
393 | * following programming-language REPL-alike form: | |
394 | * | |
395 | * foo bar "newline are supported\n" and "\xff\x00otherstuff" | |
396 | * | |
397 | * The number of arguments is stored into *argc, and an array | |
398 | * of sds is returned. The caller should sdsfree() all the returned | |
399 | * strings and finally free() the array itself. | |
400 | * | |
401 | * Note that sdscatrepr() is able to convert back a string into | |
402 | * a quoted string in the same format sdssplitargs() is able to parse. | |
403 | */ | |
404 | sds *sdssplitargs(char *line, int *argc) { | |
405 | char *p = line; | |
406 | char *current = NULL; | |
407 | char **vector = NULL; | |
408 | ||
409 | *argc = 0; | |
410 | while(1) { | |
411 | /* skip blanks */ | |
412 | while(*p && isspace(*p)) p++; | |
413 | if (*p) { | |
414 | /* get a token */ | |
415 | int inq=0; /* set to 1 if we are in "quotes" */ | |
416 | int done=0; | |
417 | ||
418 | if (current == NULL) current = sdsempty(); | |
419 | while(!done) { | |
420 | if (inq) { | |
421 | if (*p == '\\' && *(p+1)) { | |
422 | char c; | |
423 | ||
424 | p++; | |
425 | switch(*p) { | |
426 | case 'n': c = '\n'; break; | |
427 | case 'r': c = '\r'; break; | |
428 | case 't': c = '\t'; break; | |
429 | case 'b': c = '\b'; break; | |
430 | case 'a': c = '\a'; break; | |
431 | default: c = *p; break; | |
432 | } | |
433 | current = sdscatlen(current,&c,1); | |
434 | } else if (*p == '"') { | |
435 | /* closing quote must be followed by a space */ | |
436 | if (*(p+1) && !isspace(*(p+1))) goto err; | |
437 | done=1; | |
438 | } else if (!*p) { | |
439 | /* unterminated quotes */ | |
440 | goto err; | |
441 | } else { | |
442 | current = sdscatlen(current,p,1); | |
443 | } | |
444 | } else { | |
445 | switch(*p) { | |
446 | case ' ': | |
447 | case '\n': | |
448 | case '\r': | |
449 | case '\t': | |
450 | case '\0': | |
451 | done=1; | |
452 | break; | |
453 | case '"': | |
454 | inq=1; | |
455 | break; | |
456 | default: | |
457 | current = sdscatlen(current,p,1); | |
458 | break; | |
459 | } | |
460 | } | |
461 | if (*p) p++; | |
462 | } | |
463 | /* add the token to the vector */ | |
464 | vector = realloc(vector,((*argc)+1)*sizeof(char*)); | |
465 | vector[*argc] = current; | |
466 | (*argc)++; | |
467 | current = NULL; | |
468 | } else { | |
469 | return vector; | |
470 | } | |
471 | } | |
472 | ||
473 | err: | |
474 | while((*argc)--) | |
475 | sdsfree(vector[*argc]); | |
476 | free(vector); | |
477 | if (current) sdsfree(current); | |
478 | return NULL; | |
479 | } |