-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring.c
387 lines (351 loc) · 9.65 KB
/
string.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// string.c
//
// Copyright 2021 The Hook Programming Language Authors.
//
// This file is part of the Hook project.
// For detailed license information, please refer to the LICENSE file
// located in the root directory of this project.
//
#include "hook/string.h"
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include "hook/memory.h"
#include "hook/utils.h"
#ifdef _WIN32
#define strtok_r strtok_s
#endif
static inline HkString *string_allocate(int minCapacity);
static inline void add_char(HkString *str, char c);
static inline uint32_t hash(int length, char *chars);
static inline int index_of(char *chars, int subLength, char *sub);
static inline HkString *string_allocate(int minCapacity)
{
HkString *str = (HkString *) hk_allocate(sizeof(*str));
++minCapacity;
int capacity = minCapacity < HK_STRING_MIN_CAPACITY ? HK_STRING_MIN_CAPACITY : minCapacity;
capacity = hk_power_of_two_ceil(capacity);
str->refCount = 0;
str->capacity = capacity;
str->chars = (char *) hk_allocate(capacity);
str->hash = -1;
return str;
}
static inline void add_char(HkString *str, char c)
{
hk_string_ensure_capacity(str, str->length + 1);
str->chars[str->length] = c;
}
static inline uint32_t hash(int length, char *chars)
{
uint32_t hash = 2166136261u;
for (int i = 0; i < length; i++)
{
hash ^= chars[i];
hash *= 16777619;
}
return hash;
}
static inline int index_of(char *chars, int subLength, char *sub)
{
int i = 0;
for (; chars[i + subLength]; ++i)
{
if (!memcmp(&chars[i], sub, subLength))
return i;
}
if (!memcmp(&chars[i], sub, subLength))
return i;
return -1;
}
HkString *hk_string_new(void)
{
return hk_string_new_with_capacity(0);
}
HkString *hk_string_new_with_capacity(int minCapacity)
{
HkString *str = string_allocate(minCapacity);
str->length = 0;
str->chars[0] = '\0';
return str;
}
HkString *hk_string_from_chars(int length, const char *chars)
{
if (length < 0)
length = (int) strnlen(chars, INT_MAX);
HkString *str = string_allocate(length);
str->length = length;
memcpy(str->chars, chars, length);
str->chars[length] = '\0';
return str;
}
HkString *hk_string_from_stream(FILE *stream, const char delim)
{
HkString *str = string_allocate(0);
str->length = 0;
for (;;)
{
int c = fgetc(stream);
if (c == EOF || c == delim)
break;
add_char(str, (char) c);
++str->length;
}
add_char(str, '\0');
return str;
}
void hk_string_ensure_capacity(HkString *str, int minCapacity)
{
if (minCapacity <= str->capacity)
return;
int capacity = hk_power_of_two_ceil(minCapacity);
str->capacity = capacity;
str->chars = (char *) hk_reallocate(str->chars, capacity);
}
void hk_string_free(HkString *str)
{
hk_free(str->chars);
hk_free(str);
}
void hk_string_release(HkString *str)
{
hk_decr_ref(str);
if (hk_is_unreachable(str))
hk_string_free(str);
}
HkString *hk_string_copy(HkString *str)
{
int length = str->length;
HkString *result = string_allocate(length);
memcpy(result->chars, str->chars, length);
result->length = length;
result->chars[length] = '\0';
return result;
}
HkString *hk_string_concat(HkString *str1, HkString *str2)
{
int length = str1->length + str2->length;
HkString *result = string_allocate(length);
memcpy(result->chars, str1->chars, str1->length);
memcpy(&result->chars[str1->length], str2->chars, str2->length);
result->length = length;
result->chars[length] = '\0';
return result;
}
void hk_string_inplace_concat_char(HkString *dest, char c)
{
int length = dest->length;
hk_string_ensure_capacity(dest, length + 2);
dest->chars[length] = c;
dest->chars[length + 1] = '\0';
dest->length += 1;
}
void hk_string_inplace_concat_chars(HkString *dest, int length, const char *chars)
{
if (length < 0)
length = (int) strnlen(chars, INT_MAX);
int new_length = dest->length + length;
hk_string_ensure_capacity(dest, new_length + 1);
memcpy(&dest->chars[dest->length], chars, length);
dest->length = new_length;
dest->chars[new_length] = '\0';
dest->hash = -1;
}
void hk_string_inplace_concat(HkString *dest, HkString *src)
{
int length = dest->length + src->length;
hk_string_ensure_capacity(dest, length + 1);
memcpy(&dest->chars[dest->length], src->chars, src->length);
dest->length = length;
dest->chars[length] = '\0';
dest->hash = -1;
}
int hk_string_index_of_chars(HkString *str, int length, const char *chars)
{
if (length < 0)
length = (int) strnlen(chars, INT_MAX);
if (!length || length > str->length)
return -1;
return index_of(str->chars, length, (char *) chars);
}
int hk_string_index_of(HkString *str, HkString *sub)
{
int strLength = str->length;
int subLength = sub->length;
if (!subLength || subLength > strLength)
return -1;
return index_of(str->chars, subLength, sub->chars);
}
HkString *hk_string_replace_all(HkString *str, HkString *sub1, HkString *sub2)
{
int strLength = str->length;
int subLength = sub1->length;
if (!subLength || subLength > strLength)
return hk_string_copy(str);
HkString *result = string_allocate(0);
result->length = 0;
char *strChars = str->chars;
char *subChars = sub1->chars;
for (;;)
{
if (!strLength || strLength > str->length)
break;
int index = index_of(strChars, subLength, subChars);
if (index == -1)
break;
hk_string_inplace_concat_chars(result, index, strChars);
hk_string_inplace_concat(result, sub2);
int count = index + subLength;
strLength -= count;
strChars += count;
}
hk_string_inplace_concat_chars(result, -1, strChars);
return result;
}
HkString *hk_string_slice(HkString *str, int start, int stop)
{
int length = str->length;
start = start < 0 ? 0 : start;
start = start > length ? length : start;
stop = stop < 0 ? length : stop;
stop = stop > length ? length : stop;
length = stop - start;
length = length < 0 ? 0 : length;
HkString *result = string_allocate(length);
result->length = length;
memcpy(result->chars, &str->chars[start], length);
result->chars[length] = '\0';
return result;
}
HkArray *hk_string_split(HkString *str, HkString *sep)
{
HkArray *arr = hk_array_new();
// TODO: Do not use strtok_r and do not copy the string.
HkString *_str = hk_string_copy(str);
char *cur = _str->chars;
char *tk = strtok_r(cur, sep->chars, &cur);
while (tk)
{
HkValue elem = hk_string_value(hk_string_from_chars(-1, tk));
hk_array_inplace_append_element(arr, elem);
tk = strtok_r(NULL, sep->chars, &cur);
}
hk_string_free(_str);
return arr;
}
void hk_string_print(HkString *str, bool quoted)
{
printf(quoted ? "\"%.*s\"" : "%.*s", str->length, str->chars);
}
uint32_t hk_string_hash(HkString *str)
{
if (str->hash == -1)
str->hash = hash(str->length, str->chars);
return (uint32_t) str->hash;
}
bool hk_string_equal(HkString *str1, HkString *str2)
{
return str1 == str2 || (str1->length == str2->length
&& !memcmp(str1->chars, str2->chars, str1->length));
}
int hk_string_compare(HkString *str1, HkString *str2)
{
int result = strcmp(str1->chars, str2->chars);
return result > 0 ? 1 : (result < 0 ? -1 : 0);
}
HkString *hk_string_lower(HkString *str)
{
int length = str->length;
HkString *result = string_allocate(length);
result->length = length;
for (int i = 0; i < length; ++i)
result->chars[i] = (char) tolower(str->chars[i]);
result->chars[length] = '\0';
return result;
}
HkString *hk_string_upper(HkString *str)
{
int length = str->length;
HkString *result = string_allocate(length);
result->length = length;
for (int i = 0; i < length; ++i)
result->chars[i] = (char) toupper(str->chars[i]);
result->chars[length] = '\0';
return result;
}
bool hk_string_trim(HkString *str, HkString **result)
{
int length = str->length;
if (!length)
return false;
int l = 0;
while (isspace(str->chars[l]))
++l;
int high = length - 1;
int h = high;
while (h > l && isspace(str->chars[h]))
--h;
if (!l && h == high)
return false;
HkString *_result = string_allocate(h - l + 1);
int j = 0;
for (int i = l; i <= h; ++i)
_result->chars[j++] = str->chars[i];
_result->chars[j] = '\0';
_result->length = j;
*result = _result;
return true;
}
bool hk_string_starts_with(HkString *str1, HkString *str2)
{
if (!str1->length || !str2->length || str1->length < str2->length)
return false;
return !memcmp(str1->chars, str2->chars, str2->length);
}
bool hk_string_ends_with(HkString *str1, HkString *str2)
{
if (!str1->length || !str2->length || str1->length < str2->length)
return false;
return !memcmp(&str1->chars[str1->length - str2->length], str2->chars, str2->length);
}
HkString *hk_string_reverse(HkString *str)
{
int length = str->length;
HkString *result = string_allocate(length);
result->length = length;
for (int i = 0; i < length; ++i)
result->chars[i] = str->chars[length - i - 1];
result->chars[length] = '\0';
return result;
}
void hk_string_serialize(HkString *str, FILE *stream)
{
fwrite(&str->capacity, sizeof(str->capacity), 1, stream);
fwrite(&str->length, sizeof(str->length), 1, stream);
fwrite(str->chars, str->length + 1, 1, stream);
fwrite(&str->hash, sizeof(str->hash), 1, stream);
}
HkString *hk_string_deserialize(FILE *stream)
{
int capacity;
int length;
if (fread(&capacity, sizeof(capacity), 1, stream) != 1)
return NULL;
if (fread(&length, sizeof(length), 1, stream) != 1)
return NULL;
HkString *str = string_allocate(capacity);
str->length = length;
str->chars[length] = '\0';
if (fread(str->chars, length + 1, 1, stream) != 1)
{
hk_string_free(str);
return NULL;
}
if (fread(&str->hash, sizeof(str->hash), 1, stream) != 1)
{
hk_string_free(str);
return NULL;
}
return str;
}