|
|
|
@ -77,14 +77,7 @@ bool String::equals(const char *source, int sourceLength) const { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 以上情况都不符合时才进行逐字比较
|
|
|
|
|
// 能执行到此处两者长度肯定相等,因此访问数组不会越界
|
|
|
|
|
for (int i = 0; i < length; ++i) { |
|
|
|
|
if (data[i] != source[i]) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
return strcmp(this->data, source) == 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String &String::operator=(const String &source) { |
|
|
|
@ -98,6 +91,15 @@ String &String::operator=(const String &source) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String &String::operator=(const char *source) { |
|
|
|
|
if (source == nullptr) { |
|
|
|
|
delete[] data; |
|
|
|
|
this->data = nullptr; |
|
|
|
|
length = 0; |
|
|
|
|
allocSize = 0; |
|
|
|
|
|
|
|
|
|
return *this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
copyData(source, (int) strlen(source)); |
|
|
|
|
|
|
|
|
|
return *this; |
|
|
|
@ -144,7 +146,7 @@ void String::addString(const char *source, int sourceLength) { |
|
|
|
|
|
|
|
|
|
// 本对象nullptr的话就直接新分配
|
|
|
|
|
if (data == nullptr) { |
|
|
|
|
allocSize = sourceLength + 2 + 1; |
|
|
|
|
allocSize = sourceLength + 1; |
|
|
|
|
this->data = new char[allocSize](); |
|
|
|
|
|
|
|
|
|
length = sourceLength; |
|
|
|
@ -153,13 +155,13 @@ void String::addString(const char *source, int sourceLength) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 如果拼接后字符长度超过已分配的内存大小,则重新分配data内存大小
|
|
|
|
|
if (allocSize < (length + sourceLength + 2)) { |
|
|
|
|
if (allocSize < (length + sourceLength + 1)) { |
|
|
|
|
// 保存拼接前的原数据,以便重新分配足够的内存
|
|
|
|
|
char *old = new char[length + 1](); |
|
|
|
|
strcpy(old, data); |
|
|
|
|
|
|
|
|
|
// 重新分配内存,防止拼接后溢出,2为两个横杠大小
|
|
|
|
|
length += sourceLength + 2; |
|
|
|
|
length += sourceLength; |
|
|
|
|
allocSize = length + 1; |
|
|
|
|
|
|
|
|
|
delete[] data; |
|
|
|
@ -169,6 +171,7 @@ void String::addString(const char *source, int sourceLength) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
strcat(data, source); |
|
|
|
|
length += sourceLength; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -202,26 +205,25 @@ std::istream &operator>>(std::istream &inputStream, String &source) { |
|
|
|
|
return inputStream; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char String::operator[](int i) { |
|
|
|
|
return (i < length && i >= 0) ? data[i] : '\0'; |
|
|
|
|
char &String::operator[](int i) { |
|
|
|
|
return (i < length && i >= 0) ? data[i] : data[0]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加号运算重载,注意,每次加号运算都会产生一个新的对象 |
|
|
|
|
* 这里不用引用返回是因为返回局部对象不是安全的 |
|
|
|
|
*/ |
|
|
|
|
String &String::operator+(const char *source) { |
|
|
|
|
String String::operator+(const char *source) { |
|
|
|
|
int sourceLength = (int) strlen(source); |
|
|
|
|
|
|
|
|
|
String target(this->length + sourceLength + 1); |
|
|
|
|
target = this->data; |
|
|
|
|
String target(this->data, this->length + sourceLength + 1); |
|
|
|
|
target += source; |
|
|
|
|
|
|
|
|
|
return target; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String &String::operator+(const String &source) { |
|
|
|
|
String target(this->length + source.length); |
|
|
|
|
target = this->data; |
|
|
|
|
String String::operator+(const String &source) { |
|
|
|
|
String target(this->data, this->length + source.length); |
|
|
|
|
target += source; |
|
|
|
|
|
|
|
|
|
return target; |
|
|
|
|