-
Notifications
You must be signed in to change notification settings - Fork 1
/
substrings.c
650 lines (495 loc) · 15.4 KB
/
substrings.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
/*SubStrings C-String manipulation library. This software is public domain.
* Please read the file UNLICENSE.TXT for more information.*/
#define NULL ((void*)0)
#include "substrings.h"
enum { false, true };
/*Function prototypes.*/
const struct _SubStrings SubStrings =
{ /*Add all functions to a proper place in this struct, and in substrings.h's definition of its type.*/
SubStrings_Compare, SubStrings_NCompare, SubStrings_CaseCompare, SubStrings_CaseNCompare,
SubStrings_StartsWith, SubStrings_EndsWith,
SubStrings_Length, SubStrings_Copy, SubStrings_Cat,
SubStrings_Find, SubStrings_CFind, SubStrings_Replace,
SubStrings_Split, SubStrings_Between, SubStrings_Extract, SubStrings_Reverse,
SubStrings_CopyUntil, SubStrings_CopyUntilC, SubStrings_FindAnyOf,
SubStrings_Strip, SubStrings_StripC, SubStrings_StripTrailingChars,
SubStrings_StripLeadingChars,
{ SubStrings_LP_NextLine, SubStrings_LP_WhitespaceJump, SubStrings_LP_GetLine },
{ SubStrings_ASCII_UpperC, SubStrings_ASCII_LowerC,
SubStrings_ASCII_UpperS, SubStrings_ASCII_LowerS,
SubStrings_ASCII_IsUpperC, SubStrings_ASCII_IsLowerC, SubStrings_ASCII_IsDigitC,
SubStrings_ASCII_IsUpperS, SubStrings_ASCII_IsLowerS, SubStrings_ASCII_IsDigitS}
};
/*Actual functions.*/
unsigned SubStrings_Length(const char *const String)
{
register const char *Worker = String;
while (*Worker) ++Worker;
return Worker - String;
}
SSBool SubStrings_Compare(register const char *Match, register const char *Source)
{
for (; *Match && *Source; ++Match, ++Source)
{
if (*Match != *Source) return false;
}
if (*Match != *Source) return false; /*If one string is shorter than the other*/
return true;
}
SSBool SubStrings_NCompare(register const char *Match, const unsigned Length, register const char *Source)
{
register unsigned Inc = 0;
for (; Inc < Length && *Match && *Source; ++Inc, ++Match, ++Source)
{
if (*Match != *Source) return false;
}
if (*Match != *Source) return false;
return true;
}
SSBool SubStrings_CaseCompare(register const char *Match, register const char *Source)
{
for (; *Match && *Source; ++Match, ++Source)
{
register char Cmp1 = *Match;
register char Cmp2 = *Source;
if (Cmp1 >= 'A' && Cmp1 <= 'Z') Cmp1 = Cmp1 + ('a' - 'A');
if (Cmp2 >= 'A' && Cmp2 <= 'Z') Cmp2 = Cmp2 + ('a' - 'A');
if (Cmp1 != Cmp2) return false;
}
if (*Match != *Source) return false;
return true;
}
SSBool SubStrings_CaseNCompare(register const char *Match, const unsigned Length, register const char *Source)
{
unsigned Inc = 0;
for (; Inc < Length && *Match && *Source; ++Inc, ++Match, ++Source)
{
register char Cmp1 = *Match;
register char Cmp2 = *Source;
if (Cmp1 >= 'A' && Cmp1 <= 'Z') Cmp1 = Cmp1 + ('a' - 'A');
if (Cmp2 >= 'A' && Cmp2 <= 'Z') Cmp2 = Cmp2 + ('a' - 'A');
if (Cmp1 != Cmp2) return false;
}
if (*Match != *Source) return false;
return true;
}
SSBool SubStrings_StartsWith(register const char *Match, register const char *Source)
{
const unsigned Len = SubStrings_Length(Match);
register unsigned Inc = 0;
if (!Len) return false;
for (; *Match == *Source && *Match && *Source; ++Inc, ++Match, ++Source);
if (Len == Inc) return true;
return false;
}
SSBool SubStrings_EndsWith(register const char *Match, register const char *Source)
{
const unsigned SourceLen = SubStrings_Length(Source);
const unsigned MatchLen = SubStrings_Length(Match);
register unsigned Inc = 0;
if (SourceLen < MatchLen || !MatchLen) return false;
Source += SourceLen;
Source -= MatchLen;
for (; *Source == *Match && *Source && *Match; ++Source, ++Match, ++Inc);
if (Inc == MatchLen) return true;
return false;
}
unsigned SubStrings_Strip(const char *const Match, char *const Source)
{ /*Removes all matching patterns from Source and returns the number of detections.*/
register char *Worker = Source;
const unsigned Len = SubStrings_Length(Source);
const unsigned MatchLen = SubStrings_Length(Match);
register unsigned Inc = 0;
for (; (Worker = SubStrings_Find(Match, 1, Worker)); ++Inc)
{
SubStrings_Copy(Worker, Worker + MatchLen, Len);
}
return Inc;
}
unsigned SubStrings_StripC(const char *const Match, char *const Source)
{
register char *Worker = Source;
const unsigned Len = SubStrings_Length(Source);
register unsigned Inc = 0;
for (; (Worker = SubStrings_FindAnyOf(Match, Worker)); ++Inc)
{
SubStrings_Copy(Worker, Worker + 1, Len);
}
return Inc;
}
char *SubStrings_LP_NextLine(const char *InStream)
{
register const char *Worker = InStream;
while (*Worker != '\r' && *Worker != '\n' && *Worker != '\0') ++Worker;
if (*Worker == '\0') return NULL;
while (*Worker == '\r' || *Worker == '\n') ++Worker;
if (*Worker == '\0') return NULL;
return (char*)Worker;
}
char *SubStrings_LP_WhitespaceJump(const char *InStream)
{
register const char *Worker = InStream;
while (*Worker != ' ' && *Worker != '\t' && *Worker != '\r' && *Worker != '\n' && *Worker != '\0') ++Worker;
if (*Worker == '\0' || *Worker == '\r' || *Worker == '\n') return NULL;
while (*Worker == ' ' || *Worker == '\t') ++Worker;
if (*Worker == '\0' || *Worker == '\r' || *Worker == '\n') return NULL;
return (char*)Worker;
}
char SubStrings_ASCII_LowerC(const char C)
{
if (C >= 'A' && C <= 'Z') return C + ('a' - 'A');
else return C;
}
char SubStrings_ASCII_UpperC(const char C)
{
if (C >= 'a' && C <= 'z') return C - ('a' - 'A');
else return C;
}
char *SubStrings_ASCII_UpperS(char *const S)
{
register char *Worker = S;
for (; *Worker != '\0'; ++Worker)
{
if (*Worker >= 'a' && *Worker <= 'z')
{
*Worker -= ('a' - 'A');
}
}
return S;
}
char *SubStrings_ASCII_LowerS(char *const S)
{
register char *Worker = S;
for (; *Worker != '\0'; ++Worker)
{
if (*Worker >= 'A' && *Worker <= 'Z')
{
*Worker += ('a' - 'A');
}
}
return S;
}
SSBool SubStrings_ASCII_IsUpperC(const char Char)
{
if (Char >= 'A' && Char <= 'Z') return true;
return false;
}
SSBool SubStrings_ASCII_IsLowerC(const char Char)
{
if (Char >= 'a' && Char <= 'z') return true;
return false;
}
SSBool SubStrings_ASCII_IsDigitC(const char Char)
{
if (Char >= '0' && Char <= '9') return true;
return false;
}
SSBool SubStrings_ASCII_IsUpperS(register const char *String)
{
if (!*String) return false;
for (; *String != '\0'; ++String)
{
if (*String < 'A' || *String > 'Z') return false;
}
return true;
}
SSBool SubStrings_ASCII_IsLowerS(register const char *String)
{
if (!*String) return false;
for (; *String != '\0'; ++String)
{
if (*String < 'a' || *String > 'z') return false;
}
return true;
}
SSBool SubStrings_ASCII_IsDigitS(register const char *String)
{
if (!*String) return false;
for (; *String != '\0'; ++String)
{
if (*String < '0' || *String > '9') return false;
}
return true;
}
char *SubStrings_Between(register char *Dest, const char *After, const char *Before, const char *InStream)
{ /*Pull something out of the middle of a string.*/
return SubStrings_Extract(Dest, SubStrings_Length(InStream) + 1, After, Before, InStream); /*Making an assumption for DestSize*/
}
char *SubStrings_Extract(register char *Dest, register unsigned DestSize, const char *After, const char *Before, const char *InStream)
{ /*Pull something out of the middle of a string.*/
const char *Begin;
register const char *Worker;
register const char *End;
const char *RetVal;
if ((After && !SubStrings_Find(After, 1, InStream)) || (Before && !SubStrings_Find(Before, 1, InStream)))
{ /*Check if we're being given bad things to search for.*/
return NULL;
}
Begin = After ? SubStrings_Find(After, 1, InStream) : InStream;
Worker = Begin + (After ? SubStrings_Length(After) : 0);
End = Before ? SubStrings_Find(Before, 1, Worker) : InStream + SubStrings_Length(InStream);
RetVal = Worker;
for (; DestSize - 1 > 0 && Worker != End; ++Worker, ++Dest, --DestSize)
{
*Dest = *Worker;
}
*Dest = '\0';
return (char*)RetVal;
}
unsigned SubStrings_Copy(register char *Dest, register const char *Source, unsigned Max)
{
register unsigned Inc = 0;
for (; *Source != '\0' && Inc < Max - 1; ++Inc) *Dest++ = *Source++;
*Dest = '\0';
return Inc;
}
SSBool SubStrings_CopyUntil(char *Dest, register unsigned DestTotalSize,
const char **Ptr, const char *const Trigger, const SSBool SkipPastAdjacentTriggers)
{ /*Copy Source to Dest until the string Until, copying a maximum of DestTotalSize - 1 characters.*/
register const char *Worker = NULL;
register const char *Stopper = NULL;
if (!*Ptr || **Ptr == '\0') return false;
Worker = *Ptr;
Stopper = SubStrings_Find(Trigger, 1, *Ptr);
for (; DestTotalSize - 1 > 0 && Worker != Stopper && *Worker != '\0'; ++Dest, ++Worker, --DestTotalSize)
{
*Dest = *Worker;
}
*Dest = '\0';
if (!Stopper) *Ptr = NULL;
else
{
const unsigned TLen = SubStrings_Length(Trigger);
*Ptr = Stopper + TLen;
if (SkipPastAdjacentTriggers)
{ /*So if we have wibblederpderpderpderpwibble, we get wibblewibble.*/
while (SubStrings_Compare(Trigger, *Ptr)) *Ptr += TLen;
}
}
return true;
}
char *SubStrings_FindAnyOf(const char *CharList, const char *Source)
{ /*Like strpbrk().*/
register const char *Worker = Source;
register const char *CL;
for (; *Worker != '\0'; ++Worker)
{
for (CL = CharList; *CL != '\0'; ++CL)
{
if (*Worker == *CL) return (char*)Worker;
}
}
return NULL;
}
SSBool SubStrings_CopyUntilC(register char *Dest, register unsigned DestTotalSize, const char **Ptr,
const char *Triggers, const SSBool SkipPastAdjacentTriggers)
{ /*Same as CopyUntil(), except it does strpbrk() style matching instead of strstr() style.*/
register const char *Worker = NULL;
register const char *Stopper = NULL;
if (!*Ptr || **Ptr == '\0') return false;
Worker = *Ptr;
Stopper = SubStrings_FindAnyOf(Triggers, *Ptr);
for (; DestTotalSize - 1 > 0 && Worker != Stopper && *Worker != '\0'; ++Dest, ++Worker, --DestTotalSize)
{
*Dest = *Worker;
}
*Dest = '\0';
if (!Stopper) *Ptr = NULL;
else
{
const char *CL;
CheckRestart:
for (CL = Triggers; *CL != '\0'; ++CL)
{
if (*Worker == *CL)
{
++Worker;
if (SkipPastAdjacentTriggers) goto CheckRestart;
else break;
}
}
*Ptr = Worker;
}
return true;
}
unsigned SubStrings_Cat(char *Dest, const char *Snip, const unsigned DestTotalSize)
{ /*Safe concatenation function.*/
register unsigned Inc = 0;
register unsigned DestTotalLength = SubStrings_Length(Dest);
if (!DestTotalSize || !*Snip) return 0;
Dest += DestTotalLength;
for (; *Snip != '\0' && DestTotalLength < DestTotalSize - 1; ++Inc, ++Snip, ++Dest, ++DestTotalLength)
{
*Dest = *Snip;
}
*Dest = '\0';
return Inc;
}
char *SubStrings_Find(const char *const Match, const int ResultNumber, const char *const InStream)
{ /*Like strstr(), but can get a certain result number.*/
register const char *Worker = InStream;
const unsigned MatchLen = SubStrings_Length(Match);
const unsigned SLen = SubStrings_Length(InStream);
register const char *SubWorker = Worker, *SubMatch = NULL;
register unsigned Inc = 0;
register int CountInc = 0;
if (MatchLen > SLen || !MatchLen || !SLen || !ResultNumber) return NULL;
for (; *Worker != '\0'; SubWorker = ++Worker)
{
for (Inc = 0, SubMatch = Match; (*SubWorker == *SubMatch) && (*SubWorker && *SubMatch);
++SubWorker, ++SubMatch, ++Inc);
if (Inc == MatchLen)
{
if (CountInc + 1 == ResultNumber) return (char*)Worker;
else ++CountInc;
}
}
return NULL;
}
char *SubStrings_CFind(const char Match, const int ResultNumber, const char *const InStream)
{ /*Like strchr(), but can get a certain result number.*/
register const char *Worker = InStream;
register int CountInc = 0;
for (; *Worker != '\0'; ++Worker)
{
if (*Worker == Match)
{
if (CountInc + 1 == ResultNumber) return (char*)Worker;
else ++CountInc;
}
}
return NULL;
}
unsigned SubStrings_Replace(register char *Stream, void *TempBuf, unsigned StreamTotalSize, const char *Match, const char *Replacement)
{ /*I decided to use some existing functions so we don't have to reinvent the wheel here.
The optimizer should do ok by inlining these.*/
char *HalfOne = TempBuf, *HalfTwo = (char*)TempBuf + StreamTotalSize;
register unsigned ReplaceCount = 0;
char *Last;
for (; SubStrings_Split(HalfOne, HalfTwo, Match, Stream, SPLIT_NOKEEP); ++ReplaceCount)
{
*Stream = '\0';
SubStrings_Cat(Stream, HalfOne, StreamTotalSize);
Last = Stream + SubStrings_Length(Stream);
SubStrings_Cat(Stream, Replacement, StreamTotalSize);
SubStrings_Cat(Stream, HalfTwo, StreamTotalSize);
Stream = Last + SubStrings_Length(Replacement);
}
return ReplaceCount;
}
SSBool SubStrings_Split(register char *HalfOneOut, register char *HalfTwoOut,
const char *const Match, const char *const InStream, int Mode)
{
register const char *Worker = InStream;
const char *const Delimiter = SubStrings_Find(Match, 1, InStream);
const char *StopH1 = NULL;
const char *StartH2 = NULL;
if (!Delimiter) return false;
switch (Mode)
{ /*What to do with the delimiter.*/
case SPLIT_NOKEEP:
StopH1 = Delimiter;
StartH2 = Delimiter + SubStrings_Length(Match);
break;
case SPLIT_HALFONE:
StartH2 = StopH1 = Delimiter + SubStrings_Length(Match);
break;
case SPLIT_HALFTWO:
StopH1 = StartH2 = Delimiter;
break;
default:
return false;
}
if (HalfOneOut)
{
for (; Worker != StopH1; ++HalfOneOut, ++Worker)
{
*HalfOneOut = *Worker;
}
*HalfOneOut = '\0';
}
if (HalfTwoOut)
{
for (Worker = StartH2; *Worker != '\0'; ++HalfTwoOut, ++Worker)
{
*HalfTwoOut = *Worker;
}
*HalfTwoOut = '\0';
}
return true;
}
char *SubStrings_Reverse(register char *OutStream, register const char *InStream)
{ /*Reverse a string of text*/
const char *End = NULL, *RetVal = OutStream;
const unsigned Len = SubStrings_Length(InStream);
InStream += Len - 1;
End = OutStream + Len;
for (; OutStream != End; --InStream, ++OutStream)
{
*OutStream = *InStream;
}
*OutStream = '\0';
return (char*)RetVal;
}
SSBool SubStrings_LP_GetLine(char *OutStream, const unsigned OutStreamTotalSize, const char **Ptr)
{ /*Takes the value of *Ptr, gets a line from it, and modifies *Ptr to point to the next line after, or returns false if bad.*/
register const char *Worker = *Ptr;
register unsigned Inc = 0;
/*No line.*/
if (*Worker == '\0') return false;
/*Do the line copy.*/
for (; Inc < OutStreamTotalSize - 1 && *Worker != '\r' && *Worker != '\n' && *Worker != '\0'; ++Worker, ++OutStream, ++Inc)
{
*OutStream = *Worker;
}
*OutStream = '\0';
if (*Worker == '\r' || *Worker == '\n') ++Worker;
if (Worker > *Ptr && Worker[-1] == '\r' && *Worker == '\n') ++Worker;
*Ptr = Worker;
return true;
}
unsigned SubStrings_StripTrailingChars(char *Stream, const char *Match)
{
register const char *M;
register unsigned StripCount = 0;
register int Inc = 0;
if (!*Stream) return 0;
while (Stream[Inc]) ++Inc;
for (--Inc; Inc >= 0; --Inc)
{
SSBool Found = false;
for (M = Match; *M; ++M)
{
if (Stream[Inc] == *M)
{
Stream[Inc] = '\0';
++StripCount;
Found = true;
break;
}
}
if (!Found) break;
}
return StripCount;
}
unsigned SubStrings_StripLeadingChars(register char *Stream, const char *Match)
{
register const char *M;
char *Orig = Stream, *Worker = Stream;
unsigned StripCount = 0;
for (; *Stream; ++Stream)
{
for (M = Match; *M; ++M)
{
if (*Stream == *M)
{
Worker = Stream + 1;
++StripCount;
break;
}
}
}
if (Stream == Orig) return 0;
SubStrings_Copy(Orig, Worker, SubStrings_Length(Worker) + 1);
return StripCount;
}