-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring.c
More file actions
627 lines (496 loc) · 14.6 KB
/
Copy pathcstring.c
File metadata and controls
627 lines (496 loc) · 14.6 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
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
624
625
626
627
#include "cstring.h"
/**
* Helper Functions
* --------------------
*/
static cstring intl_buf_grow(cstring ptr, size_t count)
{
size_t cap_ = (ptr) ?
CSTRMAX(count, pvt_cstr_total_cap_(ptr) << 1) :
CSTRMAX(count, CSTRING_DEFAULT_CAP);
cap_ = CSTRMIN(cap_, cstr_max_size());
size_t tmp_size_ = ((cap_ + 1) * sizeof(*ptr))
+ sizeof(cstr_metadata_t);
cstr_metadata_t *base_ptr_;
if(ptr){
pvt_cstr_assert(pvt_cstr_is_malloced_(ptr),
"Literals(const char[]) are not modifiable");
base_ptr_ = pvt_cstr_realloc(
pvt_cstr_dat_to_mdata_(ptr), tmp_size_);
}
else {
base_ptr_ = pvt_cstr_malloc(tmp_size_);
}
pvt_cstr_assert(base_ptr_ != NULL, "allocation failed!!");
base_ptr_->is_malloced = true;
base_ptr_->capacity = (size_t)(cap_);
if(!ptr) base_ptr_->size = 0;
return (cstring)(base_ptr_ + 1);
}
// =========================================================
// PUBLIC API
// ========================================================
void
cstr_reserve(cstring *str, size_t new_cap)
{
pvt_check_length_error(new_cap);
if(!(*str) || new_cap > cstr_capacity(*str)) {
(*str) = intl_buf_grow(*str, new_cap);
}
}
void
cstr_shrink_to_fit(cstring *str)
{
if (!(*str) || !pvt_cstr_is_malloced_(*str)) return;
cstring str_ = *str;
size_t size_ = cstr_size(str_);
if (size_ < cstr_capacity(str_)) {
size_t new_cap_ = sizeof(cstr_metadata_t) + (size_ + 1) * sizeof(char);
cstr_metadata_t *mdata = (cstr_metadata_t *)
pvt_cstr_realloc(pvt_cstr_dat_to_mdata_(str_), new_cap_);
if (mdata) {
mdata->capacity = size_;
*str = (cstring)(mdata + 1);
}
}
}
/*
* Input & Output
* ---------------
*/
void
cstr_getline(cstring *str, FILE *stream)
{
if (str == NULL || stream == NULL)
return;
cstr_clear(str);
cstring str_ = *str;
int ch_ = 0;
while ((ch_ = fgetc(stream)) != EOF)
{
if (ch_ == '\n') break;
cstr_push_back(&str_, (char)ch_);
}
cstr_push_back(&str_, 0);
*str = str_;
}
void
cstr_println(const_cstring str)
{
fputs(str, stdout);
putc('\n', stdout);
}
/**
* Modifiers
* ----------
*/
void
cstr_clear(cstring *str)
{
pvt_cstr_fill_(*str, 0, cstr_size(*str), 0);
pvt_cstr_set_total_size_(*str, 0);
}
void
cstr_push_back(cstring *str, const char ch)
{
intl_append_cnt_ch(str, 1, ch);
}
void
cstr_pop_back(cstring *str)
{
intl_erase_ch_iter(str, (cstr_iterator){cstr_end(*str).it - 1});
}
size_t
cstr_copy(cstring *str, const_cstring other, const size_t count,
const size_t pos)
{
cstring str_ = *str;
size_t cnt_ = CSTRMIN(cstr_size(str_),
CSTRMIN(count, pvt_cstr_strlen(other) - pos)
);
pvt_cstr_memmove(str_, other + pos, cnt_);
return cnt_;
}
void
cstr_resize(cstring *str, const size_t count, const char ch)
{
cstring str_ = NULL;
size_t size_ = cstr_size(*str);
cstr_reserve(str, count);
str_ = *str;
if(count > size_) {
pvt_cstr_fill_(str_, size_, count - size_, ch);
}
str_[count] = (char)0;
pvt_cstr_set_total_size_(str_, count);
}
void
cstr_swap(cstring *str, cstring *other)
{
cstring tmp_ = *str;
*str = *other;
*other = tmp_;
}
/**
* Operations
* -----------
*/
cstring
cstr_substr(const_cstring str, const size_t pos, const size_t count)
{
pvt_cstr_assert(pos < cstr_size(str), "Index out of Bounds for pos");
size_t cnt_ = CSTRMIN(count, cstr_size(str) - pos);
cstring substr_ = intl_buf_grow(NULL, cnt_);
pvt_cstr_copy_(substr_, str + pos, cnt_);
return substr_;
}
// =========================================================
// PRIVATE FUNCTIONS
// ========================================================
/*
* Functions to assign
* -------------------
*/
cstring
intl_assign_str_range(cstring *str, const_cstring other,
const size_t pos, const size_t count)
{
size_t size_ = pvt_cstr_strlen(other);
if(pos >= size_) return *str;
size_t cnt_ = CSTRMIN(count, size_ - pos);
cstr_reserve(str, cnt_);
cstring str_ = *str;
pvt_cstr_memmove(str_, other + pos, cnt_);
str_[cnt_] = (char)0;
pvt_cstr_set_total_size_(str_, cnt_);
return str_;
}
cstring
intl_assign_cnt_ch(cstring *str, const size_t count, const char ch)
{
cstr_reserve(str, count);
cstring str_ = *str;
pvt_cstr_fill_(str_, 0, count, ch);
str_[count] = (char)0;
pvt_cstr_set_total_size_(str_, count);
return str_;
}
/*
* Functions to multiple args Constructors
* ---------------------------------------
*/
CSTR_NODISCARD
cstring
intl_init_cpy_ch(const size_t cnt, const char ch)
{
cstring str_ = NULL;
if(!cnt){
str_ = intl_buf_grow(NULL, CSTRING_DEFAULT_CAP);
str_[0] = (char)0;
pvt_cstr_set_total_size_(str_, 0);
}
intl_assign_cnt_ch(&str_, cnt, ch);
return str_;
}
CSTR_NODISCARD
cstring
intl_copy_constructor(const_cstring other)
{
size_t size_ = pvt_cstr_strlen(other);
cstring str_ = intl_buf_grow(NULL, size_);
pvt_cstr_copy_(str_, other, size_);
return str_;
}
CSTR_NODISCARD
cstring
intl_init_cpy_str_w_iter(cstr_iterator begin,
cstr_iterator end)
{
pvt_check_iterator_valid_range(begin, end);
size_t size_ = cstr_distance(begin, end);
cstring str_ = intl_buf_grow(NULL, size_);
pvt_cstr_copy_(str_, begin.it, size_);
return str_;
}
CSTR_NODISCARD
cstring
intl_init_cpy_str_w_off(const_cstring other,
const size_t start, const size_t offset)
{
size_t size_ = pvt_cstr_strlen(other),
cnt_ = CSTRMIN(offset, size_ - start);
pvt_cstr_assert(other != NULL && (start < size_),
"Unsupported arguments passed for Constructor");
cstring str_ = intl_buf_grow(NULL, cnt_);
pvt_cstr_copy_(str_, other + start, cnt_);
return str_;
}
/*
* Functions to erase
* -------------------
*/
cstr_iterator
intl_erase_range_citer(cstring *str, cstr_const_iterator first,
cstr_const_iterator last)
{
cstring str_ = *str;
if((first.it < cstr_cbegin(str_).it) ||
(last.it > cstr_cend(str_).it) ||
(first.it > last.it)) {
return cstr_end(str_);
}
pvt_cstr_memmove((cstring)first.it, (cstring)last.it,
cstr_distance(last, cstr_end(str_)));
size_t size_ = cstr_size(str_) - cstr_distance(first, last);
str_[size_] = 0;
pvt_cstr_set_total_size_(str_, size_);
cstr_iterator res;
res.it = (cstring)last.it;
return res;
}
/*
* Functions to insert
* --------------------
*/
cstring
intl_insert_cnt_ch(cstring *str, const size_t index,
const size_t count, const char ch)
{
size_t old_size_ = cstr_size(*str);
pvt_cstr_assert(index <= old_size_, "index for insert is out of bounds");
if (count == 0) return *str;
size_t new_size_ = old_size_ + count;
cstr_reserve(str, new_size_);
cstring str_ = *str;
pvt_cstr_memmove(&str_[index + count], &str_[index], old_size_ - index + 1);
pvt_cstr_fill_(str_, index, count, ch);
pvt_cstr_set_total_size_(str_, new_size_);
return str_;
}
cstring
intl_insert_str_range(cstring *str, const size_t index,
const size_t count, const_cstring other)
{
size_t old_size_ = cstr_size(*str);
pvt_cstr_assert(index <= old_size_, "index for insert is out of bounds");
size_t cnt_ = CSTRMIN(count, pvt_cstr_strlen(other));
if (cnt_ == 0) return *str;
size_t new_size_ = old_size_ + cnt_;
cstr_reserve(str, new_size_);
cstring str_ = *str;
pvt_cstr_memmove(&str_[index + cnt_], &str_[index], old_size_ - index + 1);
pvt_cstr_memcpy(&str_[index], other, cnt_);
pvt_cstr_set_total_size_(str_, new_size_);
return str_;
}
cstr_iterator
intl_insert_iter_cnt_ch(cstring *str, cstr_iterator pos,
const size_t count, const char ch)
{
size_t offset_ = (size_t)(pos.it - (*str));
intl_insert_cnt_ch(str, offset_, count, ch);
cstr_iterator res;
res.it = (*str) + offset_;
return res;
}
/**
* Functions to replace
* ---------------------
*/
cstring
intl_replace_it_fill_ch(cstring *str, cstr_const_iterator first,
cstr_const_iterator last, const size_t count2,
const char ch)
{
cstring str_ = *str;
cstring first_ = (cstring)first.it,
last_ = (cstring)last.it;
size_t pos_ = (size_t)(first_ - str_);
pvt_cstr_assert((first_ >= cstr_cbegin(str_).it) &&
(last_ <= cstr_cend(str_).it) &&
(first_ <= last_),
"[cstr_replace] Iterators are out of range");
intl_erase_range_citer(str, first, last);
return intl_insert_cnt_ch(str, pos_, count2, ch);
}
cstring
intl_replace_it_str_off(cstring *str, cstr_const_iterator first,
cstr_const_iterator last, const_cstring other,
const size_t count2)
{
cstring str_ = *str;
cstring first_ = (cstring)first.it,
last_ = (cstring)last.it;
size_t count_ = CSTRMIN(count2, pvt_cstr_strlen(other)),
pos_ = (size_t)(first_ - str_);
pvt_cstr_assert((first_ >= cstr_cbegin(str_).it) &&
(last_ <= cstr_cend(str_).it) &&
(first_ <= last_),
"[cstr_replace] Iterators are out of range");
intl_erase_range_citer(str, first, last);
return intl_insert_str_range(str, pos_, count_, other);
}
/**
* Functions to compare
* ---------------------
*/
int
intl_compare_range_str_range(const_cstring str, const size_t pos1,
const size_t count1, const_cstring other,
const size_t pos2, const size_t count2)
{
size_t cnt_ = CSTRMIN(count1, count2);
return strncmp(str + pos1, other + pos2, cnt_);
}
/**
* Functions to find
* -----------------
*/
size_t
intl_find_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
if (pos >= cstr_size(str) || count == 0) return CSTR_NPOS;
if (!other) return CSTR_NPOS;
cstring substr_ = cstr_substr(other, 0, count);
const_cstring found_ = strstr(str + pos, substr_);
return found_ ? (size_t)(found_ - str) : CSTR_NPOS;
}
size_t
intl_find_ch_offset(const_cstring str, const char ch, const size_t pos)
{
size_t size_ = cstr_size(str);
if (pos >= size_) return CSTR_NPOS;
const_cstring found_ = strchr(str + pos, ch);
return found_ ? (size_t)(found_ - str) : CSTR_NPOS;
}
/**
* Functions to rfind
* ------------------
*/
size_t
intl_rfind_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
if (pos >= cstr_size(str) || count == 0) return CSTR_NPOS;
if (!other) return CSTR_NPOS;
cstring substr_ = cstr_substr(other, 0, count);
cstring found_ = NULL,
ptr_ = (cstring)str + pos;
while((ptr_ = strstr(ptr_, substr_)) != NULL){
found_ = ptr_;
++ptr_;
}
return found_ ? (size_t)(found_ - str) : CSTR_NPOS;
}
size_t
intl_rfind_ch_offset(const_cstring str, const char ch, const size_t pos)
{
size_t size_ = cstr_size(str);
if (pos >= size_) return CSTR_NPOS;
const_cstring found_ = strrchr(str + pos, ch);
return found_ ? (size_t)(found_ - str) : CSTR_NPOS;
}
/**
* HELPER FUNCTION FOR FIND OF RELATED FUNCTIONS
* ---------------------------------------------------------------------
*/
static size_t intl_find_of_impl(const_cstring str, const_cstring other,
const size_t pos, const size_t count, bool direction,
bool condition)
{
if (pos >= cstr_size(str) || count == 0) return CSTR_NPOS;
if (!other) return CSTR_NPOS;
bool set_[256] = {0};
for (size_t i = 0; i < count; ++i)
set_[(unsigned char)other[i]] = true;
if(direction){
for (size_t i = pos; i < cstr_size(str); ++i){
bool exists = set_[(unsigned char)str[i]];
if(condition ? exists : (!exists)) return i;
}
} else {
for (size_t i = pos; i > 0; --i){
bool exists = set_[(unsigned char)str[i]];
if(condition ? exists : (!exists)) return i;
}
}
return CSTR_NPOS;
}
/**
* Functions to find_first_of
* --------------------------
*/
size_t
intl_find_first_of_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
return intl_find_of_impl(str, other, pos, count, true, true);
}
size_t
intl_find_first_of_ch_offset(const_cstring str, const char ch, const size_t pos)
{
return intl_find_ch_offset(str, ch, pos);
}
/**
* Functions to find_first_not_of
* ------------------------------
*/
size_t
intl_find_first_not_of_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
return intl_find_of_impl(str, other, pos, count, true, false);
}
size_t
intl_find_first_not_of_ch_offset(const_cstring str,
const char ch, const size_t pos)
{
size_t size_ = cstr_size(str);
if (pos >= size_) return CSTR_NPOS;
for(size_t i = pos; i < size_; ++i)
if(str[i] != ch)
return i;
return CSTR_NPOS;
}
/**
* Functions to find_last_of
* --------------------------
*/
size_t
intl_find_last_of_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
return intl_find_of_impl(str, other, pos, count, false, true);
}
size_t
intl_find_last_of_ch_offset(const_cstring str, const char ch, const size_t pos)
{
size_t size_ = cstr_size(str);
if (pos >= size_) return CSTR_NPOS;
for(size_t i = pos; i > 0; --i)
if(str[i] == ch)
return i;
return CSTR_NPOS;
}
/**
* Functions to find_last_not_of
* ------------------------------
*/
size_t
intl_find_last_not_of_str_range(const_cstring str, const_cstring other,
const size_t pos, const size_t count)
{
return intl_find_of_impl(str, other, pos, count, false, false);
}
size_t
intl_find_last_not_of_ch_offset(const_cstring str,
const char ch, const size_t pos)
{
size_t size_ = cstr_size(str);
if (pos >= size_) return CSTR_NPOS;
for(size_t i = pos; i > 0; --i)
if(str[i] != ch)
return i;
return CSTR_NPOS;
}