-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSString.c
More file actions
413 lines (366 loc) · 8.94 KB
/
SString.c
File metadata and controls
413 lines (366 loc) · 8.94 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "SString.h"
char HEX[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
SString* sstr_createcs(char* str)
{
SString* sstring = (SString*)malloc(sizeof(SString));
sstring->s_capacity = 16;
sstring->s_size = 0;
sstring->s_str = (char*) malloc(sstring->s_capacity);
for(;*str != 0; str++)
{
_sstr_appendc(sstring, *str);
}
_sstr_appendc(sstring, null);
sstring->s_size--;
return sstring;
}
SString* sstr_creates(char* str, unsigned int size)
{
SString* sstring = (SString*)malloc(sizeof(SString));
sstring->s_capacity = size;
sstring->s_size = size;
sstring->s_str = (char*)malloc(size);
memcpy(sstring->s_str, str, size);
_sstr_appendc(sstring, null);
sstring->s_size--;
return sstring;
}
SString* sstr_createe()
{
SString* sstring = (SString*)malloc(sizeof(SString));
sstring->s_capacity = 2;
sstring->s_size = 0;
sstring->s_str = (char*) malloc(sstring->s_capacity);
_sstr_appendc(sstring, null);
sstring->s_size--;
return sstring;
}
SString* sstr_clone(SString* str)
{
SString* ret = (SString*)malloc(sizeof(SString));
ret->s_size = str->s_size;
ret->s_capacity = ret->s_size;
ret->s_str = (char*)malloc(sizeof(SString));
memcpy(ret->s_str, str->s_str, ret->s_size);
return ret;
}
static inline void _sstr_appendc(SString* str, char ch)
{
if(str->s_capacity == str->s_size)
{
str->s_capacity *= 2;
str->s_str = (char*)realloc(str->s_str, str->s_capacity);
}
*(str->s_str + str->s_size) = ch;
str->s_size++;
}
void sstr_appendc(SString* str, char ch)
{
_sstr_appendc(str, ch);
}
void sstr_appendcs(struct _sstring* base, char* str)
{
char* ch;
for(ch = str;*ch != null;ch++)
{
_sstr_appendc(base, *ch);
}
_sstr_appendc(base, null);
base->s_size--;
}
void sstr_appends(SString* base, char* str, unsigned int size)
{
extend: if(base->s_capacity < base->s_size+size)
{
base->s_capacity *= 2;
base->s_str = (char*)realloc(base->s_str, base->s_capacity);
goto extend;
}
memcpy(base->s_str+base->s_size, str, size);
base->s_size += size;
}
char sstr_equals(SString* base, char* str, unsigned int size)
{
if(size != base->s_size)
return 0;
unsigned int i;
for(i = 0; i < size; ++i)
{
if(*(base->s_str + i) != *(str + i))
return 0;
}
return 1;
}
signed char sstr_equals_mo(SString* base, char opts, ...)
{
va_list arguments;
va_start ( arguments, opts );
unsigned int i = 0, size = 0;
_ch:if(i < opts)
{
++i;
char* str = va_arg(arguments, char*);
size = va_arg(arguments, unsigned int);
if(size != base->s_size)
goto _ch;
unsigned int i;
for(i = 0; i < size; ++i)
{
if(*(base->s_str + i) != *(str + i))
goto _ch;
}
va_end ( arguments );
return i;
}
return -1;
}
char sstr_endswith(SString* base, char* end, unsigned int end_size)
{
if(end_size > base->s_size)
return 0;
unsigned int sp = base->s_size - end_size;
unsigned int i;
for(i = 0; i < end_size; i++)
{
if(*(end+i) != *(base->s_str + sp + i))
{
return 0;
}
}
return 1;
}
char sstr_startswith(SString* base, char* start, unsigned int start_size)
{
if(start_size > base->s_size)
return 0;
unsigned int i;
for(i = 0; i < start_size; i++)
{
if(*(base->s_str+i) != *(start + i))
{
return 0;
}
}
return 1;
}
extern inline unsigned long long sstr_toLong(SString* str)
{
unsigned long long ret = 1;
int i;
for(i = 0; i < str->s_size; ++i)
{
ret += *(str->s_str + i);
ret <<= 1;
}
return ret;
}
extern inline unsigned long long sstr_cs_toLong(char* str)
{
unsigned long long ret = 1;
int i;
for(;*str != 0;str++)
{
ret += *str;
ret <<= 1;
}
return ret;
}
static inline char sstr_smallerth(SString* str1, SString* str2)
{
int size = str1->s_size > str2->s_size ? str1->s_size : str2->s_size;
int i;
for(i = 0; i < size; ++i)
{
if(*(str1->s_str) > *(str2->s_str))
return 0;
else if(*(str1->s_str) < *(str2->s_str))
return 1;
}
return 0;
}
char sstr_smaller(SString* str1, SString* str2)
{
return sstr_smallerth(str1, str2);
}
char sstr_bigger(SString* str1, SString* str2)
{
return sstr_smallerth(str2, str1);
}
void sstr_delete(SString* sstr)
{
free(sstr->s_str);
free(sstr);
}
static inline void sstr_remove(SString* str, unsigned int a, unsigned int b)
{
//printf("%d %d\n", str->s_size, b);
//printf("%d %d %d\n", str->s_str +a, str->s_str+b, str->s_size-b);
memmove(str->s_str +a, str->s_str+b, str->s_size-b);
str->s_size -= (b-a);
_sstr_appendc(str, null);
str->s_size--;
}
static inline void sstr_removeAll(SString* str, char* remove, unsigned int size)
{
int p = 0;
int end_p;
loop: end_p = (str->s_size - size);
unsigned int _i;
for(_i = 0; _i < size; ++_i)
{
if(*(str->s_str + _i + p) != *(remove + _i))
goto br;
}
sstr_remove(str, p, p + size);
if(p < str->s_size)
{
goto loop;
}
br:if(p < end_p)
{
//printf("i%d\n",p);
++p;
goto loop;
}
_sstr_appendc(str, null);
str->s_size--;
}
void sstr_replaceAll(SString* str, char* target, unsigned int tsize, char* replacement, unsigned int rsize)
{
if(rsize == 0)
{
sstr_removeAll(str, target, tsize);
return;
}
unsigned int end_p = str->s_size - tsize;
int i = 0;
int _i;
loop:for(_i = 0; _i < tsize; ++_i)
{
if(*(str->s_str + _i + i) != *(target + _i))
goto br;
}
sstr_remove(str, i, i + tsize);
sstr_insert(str, i, replacement, rsize);
end_p+=rsize;
i+=rsize-1;
br:if(i < end_p)
{
++i; goto loop;
}
}
void sstr_print(SString* str)
{
fwrite(str->s_str, str->s_size, 1, stdout);
printf("\n");
}
void sstr_printf(SString* str)
{
fwrite(str->s_str, str->s_size, 1, stdout);
}
static inline void sstr_insert(SString* str, unsigned int pos, char* insert, unsigned int isize)
{
extend: if(str->s_capacity < str->s_size+isize)
{
str->s_capacity *= 2;
str->s_str = (char*)realloc(str->s_str, str->s_capacity);
goto extend;
}
memmove(str->s_str + pos + isize, str->s_str + pos, str->s_size-pos);
memcpy(str->s_str + pos, insert, isize);
str->s_size += isize;
}
void sstr_write(SString* str, FILE* file)
{
fwrite(str->s_str, str->s_size, 1, file);
}
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i = size-1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}
void sstr_appendh(struct _sstring* base, unsigned long long num)
{
char* appe = (char*)__builtin_alloca(64);
unsigned char i;
for(i = 0; num != 0; i++)
{
*(appe + (63 -i)) = HEX[num % 16];
num >>= 4;
}
sstr_appends(base, appe + (64-i), i);
}
void sstr_appendd(struct _sstring* base, long long num)
{
if(num == 0)
{
sstr_appendc(base, '0');
return;
}
char* appe = (char*)__builtin_alloca(64);
if(((unsigned long long)num & 9223372036854775808) == 9223372036854775808)
{
sstr_appendc(base, '-');
num *= (-1);
}
unsigned char i;
for(i = 0; num != 0; i++)
{
*(appe + (63 -i)) = 48 + (num % 10);
num /= 10;
}
sstr_appends(base, appe + (64-i), i);
}
extern inline char sstr_isEmpty(SString* str)
{
return (str->s_size == 0);
}
#ifdef __cplusplus
void sstr_clear(SString* str)
#else
extern inline void sstr_clear(SString* str)
#endif
{
str->s_size = 0;
_sstr_appendc(str, null); // set to 0, that printf outputs an empty string
str->s_size--;
}
extern inline unsigned int sstr_size(SString* str)
{
return str->s_size;
}
void sstr_fill(SString* base, char ch, unsigned int amount)
{
unsigned int c = base->s_size + amount;
extend: if(base->s_capacity < c)
{
base->s_capacity *= 2;
base->s_str = (char*)realloc(base->s_str, base->s_capacity);
goto extend;
}
for(;base->s_size < c;base->s_size++)
{
*(base->s_str + base->s_size) = ch;
}
}
void sstr_removeFromEndAfter(SString* str, char target)
{
for(;(str->s_size > 0) && (*(str->s_str +str->s_size) != target); str->s_size--);
str->s_size++;
_sstr_appendc(str, null);
str->s_size--;
}
void sstr_removeFromEnd(SString* string, unsigned int amount)
{
string->s_size-=amount;
_sstr_appendc(string, null);
string->s_size--;
}