You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
5.0 KiB
181 lines
5.0 KiB
//
|
|
// Created by lenfrex on 2022/4/4.
|
|
//
|
|
|
|
#include <cstring>
|
|
#include <algorithm>
|
|
|
|
#include "CString.h"
|
|
|
|
/**
|
|
* 按照给定大小来初始化空的字符串数据
|
|
*/
|
|
CString::CString(int size) : length(0), allocSize(size) {
|
|
data = new char[size]();
|
|
}
|
|
|
|
/**
|
|
* 不设定初始化大小就默认512个字符来初始化
|
|
*/
|
|
CString::CString() : CString(512) {}
|
|
|
|
/**
|
|
* 从源中复制字符串来构建新的对象
|
|
*/
|
|
CString::CString(const char *source) : CString(source, (int) strlen(source)) {}
|
|
|
|
/**
|
|
* 从源中复制字符串来构建新的对象,并定义初始长度,小于源字符串则会被截取
|
|
*/
|
|
CString::CString(char *source, int length) : CString(length + 1) {
|
|
strncpy(data, source, length);
|
|
data[length] = '\0';
|
|
|
|
this->length = length;
|
|
}
|
|
|
|
CString::~CString() {
|
|
delete[] data;
|
|
data = nullptr;
|
|
}
|
|
|
|
CString::CString(const CString &source) : CString(source.data) {}
|
|
|
|
/**
|
|
* 重载等号。借鉴了Java中String类的equal()方法的实现思路。
|
|
* 首先判断data是否为nullptr,同为null则true,否则就是false
|
|
* 再比较两字符串长度,如果长度不一样不用说,直接返回false;
|
|
* 相同的话再比较两字符串数据指针指向地址是否一样,一样就代表指向的是同一块内存,直接返回true
|
|
* 两者都不同才进行逐字比较
|
|
*/
|
|
bool CString::operator==(const CString &source) const {
|
|
// 首先判断两CString对象data成员是否为空指针
|
|
if (source.data == nullptr || this->data == nullptr) {
|
|
return source.data == nullptr && this->data == nullptr;
|
|
}
|
|
|
|
// 字符串长度不等,字符串肯定是不等
|
|
if (length != source.length) {
|
|
return false;
|
|
} else {
|
|
// 长度相等且data指向相同肯定等
|
|
if (source.data == this->data) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// 以上情况都不符合时才进行逐字比较
|
|
// 能执行到此处两者长度肯定相等,因此访问数组不会越界
|
|
for (int i = 0; i < length; ++i) {
|
|
if (data[i] != source.data[i]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
CString &CString::operator=(const CString &source) {
|
|
// 处理自己给自己赋值的情况,那叫啥,自赋值
|
|
if (this == &source) {
|
|
return *this;
|
|
}
|
|
|
|
// 等号右侧空指针的话直接data赋为空指针
|
|
if (source.data == nullptr) {
|
|
delete[] data;
|
|
this->data = nullptr;
|
|
length = 0;
|
|
allocSize = 0;
|
|
|
|
return *this;
|
|
}
|
|
|
|
int sourceLength = (int) strlen(source.data);
|
|
|
|
// 如果源字符串长度超过了本对象已分配的字符数组大小则重新分配长度合适的字符数组
|
|
if (allocSize < sourceLength) {
|
|
allocSize = sourceLength + 1;
|
|
|
|
delete[] data;
|
|
data = new char[allocSize];
|
|
}
|
|
|
|
strcpy(data, source.data);
|
|
length = sourceLength;
|
|
|
|
return *this;
|
|
}
|
|
|
|
CString &CString::operator+=(const CString &source) {
|
|
// nullptr不做处理
|
|
if (source.data == nullptr) {
|
|
return *this;
|
|
}
|
|
|
|
// 本对象nullptr的话就直接新分配
|
|
if (data == nullptr) {
|
|
allocSize = source.length + 2 + 1;
|
|
this->data = new char[allocSize]();
|
|
|
|
length = source.length;
|
|
} else {
|
|
length = (int) strlen(data);
|
|
}
|
|
|
|
// 如果拼接后字符长度超过已分配的内存大小,则重新分配data内存大小
|
|
if (allocSize < (length + source.length + 2)) {
|
|
// 保存拼接前的原数据,以便重新分配足够的内存
|
|
char *old = new char[length + 1]();
|
|
strcpy(old, data);
|
|
|
|
// 重新分配内存,防止拼接后溢出,2为两个横杠大小
|
|
length += source.length + 2;
|
|
allocSize = length + 1;
|
|
|
|
delete[] data;
|
|
this->data = new char[allocSize]();
|
|
|
|
strcpy(data, old);
|
|
}
|
|
|
|
strcat(data, "--");
|
|
strcat(data, source.data);
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* 按照字典顺序对比
|
|
*/
|
|
bool CString::operator>(const CString &compareString) const {
|
|
// 首先判断两CString对象data成员是否为空指针,空直接返回false
|
|
if (compareString.data == nullptr || this->data == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
// 两对象data成员首指向相同则对比谁长
|
|
if (compareString.data == this->data) {
|
|
return length > compareString.length;
|
|
}
|
|
|
|
// 以上情况都不符合时才进行比较
|
|
return strcoll(this->data, compareString.data) > 0;
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &outputStream, const CString &source) {
|
|
return outputStream << source.data;
|
|
}
|
|
|
|
std::istream &operator>>(std::istream &inputStream, CString &source) {
|
|
// 使用istream.get(char*, int)可以限制读入的长度,防止输入超长的字符串导致溢出,但是空格也会被读入
|
|
// 手动分割太麻烦,因此只好先用这种方法了
|
|
inputStream >> source.data;
|
|
source.length = (int) strlen(source.data);
|
|
|
|
return inputStream;
|
|
}
|
|
|
|
char CString::operator[](int i) {
|
|
return (i < length && i >= 0) ? data[i] : '\0';
|
|
}
|
|
|
|
|