-
Notifications
You must be signed in to change notification settings - Fork 0
/
oofstr.cpp
2023 lines (1739 loc) · 46 KB
/
oofstr.cpp
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// COPYRIGHT 1994-1998 A.D. Software, All rights reserved
// string classes
#include "oofpch_c.h" // for precompilation of core files
#ifndef H_OOFSTR
#include "oofstr.h"
#endif
#ifndef _CTYPE_H
#include <ctype.h>
#endif
// Mac toolbox includes if not using precompiled header
#ifdef _Macintosh
#ifndef __RESOURCES__
#include <resources.h> // for ReleaseResource
#endif
#ifndef __TEXTUTILS__
#include <TextUtils.h> // for GetString
#endif
#endif
#ifdef OOF_MEM_DEBUG_LAST_INCLUDE
#include OOF_MEM_DEBUG_LAST_INCLUDE
#endif
// -------------------------------------------------------
// s t a t i c v a r s
// -------------------------------------------------------
char oofString::sEmptyChar = '\0';
// -------------------------------------------------------
// U T I L I T I E S
// -------------------------------------------------------
// OOF_stricmp - compare strings ignoring case of text
int
OOF_stricmp (const char* s1, const char* s2)
{
int i;
for (i=0;s1[i]&&s2[i]&&(toupper(s1[i])==toupper(s2[i]));++i)
;
return (toupper(s1[i])-toupper(s2[i]));
}
int
OOF_strnicmp (const char* s1, const char* s2, const unsigned int n)
{
unsigned int i;
for (i=0;s1[i]&&s2[i]&&(toupper(s1[i])==toupper(s2[i]))&&i<n;i++)
;
if(i==n) // We got past the number we were meant to compare
return 0;
else
return (toupper(s1[i])-toupper(s2[i]));
}
// -------------------------------------------------------
// O O F _ S t r i n g
// -------------------------------------------------------
oofString::oofString(const oofString& rhs)
{
mLen = rhs.mLen;
if (mLen) {
mBody = new char[mLen + 1]; // room for null
assert(mBody);
strcpy(mBody, rhs.mBody);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
else
mBody=0;
}
oofString::oofString(const oofString& str1, const char *chars, const oofString& str3)
{
unsigned int charLen = 0;
if (chars)
charLen = strlen(chars);
mLen = str1.mLen + charLen + str3.mLen;
if (mLen==0)
mBody = 0;
else {
mBody = new char[mLen + 1]; // room for null
assert(mBody);
if (str1.mBody)
strcpy(mBody, str1.mBody);
else
mBody[0]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
if (charLen)
strcat(mBody, chars);
if (str3.mBody)
strcat(mBody, str3.mBody);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
}
oofString::oofString(char inChar, int repeatCount)
{
mLen = repeatCount;
mBody = new char[repeatCount+1]; // room for null
assert(mBody);
for (int i=0; i<repeatCount; i++)
mBody[i]=inChar;
mBody[repeatCount]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
oofString::oofString(const oofString& str1, const oofString& str2)
{
mLen = str1.mLen + str2.mLen;
if (mLen==0)
mBody = 0;
else {
mBody = new char[mLen + 1]; // room for null
assert(mBody);
assert((str1.mBody==0) ? (str1.mLen==0) : 1);
if (str1.mBody) {
memcpy(mBody, str1.mBody, str1.mLen);
}
assert((str2.mBody==0) ? (str2.mLen==0) : 1);
if (str2.mBody) {
memcpy(mBody+str1.mLen, str2.mBody, str2.mLen);
}
mBody[mLen]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
}
oofString::oofString(char inChar1, const oofString& str1, char inChar2)
{
mLen = str1.mLen + 2;
mBody = new char[mLen + 1]; // room for null
assert(mBody);
mBody[0]=inChar1;
assert((str1.mBody==0) ? (str1.mLen==0) : 1);
if (mLen>2) {
memcpy(mBody+1, str1.mBody, str1.mLen);
}
mBody[mLen-1]=inChar2;
mBody[mLen]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
oofString::oofString(const oofString& str1, const char *chars)
{
unsigned int charLen = 0;
if (chars)
charLen = strlen(chars);
mLen = str1.mLen + charLen;
if (mLen==0)
mBody = 0;
else {
mBody = new char[mLen + 1]; // room for null
assert(mBody);
if (str1.mBody)
strcpy(mBody, str1.mBody);
else
mBody[0]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
if (charLen)
strcat(mBody, chars);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
}
oofString::oofString(const oofString& str1, const char ch, const char *chars)
{
const unsigned int len1 = str1.mLen;
unsigned int charLen = 0;
if (chars)
charLen = strlen(chars);
mLen = len1 + charLen + 1;
mBody = new char[mLen + 1]; // room for null
assert(mBody);
if (str1.mBody)
strcpy(mBody, str1.mBody);
else
mBody[len1]=ch;
mBody[len1+1]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
if (charLen)
strcat(mBody, chars);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
oofString::oofString(const char *chars) :
mBody(0),
mLen(0)
{
if (chars)
FinishConstruction(chars, strlen(chars));
}
oofString::oofString(const char *chars, unsigned long numChars) :
mBody(0)
{
FinishConstruction(chars, numChars);
}
oofString::oofString(char *chars, unsigned long numChars, bool adopt) :
mBody(0)
{
if (adopt) { // dunno why anyone would invoke this variant with false, but allow
mBody = chars;
mLen = numChars;
assert(strlen(chars)==numChars);
}
else
FinishConstruction(chars, numChars);
}
#ifndef OOF_NO_STDLIB
/**
ctor taking an output stream.
\note you will call this constructor even from an expression like
oofString jack(ostringstream() << flush << "first line\n" << "second\n");
because it is the type of the return value from the operator<< which is last
evaluated, NOT the type of temporary stream object you start with.
*/
oofString::oofString(ostream& os) :
mBody(0),
mLen(0)
{
#if OOF_USE_ANSI_STREAMS
ostringstream oss;
oss << os.rdbuf();
string s = oss.str(); // copy into ANSI string
FinishConstruction(s.c_str(), s.size());
#else
ostringstream oss;
#ifdef __MWERKS__
oss << *(os.rdbuf()); // old Metrowerks lacked an operator<<(streambuf*)
#else
oss << os.rdbuf();
#endif
oss << ends; // not null terminated until we add one
FinishConstruction(oss.str(), oss.pcount()-1); // pcount includes the null byte!
oss.rdbuf()->freeze(0); // leave buffer for stream to delete
#endif
}
#if OOF_USE_ANSI_STREAMS
oofString::oofString(stringstream& os) : // ostringstream is typedef'd as ostrstream if using old libs
mBody(0),
mLen(0)
{
string s = os.str(); // copy into ANSI string
FinishConstruction(s.c_str(), s.size());
}
#endif
#if OOF_HAS_ANSI_STRINGS
oofString::oofString(const string& s) :
mBody(0),
mLen(0)
{
FinishConstruction(s.c_str(), s.size());
}
#endif
#endif // OOF_NO_STDLIB
#if defined(_Macintosh)
oofString::oofString(const Str255 str) : // _Macintosh
mBody(0),
mLen(0)
{
FinishConstruction((const char*)&(str[1]), str[0]);
}
#endif
oofString::~oofString()
{
delete [] mBody;
}
void
oofString::FinishConstruction(const char *chars, unsigned long numChars)
{
if (chars) {
mLen = numChars;
if (numChars) {
char* newBody = new char[numChars + 1]; // room for null
assert(newBody);
memcpy(newBody, chars, numChars);
newBody[numChars] = '\0';
mBody = newBody;
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
else
mBody = 0;
} // if pointer to chars not null
else {
mBody = 0; // guarantee regardless of calling ctor
mLen = 0;
}
}
#if defined(_Macintosh)
void
oofString::getStr255(Str255 s) const // _Macintosh
{
s[0] = mLen;
if (mLen)
memcpy(s+1, mBody, mLen);
}
void
oofString::setStr255(const Str255 s)
{
const long charLen = s[0];
const char* strStart = (char*) s+1;
setChars(strStart, charLen);
}
#endif
// _Macintosh
#if OOF_HAS_ANSI_STRINGS
oofString&
oofString::operator=(const string& s)
{
setChars(s.c_str(), s.size());
return *this;
}
oofString::operator string() const
{
return string(mBody, mLen);
}
#endif
// OOF_HAS_ANSI_STRINGS
oofString&
oofString::operator=(const oofString& rhs)
{
if (*this==rhs)
return *this;
delete[] mBody;
mLen = rhs.mLen;
if (mLen==0)
mBody = 0;
else {
mBody = new char[mLen + 1]; // room for null
assert(mBody);
strcpy(mBody, rhs.mBody);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
return *this;
}
bool
oofString::operator==(const oofString& rhs) const
{
if (this==&rhs)
return true; // trivial - same object
if (mLen!=rhs.mLen)
return false; // different lengths
for (unsigned long i=0; i<mLen; i++) {
if (mBody[i]!=rhs.mBody[i])
return false;
}
return true;
}
#ifdef _Macintosh
bool
oofString::operator==(const Str255 s) const // _Macintosh
{
if (s==0 || s[0]==0)
return (mBody==0 || mLen==0);
const unsigned long rhsLen = s[0];
if (mLen!=rhsLen)
return false; // different lengths
for (unsigned long i=0; i<rhsLen; i++) {
if (mBody[i]!=s[i+1])
return false;
}
return true;
}
void
oofString::loadResource(unsigned long strID)
{
StringHandle inStr = ::GetString(strID);
if (inStr) {
::HLock((Handle)inStr);
setStr255(*((const Str255*)inStr)); // ugly cast for Carbon/CodeWarrior Pro6
::ReleaseResource((Handle)inStr);
}
else
setChars(0,0);
}
#elif defined _Windows
void
oofString::loadResource(unsigned long strID)
{
/*
NOT YET IMPLEMENTED
AD 990426 - don't want to use implementation just yet that has oofString requiring CStrings
as that introduces an MFC dependency we are avoiding.
CString s;
if (s.LoadString(strID)) {
const char* loadedString = s;
*this = loadedString;
}
else
setChars(0,0);
*/
}
#else
void
oofString::loadResource(unsigned long /*strID*/)
{
assert(!"oofString::loadResource only implemented on Mac and Windows");
}
#endif
bool
oofString::operator==(const char* chars) const
{
if (chars==0)
return (mBody==0 || mLen==0);
const unsigned long rhsLen = strlen(chars);
if (mLen!=rhsLen)
return false; // different lengths
for (unsigned long i=0; i<rhsLen; i++) {
if (mBody[i]!=chars[i])
return false;
}
return true;
}
oofString&
oofString::operator=(const char* chars)
{
delete[] mBody;
if (chars) {
mLen = strlen(chars);
mBody = new char[mLen + 1]; // room for null
assert(mBody);
strcpy(mBody, chars);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
else {
mLen = 0;
mBody = 0;
}
return *this;
}
void
oofString::setChars(const char* chars, unsigned long charLen)
{
// NOT YET IMPLEMENTED - avoid reallocs if mBody within certain size range
// that will fit chars - would require keeping alloc length as well as mLen
delete[] mBody;
if (chars && charLen) {
mLen = charLen;
mBody = new char[mLen + 1]; // room for null
assert(mBody);
memcpy(mBody, chars, charLen);
mBody[charLen] = '\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
}
else {
mLen = 0;
mBody = 0;
}
return;
}
void
oofString::insertChars(const char* chars, unsigned long charLen, unsigned long beforeChar)
{
if (!chars || charLen==0)
return;
unsigned long totalLen = mLen + charLen;
char* oldStore = mBody;
mBody = new char[totalLen+1];
char* insertAt;
if (beforeChar>=mLen) { // appending
memcpy(mBody, oldStore, mLen);
insertAt = mBody+mLen;
}
else if (beforeChar<2) { // at front of string
insertAt = mBody;
memcpy(insertAt+charLen, oldStore, mLen); // entire old body after the inserted string
}
else {
insertAt = mBody+beforeChar;
memcpy(mBody, oldStore, beforeChar); // bit before the inserted string
memcpy(insertAt+charLen, oldStore+beforeChar, mLen-beforeChar); // after the inserted string
}
delete[] oldStore;
memcpy(insertAt, chars, charLen);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
mBody[totalLen]='\0';
mLen = totalLen;
}
void
oofString::padTo(unsigned short paddedLength, char padWith, bool rightAlign)
{
if (paddedLength <= mLen)
return;
const size_t charsToFill = paddedLength - mLen;
char* oldStore = mBody;
mBody = new char[paddedLength+1];
char* insertAt;
if (rightAlign) {
memcpy(&mBody[charsToFill], oldStore, mLen);
insertAt = mBody;
}
else{
memcpy(mBody, oldStore, mLen);
insertAt = mBody+mLen;
}
delete[] oldStore;
memset(insertAt, padWith, charsToFill);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
mBody[paddedLength]='\0';
mLen = paddedLength;
}
void
oofString::getChars(char* ioBuff, unsigned long& ioLen) const
{
unsigned long outLen = mLen;
if (ioLen < outLen)
outLen = ioLen;
else
ioLen = outLen;
memcpy(ioBuff, mBody, outLen);
}
oofString::operator const char*() const
{
if (mBody)
return mBody;
else
return "";
}
char&
oofString::operator[](unsigned long index) const
{
if ((index >= mLen) || !mBody)
return sEmptyChar;
else
return mBody[index];
}
void
oofString::operator+=(char c)
{
unsigned long totalLen = mLen + 1;
if (mLen && mBody) {
char* oldStore = mBody;
mBody = new char[totalLen+1];
memcpy(mBody, oldStore, mLen);
delete[] oldStore;
}
else {
delete[] mBody; // shortening could reduce mLen but still have mBody
mBody = new char[totalLen+1];
}
mBody[mLen] = c;
mBody[totalLen]='\0';
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
mLen = totalLen;
}
void
oofString::operator+=(const char* str)
{
const unsigned long appendLen = strlen(str);
if (appendLen>0)
AppendSome(str, appendLen);
}
void
oofString::operator+=(const oofString& src)
{
const unsigned long appendLen = src.length();
if (appendLen>0)
AppendSome(src.chars(), appendLen);
}
void
oofString::append(const char* str, unsigned long appendLen)
{
if (appendLen>0)
AppendSome(str, appendLen);
}
void
oofString::AppendSome(const char* str, unsigned long appendLen)
{
assert(appendLen>0);
const unsigned long totalLen = mLen + appendLen;
if (mLen && mBody) {
char* oldStore = mBody;
mBody = new char[totalLen+1];
memcpy(mBody, oldStore, mLen);
delete[] oldStore;
}
else {
delete[] mBody; // shortening could reduce mLen but still have mBody
mBody = new char[totalLen+1];
}
memcpy(mBody+mLen, str, appendLen);
#ifdef OOF_MEM_DEBUG_FORCE_POOL_CHECK
OOF_MEM_DEBUG_FORCE_POOL_CHECK
#endif
mBody[totalLen]='\0';
mLen = totalLen;
}
void
oofString::convertNumber(int n, const char* printfMask, bool appendIt)
{
char buf[80];
if (printfMask)
sprintf(buf, printfMask, n);
else
sprintf(buf, "%i", n);
if (appendIt)
*this += buf;
else
*this = buf;
}
void
oofString::convertNumber(unsigned long n, const char* printfMask, bool appendIt)
{
char buf[80];
if (printfMask)
sprintf(buf, printfMask, n);
else
sprintf(buf, "%lu", n);
if (appendIt)
*this += buf;
else
*this = buf;
}
void
oofString::convertNumber(double n, const char* printfMask, bool appendIt)
{
char buf[80];
if (printfMask)
sprintf(buf, printfMask, n);
else
sprintf(buf, "%g", n);
if (appendIt)
*this += buf;
else
*this = buf;
}
unsigned long
oofString::replaceChar(char fromChar, char toChar, unsigned long startAt)
{
unsigned long numReplaced = 0;
const unsigned long kLen = mLen;
for (unsigned long i=startAt; i<kLen; i++) {
if (mBody[i]==fromChar) {
mBody[i] = toChar;
numReplaced++;
}
}
return numReplaced;
}
oofString
oofString::subString(unsigned long fromChars, unsigned long numChars) const
{
if (!mBody || fromChars >= mLen)
return oofString(); // return empty string if original empty, or start past its end
assert(mLen > fromChars); // handle other case above, returning empty
const unsigned long lenAvailToCopy = mLen - fromChars;
if (numChars > lenAvailToCopy) // default is copy all remaining
numChars = lenAvailToCopy;
const char* copyFrom = &mBody[fromChars];
return oofString(copyFrom, numChars); // return string of <= length requested
}
oofString
oofString::subStringBetween(const char* leftStr, const char* rightStr) const
{
const long leftAt = findEnd(leftStr);
if (leftAt!=kNotFound) {
const long rightAt = find(rightStr, leftAt /* startFrom */);
if (rightAt!=kNotFound) {
const unsigned long subLen = rightAt-leftAt;
return subString(leftAt, subLen);
}
}
return oofString();
}
oofString
oofString::operator-(const char* inTail) const
{
const unsigned int inLen = strlen(inTail);
if (endsWith(inTail, inLen)) {
return subString(0, mLen-inLen);
}
else
return oofString();
}
oofString
oofString::subStringBetween(char leftChar, char rightChar) const
{
const long leftStartsAt = find(leftChar);
if (leftStartsAt!=kNotFound) {
const long leftAt = leftStartsAt+1; // skip the actual char, like using findEnd in substringBetween(const char*...
const long rightAt = find(rightChar, leftAt /* startFrom */);
if (rightAt!=kNotFound) {
const unsigned long subLen = rightAt-leftAt;
return subString(leftAt, subLen);
}
}
return oofString();
}
long
oofString::find(char inChar, unsigned long startFrom, bool caseSensitive) const
{
unsigned long foundAt = kNotFound;
const unsigned long kLen = mLen;
if (isalpha(inChar)) {
if (!caseSensitive)
inChar = toupper(inChar);
}
else
caseSensitive = false; // only worth hassle if alpha argument!
if (caseSensitive) {
for (unsigned long i=startFrom; i<kLen; i++) {
if (mBody[i]==inChar) { // guarantees is not null
foundAt = i;
break;
} // found match
} // find loop - comparing strings
}
else {
for (unsigned long i=startFrom; i<kLen; i++) {
if (toupper(mBody[i])==inChar) { // guarantees is not null
foundAt = i;
break;
} // found match
} // find loop - comparing strings
}
return foundAt;
}
long
oofString::findLast(char inChar, bool caseSensitive) const
{
if (mLen==0)
return kNotFound;
unsigned long foundAt = kNotFound;
if (caseSensitive) {
if (isalpha(inChar))
inChar = toupper(inChar);
else
caseSensitive = false; // only worth hassle if alpha argument!
}
if (caseSensitive) {
for (long i=mLen-1; i>=0; i--) {
if (toupper(mBody[i])==inChar) { // guarantees is not null
foundAt = i;
break;
} // found match
} // find loop - comparing strings
}
else {
for (long i=mLen-1; i>=0; i--) {
if (mBody[i]==inChar) { // guarantees is not null
foundAt = i;
break;
} // found match
} // find loop - comparing strings
}
return foundAt;
}
long
oofString::find(const char* inStr, unsigned long startFrom, bool caseSensitive, unsigned long inLen) const
{
// NOT YET IMPLEMENTED - Boyer-Moore search for longer strings
unsigned long foundAt = kNotFound;
const unsigned long kLen = mLen;
const unsigned long schLen = (inLen==ULONG_MAX) ? strlen(inStr) : inLen;
if (schLen==0)
return 0; // empty input string - matches beginning
if (schLen==1)
return find(inStr[0], startFrom, caseSensitive); // use optimal single char search
if (caseSensitive) {
for (unsigned long i=0; i<schLen; i++) {
if (!isalpha(inStr[i])) {
caseSensitive = false; // only worth hassle if wholly alpha argument!
break;
}
} // loop sch argument
}
if (caseSensitive) { // I know this repeats a lot of logic but that's efficient!
const char* schStr = inStr;
const char firstChar = schStr[0];
for (unsigned long i=startFrom; i<kLen; i++) {
if (mBody[i]==firstChar) { // guarantees is not null
if ((i+schLen) > kLen) { // hit end of source, partway through matching
foundAt = kNotFound;
break; // comparison failed
}
const char* startMatch = &mBody[i];
foundAt = i; // anticipate rest of string matches
// note start at 1 not 0 so offset both search and origin - compared [0] above
for (unsigned long k=1; startMatch[k] && schStr[k]; k++) {
if (schStr[k] != startMatch[k]) {
foundAt = kNotFound;
break; // comparison failed
}
} // loop comparing chars in a string
if (foundAt != kNotFound) // successful match of entire search str
break; // out of main loop matching first char to source
} // if first char matches
} // find loop - comparing strings
}
else { // INSENSITIVE
const unsigned long localBufLen = 20; // should be able to use stack buf for most search args
char localBuf[localBufLen];
char* schStr;
if (schLen > localBufLen)
schStr = new char[schLen+1];
else
schStr = localBuf; // for small search arg, avoid hitting the heap!
for (unsigned long iSch=0; iSch<schLen; iSch++) // optimise by pre-creating uppercased sch string
schStr[iSch] = toupper(inStr[iSch]);
schStr[schLen] = '\0';
const char firstChar = schStr[0];
for (unsigned long i=startFrom; i<kLen; i++) {
if (toupper(mBody[i])==firstChar) { // guarantees is not null
if ((i+schLen) > kLen) { // hit end of source, partway through matching
foundAt = kNotFound;
break; // comparison failed
}
const char* startMatch = &mBody[i];
foundAt = i; // anticipate rest of string matches
// note start at 1 not 0 so offset both search and origin - compared [0] above
for (unsigned long k=1; startMatch[k] && schStr[k]; k++) {
if (schStr[k] != toupper(startMatch[k])) { //** insensitive test
foundAt = kNotFound;
break; // comparison failed
}
} // loop comparing chars in a string
if (foundAt != kNotFound) // successful match of entire search str
break; // out of main loop matching first char to source
} // if first char matches
} // find loop - comparing strings
if (schStr != localBuf)
delete[] schStr; // go out of our way to avoid copy as that's a slow heap hit!
} // case sensitive or not
return foundAt;
}
long
oofString::findEnd(const char* schStr, unsigned long startFrom, bool caseSensitive) const
{
long foundAt = find(schStr, startFrom, caseSensitive);
if (foundAt!=kNotFound)
foundAt+=strlen(schStr);
return foundAt;
}
void
oofString::replaceAt(unsigned long replaceAtOffset, unsigned long replaceLen, const char* newStr, unsigned long newLen)
{
if (!newStr)
return;
const unsigned long insertLen = (newLen==ULONG_MAX) ? strlen(newStr) : newLen;
if (replaceLen==insertLen) { // lucky trivial case - it just fits
memcpy(&mBody[replaceAtOffset], newStr, replaceLen);
}
else if (replaceLen>insertLen) { // copy over then move end up, wasting some space
memcpy(&mBody[replaceAtOffset], newStr, insertLen);
const unsigned long startTrailer = replaceAtOffset+replaceLen;