]>
git.saurik.com Git - redis.git/blob - src/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
41 static void sdsOomAbort(void) {
42 fprintf(stderr
,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
46 sds
sdsnewlen(const void *init
, size_t initlen
) {
49 sh
= zmalloc(sizeof(struct sdshdr
)+initlen
+1);
50 #ifdef SDS_ABORT_ON_OOM
51 if (sh
== NULL
) sdsOomAbort();
53 if (sh
== NULL
) return NULL
;
58 if (init
) memcpy(sh
->buf
, init
, initlen
);
59 else memset(sh
->buf
,0,initlen
);
61 sh
->buf
[initlen
] = '\0';
62 return (char*)sh
->buf
;
66 return sdsnewlen("",0);
69 sds
sdsnew(const char *init
) {
70 size_t initlen
= (init
== NULL
) ? 0 : strlen(init
);
71 return sdsnewlen(init
, initlen
);
74 size_t sdslen(const sds s
) {
75 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
79 sds
sdsdup(const sds s
) {
80 return sdsnewlen(s
, sdslen(s
));
84 if (s
== NULL
) return;
85 zfree(s
-sizeof(struct sdshdr
));
88 size_t sdsavail(sds s
) {
89 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
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
);
100 static sds
sdsMakeRoomFor(sds s
, size_t addlen
) {
101 struct sdshdr
*sh
, *newsh
;
102 size_t free
= sdsavail(s
);
105 if (free
>= addlen
) return 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();
113 if (newsh
== NULL
) return NULL
;
116 newsh
->free
= newlen
- len
;
120 sds
sdscatlen(sds s
, void *t
, size_t len
) {
122 size_t curlen
= sdslen(s
);
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';
134 sds
sdscat(sds s
, char *t
) {
135 return sdscatlen(s
, t
, strlen(t
));
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
;
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
;
151 sh
->free
= totlen
-len
;
155 sds
sdscpy(sds s
, char *t
) {
156 return sdscpylen(s
, t
, strlen(t
));
159 sds
sdscatprintf(sds s
, const char *fmt
, ...) {
165 buf
= zmalloc(buflen
);
166 #ifdef SDS_ABORT_ON_OOM
167 if (buf
== NULL
) sdsOomAbort();
169 if (buf
== NULL
) return NULL
;
171 buf
[buflen
-2] = '\0';
173 vsnprintf(buf
, buflen
, fmt
, ap
);
175 if (buf
[buflen
-2] != '\0') {
187 sds
sdstrim(sds s
, const char *cset
) {
188 struct sdshdr
*sh
= (void*) (s
-(sizeof(struct sdshdr
)));
189 char *start
, *end
, *sp
, *ep
;
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
);
199 sh
->free
= sh
->free
+(sh
->len
-len
);
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
);
208 if (len
== 0) return s
;
211 if (start
< 0) start
= 0;
215 if (end
< 0) end
= 0;
217 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
219 if (start
>= (signed)len
) start
= len
-1;
220 if (end
>= (signed)len
) end
= len
-1;
221 newlen
= (start
> end
) ? 0 : (end
-start
)+1;
225 if (start
!= 0) memmove(sh
->buf
, sh
->buf
+start
, newlen
);
227 sh
->free
= sh
->free
+(sh
->len
-newlen
);
232 void sdstolower(sds s
) {
233 int len
= sdslen(s
), j
;
235 for (j
= 0; j
< len
; j
++) s
[j
] = tolower(s
[j
]);
238 void sdstoupper(sds s
) {
239 int len
= sdslen(s
), j
;
241 for (j
= 0; j
< len
; j
++) s
[j
] = toupper(s
[j
]);
244 int sdscmp(sds s1
, sds s2
) {
245 size_t l1
, l2
, minlen
;
250 minlen
= (l1
< l2
) ? l1
: l2
;
251 cmp
= memcmp(s1
,s2
,minlen
);
252 if (cmp
== 0) return l1
-l2
;
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.
260 * On out of memory, zero length string, zero length
261 * separator, NULL is returned.
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".
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.
272 sds
*sdssplitlen(char *s
, int len
, char *sep
, int seplen
, int *count
) {
273 int elements
= 0, slots
= 5, start
= 0, j
;
275 sds
*tokens
= zmalloc(sizeof(sds
)*slots
);
276 #ifdef SDS_ABORT_ON_OOM
277 if (tokens
== NULL
) sdsOomAbort();
279 if (seplen
< 1 || len
< 0 || tokens
== NULL
) return NULL
;
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) {
290 newtokens
= zrealloc(tokens
,sizeof(sds
)*slots
);
291 if (newtokens
== NULL
) {
292 #ifdef SDS_ABORT_ON_OOM
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
312 j
= j
+seplen
-1; /* skip the separator */
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
328 #ifndef SDS_ABORT_ON_OOM
332 for (i
= 0; i
< elements
; i
++) sdsfree(tokens
[i
]);
339 void sdsfreesplitres(sds
*tokens
, int count
) {
342 sdsfree(tokens
[count
]);
346 sds
sdsfromlonglong(long long value
) {
348 unsigned long long v
;
350 v
= (value
< 0) ? -value
: value
;
351 p
= buf
+31; /* point to the last character */
356 if (value
< 0) *p
-- = '-';
358 return sdsnewlen(p
,32-(p
-buf
));
361 sds
sdscatrepr(sds s
, char *p
, size_t len
) {
362 s
= sdscatlen(s
,"\"",1);
367 s
= sdscatprintf(s
,"\\%c",*p
);
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;
376 s
= sdscatprintf(s
,"%c",*p
);
378 s
= sdscatprintf(s
,"\\x%02x",(unsigned char)*p
);
383 return sdscatlen(s
,"\"",1);