-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplestring.cpp
More file actions
73 lines (59 loc) · 1.28 KB
/
simplestring.cpp
File metadata and controls
73 lines (59 loc) · 1.28 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
/*
*For practicing the exercises of The C++ Programming language
*Author: Tao HE
*Date: May 3th, 2014
*
*/
#include "simplestring.h"
using namespace std;
using namespace simplestd;
String::String(){
rep = new Srep(0,"");
}
String::String(const String &x){
x.rep->n++;
rep = x.rep;
}
String::~String(){
if(--rep->n == 0) delete rep;
}
String& String::operator =(const String &x){
x.rep->n++;
if(--rep->n == 0) delete rep;
rep = x.rep;
return *this;
}
String::String(const char* s){
rep = new Srep(strlen(s),s);
}
String& String::operator =(const char* s){
if(rep->n == 1)
rep->assign(strlen(s),s);
else{
rep->n--;
rep = new Srep(strlen(s),s);
}
return *this;
}
String& String::operator +=(const String &x){
if(x.rep->sz == 0)
return *this;
int len = size() + x.size();
char *p = new char[len+1];
strcpy(p,rep->s);
strcat(p,x.rep->s);
if(--rep->n == 0) delete rep;
rep = new Srep(len,p);
return *this;
}
String& String::operator +=(const char *s){
if(strlen(s) == 0)
return *this;
int len = size() + strlen(s);
char *p = new char[len+1];
strcpy(p,rep->s);
strcat(p,s);
if(--rep->n == 0) delete rep;
rep = new Srep(len,p);
return *this;
}