/
string.c
859 lines (692 loc) · 30 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
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/***********************************************************************************************************************************
String Handler
***********************************************************************************************************************************/
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/assert.h"
#include "common/debug.h"
#include "common/memContext.h"
#include "common/type/string.h"
/***********************************************************************************************************************************
Constant strings that are generally useful
***********************************************************************************************************************************/
STRING_EXTERN(CR_STR, "\r");
STRING_EXTERN(EMPTY_STR, "");
STRING_EXTERN(FSLASH_STR, "/");
STRING_EXTERN(LF_STR, "\n");
STRING_EXTERN(N_STR, "n");
STRING_EXTERN(NULL_STR, "null");
STRING_EXTERN(Y_STR, "y");
STRING_EXTERN(ZERO_STR, "0");
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
struct String
{
// This struct must always be declared first. These are the fields that are shared in common with constant strings.
struct StringCommon common;
MemContext *memContext; // Required for dynamically allocated strings
};
/***********************************************************************************************************************************
Create a new string from a zero-terminated string
***********************************************************************************************************************************/
String *
strNew(const char *string)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRINGZ, string);
FUNCTION_TEST_ASSERT(string != NULL);
FUNCTION_TEST_END();
// Create object
String *this = memNew(sizeof(String));
this->memContext = memContextCurrent();
this->common.size = strlen(string);
// Allocate and assign string
this->common.buffer = memNewRaw(this->common.size + 1);
strcpy(this->common.buffer, string);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Create a new string from a buffer
If the buffer has a NULL character this may not work as expected. All the data will be copied but only the data before the NULL
character will be used as a string.
***********************************************************************************************************************************/
String *
strNewBuf(const Buffer *buffer)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(BUFFER, buffer);
FUNCTION_TEST_ASSERT(buffer != NULL);
FUNCTION_TEST_END();
// Create object
String *this = memNew(sizeof(String));
this->memContext = memContextCurrent();
this->common.size = bufUsed(buffer);
// Allocate and assign string
this->common.buffer = memNewRaw(this->common.size + 1);
memcpy(this->common.buffer, (char *)bufPtr(buffer), this->common.size);
this->common.buffer[this->common.size] = 0;
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Create a new string from a format string with parameters (i.e. sprintf)
***********************************************************************************************************************************/
String *
strNewFmt(const char *format, ...)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRINGZ, format);
FUNCTION_TEST_ASSERT(format != NULL);
FUNCTION_TEST_END();
// Create object
String *this = memNew(sizeof(String));
this->memContext = memContextCurrent();
// Determine how long the allocated string needs to be
va_list argumentList;
va_start(argumentList, format);
this->common.size = (size_t)vsnprintf(NULL, 0, format, argumentList);
va_end(argumentList);
// Allocate and assign string
this->common.buffer = memNewRaw(this->common.size + 1);
va_start(argumentList, format);
vsnprintf(this->common.buffer, this->common.size + 1, format, argumentList);
va_end(argumentList);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Create a new string from a string with a specific length
The string may or may not be zero-terminated but we'll use that nomeclature since we're not concerned about the end of the string.
***********************************************************************************************************************************/
String *
strNewN(const char *string, size_t size)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(CHARP, string);
FUNCTION_TEST_PARAM(SIZE, size);
FUNCTION_TEST_ASSERT(string != NULL);
FUNCTION_TEST_END();
// Create object
String *this = memNew(sizeof(String));
this->memContext = memContextCurrent();
this->common.size = size;
// Allocate and assign string
this->common.buffer = memNewRaw(this->common.size + 1);
strncpy(this->common.buffer, string, this->common.size);
this->common.buffer[this->common.size] = 0;
// Return buffer
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Return the file part of a string (i.e. everything after the last / or the entire string if there is no /)
***********************************************************************************************************************************/
String *
strBase(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
const char *end = this->common.buffer + this->common.size;
while (end > this->common.buffer && *(end - 1) != '/')
end--;
FUNCTION_TEST_RESULT(STRING, strNew(end));
}
/***********************************************************************************************************************************
Does the string begin with the specified string?
***********************************************************************************************************************************/
bool
strBeginsWith(const String *this, const String *beginsWith)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRING, beginsWith);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(beginsWith != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(BOOL, strBeginsWithZ(this, strPtr(beginsWith)));
}
bool
strBeginsWithZ(const String *this, const char *beginsWith)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, beginsWith);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(beginsWith != NULL);
FUNCTION_TEST_END();
bool result = false;
unsigned int beginsWithSize = (unsigned int)strlen(beginsWith);
if (this->common.size >= beginsWithSize)
result = strncmp(strPtr(this), beginsWith, beginsWithSize) == 0;
FUNCTION_TEST_RESULT(BOOL, result);
}
/***********************************************************************************************************************************
Append a string
***********************************************************************************************************************************/
String *
strCat(String *this, const char *cat)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, cat);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(cat != NULL);
FUNCTION_TEST_END();
// Determine length of string to append
size_t sizeGrow = strlen(cat);
// Allocate and append string
MEM_CONTEXT_BEGIN(this->memContext)
{
this->common.buffer = memGrowRaw(this->common.buffer, this->common.size + sizeGrow + 1);
}
MEM_CONTEXT_END();
strcpy(this->common.buffer + this->common.size, cat);
this->common.size += sizeGrow;
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Append a character
***********************************************************************************************************************************/
String *
strCatChr(String *this, char cat)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(CHAR, cat);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(cat != 0);
FUNCTION_TEST_END();
// Allocate and append character
MEM_CONTEXT_BEGIN(this->memContext)
{
this->common.buffer = memGrowRaw(this->common.buffer, this->common.size + 2);
}
MEM_CONTEXT_END();
this->common.buffer[this->common.size++] = cat;
this->common.buffer[this->common.size] = 0;
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Append a formatted string
***********************************************************************************************************************************/
String *
strCatFmt(String *this, const char *format, ...)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, format);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(format != NULL);
FUNCTION_TEST_END();
// Determine how long the allocated string needs to be
va_list argumentList;
va_start(argumentList, format);
size_t sizeGrow = (size_t)vsnprintf(NULL, 0, format, argumentList);
va_end(argumentList);
// Allocate and append string
MEM_CONTEXT_BEGIN(this->memContext)
{
this->common.buffer = memGrowRaw(this->common.buffer, this->common.size + sizeGrow + 1);
}
MEM_CONTEXT_END();
va_start(argumentList, format);
vsnprintf(this->common.buffer + this->common.size, sizeGrow + 1, format, argumentList);
va_end(argumentList);
this->common.size += sizeGrow;
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
C-style string compare
***********************************************************************************************************************************/
int
strCmp(const String *this, const String *compare)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRING, compare);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(compare != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(INT, strcmp(strPtr(this), strPtr(compare)));
}
int
strCmpZ(const String *this, const char *compare)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, compare);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(compare != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(INT, strcmp(strPtr(this), compare));
}
/***********************************************************************************************************************************
Duplicate a string from an existing string
***********************************************************************************************************************************/
String *
strDup(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_END();
String *result = NULL;
if (this != NULL)
result = strNew(strPtr(this));
FUNCTION_TEST_RESULT(STRING, result);
}
/***********************************************************************************************************************************
Is the string empty?
***********************************************************************************************************************************/
bool
strEmpty(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(BOOL, strSize(this) == 0);
}
/***********************************************************************************************************************************
Does the string end with the specified string?
***********************************************************************************************************************************/
bool
strEndsWith(const String *this, const String *endsWith)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRING, endsWith);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(endsWith != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(BOOL, strEndsWithZ(this, strPtr(endsWith)));
}
bool
strEndsWithZ(const String *this, const char *endsWith)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, endsWith);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(endsWith != NULL);
FUNCTION_TEST_END();
bool result = false;
unsigned int endsWithSize = (unsigned int)strlen(endsWith);
if (this->common.size >= endsWithSize)
result = strcmp(strPtr(this) + (this->common.size - endsWithSize), endsWith) == 0;
FUNCTION_TEST_RESULT(BOOL, result);
}
/***********************************************************************************************************************************
Are two strings equal?
There are two separate implementations because string objects can get the size very efficiently whereas the zero-terminated strings
would need a call to strlen().
***********************************************************************************************************************************/
bool
strEq(const String *this, const String *compare)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRING, compare);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(compare != NULL);
FUNCTION_TEST_END();
bool result = false;
if (this->common.size == compare->common.size)
result = strcmp(strPtr(this), strPtr(compare)) == 0;
FUNCTION_TEST_RESULT(BOOL, result);
}
bool
strEqZ(const String *this, const char *compare)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, compare);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(compare != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(BOOL, strcmp(strPtr(this), compare) == 0);
}
/***********************************************************************************************************************************
Upper-case the first letter
***********************************************************************************************************************************/
String *
strFirstUpper(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
if (this->common.size > 0)
this->common.buffer[0] = (char)toupper(this->common.buffer[0]);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Lower-case the first letter
***********************************************************************************************************************************/
String *
strFirstLower(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
if (this->common.size > 0)
this->common.buffer[0] = (char)tolower(this->common.buffer[0]);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Upper-case entire string
***********************************************************************************************************************************/
String *
strUpper(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
if (this->common.size > 0)
for (unsigned int idx = 0; idx <= this->common.size; idx++)
this->common.buffer[idx] = (char)toupper(this->common.buffer[idx]);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Upper-case entire string
***********************************************************************************************************************************/
String *
strLower(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
if (this->common.size > 0)
for (unsigned int idx = 0; idx <= this->common.size; idx++)
this->common.buffer[idx] = (char)tolower(this->common.buffer[idx]);
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Return the path part of a string (i.e. everything before the last / or "" if there is no /)
***********************************************************************************************************************************/
String *
strPath(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
const char *end = this->common.buffer + this->common.size;
while (end > this->common.buffer && *(end - 1) != '/')
end--;
FUNCTION_TEST_RESULT(
STRING,
strNewN(
this->common.buffer,
end - this->common.buffer <= 1 ? (size_t)(end - this->common.buffer) : (size_t)(end - this->common.buffer - 1)));
}
/***********************************************************************************************************************************
Return string ptr
***********************************************************************************************************************************/
const char *
strPtr(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_END();
char *result = NULL;
if (this != NULL)
result = this->common.buffer;
FUNCTION_TEST_RESULT(CHARP, result);
}
/***********************************************************************************************************************************
Quote a string
***********************************************************************************************************************************/
String *
strQuote(const String *this, const String *quote)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRING, quote);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(quote != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, strQuoteZ(this, strPtr(quote)));
}
String *
strQuoteZ(const String *this, const char *quote)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(STRINGZ, quote);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(quote != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, strNewFmt("%s%s%s", quote, strPtr(this), quote));
}
/***********************************************************************************************************************************
Replace a character with another character
***********************************************************************************************************************************/
String *
strReplaceChr(String *this, char find, char replace)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(CHAR, find);
FUNCTION_TEST_PARAM(CHAR, replace);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
for (unsigned int stringIdx = 0; stringIdx < this->common.size; stringIdx++)
{
if (this->common.buffer[stringIdx] == find)
this->common.buffer[stringIdx] = replace;
}
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Return a substring given only the start position
***********************************************************************************************************************************/
String *
strSub(const String *this, size_t start)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(SIZE, start);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(start < this->common.size);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, strSubN(this, start, this->common.size - start));
}
/***********************************************************************************************************************************
Return a substring given the start position and size
***********************************************************************************************************************************/
String *
strSubN(const String *this, size_t start, size_t size)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(SIZE, start);
FUNCTION_TEST_PARAM(SIZE, size);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(start < this->common.size);
FUNCTION_TEST_ASSERT(start + size <= this->common.size);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(STRING, strNewN(this->common.buffer + start, size));
}
/***********************************************************************************************************************************
Return string size
***********************************************************************************************************************************/
size_t
strSize(const String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
FUNCTION_TEST_RESULT(SIZE, this->common.size);
}
/***********************************************************************************************************************************
Trim whitespace from the beginnning and end of a string
***********************************************************************************************************************************/
String *
strTrim(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
// Nothing to trim if size is zero
if (this->common.size > 0)
{
// Find the beginning of the string skipping all whitespace
char *begin = this->common.buffer;
while (*begin != 0 && (*begin == ' ' || *begin == '\t' || *begin == '\r' || *begin == '\n'))
begin++;
// Find the end of the string skipping all whitespace
char *end = this->common.buffer + (this->common.size - 1);
while (end > begin && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n'))
end--;
// Is there anything to trim?
size_t newSize = (size_t)(end - begin + 1);
if (begin != this->common.buffer || newSize < strSize(this))
{
// Calculate new size
this->common.size = newSize;
// Move the substr to the beginning of the buffer
memmove(this->common.buffer, begin, this->common.size);
this->common.buffer[this->common.size] = 0;
MEM_CONTEXT_BEGIN(this->memContext)
{
// Resize the buffer
this->common.buffer = memGrowRaw(this->common.buffer, this->common.size + 1);
}
MEM_CONTEXT_END();
}
}
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Return the index to the location of the the first occurrence of a character within a string, else -1
***********************************************************************************************************************************/
int
strChr(const String *this, char chr)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_PARAM(CHAR, chr);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_END();
int result = -1;
if (this->common.size > 0)
{
const char *ptr = strchr(this->common.buffer, chr);
if (ptr != NULL)
result = (int)(ptr - this->common.buffer);
}
FUNCTION_TEST_RESULT(INT, result);
}
/***********************************************************************************************************************************
Truncate the end of a string from the index provided to the current end (e.g. 123KB pass index of K returns 123)
***********************************************************************************************************************************/
String *
strTrunc(String *this, int idx)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_ASSERT(this != NULL);
FUNCTION_TEST_ASSERT(idx >= 0 && (size_t)idx <= this->common.size);
FUNCTION_TEST_END();
if (this->common.size > 0)
{
// Reset the size to end at the index
this->common.size = (size_t)(idx);
this->common.buffer[this->common.size] = 0;
MEM_CONTEXT_BEGIN(this->memContext)
{
// Resize the buffer
this->common.buffer = memGrowRaw(this->common.buffer, this->common.size + 1);
}
MEM_CONTEXT_END();
}
FUNCTION_TEST_RESULT(STRING, this);
}
/***********************************************************************************************************************************
Convert an object to a zero-terminated string for logging
***********************************************************************************************************************************/
size_t strObjToLog(const void *object, StrObjToLogFormat formatFunc, char *buffer, size_t bufferSize)
{
size_t result = 0;
MEM_CONTEXT_TEMP_BEGIN()
{
String *string = NULL;
if (object == NULL)
string = strNew("null");
else
string = formatFunc(object);
result = (size_t)snprintf(buffer, bufferSize, "%s", strPtr(string));
}
MEM_CONTEXT_TEMP_END();
return result;
}
/***********************************************************************************************************************************
Render as string for logging
***********************************************************************************************************************************/
String *
strToLog(const String *this)
{
return strNewFmt("{\"%s\"}", strPtr(this));
}
/***********************************************************************************************************************************
Format sizes (file, buffer, etc.) in human-readable form
***********************************************************************************************************************************/
String *
strSizeFormat(const uint64_t size)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(UINT64, size);
FUNCTION_TEST_END();
String *result = NULL;
if (size < 1024)
result = strNewFmt("%" PRIu64 "B", size);
else if (size < (1024 * 1024))
{
if ((int)((double)size / 102.4) % 10 != 0)
result = strNewFmt("%.1fKB", ((double)size / 102.4) / 10);
else
result = strNewFmt("%dKB", (int)((double)size / 102.4) / 10);
}
else if (size < (1024 * 1024 * 1024))
{
if ((int)((double)size / 1024 / 102.4) % 10 != 0)
result = strNewFmt("%.1fMB", ((double)size / 1024 / 102.4) / 10);
else
result = strNewFmt("%dMB", (int)((double)size / 1024 / 102.4) / 10);
}
else
{
if ((int)((double)size / 1024 / 1024 / 102.4) % 10 != 0)
result = strNewFmt("%.1fGB", ((double)size / 1024 / 1024 / 102.4) / 10);
else
result = strNewFmt("%dGB", (int)((double)size / 1024 / 1024 / 102.4) / 10);
}
FUNCTION_TEST_RESULT(STRING, result);
}
/***********************************************************************************************************************************
Free the string
***********************************************************************************************************************************/
void
strFree(String *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STRING, this);
FUNCTION_TEST_END();
if (this != NULL)
{
MEM_CONTEXT_BEGIN(this->memContext)
{
memFree(this->common.buffer);
memFree(this);
}
MEM_CONTEXT_END();
}
FUNCTION_TEST_RESULT_VOID();
}