This repository was archived by the owner on Mar 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr.cpp
More file actions
742 lines (591 loc) · 13.5 KB
/
str.cpp
File metadata and controls
742 lines (591 loc) · 13.5 KB
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
/* str.cpp
* String, StringTokens, and StringMask classes
*
* Copyright (c) 1996-2001 Alien Internet Services
* This version is somewhat outdated.
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include "str.h"
// This is used in iostream reading
#define BUFFSIZE 4096 // 4k
/* String - [Various Forms] Constructors/copiers/converters
* Original 07/96 simonb
*/
String::String(char const *s)
: ptr(new strptr),
len(strlen(s))
{
ptr->str = new char[len + 1];
strcpy(ptr->str, s);
}
String::String(String const &s)
: ptr(s.ptr),
len(s.len)
{
ptr->ref++;
}
String::String(unsigned char i)
{
char temp[16];
sprintf(temp, "%d", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(long i)
{
char temp[32];
sprintf(temp, "%ld", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(unsigned long i)
{
char temp[32];
sprintf(temp, "%ld", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(int i)
{
char temp[32];
sprintf(temp, "%d", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(unsigned int i)
{
char temp[32];
sprintf(temp, "%d", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(double i)
{
char temp[128];
sprintf(temp, "%f", i);
ptr = new strptr;
len = strlen(temp);
ptr->str = new char[len + 1];
strcpy(ptr->str, temp);
}
String::String(char c)
{
ptr = new strptr;
ptr->str = new char[2];
ptr->str[0] = c;
ptr->str[1] = '\0';
len = 1;
}
/* ~String - Destructor
* Original 07/96 simonb
*/
String::~String()
{
// Sanity check
if (--ptr->ref == 0) {
delete[] ptr->str;
delete ptr;
}
}
/* String Operators
* Original 07/96 simonb
* 09/01/01 simonb - Fixed potential problems with [] operator
*/
String &String::operator=(char const *s)
{
// Clear up old pointer, if any
if (ptr->ref > 1) {
ptr->ref--;
ptr = new strptr;
} else {
delete[] ptr->str;
}
len = strlen(s);
ptr->str = new char[len + 1];
strcpy(ptr->str, s);
return *this;
}
String &String::operator=(String const &s)
{
// Protection against string = string possibility
s.ptr->ref++;
// Clear up old pointer, if any
if (--ptr->ref == 0) {
delete[] ptr->str;
delete ptr;
}
ptr = s.ptr;
len = s.len;
return *this;
}
char &String::operator[](length_t i)
{
// Range check
if ((i < 0) || (i > len)) {
return ptr->str[len + 1];
}
return ptr->str[i];
}
char const &String::operator[](length_t i) const
{
// Range check
if ((i < 0) || (i > len)) {
return ptr->str[len + 1];
}
return ptr->str[i];
}
String String::operator+(char const *s) const
{
char *temp = new char[len + strlen(s) + 1];
strcpy(temp, ptr->str);
strcat(temp, s);
String result(temp);
delete temp;
return result;
}
String String::operator+(String const &s) const
{
char *temp = new char[len + s.len + 1];
strcpy(temp, ptr->str);
strcat(temp, s.ptr->str);
String result(temp);
delete temp;
return result;
}
istream &operator>>(istream &s, String &line)
{
// Check to make sure we are not going to mess this up
if (line.ptr->ref > 1) {
line.ptr->ref--;
line.ptr = new String::strptr;
} else {
delete[] line.ptr->str;
}
char buf[BUFFSIZE];
char c;
s.get(buf, BUFFSIZE, '\n');
s.get(c);
line.len = strlen(buf);
line.ptr->str = new char[line.len + 1];
strcpy(line.ptr->str, buf);
return s;
}
/* find - Find a character in a string
* Original 07/96 simonb
*/
String::length_t String::find(char c) const
{
char *s = strchr(ptr->str, c);
// Check that worked
if (s) {
return -1;
}
return (length_t)(s - ptr->str);
}
/* subString - Return a cut section of a string
* Original 07/96 simonb
*/
String String::subString(length_t start, length_t end) const
{
// Sanity check
if (end < start) {
return "";
}
char *temp = new char[end-start+2];
strncpy(temp, (ptr->str + start), (end - start + 1));
temp[end - start + 1] = '\0';
String res(temp);
delete temp;
return res;
}
/* toLower - Convert an entire string to lower case
* Original 07/96 simonb
*/
String String::toLower() const
{
char *temp = new char[len + 1];
for (register unsigned int i = (len + 1); i--;) {
if (isupper(ptr->str[i])) {
temp[i] = tolower(ptr->str[i]);
} else {
temp[i] = ptr->str[i];
}
}
String res(temp);
delete temp;
return res;
}
/* toUpper - Convert an entire string to upper case
* Original 07/96 simonb
*/
String String::toUpper() const
{
char *temp = new char[len + 1];
for (register unsigned int i = (len + 1); i--;) {
if (islower(ptr->str[i])) {
temp[i] = toupper(ptr->str[i]);
} else {
temp[i] = ptr->str[i];
}
}
String res(temp);
delete temp;
return res;
}
/* IRCtoLower - Convert to lowercase while considering irc chars (eg {} and [])
* Original 12/08/01 simonb
*/
String String::IRCtoLower() const
{
char *temp = new char[len + 1];
for (register unsigned int i = (len + 1); i--;) {
switch (ptr->str[i]) {
case '[':
temp[i] = '{';
continue;
case ']':
temp[i] = '}';
continue;
case '\\':
temp[i] = '|';
continue;
case '~':
temp[i] = '^';
continue;
default:
if (isupper(ptr->str[i])) {
temp[i] = tolower(ptr->str[i]);
} else {
temp[i] = ptr->str[i];
}
}
}
String res(temp);
delete temp;
return res;
}
/* trim - Trim away spaces, tabs, and CR/LF's from the start/end of a string
* Original 06/03/99 simonb
*/
String String::trim(void) const
{
length_t i = 0;
length_t j = len - 1;
while ((i < j) &&
((ptr->str[i] == ' ') ||
(ptr->str[i] == '\t') ||
(ptr->str[i] == '\r') ||
(ptr->str[i] == '\n'))) {
i++;
}
while (j > 0 &&
((ptr->str[j] == ' ') ||
(ptr->str[j] == '\t') ||
(ptr->str[j] == '\r') ||
(ptr->str[j] == '\n'))) {
j--;
}
return subString(i, j);
}
/* trimQuotes - Same as regular trim(), but for removing ' and "'s
* Original 11/08/01 simonb
*/
String String::trimQuotes(void) const
{
length_t i = 0;
length_t j = len - 1;
while ((i < j) &&
((ptr->str[i] == ' ') ||
(ptr->str[i] == '\'') ||
(ptr->str[i] == '"'))) {
i++;
}
while (j > 0 &&
((ptr->str[j] == ' ') ||
(ptr->str[j] == '\'') ||
(ptr->str[j] == '"'))) {
j--;
}
return subString(i, j);
}
/* pad - Make string exactly n by cropping or adding spaces to the end
* Original 06/03/99 simonb
*/
String String::pad(length_t n, char c) const
{
length_t l = len;
if (n <= l) {
return subString(0, n-1);
}
char *temp = new char[n+1];
strcpy(temp, ptr->str);
for (register length_t i = l; i < n; i++) {
temp[i] = ' ';
}
temp[n] = '\0';
String res(temp);
delete temp;
return res;
}
/* prepad - Make string exactly n by cropping or adding spaces to the start
* Original 06/03/99 simonb
* Note: this really could be more efficient :-/
*/
String String::prepad(length_t n, char c) const
{
char *temp = new char[n+1];
strcpy(temp, ptr->str);
String out(temp);
delete temp;
while (out.len < n) {
out = String((char)c) + out;
}
return out;
}
/* printf - Essentially printf with String class return
* Original 09/06/00 simonb
* Note: Doesn't work that well, eg. with String class vars.
* AFAIK there isn't a way to define 'class String' as being passable
* though '...' -- the compiler is just stupid since we DO have a
* (char const *)() overload operator defined :(
*/
String String::printf(char *format, ...)
{
static char buff[10000];
va_list ap;
va_start(ap, format);
vsprintf(buff, format, ap);
va_end(ap);
buff[9999] = '\0'; // safety!
return String(buff);
}
/* hasMoreTokens - Check if the string has more tokens
* Original 08/02/99 simonb
*/
bool StringTokens::hasMoreTokens(void) const
{
if (pos == str.length()) {
return false;
}
for (register String::length_t i = pos; i < str.length(); i++) {
if ((str[i] != ' ') &&
(str[i] != '\t')) {
return true;
}
}
return false;
}
bool StringTokens::hasMoreTokens(char c) const
{
if (pos == str.length()) {
return false;
}
for (register String::length_t i = pos; i < str.length(); i++) {
if (str[i] != c) {
return true;
}
}
return false;
}
/* countTokens - [Various Forms] Count the number of tokens
* Original 08/02/99 simonb
*/
unsigned int StringTokens::countTokens(void)
{
unsigned int i = 0;
StringTokens s(str);
while (s.hasMoreTokens()) {
s.nextToken();
i++;
}
return i;
}
unsigned int StringTokens::countTokens(char c)
{
unsigned int i = 0;
StringTokens s(str);
while (s.hasMoreTokens(c)) {
s.nextToken(c);
i++;
}
return i;
}
/* nextToken - [Various Forms] Grab the next token
* Original 08/02/99 simonb
* Note: printf bug?
*/
String StringTokens::nextToken(void)
{
String::length_t i = pos;
while ((i < str.length()) &&
((str[i] == ' ') || (str[i] == '\t'))) {
i++;
}
for (String::length_t j = i; j < str.length(); j++) {
if ((str[j] == ' ') || (str[j] == '\t')) {
pos = j + 1;
return str.subString(i, j - 1);
}
}
pos = str.length();
return str.subString(i, str.length() - 1);
}
String StringTokens::nextToken(char c, bool empty)
{
String::length_t i = pos;
while ((i < str.length()) && (str[i] == c)) {
i++;
}
for (String::length_t j = i; j < str.length(); j++) {
if (str[j] == c) {
pos = j + 1;
return str.subString(i, j - 1);
}
}
if (empty) {
return "";
}
pos = str.length();
return str.subString(i, str.length() - 1);
}
/* nextColonToken - Get the next token, or the rest, depending on a :
* Original 11/08/01 simonb
*/
String StringTokens::nextColonToken(void)
{
if (pos == str.length()) {
return "";
}
if (str[pos] == ':') {
pos++;
return rest();
}
return nextToken();
}
/* rest - Grab the rest of the string ignoring the remaining tokens
* Original 08/02/99 simonb
*/
String StringTokens::rest(void)
{
if (pos == str.length()) {
return "";
}
while ((pos < str.length()) &&
(str[pos] == ' ' || str[pos] == '\t')) {
pos++;
}
return str.subString(pos, str.length() - 1);
}
/* match - Do the actual match check
* Original 15/02/96 simonb
* 18/07/98 simonb - Ported from pascal to C++
* 20/07/98 simonb - Changed String to char const
* 07/09/01 simonb - Partial rewrite for more speed (no damned recursion!!)
*/
bool StringMask::match(char const *m, char const *n)
{
bool escaped = false;
// sanity check for strings, the neat way returning true/false
if (!*m) {
// Of course if they are both null then this is a match
if (!*n) {
return true;
} else {
return false;
}
} else {
if (!*n) {
return false;
}
}
// While the string is valid, run through it
while (*m) {
// Check if this is being escaped
if (escaped) {
// Next character for both strings
m++;
n++;
escaped = false;
} else {
// Check what to do with this character
switch (*m) {
case '*': // Anything goes metacharacter
// Loop until the next character is NOT the astericks metacharacter
while (*m && (*m == '*')) {
m++;
}
// Loop until the given string ends or we find a matching char
while (*n && (*n != *m)) {
n++;
}
/* If the next char is the null, bail out now and say true since the
* rest of the line will of course match
*/
if (!*m && !*n) {
return true;
}
continue;
case '?': // Skip one char metacharacter
// If the string is broken already, this cannot count as a char.
if (!*n) {
return false;
}
m++;
n++;
// If both the next chars are nulls, this obviously worked OK
if (!*m && !*n) {
return true;
}
continue;
case '\\': // Escape character
escaped = true;
default:
// Check if these two characters match in both strings
if (*m != *n) {
return false;
}
// Next character for both strings
m++;
n++;
// If both of these are at the end of line, naturally they are OK!
if (!*m && !*n) {
return true;
}
}
}
}
return false;
}
#ifdef STL_HAS_HASH
/* hash<String> - generic hash template for our String class
* Original 24/08/01 simonb
*/
size_t hash<String>::operator()(String const &str) const
{
char const *s = (char const *)str;
size_t size = 0; // Should we offset this?
/* Run through the string; Note the last number should be a prime for
* minimising collisions. Everywhere I read people seem to like magic
* number 17 as a generic prime, so I have followed the fashion :)
*/
for (; *s; s++) {
size = (size * 17) ^ *s;
}
return size;
}
#endif