-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsds.c
302 lines (259 loc) · 5.89 KB
/
sds.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "sds.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
sds sdsNewLen(const void *init, size_t initLen)
{
struct sdshdr * sh;
sh = malloc(sizeof(struct sdshdr) + initLen + 1);
if (sh == NULL)
fprintf(stderr, "malloc() error");
sh->len = initLen;
if (strlen(init) >= initLen)
sh->free = 0;
else
sh->free = initLen - strlen(init);
if (initLen) {
if (init)
memcpy(sh->buf, init, initLen);
else
memset(sh->buf, 0, initLen);
}
sh->buf[initLen] = '\0';
return (char *)sh->buf;
}
sds sdsNew(const char *init)
{
size_t initLen = (init == NULL) ? 0 : strlen(init);
return sdsNewLen(init, initLen);
}
sds sdsEmpty()
{
return sdsNewLen("", 0);
}
size_t sdsLen(const sds s)
{
struct sdshdr *sh;
sh = (void *)(s - sizeof(*sh));
return sh->len;
}
sds sdsDup(const sds s)
{
return sdsNewLen(s, sdsLen(s));
}
void sdsFree(sds s)
{
if (s == NULL)
return ;
free(s - sizeof(struct sdshdr));
}
size_t sdsAvail(const sds s)
{
struct sdshdr *sh = (void *) (s - sizeof(struct sdshdr));
return sh->free;
}
static sds sdsMakeRoom(sds s, size_t len)
{
struct sdshdr *sh, *newsh;
size_t freeLen = sdsAvail(s);
if (freeLen >= len) return s;
sh = (void *)(s - sizeof(struct sdshdr));
size_t useLen = sdsLen(s);
newsh = realloc(sh, sizeof(struct sdshdr) + (useLen + len) * 2 + 1);
if (newsh == NULL)
fprintf(stderr, "realloc() error");
newsh->len = useLen + len;
newsh->free = useLen + 2 * len;
return newsh->buf;
}
sds sdsCatLen(sds s, void *t, size_t len)
{
struct sdshdr *sh;
size_t oldLen = sdsLen(s);
s = sdsMakeRoom(s, len);
memcpy(s + oldLen, t, len);
sh = (void *)(s - sizeof(struct sdshdr));
sh->len = oldLen + len;
sh->free = sh->free - len;
s[sh->len] = '\0';
return s;
}
sds sdsCat(sds s, char * t)
{
return sdsCatLen(s, t, strlen(t));
}
sds sdsTrimSet(sds s, const char *cTrim)
{
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
char *start, *end, *sp, *ep;
size_t len;
sp = start = s;
ep = end = s+sdsLen(s)-1;
while(sp <= end && strchr(cTrim, *sp)) sp++;
while(ep > start && strchr(cTrim, *ep)) ep--;
len = (sp > ep) ? 0 : ((ep-sp)+1);
if (sh->buf != sp) memmove(sh->buf, sp, len);
sh->buf[len] = '\0';
sh->free = sh->free+(sh->len-len);
sh->len = len;
return s;
}
/* bug, need modify
sds sdsTrim(sds s, const char *cTrim)
{
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
char *sp, *ep, *cp;
size_t len = strlen(cTrim);
sp = s;
ep = s + sdsLen(s) - len;
cp = cTrim;
for (int i = 0; i < len; i++) {
if (*sp == *cp) {
sp++;
cp++;
} else {
sp = s;
break;
}
}
cp = cTrim;
for (int i = 0; i < len; i++) {
if (*ep == *cp) {
ep++;
cp++;
} else
break;
}
if (ep == s + sdsLen(s))
ep = s + sdsLen(s) - strlen(cTrim);
else
ep = s + sdsLen(s);
len = (sp > ep) ? 0 : (ep - sp);
if (sh->buf != sp) memmove(sh->buf, sp, len);
sh->buf[len] = '\0';
sh->free = sh->free + (sh->len - len);
sh->len = len;
return sh->buf;
}
*/
sds sdsRange(sds s, long start, long end)
{
struct sdshdr *newsh;
size_t newLen, len = sdsLen(s);
if (len == 0) return s;
if (start < 0) {
start = start + len;
if (start < 0)
start = 0;
}
if (end < 0) {
end = end + len;
if (end < 0)
end = 0;
}
newLen = (start >= end) ? 0 : (end - start) + 1;
if (newLen == 0)
return sdsEmpty();
else {
if (start >= (signed)len) start = len;
if (end >= (signed)len) end = len;
newLen = (start >= end) ? 0 : (end - start);
}
newsh = (void *)(sdsNewLen(s + start, newLen) - sizeof(struct sdshdr));
return newsh->buf;
}
int sdsCmp(sds s1, sds s2)
{
int len_s1, len_s2, min_len;
int cmp;
len_s1 = sdsLen(s1);
len_s2 = sdsLen(s2);
min_len = (len_s1 < len_s2) ? len_s1 : len_s2;
cmp = memcmp(s1, s2, min_len);
return cmp;
}
sds *sdsSplitLen(char *s, int len, char *sep, int seplen, int *count)
{
int elements = 0, slots = 5, start = 0, j;
if (len < 0)
fprintf(stderr, "The source string's length is less than 0.\n");
if (seplen < 1)
fprintf(stderr, "The sep string's length is less than 1.\n");
sds *tokens = malloc(sizeof(sds) * slots);
if (tokens == NULL)
fprintf(stderr, "malloc() error");
for (j = 0; j < (len - (seplen - 1)); j++) {
if (slots < elements + 2) {
slots *= 2;
sds *newSlots = realloc(tokens, sizeof(sds) * slots);
if (newSlots == NULL)
goto cleanup;
tokens = newSlots;
}
if ((seplen == 1 && *(s + j) == sep[0]) || (memcmp(s + j, sep, seplen) == 0)) {
tokens[elements] = sdsNewLen(s + start, j - start);
if (tokens[elements] == NULL)
goto cleanup;
elements++;
start = j + seplen;
j = j + seplen - 1;
}
}
tokens[elements] = sdsNewLen(s + start, len - start);
if (tokens[elements] == NULL)
goto cleanup;
elements++;
*count = elements;
return tokens;
cleanup:
{
int i;
for (i = 0; i < elements; i++) sdsFree(tokens[i]);
free(tokens);
return NULL;
}
}
void sdsFreeSplitRes(sds *tokens, int count)
{
if (!tokens) return;
while (count--)
sdsFree(tokens[count]);
free(tokens);
}
void sdsUpdateLen(sds s)
{
struct sdshdr *sh = (void *)(s - sizeof(struct sdshdr));
sh->len = strlen(s);
}
int sdsStartsWith(sds s, char *str)
{
size_t len = strlen(str);
if (strlen(str) <= len && strncmp(s, str, len) == 0)
return SDS_TRUE;
return SDS_FALSE;
}
sds sdsCatPrintf(sds s, const char *fmt, ...)
{
va_list ap;
char *buf, *t;
size_t buflen = 32;
while (1) {
buf = malloc(buflen);
if (buf == NULL) return NULL;
buf[buflen - 2] = '\0';
va_start(ap, fmt);
vsnprintf(buf, buflen, fmt, ap);
va_end(ap);
if (buf[buflen -2] != '\0') {
free(buf);
buflen *= 2;
continue;
}
break;
}
t = sdsCat(s, buf);
free(buf);
return t;
}