-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
173 lines (171 loc) · 3.6 KB
/
String.cpp
File metadata and controls
173 lines (171 loc) · 3.6 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
#include "String.h"
String::String()
{
this->length = 0;
this->buff = (char*)malloc(sizeof(char));
}
String::String(const String& object)
{
this->length = object.length;
this->buff = (char*)malloc(sizeof(char) * this->length + 1);
memmCpy(this->buff, this->length, object.buff, object.length);
this->buff[this->length] = '\0';
}
String::String(char ch)
{
this->length = 1;
this->buff = (char*)malloc(sizeof(char) * this->length + 1);
(this->buff)[0] = ch;
(this->buff)[1] = 0;
}
String::String(const char* str)
{
this->length = this->strLen(str);
this->buff = (char*)malloc(sizeof(char) * this->length + 1);
this->memmCpy(this->buff, this->length, str, this->length);
this->buff[this->length] = '\0';
}
int String::replaceAll(char ch, char c)
{
int count = 0;
for (int i = 0; i < this->length; i++)
{
if (this->buff[i] == ch)
{
this->buff[i] = c;
count++;
}
}
return count;
}
String String::subString(int startIndex, int endIndex)
{
//不合法 抛空指针
if (startIndex<0 || startIndex>length ||
endIndex<0 || endIndex>length ||
endIndex < startIndex)
{
return *(String*)0;
}
String desc;
desc.length = endIndex - startIndex;
desc.alloc(endIndex - startIndex);
desc.memmCpy(desc.buff, endIndex - startIndex, (const char*)(this->buff + startIndex), endIndex - startIndex);
desc.buff[desc.length] = '\0';
return desc;
}
const char* String::getCstr()
{
return this->buff;
}
bool String::isEmpty()
{
if (length == 0)
return true;
return false;
}
bool String::equals(const String& desc)
{
if (this->length != desc.length)
return false;
for (int i = 0; i < desc.length; i++)
{
if (this->buff[i] != desc.buff[i])
return false;
}
return true;
}
bool String::equals(const char* str)
{
int lenth = String::strLen(str);
if (this->length != lenth)
return false;
for (int i = 0; i < this->length; i++)
{
if (this->buff[i] != str[i])
return false;
}
return true;
}
String& String::operator+(char ch)
{
this->alloc(this->length + 1);
this->memmCpy(buff + length, 1, &ch, 1);
return *this;
}
String& String::operator+(char* cstr)
{
int cStrLength = strLen(cstr);
this->alloc(this->length + cStrLength);
this->memmCpy(buff + length, cStrLength, cstr, cStrLength);
return *this;
}
String& String::operator+(String& obj)
{
int tempLength = obj.length + this->length + 1;
char* tempBuffer = (char*)malloc(sizeof(char) * tempLength);
memmCpy(tempBuffer, tempLength, this->buff, this->length);
memmCpy(tempBuffer + this->length, tempLength - (this->length), obj.buff, obj.length);
//释放buffer
this->destroy();
tempBuffer[tempLength] = 0;
this->buff = tempBuffer;
this->length = tempLength;
return *this;
}
bool String::operator==(const String& cmO)
{
return this->equals(cmO);
}
String::~String()
{
if ((this->buff))
{
free(this->buff);
this->buff = NULL;
this->length = 0;
}
}
String String::operator=(char* str)
{
String t(str);
return t;
}
void String::memmCpy(char* desc, int dLength, const char* from, int fLength)
{
for (int in = 0; in < fLength; in++)
{
//截断 防止OOM
if (dLength == in)
{
desc[dLength + 1] = 0;
break;
}
desc[in] = from[in];
}
}
int String::strLen(const char* str)
{
int length = 0;
while (str[length] != '\0')
length++;
return length;
}
void String::destroy()
{
if ((this->buff))
{
free(this->buff);
this->buff = NULL;
this->length = 0;
}
}
void String::alloc(size_t size)
{
int fromlength = this->length;
char* descBuffer = (char*)malloc(sizeof(char) * (this->length + (int)size) + 1);
this->memmCpy(descBuffer, (this->length + (int)size), this->buff, this->length);
this->destroy();
this->buff = descBuffer;
this->length = fromlength;
}