]>
Commit | Line | Data |
---|---|---|
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 <stdarg.h> | |
37 | #include <string.h> | |
38 | #include <ctype.h> | |
39 | #include "zmalloc.h" | |
40 | ||
41 | static void sdsOomAbort(void) { | |
42 | fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n"); | |
43 | abort(); | |
44 | } | |
45 | ||
46 | sds sdsnewlen(const void *init, size_t initlen) { | |
47 | struct sdshdr *sh; | |
48 | ||
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; | |
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 | ||
65 | sds sdsempty(void) { | |
66 | return sdsnewlen("",0); | |
67 | } | |
68 | ||
69 | sds sdsnew(const char *init) { | |
70 | size_t initlen = (init == NULL) ? 0 : strlen(init); | |
71 | return sdsnewlen(init, initlen); | |
72 | } | |
73 | ||
74 | size_t sdslen(const sds s) { | |
75 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
76 | return sh->len; | |
77 | } | |
78 | ||
79 | sds sdsdup(const sds s) { | |
80 | return sdsnewlen(s, sdslen(s)); | |
81 | } | |
82 | ||
83 | void sdsfree(sds s) { | |
84 | if (s == NULL) return; | |
85 | zfree(s-sizeof(struct sdshdr)); | |
86 | } | |
87 | ||
88 | size_t sdsavail(sds s) { | |
89 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
90 | return sh->free; | |
91 | } | |
92 | ||
93 | void sdsupdatelen(sds s) { | |
94 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
95 | int reallen = strlen(s); | |
96 | sh->free += (sh->len-reallen); | |
97 | sh->len = reallen; | |
98 | } | |
99 | ||
100 | static sds sdsMakeRoomFor(sds s, size_t addlen) { | |
101 | struct sdshdr *sh, *newsh; | |
102 | size_t free = sdsavail(s); | |
103 | size_t len, newlen; | |
104 | ||
105 | if (free >= addlen) return s; | |
106 | len = sdslen(s); | |
107 | sh = (void*) (s-(sizeof(struct sdshdr))); | |
108 | newlen = (len+addlen)*2; | |
109 | newsh = zrealloc(sh, sizeof(struct sdshdr)+newlen+1); | |
110 | #ifdef SDS_ABORT_ON_OOM | |
111 | if (newsh == NULL) sdsOomAbort(); | |
112 | #else | |
113 | if (newsh == NULL) return NULL; | |
114 | #endif | |
115 | ||
116 | newsh->free = newlen - len; | |
117 | return newsh->buf; | |
118 | } | |
119 | ||
120 | sds sdscatlen(sds s, void *t, size_t len) { | |
121 | struct sdshdr *sh; | |
122 | size_t curlen = sdslen(s); | |
123 | ||
124 | s = sdsMakeRoomFor(s,len); | |
125 | if (s == NULL) return NULL; | |
126 | sh = (void*) (s-(sizeof(struct sdshdr))); | |
127 | memcpy(s+curlen, t, len); | |
128 | sh->len = curlen+len; | |
129 | sh->free = sh->free-len; | |
130 | s[curlen+len] = '\0'; | |
131 | return s; | |
132 | } | |
133 | ||
134 | sds sdscat(sds s, char *t) { | |
135 | return sdscatlen(s, t, strlen(t)); | |
136 | } | |
137 | ||
138 | sds sdscpylen(sds s, char *t, size_t len) { | |
139 | struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr))); | |
140 | size_t totlen = sh->free+sh->len; | |
141 | ||
142 | if (totlen < len) { | |
143 | s = sdsMakeRoomFor(s,len-sh->len); | |
144 | if (s == NULL) return NULL; | |
145 | sh = (void*) (s-(sizeof(struct sdshdr))); | |
146 | totlen = sh->free+sh->len; | |
147 | } | |
148 | memcpy(s, t, len); | |
149 | s[len] = '\0'; | |
150 | sh->len = len; | |
151 | sh->free = totlen-len; | |
152 | return s; | |
153 | } | |
154 | ||
155 | sds sdscpy(sds s, char *t) { | |
156 | return sdscpylen(s, t, strlen(t)); | |
157 | } | |
158 | ||
159 | sds sdscatprintf(sds s, const char *fmt, ...) { | |
160 | va_list ap; | |
161 | char *buf, *t; | |
162 | size_t buflen = 16; | |
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 | ||
187 | sds 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'; | |
199 | sh->free = sh->free+(sh->len-len); | |
200 | sh->len = len; | |
201 | return s; | |
202 | } | |
203 | ||
204 | sds sdsrange(sds s, int start, int 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; | |
227 | sh->free = sh->free+(sh->len-newlen); | |
228 | sh->len = newlen; | |
229 | return s; | |
230 | } | |
231 | ||
232 | void sdstolower(sds s) { | |
233 | int len = sdslen(s), j; | |
234 | ||
235 | for (j = 0; j < len; j++) s[j] = tolower(s[j]); | |
236 | } | |
237 | ||
238 | void sdstoupper(sds s) { | |
239 | int len = sdslen(s), j; | |
240 | ||
241 | for (j = 0; j < len; j++) s[j] = toupper(s[j]); | |
242 | } | |
243 | ||
244 | int 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 | */ | |
272 | sds *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 | if (len == 0) { | |
281 | *count = 0; | |
282 | return tokens; | |
283 | } | |
284 | for (j = 0; j < (len-(seplen-1)); j++) { | |
285 | /* make sure there is room for the next element and the final one */ | |
286 | if (slots < elements+2) { | |
287 | sds *newtokens; | |
288 | ||
289 | slots *= 2; | |
290 | newtokens = zrealloc(tokens,sizeof(sds)*slots); | |
291 | if (newtokens == NULL) { | |
292 | #ifdef SDS_ABORT_ON_OOM | |
293 | sdsOomAbort(); | |
294 | #else | |
295 | goto cleanup; | |
296 | #endif | |
297 | } | |
298 | tokens = newtokens; | |
299 | } | |
300 | /* search the separator */ | |
301 | if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) { | |
302 | tokens[elements] = sdsnewlen(s+start,j-start); | |
303 | if (tokens[elements] == NULL) { | |
304 | #ifdef SDS_ABORT_ON_OOM | |
305 | sdsOomAbort(); | |
306 | #else | |
307 | goto cleanup; | |
308 | #endif | |
309 | } | |
310 | elements++; | |
311 | start = j+seplen; | |
312 | j = j+seplen-1; /* skip the separator */ | |
313 | } | |
314 | } | |
315 | /* Add the final element. We are sure there is room in the tokens array. */ | |
316 | tokens[elements] = sdsnewlen(s+start,len-start); | |
317 | if (tokens[elements] == NULL) { | |
318 | #ifdef SDS_ABORT_ON_OOM | |
319 | sdsOomAbort(); | |
320 | #else | |
321 | goto cleanup; | |
322 | #endif | |
323 | } | |
324 | elements++; | |
325 | *count = elements; | |
326 | return tokens; | |
327 | ||
328 | #ifndef SDS_ABORT_ON_OOM | |
329 | cleanup: | |
330 | { | |
331 | int i; | |
332 | for (i = 0; i < elements; i++) sdsfree(tokens[i]); | |
333 | zfree(tokens); | |
334 | return NULL; | |
335 | } | |
336 | #endif | |
337 | } | |
338 | ||
339 | void sdsfreesplitres(sds *tokens, int count) { | |
340 | if (!tokens) return; | |
341 | while(count--) | |
342 | sdsfree(tokens[count]); | |
343 | zfree(tokens); | |
344 | } | |
345 | ||
346 | sds sdsfromlonglong(long long value) { | |
347 | char buf[32], *p; | |
348 | unsigned long long v; | |
349 | ||
350 | v = (value < 0) ? -value : value; | |
351 | p = buf+31; /* point to the last character */ | |
352 | do { | |
353 | *p-- = '0'+(v%10); | |
354 | v /= 10; | |
355 | } while(v); | |
356 | if (value < 0) *p-- = '-'; | |
357 | p++; | |
358 | return sdsnewlen(p,32-(p-buf)); | |
359 | } | |
360 | ||
361 | sds sdscatrepr(sds s, char *p, size_t len) { | |
362 | s = sdscatlen(s,"\"",1); | |
363 | while(len--) { | |
364 | switch(*p) { | |
365 | case '\\': | |
366 | case '"': | |
367 | s = sdscatprintf(s,"\\%c",*p); | |
368 | break; | |
369 | case '\n': s = sdscatlen(s,"\\n",1); break; | |
370 | case '\r': s = sdscatlen(s,"\\r",1); break; | |
371 | case '\t': s = sdscatlen(s,"\\t",1); break; | |
372 | case '\a': s = sdscatlen(s,"\\a",1); break; | |
373 | case '\b': s = sdscatlen(s,"\\b",1); break; | |
374 | default: | |
375 | if (isprint(*p)) | |
376 | s = sdscatprintf(s,"%c",*p); | |
377 | else | |
378 | s = sdscatprintf(s,"\\x%02x",(unsigned char)*p); | |
379 | break; | |
380 | } | |
381 | p++; | |
382 | } | |
383 | return sdscatlen(s,"\"",1); | |
384 | } |