-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathm_qstr.cpp
1425 lines (1232 loc) · 28.5 KB
/
m_qstr.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
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2004 James Haley
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//--------------------------------------------------------------------------
//
// Reallocating string structure
//
// What this class guarantees:
// * The string will always be null-terminated
// * Indexing functions always check array bounds
// * Insertion functions always reallocate when needed
//
// Of course, using getBuffer can negate these, so avoid it
// except for passing a char * to read op functions.
//
// By James Haley
//
//-----------------------------------------------------------------------------
#include "z_zone.h"
#include "d_dehtbl.h" // for D_HashTableKey
#include "i_system.h"
#include "m_qstr.h"
#include "m_misc.h" // for M_Strupr/M_Strlwr
#include "m_strcasestr.h" // for M_StrCaseStr
//#include "p_saveg.h"
#include "d_io.h" // for strcasecmp
const size_t qstring::npos = ((size_t) -1);
const size_t qstring::basesize = 16;
const qstring qstring::emptyString;
//=============================================================================
//
// Constructors, Destructors, and Reinitializers
//
void qstring::unLocalize(size_t pSize)
{
if(isLocal())
{
buffer = ecalloc(char *, 1, pSize);
size = pSize;
strcpy(buffer, local);
memset(local, 0, basesize);
}
}
//
// qstring::createSize
//
// Creates a qstring with a given initial size, which helps prevent
// unnecessary initial reallocations. Resets insertion point to zero.
// This is safe to call on an existing qstring to reinitialize it.
//
qstring &qstring::createSize(size_t pSize)
{
// Can remain local?
if(isLocal() && pSize <= basesize)
clear();
else
{
unLocalize(pSize);
// Don't realloc if not needed
if(size < pSize)
{
buffer = erealloc(char *, buffer, pSize);
size = pSize;
}
clear();
}
return *this;
}
//
// qstring::create
//
// Gives the qstring a buffer of the default size and initializes
// it to zero. Resets insertion point to zero. This is safe to call
// on an existing qstring to reinitialize it.
//
qstring &qstring::create()
{
return createSize(basesize);
}
//
// qstring::initCreate
//
// Initializes a qstring struct to all zeroes, then calls
// qstring::create. This is for safety the first time a qstring
// is created (if qstr::buffer is uninitialized, realloc will
// crash).
//
qstring &qstring::initCreate()
{
buffer = local;
size = basesize;
clear();
return create();
}
//
// qstring::initCreateSize
//
// Initialization and creation with size specification.
//
qstring &qstring::initCreateSize(size_t pSize)
{
buffer = local;
size = basesize;
clear();
return createSize(pSize);
}
//
// qstring::freeBuffer
//
// Frees the qstring object's buffer, if it is not localized. The qstring
// returns to being localized at that point, and will be empty.
//
void qstring::freeBuffer()
{
if(buffer && !isLocal())
efree(buffer);
// return to being local
buffer = local;
size = basesize;
clear();
}
//
// qstring Move Constructor
//
// haleyjd 05/22/2013: Enable C++11 move semantics for qstring instances.
// Required for efficiency when using qstring with Collection<T>.
//
qstring::qstring(qstring &&other)
: ZoneObject(), index(0), size(16)
{
// When other is not localized, take direct ownership of its buffer
if(!other.isLocal())
{
buffer = other.buffer;
index = other.index;
size = other.size;
memset(local, 0, sizeof(local));
// leave the other object in a usable state, it's not necessarily dead.
other.buffer = NULL;
other.freeBuffer(); // returns to being localized
}
else
{
// Copy the local buffer
memcpy(local, other.local, sizeof(local));
buffer = local;
index = other.index;
}
}
//=============================================================================
//
// Basic Properties
//
//
// qstring::charAt
//
// Indexing function to access a character in a qstring. This is slower but
// more secure than using qstring::getBuffer with array indexing.
//
char qstring::charAt(size_t idx) const
{
if(idx >= size)
I_Error("qstring::charAt: index out of range\n");
return buffer[idx];
}
//
// qstring::bufferAt
//
// Gets a pointer into the buffer at the given position, if that position would
// be valid. Returns NULL otherwise. The same caveats apply as with qstring::getBuffer.
//
char *qstring::bufferAt(size_t idx)
{
return idx < size ? buffer + idx : NULL;
}
//
// qstring::operator []
//
// Read-write variant.
//
char &qstring::operator [] (size_t idx)
{
if(idx >= size)
I_Error("qstring::operator []: index out of range\n");
return buffer[idx];
}
//
// qstring::operator []
//
// Read-only variant.
//
const char &qstring::operator [] (size_t idx) const
{
if(idx >= size)
I_Error("qstring::operator []: index out of range\n");
return buffer[idx];
}
//=============================================================================
//
// Basic String Ops
//
//
// qstring::clear
//
// Sets the entire qstring buffer to zero, and resets the insertion index.
// Does not reallocate the buffer.
//
qstring &qstring::clear()
{
memset(buffer, 0, size);
index = 0;
return *this;
}
//
// qstring::clearOrCreate
//
// Creates a qstring, or clears it if it is already valid.
//
qstring &qstring::clearOrCreate(size_t pSize)
{
return createSize(pSize);
}
//
// qstring::grow
//
// Grows the qstring's buffer by the indicated amount. This is automatically
// called by other qstring methods, so there is generally no need to call it
// yourself.
//
qstring &qstring::grow(size_t len)
{
if(len > 0)
{
size_t newsize = size + len;
if(isLocal()) // are we local?
{
if(newsize > basesize) // can we stay local?
unLocalize(newsize);
}
else
{
buffer = erealloc(char *, buffer, newsize);
memset(buffer + size, 0, len);
size += len;
}
}
return *this;
}
//=============================================================================
//
// Concatenation and Insertion/Deletion/Copying Functions
//
//
// qstring::Putc
//
// Adds a character to the end of the qstring, reallocating via buffer doubling
// if necessary.
//
qstring &qstring::Putc(char ch)
{
if(index >= size - 1) // leave room for \0
grow(size); // double buffer size
buffer[index++] = ch;
return *this;
}
//
// qstring::operator +=
//
// Overloaded += for characters
//
qstring &qstring::operator += (char ch)
{
return Putc(ch);
}
//
// qstring::Delc
//
// Deletes a character from the end of the qstring.
//
qstring &qstring::Delc()
{
if(index > 0)
{
index--;
buffer[index] = '\0';
}
return *this;
}
//
// qstring::concat
//
// Concatenates a C string onto the end of a qstring, expanding the buffer if
// necessary.
//
qstring &qstring::concat(const char *str)
{
size_t cursize = size;
size_t newsize = index + strlen(str) + 1;
if(newsize > cursize)
grow(newsize - cursize);
strcat(buffer, str);
index = strlen(buffer);
return *this;
}
//
// qstring::concat
//
// Concatenates a qstring to a qstring. Convenience routine.
//
qstring &qstring::concat(const qstring &src)
{
return concat(src.buffer);
}
//
// qstring::operator +=
//
// Overloaded += for const char *
//
qstring &qstring::operator += (const char *other)
{
return concat(other);
}
//
// qstring::operator +=
//
// Overloaded += for qstring &
//
qstring &qstring::operator += (const qstring &other)
{
return concat(other);
}
//
// qstring::insert
//
// Inserts a string at a given position in the qstring. If the
// position is outside the range of the string, an error will occur.
//
qstring &qstring::insert(const char *insertstr, size_t pos)
{
char *insertpoint;
size_t charstomove;
size_t insertstrlen = strlen(insertstr);
size_t totalsize = index + insertstrlen + 1;
// pos must be between 0 and dest->index - 1
if(pos >= index)
I_Error("qstring::insert: position out of range\n");
// grow the buffer to hold the resulting string if necessary
if(totalsize > size)
grow(totalsize - size);
insertpoint = buffer + pos;
charstomove = index - pos;
// use memmove for absolute safety
memmove(insertpoint + insertstrlen, insertpoint, charstomove);
memmove(insertpoint, insertstr, insertstrlen);
index = strlen(buffer);
return *this;
}
//
// qstring::copy
//
// Copies a C string into the qstring. The qstring is cleared first,
// and then set to the contents of *str.
//
qstring &qstring::copy(const char *str)
{
if(index > 0)
clear();
return concat(str);
}
//
// qstring::copy
//
// Copies at most count bytes from the C string into the qstring.
//
qstring &qstring::copy(const char *str, size_t count)
{
if(index > 0)
clear();
size_t newsize = count + 1;
if(newsize > size)
grow(newsize - size);
strncpy(buffer, str, count);
index = strlen(buffer);
return *this;
}
//
// qstring::copy
//
// Overloaded for qstring &
//
qstring &qstring::copy(const qstring &src)
{
if(index > 0)
clear();
return concat(src);
}
//
// qstring::operator =
//
// Assignment from a qstring &
//
qstring &qstring::operator = (const qstring &other)
{
return copy(other);
}
//
// qstring::operator =
//
// Assignment from a const char *
//
qstring &qstring::operator = (const char *other)
{
return copy(other);
}
//
// qstring::copyInto
//
// Copies the qstring into a C string buffer.
//
char *qstring::copyInto(char *dest, size_t pSize) const
{
return strncpy(dest, buffer, pSize);
}
//
// qstring::copyInto
//
// Copies one qstring into another.
//
qstring &qstring::copyInto(qstring &dest) const
{
if(dest.index > 0)
dest.clear();
return dest.concat(*this);
}
//
// qstring::swapWith
//
// Exchanges the contents of two qstrings.
//
void qstring::swapWith(qstring &str2)
{
char *tmpbuffer;
size_t tmpsize;
size_t tmpindex;
// Both must be unlocalized.
unLocalize(size);
str2.unLocalize(str2.size);
tmpbuffer = this->buffer; // make a shallow copy
tmpsize = this->size;
tmpindex = this->index;
this->buffer = str2.buffer;
this->size = str2.size;
this->index = str2.index;
str2.buffer = tmpbuffer;
str2.size = tmpsize;
str2.index = tmpindex;
}
//
// qstring::lstrip
//
// Removes occurrences of a specified character at the beginning of a qstring.
//
qstring &qstring::lstrip(char c)
{
size_t i = 0;
size_t len = index;
while(buffer[i] != '\0' && buffer[i] == c)
++i;
if(i)
{
if((len -= i) == 0)
clear();
else
{
memmove(buffer, buffer + i, len);
memset(buffer + len, 0, size - len);
index -= i;
}
}
return *this;
}
//
// qstring::rstrip
//
// Removes occurrences of a specified character at the end of a qstring.
//
qstring &qstring::rstrip(char c)
{
while(index && buffer[index - 1] == c)
Delc();
return *this;
}
//
// qstring::truncate
//
// Truncates the qstring to the indicated position.
//
qstring &qstring::truncate(size_t pos)
{
// pos must be between 0 and qstr->index - 1
if(pos >= index)
I_Error("qstring::truncate: position out of range\n");
memset(buffer + pos, 0, index - pos);
index = pos;
return *this;
}
//
// qstring::erase
//
// std::string-compatible erasure function.
//
qstring &qstring::erase(size_t pos, size_t n)
{
// truncate handles the case of n == qstring::npos
if(!n)
return *this;
else if(n == npos)
return truncate(pos);
// pos must be between 0 and qstr->index - 1
if(pos >= index)
I_Error("qstring::erase: position out of range\n");
size_t endPos = pos + n;
if(endPos > index)
endPos = index;
char *to = buffer + pos;
char *from = buffer + endPos;
char *end = buffer + index;
while(to != end)
{
*to = *from;
++to;
if(from != end)
++from;
}
index -= (endPos - pos);
return *this;
}
//=============================================================================
//
// Stream Insertion Operators
//
qstring &qstring::operator << (const qstring &other)
{
return concat(other);
}
qstring &qstring::operator << (const char *other)
{
return concat(other);
}
qstring &qstring::operator << (char ch)
{
return Putc(ch);
}
qstring &qstring::operator << (int i)
{
char buf[33];
M_Itoa(i, buf, 10);
return concat(buf);
}
qstring &qstring::operator << (double d)
{
char buf[1079]; // srsly...
psnprintf(buf, sizeof(buf), "%f", d);
return concat(buf);
}
//=============================================================================
//
// Comparison Functions
//
//
// qstring::strCmp
//
// C-style string comparison/collation ordering.
//
int qstring::strCmp(const char *str) const
{
return strcmp(buffer, str);
}
//
// qstring::strNCmp
//
// C-style string compare with length limitation.
//
int qstring::strNCmp(const char *str, size_t maxcount) const
{
return strncmp(buffer, str, maxcount);
}
//
// qstring::strCaseCmp
//
// Case-insensitive C-style string compare.
//
int qstring::strCaseCmp(const char *str) const
{
return strcasecmp(buffer, str);
}
//
// qstring::strNCaseCmp
//
// Case-insensitive C-style compare with length limitation.
//
int qstring::strNCaseCmp(const char *str, size_t maxcount) const
{
return strncasecmp(buffer, str, maxcount);
}
//
// qstring::compare
//
// C++ style comparison. True return value means it is equal to the argument.
//
bool qstring::compare(const char *str) const
{
return !strcmp(buffer, str);
}
//
// qstring::compare
//
// Overload for qstrings.
//
bool qstring::compare(const qstring &other) const
{
return !strcmp(buffer, other.buffer);
}
//
// qstring::operator ==
//
// Overloaded comparison operator for const char *
//
bool qstring::operator == (const char *other) const
{
return !strcmp(buffer, other);
}
//
// qstring::operator ==
//
// Overloaded comparison operator for qstring &
//
bool qstring::operator == (const qstring &other) const
{
return !strcmp(buffer, other.buffer);
}
//
// qstring::operator !=
//
// Overloaded comparison operator for const char *
//
bool qstring::operator != (const char *other) const
{
return strcmp(buffer, other) != 0;
}
//
// qstring::operator !=
//
// Overloaded comparison operator for qstring &
//
bool qstring::operator != (const qstring &other) const
{
return strcmp(buffer, other.buffer) != 0;
}
//=============================================================================
//
// Hash Code Functions
//
// These are just convenience wrappers.
//
//
// qstring::HashCodeStatic
//
// Static version, for convenience and so that the convention of hashing a
// null pointer to 0 hash code is enforceable without special checks, even
// if the thing being hashed isn't a qstring instance.
//
unsigned int qstring::HashCodeStatic(const char *str)
{
return D_HashTableKey(str ? str : "");
}
//
// qstring::HashCodeCaseStatic
//
// As above, but with case sensitivity.
//
unsigned int qstring::HashCodeCaseStatic(const char *str)
{
return D_HashTableKeyCase(str ? str : "");
}
//
// qstring::hashCode
//
// Calls the standard D_HashTableKey that is used for the vast majority of
// string hash code computations in Eternity.
//
unsigned int qstring::hashCode() const
{
return HashCodeStatic(buffer);
}
//
// qstring::hashCodeCase
//
// Returns a hash code computed with the case of characters being treated as
// relevant to the computation.
//
unsigned int qstring::hashCodeCase() const
{
return HashCodeCaseStatic(buffer);
}
//=============================================================================
//
// Search Functions
//
//
// qstring::strChr
//
// Calls strchr on the qstring ("find first of", C-style).
//
const char *qstring::strChr(char c) const
{
return strchr(buffer, c);
}
//
// qstring::strRChr
//
// Calls strrchr on the qstring ("find last of", C-style)
//
const char *qstring::strRChr(char c) const
{
return strrchr(buffer, c);
}
//
// qstring::findFirstOf
//
// Finds the first occurance of a character in the qstring and returns its
// position. Returns qstring_npos if not found.
//
size_t qstring::findFirstOf(char c) const
{
const char *rover = buffer;
bool found = false;
while(*rover)
{
if(*rover == c)
{
found = true;
break;
}
++rover;
}
return found ? rover - buffer : npos;
}
//
// qstring::findFirstNotOf
//
// Finds the first occurance of a character in the qstring which does not
// match the provided character. Returns qstring_npos if not found.
//
size_t qstring::findFirstNotOf(char c) const
{
const char *rover = buffer;
bool found = false;
while(*rover)
{
if(*rover != c)
{
found = true;
break;
}
++rover;
}
return found ? rover - buffer : npos;
}
//
// qstring::findLastOf
//
// Find the last occurrance of a character in the qstring which matches
// the provided character. Returns qstring::npos if not found.
//
size_t qstring::findLastOf(char c) const
{
const char *rover;
bool found = false;
if(!index)
return npos;
rover = buffer + index - 1;
do
{
if(*rover == c)
{
found = true;
break;
}
}
while((rover == buffer) ? false : (--rover, true));
return found ? rover - buffer : npos;
}
//
// qstring::findSubStr
//
// Calls strstr on the qstring. If the passed-in string is found, then the
// return value points to the location of the first instance of that substring.
//
const char *qstring::findSubStr(const char *substr) const
{
return strstr(buffer, substr);
}
//
// qstring::findSubStrNoCase
//
// haleyjd 10/28/11: call strcasestr on the qstring, courtesy of implementation
// of the non-standard routine adapted from GNUlib.
//
const char *qstring::findSubStrNoCase(const char *substr) const
{
return M_StrCaseStr(buffer, substr);
}
//
// qstring::find
//
// std::string-compatible find function.
//
size_t qstring::find(const char *s, size_t pos) const
{
// pos must be between 0 and index - 1
if(pos >= index)
I_Error("qstring::find: position out of range\n");
char *base = buffer + pos;
char *substr = strstr(base, s);
return substr ? substr - buffer : npos;
}
//=============================================================================
//
// Conversion Functions
//
//
// Integers
//
//
// qstring::toInt
//
// Returns the qstring converted to an integer via atoi.
//
int qstring::toInt() const
{
return atoi(buffer);
}
//
// qstring::toLong
//
// Returns the qstring converted to a long integer via strtol.
//