-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddcString.h
739 lines (647 loc) · 15.6 KB
/
ddcString.h
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#ifndef __ddcString__
#define __ddcString__
#include <malloc.h>
#include <stdarg.h>
#ifndef __ddcLib_DStatus__
#define __ddcLib_DStatus__
#ifndef __BYINC
#define __BYINC 10
#endif
typedef char bool;
typedef enum DOStatus DOStatus;
typedef enum DODelete DODelete;
enum DOStatus { DOS_INACTIVE=0, DOS_ACTIVE, DOS_DELETED };
enum DODelete { DOD_MANUAL=0, DOD_AUTO };
#endif
typedef struct ddString ddString;
typedef enum ddStringTypes ddStringTypes;
enum ddStringTypes { DDSTRING_DYNAMIC=0, DDSTRING_CONSTANT, DDSTRING_STATIC };
ddString make_ddString(const char* _c);
ddString make_format_ddString_length(const char* _c, ...);
ddString make_ddString_length(const char* _c, unsigned long _l);
ddString make_ddString_capacity(const char* _c, unsigned long _cap);
ddString make_auto_ddString(const char* _c);
ddString make_constant_ddString(const char* _c);
ddString make_constant_ddString_length(const char* _c, unsigned long _l);
const ddString make_full_constant_ddString(const char* _c);
ddString make_empty_ddString();
ddString make_multi_ddString(const ddString _c, unsigned long _n);
ddString make_multi_ddString_cstring(const char* _c, unsigned long _n);
ddString make_ddString_from_int(int _v);
ddString make_ddString_buf_from_int(char* _c, unsigned long _l, int _v);
ddString make_ddString_from_float(float _f);
void remake_ddString(ddString* _ds, const char* _c);
void raze_ddString(ddString* _d);
void raze_auto_ddString(ddString* _d);
void raze_constant_ddString(ddString* _d);
ddString int_to_ddString(int _v, ddString _ds);
ddString float_to_ddString(float _f, ddString _ds);
void ddString_format(ddString* _ds, const char* _fmt, ...);
void ddString_empty(ddString* _ds);
void ddString_make_constant(ddString* _ds);
void ddString_resize(ddString* _d, unsigned long _nl);
void ddString_copy(ddString* _d, const ddString _s);
void ddString_copy_cstring(ddString* _d, const char* _s);
void ddString_add(ddString* _d, const ddString _s);
void ddString_add_cstring(ddString* _d, const char* _s);
bool ddString_compare(const ddString _d, const ddString _s);
bool ddString_compare_cstring(const ddString _d, const char* _s);
void ddString_push_back(ddString* _d, const ddString);
void ddString_push_front(ddString* _d, const ddString);
void ddString_push_cstring_back(ddString* _d, const char* _ch);
void ddString_push_cstring_front(ddString* _d, const char* _ch);
void ddString_push_char_back(ddString* _d, const char _c);
void ddString_push_char_front(ddString* _d, const char _c);
void ddString_insert_char_at(ddString* _d, char _c, unsigned long _i);
void ddString_delete_at(ddString* _d, unsigned long i);
void ddString_pop_back(ddString* _d, unsigned long _n);
int ddString_to_int(const ddString _ds);
void ddString_close(ddString _d);
static int __floatTdsCount(int number, int count);
void cstring_get_length(const char* _c, unsigned long* _l);
unsigned long cstring_length(const char* _c);
void cstring_copy(char* _d, const char* _s, unsigned long _len);
void cstring_copy_offset(char* _d, const char* _s, unsigned long _do, unsigned long _so, unsigned long _l);
bool cstring_compare(const char* _d, const char* _s);
bool cstring_compare_length(const char* _d, const char* _s, unsigned long _l);
void __cstring_copy(char* _d, const char* _s, unsigned long _len);
void __cstring_copy_offset(char* _d, const char* _s, unsigned long _do, unsigned long _so, unsigned long _l);
bool __cstring_compare(const char* _d, const char* _s);
struct ddString
{
char* cstr;
unsigned long length;
unsigned long capacity;
DOStatus status;
DODelete aDelete;
ddStringTypes type;
};
unsigned long cstring_length(const char* _c)
{
unsigned long _o = 0;
for (; _c[_o] != '\0'; _o++);
return _o;
}
void cstring_get_length(const char* _c, unsigned long* _l)
{
for (*_l = 0; _c[*_l] != '\0'; (*_l)++);
}
void cstring_copy(char* _d, const char* _s, unsigned long _len)
{
for (unsigned long i = 0; i < _len; i++)
{
*_d = *_s;
_d++;
_s++;
}
}
void cstring_copy_offset(char* _d, const char* _s, unsigned long _do, unsigned long _so, unsigned long _l)
{
_d += _do;
_s += _so;
for (unsigned long i = 0; i < _l; i++)
{
*_d = *_s;
_d++;
_s++;
}
}
bool cstring_compare_length(const char* _d, const char* _s, unsigned long _l)
{
for (unsigned long i = 0; i < _l && _d[i] && _s[i]; i++)
{
if (_d[i] != _s[i]) return 0;
}
return 1;
}
bool cstring_compare(const char* _d, const char* _s)
{
unsigned long _dl;
cstring_get_length(_d, &_dl);
unsigned long _sl;
cstring_get_length(_s, &_sl);
if (_sl != _dl) return 0;
for (unsigned long i = 0; i < _dl; i++)
{
if (_d[i] != _s[i]) return 0;
}
return 1;
}
ddString make_format_ddString(const char* _fmt, ...)
{
ddString _ds = make_ddString("");
_ds.length = 0;
va_list ap;
va_start(ap, _fmt);
unsigned long _len;
cstring_get_length(_fmt, &_len);
for (int i = 0; i < _len; i++)
{
switch (_fmt[i])
{
case '%':
{
switch (_fmt[i+1])
{
case 'f':
{
ddString vfloat = make_ddString_from_float(va_arg(ap, double));
ddString_push_back(&_ds, vfloat);
raze_ddString(&vfloat);
i++;
break;
}
case 'd':
{
ddString vint = make_ddString_from_int(va_arg(ap, int));
ddString_push_back(&_ds, vint);
raze_ddString(&vint);
i++;
break;
}
case 'c':
ddString_push_char_back(&_ds, va_arg(ap, int));
i++;
break;
case 's':
ddString_push_cstring_back(&_ds, va_arg(ap, char*));
i++;
break;
}
break;
}
default: ddString_push_char_back(&_ds, _fmt[i]); break;
}
}
va_end(ap);
ddString_close(_ds);
return _ds;
}
void ddString_format(ddString* _ds, const char* _fmt, ...)
{
_ds->length = 0;
va_list ap;
va_start(ap, _fmt);
unsigned long _len;
cstring_get_length(_fmt, &_len);
for (int i = 0; i < _len; i++)
{
switch (_fmt[i])
{
case '%':
{
switch (_fmt[i+1])
{
case 'd':
{
ddString vint = make_ddString_from_int(va_arg(ap, int));
ddString_push_back(_ds, vint);
raze_ddString(&vint);
i++;
break;
}
case 'c':
ddString_push_char_back(_ds, va_arg(ap, int));
i++;
break;
case 's':
ddString_push_cstring_back(_ds, va_arg(ap, char*));
i++;
break;
}
break;
}
default: ddString_push_char_back(_ds, _fmt[i]); break;
}
}
va_end(ap);
ddString_close(*_ds);
}
void ddString_copy(ddString* _d, const ddString _s)
{
if (_d->capacity < _s.length)
ddString_resize(_d, _s.length + __BYINC);
cstring_copy(_d->cstr, _s.cstr, _s.length);
_d->status = DOS_ACTIVE;
}
void ddString_resize(ddString* _d, unsigned long _nl)
{
char* _t = malloc(_nl);
cstring_copy(_t, _d->cstr, _d->length);
raze_ddString(_d);
_d->capacity = _nl;
_d->cstr = _t;
}
void ddString_add(ddString* _d, const ddString _s)
{
char* _n = malloc(_d->length + _s.length);
cstring_copy_offset(_n, _d->cstr, 0, 0, _d->length);
cstring_copy_offset(_n, _s.cstr, _d->length, 0, _s.length);
raze_ddString(_d);
_d->cstr = _n;
_d->length += _s.length;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
bool ddString_compare(const ddString _d, const ddString _s)
{
if (_d.length != _s.length)
return 0;
for (unsigned long i = 0; i < _d.length; i++)
if (_d.cstr[i] != _s.cstr[i])
return 0;
return 1;
}
bool ddString_compare_cstring(const ddString _d, const char* _s)
{
unsigned long _len;
cstring_get_length(_s, &_len);
if (_d.length != _len)
return 0;
for (unsigned long i = 0; i < _d.length; i++)
if (_d.cstr[i] != _s[i])
return 0;
return 1;
}
void ddString_push_cstring_back(ddString* _d, const char* _ch)
{
unsigned long _clen;
cstring_get_length(_ch, &_clen);
if (_d->capacity < _d->length + _clen)
ddString_resize(_d, _d->length + _clen + __BYINC);
cstring_copy_offset(_d->cstr, _ch, _d->length, 0, _clen);
_d->length += _clen;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
void ddString_push_back(ddString* _d, const ddString _s)
{
if (_d->capacity < _d->length + _s.length)
ddString_resize(_d, _d->length + _s.length + __BYINC);
cstring_copy_offset(_d->cstr, _s.cstr, _d->length, 0, _s.length);
_d->length += _s.length;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
void ddString_push_char_back(ddString* _d, const char _c)
{
if (_d->capacity < _d->length + 1)
ddString_resize(_d, _d->length + 1 + __BYINC);
_d->cstr[_d->length] = _c;
_d->length += 1;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
void ddString_push_cstring_front(ddString* _d, const char* _ch)
{
unsigned long _clen;
cstring_get_length(_ch, &_clen);
if (_d->capacity < _d->length + _clen)
ddString_resize(_d, _d->length + _clen + __BYINC);
char* _t = malloc(_d->length + _clen);
cstring_copy(_t, _ch, _clen);
cstring_copy_offset(_t, _d->cstr, _clen, 0, _d->length);
raze_ddString(_d);
_d->length += _clen;
_d->cstr = _t;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
void ddString_push_front(ddString* _d, const ddString _s)
{
if (_d->capacity < _d->length + _s.length)
ddString_resize(_d, _d->length + _s.length + __BYINC);
char* _t = malloc(_d->length + _s.length);
cstring_copy(_t, _s.cstr, _s.length);
cstring_copy_offset(_t, _d->cstr, _s.length, 0, _d->length);
raze_ddString(_d);
_d->length += _s.length;
_d->cstr = _t;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
void ddString_push_char_front(ddString* _d, const char _c)
{
if (_d->capacity < _d->length + 1)
ddString_resize(_d, _d->length + 1 + __BYINC);
for (unsigned long i = _d->length; i >= 1; i--)
{
_d->cstr[i] = _d->cstr[i-1];
}
_d->cstr[0] = _c;
_d->length += 1;
_d->status = DOS_ACTIVE;
ddString_close(*_d);
}
ddString make_ddString_buf_from_int(char* _c, unsigned long _l, int _v)
{
ddString _o;
_o.cstr = _c;
_o.length = 0;
_o.capacity = _l+10;
if (_v == 0)
{
_c[0] = '0';
_c[1] = 0;
return _o;
}
char sign;
if (_v < 0)
{
sign = '-';
_v *= -1;
}
while (_v)
{
unsigned long x = _v % 10;
_v /= 10;
ddString_push_char_front(&_o, (char)('0' + x));
}
if (_o.length == 0)
{
_c[0] = '0';
_c[1] = 0;
return _o;
}
if (sign == '-') ddString_push_char_front(&_o, sign);
return _o;
}
ddString make_ddString_from_int(int _v)
{
if (_v == 0) return make_ddString("0");
char sign;
if (_v < 0)
{
sign = '-';
_v *= -1;
}
ddString _o = make_empty_ddString();
while (_v)
{
unsigned long x = _v % 10;
_v /= 10;
ddString_push_char_front(&_o, (char)('0' + x));
}
if (_o.length == 0) remake_ddString(&_o, "0");
if (sign == '-') ddString_push_char_front(&_o, sign);
return _o;
}
static int __floatTdsCount(int n, int c)
{
int _o = 1;
while(c-- > 0)
_o *= n;
return _o;
}
ddString make_ddString_from_float(float _f)
{
long long int l1;
long long int l2;
long long int i;
long long int n1;
long long int pos;
long long int sign;
float n2;
sign = -1;
if (_f < 0)
{
sign = '-';
_f *= -1;
}
n2 = _f;
n1 = _f;
l1 = 0;
l2 = 0;
while( (n2 - (float)n1) != 0.0 && !((n2 - (float)n1) < 0.0) )
{
n2 = _f * (__floatTdsCount(10.0, l2 + 1));
n1 = n2;
l2++;
}
for (l1 = (_f > 1) ? 0 : 1; _f > 1; l1++)
_f /= 10;
pos = l1;
l1 = l1 + 1 + l2;
n1 = n2;
if (sign == '-')
{
l1++;
pos++;
}
ddString _o = make_multi_ddString_cstring("0", l1);
for (i = l1; i >= 0 ; i--)
{
if (i == (l1))
_o.cstr[i] = '\0';
else if(i == (pos))
_o.cstr[i] = '.';
else if(sign == '-' && i == 0)
_o.cstr[i] = '-';
else
{
_o.cstr[i] = (n1 % 10) + '0';
n1 /= 10;
}
}
if (_o.cstr[0] == '0' && _o.cstr[_o.length-1] == '.')
{
ddString_push_char_front(&_o, '1');
}
return _o;
}
void ddString_make_constant(ddString* _ds)
{
const char* _t = _ds->cstr;
_ds->cstr = malloc(_ds->capacity);
cstring_copy(_ds->cstr, _t, _ds->length);
_ds->type = DDSTRING_DYNAMIC;
}
ddString make_multi_ddString_cstring(const char* _c, unsigned long _n)
{
unsigned long _len;
cstring_get_length(_c, &_len);
char* _t = malloc((_len*_n) + __BYINC);
for (unsigned long i = 0; i < _n*_len; i += _len)
for (unsigned long j = 0; j < _len; j++)
_t[i+j] = _c[j];
ddString _o;
_o.capacity = (_len*_n) + __BYINC;
_o.length = _len*_n;
_o.cstr = _t;
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_DYNAMIC;
return _o;
}
void ddString_empty(ddString* _ds)
{
raze_ddString(_ds);
*(_ds) = make_ddString_length("", 0);
}
void remake_ddString(ddString* _ds, const char* _c)
{
raze_ddString(_ds);
*_ds = make_ddString(_c);
}
ddString make_constant_ddString_length(const char* _c, unsigned long _l)
{
ddString _o;
_o.length = _l;
_o.capacity = _l + __BYINC;
_o.cstr = (char*)_c;
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_CONSTANT;
return _o;
}
ddString make_constant_ddString(const char* _c)
{
ddString _o;
unsigned long _len = 0;
cstring_get_length(_c, &_len);
_o.length = _len;
_o.capacity = _len + __BYINC;
_o.cstr = (char*)_c;
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_CONSTANT;
return _o;
}
const ddString make_full_constant_ddString(const char* _c)
{
ddString _o;
unsigned long _len = 0;
cstring_get_length(_c, &_len);
_o.length = _len;
_o.capacity = _len + __BYINC;
_o.cstr = (char*)_c;
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_CONSTANT;
return _o;
}
ddString make_ddString_capacity(const char* _c, unsigned long _cap)
{
ddString _o;
unsigned long _len = 0;
cstring_get_length(_c, &_len);
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_DYNAMIC;
_o.length = _len;
_o.capacity = _cap;
_o.cstr = malloc(_o.capacity);
cstring_copy(_o.cstr, _c, _o.length);
return _o;
}
ddString make_ddString_length(const char* _c, unsigned long _l)
{
ddString _o;
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_DYNAMIC;
_o.length = _l;
_o.capacity = _l + __BYINC;
_o.cstr = malloc(_o.capacity);
cstring_copy(_o.cstr, _c, _o.length);
ddString_close(_o);
return _o;
}
ddString make_ddString(const char* _c)
{
ddString _o;
unsigned long _len = 0;
cstring_get_length(_c, &_len);
_o.status = DOS_ACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_DYNAMIC;
_o.length = _len;
_o.capacity = _len + __BYINC;
_o.cstr = malloc(_o.capacity);
cstring_copy(_o.cstr, _c, _o.length);
ddString_close(_o);
return _o;
}
ddString make_empty_ddString()
{
ddString _o;
_o.length = 0;
_o.capacity = __BYINC;
_o.status = DOS_INACTIVE;
_o.aDelete = DOD_MANUAL;
_o.type = DDSTRING_DYNAMIC;
_o.cstr = malloc(_o.capacity);
return _o;
}
ddString make_auto_ddString(const char* _c)
{
ddString _o = make_ddString(_c);
_o.aDelete = DOD_AUTO;
return _o;
}
void raze_ddString(ddString* _d)
{
if (_d->status != DOS_DELETED && _d->type != DDSTRING_CONSTANT)
{
free(_d->cstr);
}
_d->status = DOS_DELETED;
}
void raze_constant_ddString(ddString* ds)
{
ds->status = DOS_DELETED;
}
void raze_auto_ddString(ddString* _d)
{
if (_d->aDelete == DOD_AUTO && _d->type != DDSTRING_CONSTANT)
raze_ddString(_d);
}
int ddString_to_int(const ddString _ds)
{
char* st = _ds.cstr;
int out = 0;
int sign = 1 - ((*st == 45)*2);
while(*st)
{
if (!(*st >= 48 && *st <=57))
{
st++;
continue;
}
out = (out*10) + (*st++ - 48);
}
return out * sign;
}
void ddString_delete_at(ddString* _d, unsigned long i)
{
for (; i < _d->length; i++)
{
_d->cstr[i] = _d->cstr[i+1];
}
_d->length--;
ddString_close(*_d);
}
void ddString_insert_char_at(ddString* _d, char _c, unsigned long _i)
{
for (unsigned long i = _d->length; i > _i; i--)
{
_d->cstr[i] = _d->cstr[i-1];
}
_d->cstr[_i] = _c;
_d->length++;
ddString_close(*_d);
}
void ddString_pop_back(ddString* _d, unsigned long _n)
{
for (unsigned long i = 1; i < _n+1; i++)
{
_d->cstr[_d->length-i] = '\0';
}
_d->length -= _n;
ddString_close(*_d);
}
void ddString_close(ddString _d)
{
_d.cstr[_d.length] = 0;
}
#endif