forked from zvelo/ngrams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystring.h
593 lines (500 loc) · 16.6 KB
/
mystring.h
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
/*******************************************************************
C++ Package of Ternary Search Tree
Copyright (C) 2006 Zheyuan Yu
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.
Read full GPL at http://www.gnu.org/copyleft/gpl.html
Email me at [email protected] if you have any question or comment
WebSite: http://www.cs.dal.ca/~zyu
*************************************************************************/
#ifndef MY_STRING_H_
#define MY_STRING_H_
#include <iostream>
#include <assert.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
class StringIndexOutOfBounds { };
class String
{
public:
// The size type used
typedef unsigned int size_type;
// String empty constructor
String () : buffer(NULL), strLength(0), bufferLength(0)
{
}
// String constructors, mark them explicit to force us to find unnecessary casting
//Construct a String from a null-terminated character array.
//explicit
String( const char *cstring );
//Construct a String from a char
//explicit
String( const char ch );
//construct a String from an integer
String ( int value );
/**
* Construct a String with char filled len times
*
* @param len length of new String.
* @param char to fill the String.
*/
String(size_t len, const char chr = ' ');
/**
* Construct a String with subString of str from index start with specified length
* @param str new String
* @param start - start index
* @param len - len of the subString
*/
String( const String &str, size_t start, size_t len );
/**
* Copy constructor
*/
String( const String & copy );
/**
* Destructor
*/
virtual ~String( )
{
if ( this->bufferLength )
free( this->buffer );
this->buffer =0;
this->bufferLength = this->strLength =0;
}
// String operators
const String & operator = ( const char * content );
const String & operator = ( const String & copy );
const String & operator = ( const char ch );
/* bool operator == ( const String & str ) const;
bool operator == ( const char* str ) const;
bool operator != ( const String & str ) const;
bool operator != ( const char* str ) const;
bool operator < ( const String & str ) const;
bool operator < ( const char* str ) const;
bool operator > ( const String & str ) const;
bool operator > ( const char* str ) const;
bool operator <= ( const String & str ) const;
bool operator <= ( const char* str ) const;
bool operator >= ( const String & str ) const;
bool operator >= ( const char* str ) const;
*/
bool operator == ( const String & str ) const
{
return this->strLength == str.strLength && compare( str.c_str() ) == 0 ;
}
bool operator == ( const char * str ) const
{
return this->compare( str ) == 0 ;
}
bool operator!=( const String & str ) const
{
return this->strLength != str.strLength || !( *this == str );
}
bool operator!=( const char* str ) const
{
return !( *this == str );
}
bool operator < ( const String & str ) const
{
return this->compare( str.c_str() ) < 0 ;
}
bool operator < ( const char * str ) const
{
return this->compare( str ) < 0 ;
}
bool operator > ( const String & str ) const
{
return this->compare( str.c_str() ) > 0 ;
}
bool operator > ( const char * str ) const
{
return this->compare( str ) > 0 ;
}
bool operator <= ( const String & str ) const
{
return this->compare( str.c_str() ) <= 0 ;
}
bool operator <= ( const char * str ) const
{
return this->compare( str ) <= 0 ;
}
bool operator >= ( const String & str ) const
{
return this->compare( str.c_str() ) >= 0 ;
}
bool operator >= ( const char * str ) const
{
return this->compare( str ) >= 0 ;
}
// String += ( will use underline append operation )
String & operator += ( const char * suffix )
{
assert( suffix );
this->append ( suffix );
return *this;
}
String & operator += ( int single )
{
this->append (single);
return *this;
}
String& operator += ( const String & suffix )
{
this->append (suffix);
return *this;
}
char operator[]( unsigned k ) const; // Accessor operator[]
char & operator[]( unsigned k ); // Mutator operator[]
/**
* Method to reserve a big amount of data when we know we'll need it.
* Be aware that this function clears the content of the dtring if any exists.
*/
void reserve ( size_t size );
/**
* New size computation. It is simplistic right now : it returns twice the amount
* we need
*/
size_t assign_new_size (size_t minimum_to_allocate)
{
return minimum_to_allocate << 1;
}
/**
* convert String into a classic char *
*/
const char * c_str( ) const
{
return this->buffer;
}
/**
* check whether String is empty
*/
bool isEmpty () const
{
return this->length () ? false : true;
}
/**
* Whether String is null
*
* @return true if is null.
*/
inline bool operator!(void) const
{
return this->isNull();
}
/**
* return true if the characters is NULL
*/
bool isNull () const
{
return this->buffer == NULL;
}
// Return String length
size_t length( ) const
{
return strLength ? strLength : 0;
}
// Return String allocated size
size_t getSize( ) const
{
return bufferLength ? bufferLength : 0;
}
// single char extraction
const char& at ( size_t index ) const
{
assert( index < this->length () );
return this->buffer [index];
}
enum { MAX_LENGTH = 2048 }; // Maximum length for input String
String & append ( const char *suffix )
{
return this->append( suffix, strlen(suffix) );
}
String & append( const char* str, size_t len );
// append function for another String
String & append ( const String & suffix )
{
return this->append ( suffix.c_str (), suffix.length() );
}
// append for a single char.
String & append(int c);
/**
* Insert c_String into a String.
*
* @param start starting offset to insert at.
* @param cstring to insert
*/
void insert(size_t start, const char *str);
/**
* Insert other String into a String.
*
* @param start String offset to insert at.
* @param str String to insert.
*/
void insert(size_t start, const String &str);
/**
* Insert c_String into a String.
*
* @param start starting offset to insert at.
* @param cstring to insert
* @param len length of the String
*/
void insert(size_t start, const char *str, size_t len );
/**
* Retrieves a subString from this instance.
* The subString starts at a specified character position, and extracted to the end
*/
inline String subString( size_t start ) const
{
return subString ( start, this->strLength );
}
/**
* Return a new String that contains a specific subString of the
* current String.
*
* @return new String.
* @param start starting offset for extracted subString.
* @param len length of subString.
*/
inline String subString( size_t start, size_t len) const
{
return String( *this, start, len );
};
/**
* Returns a copy of this String in uppercase.
* @return new String in upper case
*/
String toUpper();
/**
* Returns a copy of this String in lower.
* @return new String in lower case
*/
String toLower();
/**
* Removes all space from both the start and end of this instance.
*/
inline String & trim( )
{
return this->trim( " " );
}
/**
* Removes all occurrences of a set of characters specified in an array from both the start and end of this instance.
*
* @param trimChars - An char to be removed or a null reference.
* @return The String that remains after all occurrences of the characters in trimChars are removed from both the start and end.
* If trimChars is a null reference, white space characters are removed instead.
*/
inline String & trim(const char *trimChars)
{
return this->trimEnd( trimChars ).trimStart( trimChars );
}
/**
* Removes all space from the end of this instance.
*/
inline String & trimEnd( )
{
return this->trimEnd( " " );
}
/**
* Removes all occurrences of a set of characters specified in an array from the end of this instance.
*
* @param trimChars - An char to be removed or a null reference.
* @return The String that remains after all occurrences of the characters in trimChars are removed from the end.
* If trimChars is a null reference, white space characters are removed instead.
*/
String & trimEnd( const char * trimChars );
/**
* Removes all space from the start of this instance.
*/
inline String & trimStart( ) { return trimStart( " " ); }
/**
* Removes all occurrences of a set of characters specified in an array from the beginning of this instance.
*
* @param trimChars - An char to be removed or a null reference.
* @return The String that remains after all occurrences of characters in trimChars are removed from the beginning.
* If trimChars is a null reference, white space characters are removed instead.
*/
String & trimStart( const char *trimChars );
/**
* Removes a specified number of characters from this instance beginning at a specified position.
*
* @param startIndex - The position in this instance to begin deleting characters.
* @param count number of characters to erase.
* @return current String instance less count number of characters.
*/
void remove( size_t startIndex, size_t count );
String & replace ( const char * oldValue, const char * newValue )
{
if ( oldValue && newValue )
{
int index = 0;
while ( ( index = indexOf( oldValue, index ) ) != -1 )
{
replace ( index, strlen( oldValue ), newValue );
index++;
}
}
return *this;
}
/**
* Replace text at a specific position in the String with new
* text.
*
* @param startIndex - starting offset to replace at.
* @param len - length of text to remove.
* @param text - text to replace with.
* @param count - size of replacement text.
*/
String & replace(size_t startIndex, size_t len, const char *text, size_t count );
/**
* Replace text at a specific position in the String with new
* String,
*
* @param startIndex starting offset to replace at.
* @param len length of text to remove.
* @param replStr String to replace with.
*/
String & replace( size_t startIndex, size_t len, const String &replStr );
/**
* get total number of occurrence of chars that appears in the String
* @param chars - pattern String
* @return total number of occurrence
*/
int getOccurrence( const char * chars ) const;
/**
* Reports the index of the first occurrence of a String with length len, or one or more characters, within this instance.
* @param chars - NULL terminated char String
* @return The index position of value if that character is found, or -1 if it is not.
*/
int indexOf( const String& str ) const { return indexOf( str.c_str() ); }
/**
* Reports the index of the first occurrence of a String with length len, or one or more characters, within this instance.
* @param chars - NULL terminated char String
* @return The index position of value if that character is found, or -1 if it is not.
*/
int indexOf( const char * chars ) const { return chars ? indexOf( chars, 0 ) : -1; }
/**
* Reports the index of the first occurrence of a String with length len, or one or more characters, within this instance.
* @param chars - NULL terminated char String
* @param startIndex - start position of the String for searching
* @return The index position of value if that character is found, or -1 if it is not.
*/
int indexOf( const char * chars, size_t startIndex ) const { return indexOf( chars, startIndex, chars? strlen(chars) : 0 ); }
/**
* Reports the index of the first occurrence of a String with length len, or one or more characters, within this instance.
* @param chars - NULL terminated char String
* @param startIndex - The search starting position.
* @param len - length of the String for search
* @return The index position of value if that character is found, or -1 if it is not.
*/
int indexOf( const char * chars, size_t startIndex, size_t len ) const ;
/**
* String Pattern Match with Boyer-Moore algorithm, which is
* considered as the most efficient algorithm in usual applications.
*
* @param pattern - NULL terminated pattern String ( can also be called keyword )
* @param len - length of the pattern String
* @param startIndex - the start position of the String for searching.
* @return The index position of value if pattern String is found, or -1 if it is not.
*/
int BoyerMooreSearch( const char * str, const char* pattern, size_t len ) const;
/**
* Re-allocate buffer space for String.
* resize methods changes the size of the String buffer to the given size.
* size can be any size, larger or smaller than the original.
* If len is zero, then the String becomes a null String.
*
* @param size new size to use.
* @example
* str = "Hello world";
* str.resize(6);
* // str == "Hello"
* str.resize(80);
* // str == "Hello"
*/
void resize(size_t newSize );
// Releases the memory that not used by String, to save memory.
void squeeze ();
/**
* whether the String is a number
*/
bool isNumber() const
{
bool ret = true;
for ( unsigned i=0; i<this->strLength; i++)
{
if ( !isdigit( (unsigned char)buffer[i] ) )
{
ret = false;
break;
}
}
return ret;
}
/**
* Reset the String to an empty String.
*/
void empty ()
{
if ( this->bufferLength )
{
this->buffer[0] = 0;
this->strLength = 0;
}
}
private:
char *buffer; // storage for characters
size_t strLength; // length of String (# of characters)
size_t bufferLength; // capacity of buffer
// Internal function that clears the content of a String
void emptyIt ()
{
if ( this->bufferLength )
{
free( this->buffer );
}
this->init();
}
/**
* Internal function that initialize the String object
*/
void init();
/**
* A derivable low level comparison operator. This can be used
* to create custom comparison data types in derived String
* classes.
*
* @return 0 if match, or value for ordering.
* @param text text to compare.
* @param len length of text to compare.
* @param index offset from start of String, used in searchs.
*/
inline int compare(const char *text, size_t len = 0, size_t index = 0) const
{
if(!text)
{
text = "";
}
return index > this->strLength ? -1 : len ? strncmp( this->buffer + index, text, len) : strcmp( this->buffer + index, text);
}
};
ostream & operator<<( ostream & out, const String & str ); // Output
istream & operator>>( istream & in, String & str ); // Input
istream & getline( istream & in, String & str ); // Read line
String operator + ( const String &s1, const char c2 );
String operator + ( const char c1, const String &s2);
String operator + ( const String &s1, const String &s2 );
String operator + ( const String &s1, const char *s2 );
String operator + ( const char *s1, const String &s2 );
String operator + ( const String & s1, const char* s2 );
String operator + (const char* s1, const String & s2);
#endif