-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathString.cpp
540 lines (493 loc) · 13.4 KB
/
String.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
// =====================================================================
// Includes
// =====================================================================
#include "String.h"
// =====================================================================
// Definition of static attribute
// =====================================================================
const size_t String::MAX_SIZE = 100;
// =====================================================================
// Constructors
// =====================================================================
/**
* \brief Defaut Constructor
* \details default
* \param void
* \return \e void
*/
String::String(){
size_=0;
capacity_=getCapacity(0);
data_=new char[capacity_+1];
data_[size_]='\0';
}
/**
* \brief Fill Constructor
* \details repetition of a single character
* \param size_t n as length of the String
* \param char c as repeted character
* \return \e void
*/
String::String(size_t n , char c){
if (n>MAX_SIZE) {
printf("Warning: input size exceed the MAX_SIZE,"
"it will be shortened to MAX_SIZE which is %lu \n"
,MAX_SIZE);
n=MAX_SIZE;
}
size_=n;
capacity_=getCapacity(n);
data_=new char[capacity_+1];
for (unsigned int i =0; i<n; i++){
data_[i]=c;
}
data_[n]='\0';
}
/**
* \brief Copy Constructor
* \details
* \param const String& s as copied String
* \return \e void
*/
String::String (const String& str){
size_=str.size();
capacity_=getCapacity(size_);
data_= new char[capacity_+1];
for(unsigned int i=0; i<size_;i++)
data_[i]=str[i];
data_[size_]='\0';
}
/**
* \brief Constructor from c-string
* \details from a null-terminated c-string
* \param char* str_in
* \return \e void
*/
String::String(char* str_in){
size_=length(str_in);
capacity_=getCapacity(size_);
data_=new char[capacity_+1];
for (unsigned int i = 0; i < size_; i++)
data_[i]=str_in[i];
data_[size_]='\0';
}
// =====================================================================
// Destructor
// =====================================================================
/**
* \brief Destructor
* \details
* \param void
* \return \e void
*/
String::~String()
{
delete[] data_;
data_=nullptr;
}
// =====================================================================
// Protected Methods
// =====================================================================
/**
* \brief getCapacity
* \details calculate capacity from a given string size
* \param size_t size
* \return size_t size
*/
size_t String::getCapacity(size_t size){
size=size*2;
if (size>(MAX_SIZE/2))
return MAX_SIZE;
else
return size;
}
/**
* \brief length
* \details get the length of a null-terminated chain
* \param const char* s
* \return size_t len
*/
size_t String::length(const char* s){
size_t len=0;
while (s[len] != '\0'){
if (len>= MAX_SIZE){
printf("Warning : c_str input is too long and will be"
"shortened to MAX_SIZE which is %lu\n", MAX_SIZE);
break;
}
len++;
}
return len;
}
// =====================================================================
// Public Methods
// =====================================================================
/**
* \brief c_str
* \details return a pointer to an array that contains a null terminated
* sequence of character (a c-string) representing the current value of
* the String object
* \param void
* \return char* data_
*/
const char* String::c_str() const noexcept{
return data_;
}
/**
* \brief clear
* \details erase the content of the String wich become an empty String
* with a length of 0 characters
* \param void
* \return void
*/
void String::clear() noexcept{
data_[0]='\0';
size_=0;
}
/**
* \brief empty
* \details whether the String is empty or not, true if the String
* length is 0, false otherwise
* \param void
* \return bool
*/
bool String :: empty() const noexcept{
if (size_==0){
return true;
}
else {
return false;
}
}
/**
* \brief reserve
* \details adapt the String capacity_ to a length up to n characters
* If n is greater than the current string size while smaller than
* MAX_SIZE, the capacity is set to n
* If its smaller than size then the capacity is set to size
* \param size_t n
* \return void
*/
void String :: reserve (size_t n){
if (n>size_){// do nothing if not
if (n>MAX_SIZE){
printf("Warning(reserve): can't allow required capacity\n");
//When requied size exceed, refuse and do nothing
} else {
//realloc
char* new_data = new char[n+1];
for (unsigned int i =0; i<=size_; i++)
new_data[i]=data_[i];
delete[] data_;
data_=new_data;
capacity_=n;
}
}
}
/**
* \brief reserve overload
* \details set the capacity to size
* \param void
* \return void
*/
void String :: reserve (){
char* new_data;
new_data = new char[size_];
for (unsigned int i =0; i<size_; i++){
new_data[i]=data_[i];
}
delete[] data_;
data_=new_data;
capacity_=size_;
}
/**
* \brief resize
* \details resize a String to length of count character, complete with
* space if needed
* \param size_t count
* \return void
*/
void String::resize(size_t count){
if (count > MAX_SIZE){
printf("Warning (String::resize): requied size exceed MAX_SIZE "
"and it will be shortened to MAX_SIZE which is %zu\n",
MAX_SIZE);
count=MAX_SIZE;
}
if (count > size_){
if (count < capacity_) {
for (size_t i=size_; i<count; i++)
data_[i]=' ';
data_[count]='\0';
size_=count;
} else {
char* nptr= new char [getCapacity(count)+1];
for (unsigned int i =0; i<size_; i++)
nptr[i]=data_[i];
for (size_t i=size_; i<count; i++)
nptr[i]=' ';
nptr[count]='\0';
delete[] data_;
data_=nptr;
size_=count;
}
} else {
data_[count]='\0';
size_=count;
}
}
/**
* \brief resize
* \details resize a String to length of count character, complete with
* character c if needed
* \param size_t count, char c
* \return void
*/
void String::resize(size_t count, char c){
if (count > MAX_SIZE){
printf("Warning (String::resize): requied size exceed MAX_SIZE "
"and it will be shortened to MAX_SIZE which is %zu\n",
MAX_SIZE);
count=MAX_SIZE;
}
if (count > size_){
if (count < capacity_) {
for (unsigned int i=size_; i< count; i++)
data_[i]=c;
data_[count]='\0';
size_=count;
} else {
char* nptr=new char [count+1];
for (unsigned int i=0; i<size_; i++)
nptr[i]=data_[i];
for (size_t i=size_; i< count; i++)
nptr[i]=c;
nptr[count]='\0';
delete[] data_;
data_=nptr;
size_=count;
}
} else {
data_[count]='\0';
size_=count;
}
}
// =====================================================================
// Getters
// =====================================================================
/**
* \brief length
* \details get the length of a String
* \param void
* \return size_t
*/
size_t String::length() const noexcept{
return size_;
}
/**
* \brief length
* \details get the maximum length the String can reach
* \param void
* \return size_t
*/
size_t String::max_size() const noexcept{
return MAX_SIZE;
}
/**
* \brief size
* \details get the length of a String
* \param void
* \return size_t
*/
size_t String::size()const noexcept {
return size_;
}
/**
* \brief capacity
* \details get the size of the storage space currently allocated for
* the String
* \param void
* \return size_t
*/
size_t String::capacity() const noexcept{
return capacity_;
}
// =====================================================================
// Operators' definitions
// =====================================================================
/**
* \brief operator+ overloading
* \details returns a newly constructed String object with its value
* being the concatenation of the characters in a String A followed by
* those of String B
* \param String&A
* \param String&B
* \return String
*/
String operator+(const String& lhs,const String& rhs){
String result;
if (lhs.size_+rhs.size_>String :: MAX_SIZE){ //why String::MAX_SIZE
printf("Warning (operator+()):too long, "
"taking only left hand side");
result.size_=lhs.size_;
result.capacity_=lhs.capacity_;
delete[] result.data_;
result.data_=new char [result.capacity_+1];
for (unsigned int i=0; i<result.size_; i++)
result.data_[i]=lhs.data_[i];
} else {
result.size_=lhs.size_+rhs.size_;
result.capacity_=result.getCapacity(result.size_);
delete[] result.data_;
result.data_=new char [result.capacity_+1];
for (unsigned int i=0; i<lhs.size(); i++)
result.data_[i]=lhs.data_[i];
for (unsigned int i=lhs.size(); i<=result.size(); i++)
result.data_[i]=rhs.data_[i-lhs.size_];
//result.data_[result.size_]='\0';
}
return result;
}
/**
* \brief operator+ overloading
* \details returns a newly constructed String object with its value
* being the concatenation of the characters in a String lhs followed
* by those of c_string rhs
* \param String& lhs
* \param char* rhs
* \return String
*/
String operator+(const String& lhs, const char* rhs){
String result;
int rhs_length = result.length(rhs);
//In case that string is super long
if (lhs.length()+rhs_length > String::MAX_SIZE ) {
printf("Warning (operator+): strings concatenation size "
"out of range, taking only left hand side\n");
result=lhs;
return result;
} else {
result.size_=lhs.length()+rhs_length;
result.capacity_=result.getCapacity(result.size_);
result.data_= new char[result.capacity_ + 1];
for (unsigned int i=0; i < lhs.length(); i++)
result.data_[i]=lhs.data_[i];
for (int i=0; i < rhs_length; i++)
result.data_[i+lhs.length()]=rhs[i];
result.data_[result.size_]='\0';
// Need to give '\0' manually in case str.len()=0 and c_str=100
// and not null-terminated
// Here, we need to make a choice bewteen lhs.length() and
// lhs.size_
// lhs.length() is a legal public method which not requires
// make friend of 2 objects
// On the other hand, it will be called at each turn in the for
// loop, which could be a lot if string is long.
return result;
}
}
/**
* \brief operator+ overloading
* \details returns a newly constructed String object with its value
* being the concatenation of the characters in a String lhs followed
* by those of c_string rhs
* \param String& lhs
* \param char* rhs
* \return String
*/
String operator+(const char* lhs, const String& rhs){
String result;
int lhs_length = result.length(lhs);
//In case that string is super long
if (lhs_length+rhs.length() > String::MAX_SIZE ) {
printf("Warning(operator+): strings concatenation size out "
"of range, taking only left hand side\n");
result=lhs;
return result;
} else {
result.size_=rhs.length()+lhs_length;
result.capacity_=result.getCapacity(result.size_);
result.data_= new char[result.capacity_ + 1];
for (unsigned int i=0; i < lhs_length; i++)
result.data_[i]=lhs[i];
for (int i=0; i <= rhs.length(); i++)
result.data_[i+lhs_length]=rhs.data_[i];
// Here, we need to make a choice bewteen lhs.length() and
// lhs.size_
// lhs.length() is a legal public method which not requires
// make friend of 2 objects
// On the other hand, it will be called at each turn in the for
// loop, which could be a lot if string is long.
return result;
}
}
/**
* \brief operator+ overloading
* \details returns a newly constructed String object with its value
* being the concatenation of the characters in a String s followed by
* c character
* \param String&s
* \param char c
* \return String
*/
String operator+(const String& s, const char c){
String result;
if(s.size()==String::MAX_SIZE){
printf("Warning(operator +): "
"MAX_SIZE achived, failed add one more char\n");
result=s;
return result;
}else{
result.size_=s.size_+1;
result.capacity_=result.getCapacity(result.size_);
result.data_=new char[result.capacity_+1];
for(unsigned int i=0; i<s.size_;i++)
result.data_[i]=s.data_[i];
result.data_[s.size_]=c;
result.data_[s.size_+1]='\0';
return result;
}
}
String operator+(const char c, const String& s){
String result;
if(s.size()==String::MAX_SIZE){
printf("Warning(operator +): "
"failed to add a max-sized string\n");
result=c;
return result;
}else{
result.size_=s.size_+1;
result.capacity_=result.getCapacity(result.size_);
result.data_=new char[result.capacity_+1];
result.data_[0]=c;
for(unsigned int i=1; i<=s.size_;i++)
result.data_[i]=s.data_[i];
result.data_[s.size_+1]='\0';
return result;
}
}
/**
* \brief operator[] overload
* \details returns a reference to the character at position pos in the
* String
* \param int pos
* \return char&
*/
char& String::operator[] (size_t pos){
if (pos<=MAX_SIZE)
return data_[pos];
else{
printf("Warning(operator[]): "
"Requied position out of range, returning last char\n");
return data_[MAX_SIZE];//Haven't found a better solution;
}
}
const char& String::operator[] (size_t pos) const{
if (pos<=MAX_SIZE)
return data_[pos];
else{
printf("Warning(operator[]): "
"Requied position out of range, returning last char\n");
return data_[MAX_SIZE];//Haven't found a better solution;
}
}