]>
git.saurik.com Git - redis.git/blob - deps/hiredis/sds.c
1 /* SDSLib, A C dynamic strings library
3 * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #define SDS_ABORT_ON_OOM
39 static void sdsOomAbort(void) {
40 fprintf(stderr
,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
44 sds
sdsnewlen(const void *init
, size_t initlen
) {
47 sh
= malloc(sizeof(struct sdshdr
)+initlen
+1);
48 #ifdef SDS_ABORT_ON_OOM
49 if (sh
== NULL
) sdsOomAbort();
51 if (sh
== NULL
) return NULL
;
56 if (init
) memcpy(sh
->buf
, init
, initlen
);
57 else memset(sh
->buf
,0,initlen
);
59 sh
->buf
[initlen
] = '\0';
60 return (char*)sh
->buf
;
64 return sdsnewlen("",0);
67 sds
sdsnew(const char *init
) {
68 size_t initlen
= (init
== NULL
) ? 0 : strlen(init
);
69 return sdsnewlen(init
, initlen
);
72 size_t sdslen(const sds s
) {
73 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
77 sds
sdsdup(const sds s
) {
78 return sdsnewlen(s
, sdslen(s
));
82 if (s
== NULL
) return;
83 free(s
-sizeof(struct sdshdr
));
86 size_t sdsavail(sds s
) {
87 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
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
);
98 static sds
sdsMakeRoomFor(sds s
, size_t addlen
) {
99 struct sdshdr
*sh
, *newsh
;
100 size_t free
= sdsavail(s
);
103 if (free
>= addlen
) return 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();
111 if (newsh
== NULL
) return NULL
;
114 newsh
->free
= newlen
- len
;
118 sds
sdscatlen(sds s
, const void *t
, size_t len
) {
120 size_t curlen
= sdslen(s
);
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';
132 sds
sdscat(sds s
, const char *t
) {
133 return sdscatlen(s
, t
, strlen(t
));
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
;
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
;
149 sh
->free
= totlen
-len
;
153 sds
sdscpy(sds s
, char *t
) {
154 return sdscpylen(s
, t
, strlen(t
));
157 sds
sdscatvprintf(sds s
, const char *fmt
, va_list ap
) {
163 buf
= malloc(buflen
);
164 #ifdef SDS_ABORT_ON_OOM
165 if (buf
== NULL
) sdsOomAbort();
167 if (buf
== NULL
) return NULL
;
169 buf
[buflen
-2] = '\0';
171 vsnprintf(buf
, buflen
, fmt
, cpy
);
172 if (buf
[buflen
-2] != '\0') {
184 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
188 t
= sdscatvprintf(s
,fmt
,ap
);
193 sds
sdstrim(sds s
, const char *cset
) {
194 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
195 char *start
, *end
, *sp
, *ep
;
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
);
205 sh
->free
= sh
->free
+(sh
->len
-len
);
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
);
214 if (len
== 0) return s
;
217 if (start
< 0) start
= 0;
221 if (end
< 0) end
= 0;
223 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
225 if (start
>= (signed)len
) start
= len
-1;
226 if (end
>= (signed)len
) end
= len
-1;
227 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
231 if (start
!= 0) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
233 sh
->free
= sh
->free
+(sh
->len
-newlen
);
238 void sdstolower(sds s
) {
239 int len
= sdslen(s
), j
;
241 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
244 void sdstoupper(sds s
) {
245 int len
= sdslen(s
), j
;
247 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
250 int sdscmp(sds s1
, sds s2
) {
251 size_t l1
, l2
, minlen
;
256 minlen
= (l1
< l2
) ? l1
: l2
;
257 cmp
= memcmp(s1
,s2
,minlen
);
258 if (cmp
== 0) return l1
-l2
;
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.
266 * On out of memory, zero length string, zero length
267 * separator, NULL is returned.
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".
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.
278 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
279 int elements
= 0, slots
= 5, start
= 0, j
;
281 sds
*tokens
= malloc(sizeof(sds
)*slots
);
282 #ifdef SDS_ABORT_ON_OOM
283 if (tokens
== NULL
) sdsOomAbort();
285 if (seplen
< 1 || len
< 0 || tokens
== NULL
) return NULL
;
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) {
296 newtokens
= realloc(tokens
,sizeof(sds
)*slots
);
297 if (newtokens
== NULL
) {
298 #ifdef SDS_ABORT_ON_OOM
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
318 j
= j
+seplen
-1; /* skip the separator */
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
334 #ifndef SDS_ABORT_ON_OOM
338 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
345 void sdsfreesplitres(sds
*tokens
, int count
) {
348 sdsfree(tokens
[count
]);
352 sds
sdsfromlonglong(long long value
) {
354 unsigned long long v
;
356 v
= (value
< 0) ? -value
: value
;
357 p
= buf
+31; /* point to the last character */
362 if (value
< 0) *p
-- = '-';
364 return sdsnewlen(p
,32-(p
-buf
));
367 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
368 s
= sdscatlen(s
,"\"",1);
373 s
= sdscatprintf(s
,"\\%c",*p
);
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;
382 s
= sdscatprintf(s
,"%c",*p
);
384 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
389 return sdscatlen(s
,"\"",1);
392 /* Split a line into arguments, where every argument can be in the
393 * following programming-language REPL-alike form:
395 * foo bar "newline are supported\n" and "\xff\x00otherstuff"
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.
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.
404 sds
*sdssplitargs(char *line
, int *argc
) {
406 char *current
= NULL
;
407 char **vector
= NULL
;
412 while(*p
&& isspace(*p
)) p
++;
415 int inq
=0; /* set to 1 if we are in "quotes" */
418 if (current
== NULL
) current
= sdsempty();
421 if (*p
== '\\' && *(p
+1)) {
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;
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
;
439 /* unterminated quotes */
442 current
= sdscatlen(current
,p
,1);
457 current
= sdscatlen(current
,p
,1);
463 /* add the token to the vector */
464 vector
= realloc(vector
,((*argc
)+1)*sizeof(char*));
465 vector
[*argc
] = current
;
475 sdsfree(vector
[*argc
]);
477 if (current
) sdsfree(current
);