-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstrlib.c
623 lines (492 loc) · 13.3 KB
/
gstrlib.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#include "gstrlib.h"
#include "aux.h"
#include "error.h"
/*
Internal struct that GString.internal points to.
Contains contents of the actual string, as well as its length.
This is done to hide struct from user, so that user cannot access it.
*/
typedef struct{
char *builder;
int length;
}GStr_Builder;
/*------------------ERROR CHECKING FUNCTIONS-----------------*/
int bound_check(GString *g, int start, int end, int length, const char *func){
GStr_Builder *b = (GStr_Builder *) g->internal;
/*In case user enters something past boundaries*/
if(start < 0 || end < 0 || start >= length || end > length || start > end){
fprintf(stderr, "Error: Index out of bounds in function %s\n", func);
exit(IND_OUT_BOUNDS);
}
return NO_ERROR;
}
/*Checks for null pointers, exits if found*/
EXCEPTION err_check(GString *g, const char *func){
if(!g){
fprintf(stderr, "Error: NULL POINTER EXCEPTION in function %s.\n", func);
exit(NULL_PTR);
}
GStr_Builder *b = (GStr_Builder *) g->internal;
if(!b){
fprintf(stderr, "Error: NULL POINTER EXCEPTION in function %s.\n", func);
exit(NULL_PTR);
}
return NO_ERROR;
}
/*--------------------"CONSTRUCTOR"----------------------*/
GString *new_GString(char * str){
GString *gstring = malloc(sizeof(*gstring));
gstring->internal = malloc(sizeof(GStr_Builder));
/*cast void pointer to private struct*/
GStr_Builder *b = (GStr_Builder *) gstring->internal;
if(str == NULL){
b->builder = NULL;
b->length = 0;
}
else{
b->length = len(str);
b->builder = malloc(b->length * sizeof(char) + 1);
copy(b->builder, str);
}
return gstring;
}
/*--------------------APPEND FUNCTIONS-------------------*/
GString *append_str(GString *g, char *str){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*reallocate enough memory for the 2 strings*/
b->builder = realloc(b->builder, sizeof(char) * (b->length + len(str)) + 1);
/*start from end of the first string and copy*/
copy(b->builder + b->length, str);
b->length += len(str);
/*null terminator*/
*(b->builder + b->length) = '\0';
return g;
}
GString *append_char(GString *g, char c){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*make room for one more char*/
b->length++;
/*add one byte for null terminator*/
b->builder = realloc(b->builder, sizeof(char) * (b->length + 1 + 1));
*(b->builder + b->length - 1) = c;
/*null terminator*/
*(b->builder + b->length) = '\0';
return g;
}
GString *append_double(GString *g, double val){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char hold[12 * 4];
sprintf(hold, "%f", val);
int temp = len(hold);
/*+1 for null terminator*/
b->builder = realloc(b->builder, sizeof(char) * (b->length + temp + 1));
/*copy new string at the end*/
copy(b->builder + b->length, hold);
/*update length*/
b->length += temp;
*(b->builder + b->length) = '\0';
return g;
}
// /*Literally the same thing as append_double*/
GString *append_float(GString *g, float val){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char hold[12 * 4];
sprintf(hold, "%f", val);
int temp = len(hold);
b->builder = realloc(b->builder, sizeof(char) * (b->length + temp + 1));
copy(b->builder + b->length, hold);
/*update length*/
b->length += temp;
*(b->builder + b->length) = '\0';
return g;
}
/*
Appending numbers is the same...I wonder if there is easier
way to do this...
*/
GString *append_int(GString *g, int val){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char hold[12 * 4];
sprintf(hold, "%d", val);
int temp = len(hold);
b->builder = realloc(b->builder, sizeof(char) * (b->length + temp + 1));
copy(b->builder + b->length, hold);
/*update length*/
b->length += temp;
/*null terminator*/
*(b->builder + b->length) = '\0';
return g;
}
/*----------------------ACCESS FUNCTIONS---------------------*/
char get(GString *g, int index){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(index < 0)
index += b->length;
bound_check(g, index, index, b->length, __func__);
return '\0';
return *(b->builder + index);
}
int length(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
return b->length;
}
char *substring(GString *g, int start, int end){
/*standard error checking*/
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*Handle negative indices like Python*/
if(start < 0)
start += b->length;
if(end < 0)
end += b->length;
if(start >= end)
return "";
bound_check(g, start, end, b->length, __func__);
char *fill = malloc(sizeof(char) * (end - start + 1));
int i;
/*Fill in temp, excluding the end index*/
for(i = start; i < end; i++)
fill[i - start] = b->builder[i];
fill[end-start] = '\0';
return fill;
}
char *reverse_substring(GString *g, int start, int end){
/*standard error checking*/
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*Handle negative indices like Python*/
if(start < 0)
start += b->length;
if(end < 0)
end += b->length;
if(start >= end)
return "";
bound_check(g, start, end, b->length, __func__);
char *fill = malloc(sizeof(char) * (end - start + 1));
int i;
/*Fill in temp, excluding the end index*/
for(i = start; i < end; i++)
fill[end - i - 1] = b->builder[i];
fill[end-start] = '\0';
return fill;
}
int *find(GString *g, char *str, int n){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(len(str) > b->length)
return NULL;
int i;
int *arr = malloc(sizeof(int) * n);
for(i = 0; i < n; i++)
*(arr + i) = -1;
char *src = b->builder;
char *dest = str;
register int j, k;
int index = 0;
for(i = 0; i < b->length; i++){
/*as long as equal, continue*/
for(j = i, k = 0; *src && *dest && src[j] == *dest;j++, k++, *dest++)
;
/*if dest == NULL, then we have found a match*/
if(!*dest)
arr[index++] = i;
/*reset dest to beginning of str*/
dest-=k;
}
return arr;
}
int index_of(GString *g, char *str){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(len(str) > b->length)
return -1;
int i;
char *src = b->builder;
char *dest = str;
register int j, k;
for(i = 0; i < b->length; i++){
/*as long as equal, continue*/
for(j = i, k = 0; *src && *dest && src[j] == *dest;j++, k++, *dest++)
;
/*if dest == NULL, then we have found a match*/
if(!*dest)
return i;
/*reset dest to beginning of str*/
dest-=k;
}
return -1;
}
int index_from(GString *g, char *str, int index){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(index < 0)
index += b->length;
bound_check(g, index, index, b->length, __func__);
if(len(str) > (b->length-index))
return IND_OUT_BOUNDS;
char *src = b->builder;
char *dest = str;
int i, j, k;
for(i = index; i < b->length; i++){
for(j = i, k = 0; *src && *dest && src[j] == *dest; dest++, j++, k++);
if(!*dest)
return i;
/*reset pointer to beginning of string*/
dest-=k;
}
return -1;
}
int last_index_of(GString *g, char *str){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(len(str) > b->length)
return IND_OUT_BOUNDS;
int dest_len = len(str);
char *src = b->builder;
char *dest = str + dest_len - 1;
int i, j, k;
/*Go and match in reverse*/
for(i = b->length - 1; i >= 0; i--){
for(j = i, k = dest_len - 1; k >= 0 && j >= 0 && src[j] == *dest; *dest--, j--, k--)
;
if(k == -1)
return j + 1;
/*reset pointer to end of the string*/
dest += (dest_len-1) - k;
}
return -1;
}
int last_index_from(GString *g, char *str, int index){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(index < 0)
index += b->length;
bound_check(g, index, index, b->length, __func__);
if(len(str) > (b->length - index))
return IND_OUT_BOUNDS;
int dest_len = len(str);
char *src = b->builder;
char *dest = str + dest_len - 1;
int i, j, k;
for(i = index; i >= 0; i--){
for(j = i, k = dest_len-1;j >= 0 && k >= 0 && src[j] == *dest; *dest--, j--, k--)
;
if(k == -1)
return j + 1;
/*reset pointer to end of string*/
dest += (dest_len - 1) - k;
}
return -1;
}
int occur(GString *g, char *str){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(len(str) > b->length)
return 0;
int i, total = 0;
char *src = b->builder;
char *dest = str;
register int j, k;
for(i = 0; i < b->length; i++){
/*as long as equal, continue*/
for(j = i, k = 0; *src && *dest && src[j] == *dest;j++, k++, *dest++)
;
/*if dest == NULL, then we have found a match*/
if(!*dest)
++total;
/*reset dest to beginning of str*/
dest-=k;
}
return total;
}
/*---------------------MODIFIER FUNCTIONS--------------------*/
GString *replace(GString *g, int start, int end, char *str){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(start < 0)
start += b->length;
if(end < 0)
end += b->length;
bound_check(g, start, end, b->length, __func__);
if(start >= end)
return g;
int i;
for(i = start; *str && i < end; i++){
*(b->builder + i) = *str++;
}
/*Word was shorter than interval, remove excess characters*/
if(i < end)
delete(g, i, end);
/*If word was longer than interval, insert remaining characters*/
if(*str)
insert(g, str, end);
return g;
}
GString *delete(GString *g, int start, int end){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(start < 0)
start += b->length;
if(end < 0)
end += b->length;
bound_check(g, start, end, b->length, __func__);
if(start >= end)
return g;
char *p1 = b->builder + start;
char *p2 = b->builder + end;
int i;
/*Length will decrease by end-start characters*/
b->length -= (end - start);
for(i = start; *p2 ; i++){
*p1++ = *p2++;
}
/*terminate the string*/
*p1 = '\0';
return g;
}
GString *set_char(GString *g, char c, int index){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
if(index < 0)
index += b->length;
bound_check(g, index, index, b->length, __func__);
*(b->builder + index) = c;
return g;
}
GString *remove_char_at(GString *g, int index){
err_check(g, __func__);
return delete(g, index, index + 1);
}
GString *remove_char(GString *g, char c, int occur){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
bound_check(g, occur, occur, b->length, __func__);
int track = 0;
int i;
for(i = 0; i < b->length && track < occur; i++){
if(*(b->builder + i) == c)
++track;
}
if(track == occur)
return remove_char_at(g, i - 1);
else //dont change anything
return g;
}
GString *remove_num_char(GString *g, char c, int num){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
bound_check(g, num, num, b->length, __func__);
int i, j;
for(i = 0, j = 0; i < b->length && j < num; i++){
if(*(b->builder + i) == c){
remove_char_at(g, i--);
++j;
}
}
}
GString *reverse(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char *beg = b->builder;
char *end = b->builder + b->length-1;
char c;
while(end >= beg){
c = *beg;
*beg++ = *end;
*end-- = c;
}
return g;
}
GString *insert(GString *g, char *str, int index){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*Handle negative indices*/
if(index < 0)
index += b->length;
bound_check(g, index, index, b->length, __func__);
if(index >= b->length)
return append_str(g, str);
int l = len(str);
char *temp = b->builder;
/*make space for new string*/
b->builder = malloc(sizeof(char) * b->length + l + 1);
b->length += l;
int i, j;
/*copy the first half of the old string*/
for(i = 0; i < index; i++)
*(b->builder + i) = *(temp + i);
/*copy the new string*/
for(j = index; j < l + index; j++)
*(b->builder + j) = *(str + j - index);
/*copy the second half of the old string*/
for(;j < b->length; j++)
*(b->builder + j) = *(temp + j - l);
/*Don't forget null terminator*/
b->builder[j] = '\0';
free(temp);
return g;
}
// /*------------------MISCELLANEOUS FUNCTIONS----------------*/
char *toString(GString *g){
err_check(g, __func__);
GStr_Builder *b= (GStr_Builder *) g->internal;
return b->builder;
}
GString *ltrim(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
/*find last occurrence of white space before*/
int count = -1;
while(b->builder[++count] == ' ')
;
replace(g, 0, count, "");
return g;
}
GString *rtrim(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char *end = b->builder + b->length - 1;
int track = 0;
/*Find the right most character that's not white space*/
while(*(end-track) == ' '){
--b->length;
++track;
}
/*If white space was detected*/
if(track > 0)
*(b->builder + b->length) = '\0';
return g;
}
GString *trim(GString *g){
rtrim(g);
ltrim(g);
return g;
}
void print(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
printf("%s\nLength: %d\n", b->builder, b->length);
}
void for_each(GString *g, void *(*func)(char)){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
char *trav = b->builder;
while(*trav){
func(*trav);
*trav++;
}
}
void free_GString(GString *g){
err_check(g, __func__);
GStr_Builder *b = (GStr_Builder *) g->internal;
free(b->builder);
free(b);
free(g);
}