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
|
#include <stdlib.h>
#include <string.h>
#include "fstring.h"
/*
* Search first occurence of character in string
*/
ssize_t
fstrchr (f_str_t *src, char c)
{
register ssize_t cur = 0;
while (cur < src->len) {
if (*(src->begin + cur) == c) {
return cur;
}
cur ++;
}
return -1;
}
/*
* Search last occurence of character in string
*/
ssize_t
fstrrchr (f_str_t *src, char c)
{
register ssize_t cur = src->len;
while (cur > 0) {
if (*(src->begin + cur) == c) {
return cur;
}
cur --;
}
return -1;
}
/*
* Search for pattern in orig
*/
ssize_t
fstrstr (f_str_t *orig, f_str_t *pattern)
{
register ssize_t cur = 0, pcur = 0;
if (pattern->len > orig->len) {
return -1;
}
while (cur < orig->len) {
if (*(orig->begin + cur) == *pattern->begin) {
while (cur < orig->len && pcur < pattern->len) {
if (*(orig->begin + cur) != *(pattern->begin + pcur)) {
pcur = 0;
break;
}
cur ++;
pcur ++;
}
return cur - pattern->len;
}
cur ++;
}
return -1;
}
/*
* Split string by tokens
* word contains parsed word
*
* Return: -1 - no new words can be extracted
* 1 - word was extracted and there are more words
* 0 - last word extracted
*/
int
fstrtok (f_str_t *text, const char *sep, f_tok_t *state)
{
register size_t cur;
const char *csep = sep;
if (state->pos >= text->len) {
return -1;
}
cur = state->pos;
while (cur < text->len) {
while (*csep) {
if (*(text->begin + cur) == *csep) {
state->word.begin = (text->begin + state->pos);
state->word.len = cur - state->pos;
state->pos = cur + 1;
return 1;
}
csep ++;
}
csep = sep;
cur ++;
}
/* Last word */
state->word.begin = (text->begin + state->pos);
state->word.len = cur - state->pos;
state->pos = cur;
return 0;
}
/*
* Copy one string into other
*/
size_t
fstrcpy (f_str_t *dest, f_str_t *src)
{
register size_t cur = 0;
if (dest->size < src->len) {
return 0;
}
while (cur < src->len && cur < dest->size) {
*(dest->begin + cur) = *(src->begin + cur);
cur ++;
}
return cur;
}
/*
* Concatenate two strings
*/
size_t
fstrcat (f_str_t *dest, f_str_t *src)
{
register size_t cur = src->len;
if (dest->size < src->len + dest->len) {
return 0;
}
while (cur < src->len && cur < dest->size) {
*(dest->begin + cur) = *(src->begin + cur);
cur ++;
}
dest->len += src->len;
return cur;
}
/*
* Make copy of string to 0-terminated string
*/
char*
fstrcstr (f_str_t *str, memory_pool_t *pool)
{
char *res;
res = memory_pool_alloc (pool, str->len + 1);
memcpy (res, str->begin, str->len);
res[str->len] = 0;
return res;
}
/*
* Push one character to fstr
*/
int
fstrpush (f_str_t *dest, char c)
{
if (dest->size < dest->len) {
/* Need to reallocate string */
return 0;
}
*(dest->begin + dest->len) = c;
dest->len ++;
return 1;
}
/*
* Allocate memory for f_str_t
*/
f_str_t*
fstralloc (memory_pool_t *pool, size_t len)
{
f_str_t *res = memory_pool_alloc (pool, sizeof (f_str_t));
if (res == NULL) {
return NULL;
}
res->begin = memory_pool_alloc (pool, len);
if (res->begin == NULL) {
free (res);
return NULL;
}
res->size = len;
res->len = 0;
return res;
}
/*
* Truncate string to its len
*/
f_str_t*
fstrtruncate (memory_pool_t *pool, f_str_t *orig)
{
f_str_t *res;
if (orig == NULL || orig->len == 0 || orig->size <= orig->len) {
return orig;
}
res = fstralloc (pool, orig->len);
if (res == NULL) {
return NULL;
}
fstrcpy (res, orig);
return res;
}
/*
* Enlarge string to new size
*/
f_str_t*
fstrgrow (memory_pool_t *pool, f_str_t *orig, size_t newlen)
{
f_str_t *res;
if (orig == NULL || orig->len == 0 || orig->size >= newlen) {
return orig;
}
res = fstralloc (pool, newlen);
if (res == NULL) {
return NULL;
}
fstrcpy (res, orig);
return res;
}
/*
* Return hash value for a string
*/
uint32_t
fstrhash (f_str_t *str)
{
size_t i;
uint32_t hval;
uint32_t tmp;
if (str == NULL) {
return 0;
}
hval = str->len;
for (i = 0; i < str->len; i++) {
/*
* xor in the current byte against each byte of hval
* (which alone gaurantees that every bit of input will have
* an effect on the output)
*/
tmp = *(str->begin + i) & 0xFF;
tmp = tmp | (tmp << 8) | (tmp << 16) | (tmp << 24);
hval ^= tmp;
/* add some bits out of the middle as low order bits */
hval = hval + ((hval >> 12) & 0x0000ffff) ;
/* swap most and min significative bytes */
tmp = (hval << 24) | ((hval >> 24) & 0xff);
/* zero most and min significative bytes of hval */
hval &= 0x00ffff00;
hval |= tmp;
/*
* rotate hval 3 bits to the left (thereby making the
* 3rd msb of the above mess the hsb of the output hash)
*/
hval = (hval << 3) + (hval >> 29);
}
return hval;
}
|