// CString.h
#pragma once
#include <assert.h>
#include <string.h>
class CString {
public:
CString() : m_pData(nullptr), m_nLength(1){ Init(nullptr, 1); }
CString(const char ch, int nRepeat) : m_pData(nullptr), m_nLength(1) { Fill(ch, nRepeat); }
CString(const char* cstr) : m_pData(nullptr), m_nLength(1) { cstr == nullptr ? Init(nullptr, 1) : Init(cstr, strlen(cstr)); }
CString(const char* cstr, int nLen) : m_pData(nullptr), m_nLength(1) { cstr == nullptr ? Init(nullptr, 1) : Init(cstr, nLen); }
CString(const CString& str) : m_pData(nullptr), m_nLength(1) { Init(str, str.GetLength()); }
~CString() { delete[]m_pData; }
inline const int GetLength() const { return m_nLength; }
inline operator const char* () const { return m_pData; }
inline const char operator [](int nIndex) const //获取串内上的某个字符
{
assert(nIndex > -1 && nIndex < GetLength());
return m_pData[nIndex];
}
inline char& operator [](int nIndex) //获取串内上的某个字符
{
assert(nIndex > -1 && nIndex < GetLength());
return m_pData[nIndex];
}
void clear() {
if (m_pData == nullptr) return;
*m_pData = '\0';
m_nLength = 0;
}
CString& Trim(char ch = ' ');
CString& TrimRight(char ch = ' ');
CString& TrimLeft(char ch = ' ');
CString operator++(int);
CString& operator++();
CString& operator=(const char* cstr); //删除已有文字的堆空间,重新分配
CString& operator=(const CString& str); //删除已有文字的堆空间,重新分配
CString& operator+=(const char* pszSrc); //删除已有文字的堆空间,重新分配
CString& operator+=(const CString& pszSrc); //删除已有文字的堆空间,重新分配
friend CString operator+(const char* cstr, const CString& str); //新建临时对象并推出到返回值
friend CString operator+(const CString& str, const char* cstr); //新建临时对象并推出到返回值
friend CString operator+(const CString& str1, const CString& str2);//新建临时对象并推出到返回值
friend bool operator==(const CString& str1, const CString& str2);
friend bool operator==(const char* cstr, const CString& str2);
friend bool operator==(const CString& str1, const char* cstr);
friend bool operator!=(const CString& str1, const CString& str2);
friend bool operator!=(const char* cstr, const CString& str2);
friend bool operator!=(const CString& str1, const char* cstr);
friend bool operator<(const CString& str1, const CString& str2);
friend bool operator<(const CString& str1, const CString& str2);
friend bool operator<(const char* cstr, const CString& str2);
friend bool operator<(const CString& str1, const char* cstr);
friend bool operator>(const CString& str1, const CString& str2);
friend bool operator>(const char* cstr, const CString& str2);
friend bool operator>(const CString& str1, const char* cstr);
friend bool operator<=(const CString& str1, const CString& str2);
friend bool operator<=(const char* cstr, const CString& str2);
friend bool operator<=(const CString& str1, const char* cstr);
friend bool operator>=(const CString& str1, const CString& str2);
friend bool operator>=(const char* cstr, const CString& str2);
friend bool operator>=(const CString& str1, const char* cstr);
private:
char* m_pData;
int m_nLength;
void Create(const char* cstr = nullptr, int nLen = 0);
void Init(const char* cstr = nullptr, int nLen = 0);
void Fill(const char ch, int nRepeat);
};
// CString.cpp
#include "CString.h"
void CString::Create(const char* cstr, int nLen)
{
int len = strlen(cstr);
if (nLen < 1) m_nLength = 0;
else if (nLen >= len) m_nLength = len;
else if (1 <= nLen && nLen < len) m_nLength = nLen;
m_pData = new char[len + 1];
strcpy_s(m_pData, len + 1, cstr);
if (1 <= nLen && nLen < len) m_pData[nLen] = '\0';
}
void CString::Init(const char* cstr, int nLen)
{
if (cstr == nullptr) Create("\0", 1);
else Create(cstr, nLen);
}
void CString::Fill(const char ch, int nRepeat)
{
if (nRepeat < 0) nRepeat = 0;
m_nLength = nRepeat;
m_pData = new char[nRepeat + 1];
for (int i = 0; i < nRepeat; i++)
m_pData[i] = ch;
m_pData[nRepeat] = '\0';
}
CString& CString::Trim(char ch)
{
int i = 0;
while (m_pData[i++] == ch);
i--;
strcpy_s(m_pData, m_nLength - i + 1, m_pData + i);
m_nLength -= (i + 1);
i = m_nLength - 1;
while (m_pData[i--] == ch);
i++;
m_nLength = i;
m_pData[m_nLength + 1] = '\0';
return *this;
}
CString& CString::TrimRight(char ch)
{
int i = m_nLength - 1;
while (m_pData[i--] == ch);
i++;
m_nLength = i;
m_pData[m_nLength + 1] = '\0';
return *this;
}
CString& CString::TrimLeft(char ch)
{
int i = 0;
while (m_pData[i++] == ch);
i--;
strcpy_s(m_pData, m_nLength - i + 1, m_pData + i);
m_nLength -= (i + 1);
return *this;
}
CString& CString::operator=(const char* cstr)
{
if (cstr == nullptr) Init();
else {
int nLen = strlen(cstr);
char* pData = new char[nLen + 1];
if (pData == nullptr) return *this;
strcpy_s(pData, nLen + 1, cstr);
m_nLength = nLen;
delete[]m_pData;
m_pData = pData;
}
return *this;
}
CString& CString::operator=(const CString& str)
{
if ((const char*)str == nullptr) return *this;
operator=((const char*)str);
return *this;
}
CString& CString::operator+=(const char* pszSrc)
{
if (pszSrc == nullptr) return *this;
int nLen = strlen(pszSrc);
char* pData = new char[nLen + m_nLength + 1];
if (pData == nullptr) return *this;
strcpy_s(pData, nLen + m_nLength + 1, m_pData);
strcpy_s(pData + m_nLength, nLen + 1, pszSrc);
m_nLength += nLen;
delete[]m_pData;
m_pData = pData;
return *this;
}
CString& CString::operator+=(const CString& pszSrc)
{
if ((const char*)pszSrc == nullptr) return *this;
operator+=((const char*)pszSrc);
return *this;
}
CString CString::operator++(int)
{
CString str;
if (m_nLength == 0) return str;
delete []str.m_pData;
str.m_pData = nullptr;
char* pData = new char[m_nLength + 1];
strcpy_s(pData, m_nLength + 1, m_pData);
for (int i = 0; i < m_nLength; i++)
if (pData[i] + 1 >= 'a' && pData[i] + 1 <= 'z' || pData[i] + 1 >= 'A' && pData[i] + 1 <= 'Z')
pData[i] += 1;
str.m_pData = pData;
return str;
}
CString& CString::operator++()
{
if (m_nLength == 0) return *this;
for (int i = 0; i < m_nLength; i++)
if (m_pData[i] + 1 >= 'a' && m_pData[i] + 1 <= 'z' || m_pData[i] + 1 >= 'A' && m_pData[i] + 1 <= 'Z')
m_pData[i] += 1;
return *this;
}
CString operator+(const char* cstr, const CString& str)
{
CString sum;
sum += cstr, sum += str;
return sum;
}
CString operator+(const CString& str, const char* cstr)
{
CString sum;
sum += str, sum += cstr;
return sum;
}
CString operator+(const CString& str1, const CString& str2)
{
CString sum;
sum += str1, sum += str2;
return sum;
}
bool operator==(const CString& str1, const CString& str2)
{
if (strcmp(str1, str2) == 0) return true;
return false;
}
bool operator==(const char* cstr, const CString& str2)
{
if (strcmp(cstr, str2) == 0) return true;
return false;
}
bool operator==(const CString& str1, const char* cstr)
{
if (strcmp(str1, cstr) == 0) return true;
return false;
}
bool operator!=(const CString& str1, const CString& str2)
{
if (!operator==(str1, str2)) return true;
return false;
}
bool operator!=(const char* cstr, const CString& str2)
{
if (!operator==(cstr, str2)) return true;
return false;
}
bool operator!=(const CString& str1, const char* cstr)
{
if (!operator==(str1, cstr)) return true;
return false;
}
bool operator<(const CString& str1, const CString& str2)
{
if (strcmp(str1, str2) < 0) return true;
return false;
}
bool operator<(const char* cstr, const CString& str2)
{
if (strcmp(cstr, str2) < 0) return true;
return false;
}
bool operator<(const CString& str1, const char* cstr)
{
if (strcmp(str1, cstr) < 0) return true;
return false;
}
bool operator>(const CString& str1, const CString& str2)
{
if (strcmp(str1, str2) > 0) return true;
return false;
}
bool operator>(const char* cstr, const CString& str2)
{
if (strcmp(cstr, str2) > 0) return true;
return false;
}
bool operator>(const CString& str1, const char* cstr)
{
if (strcmp(str1, cstr) > 0) return true;
return false;
}
bool operator<=(const CString& str1, const CString& str2)
{
if (!operator>(str1, str2)) return true;
return false;
}
bool operator<=(const char* cstr, const CString& str2)
{
if (!operator>(cstr, str2)) return true;
return false;
}
bool operator<=(const CString& str1, const char* cstr)
{
if (!operator>(str1, cstr)) return true;
return false;
}
bool operator>=(const CString& str1, const CString& str2)
{
if (!operator<(str1, str2)) return true;
return false;
}
bool operator>=(const char* cstr, const CString& str2)
{
if (!operator<(cstr, str2)) return true;
return false;
}
bool operator>=(const CString& str1, const char* cstr)
{
if (!operator<(str1, cstr)) return true;
return false;
}
// Test.cpp
#include <iostream>
#include "CString.h"
using namespace std;
void CreateString() {
puts("创建空string并输出");
CString str1;
cout << str1 << endl;
puts("利用常量C字符串\"aaa\"创建string并输出");
CString str2("aaa");
cout << str2 << endl;
puts("利用常量C字符串\"aaa\"的第一个字符创建string并输出");
CString str3("aaa", 1);
cout << str3 << endl;
puts("利用常量C字符串\"aaa\"创建string,下越界idx = -1测试并输出");
CString str4("aaa", -1);
cout << str4 << endl;
puts("利用常量C字符串\"aaa\"创建string,上越界idx = 100测试并输出");
CString str5("aaa", 100);
cout << str5 << endl;
puts("利用三个常量C字符'a'创建string,并输出");
CString str6('a', 3);
cout << str6 << endl;
puts("利用-1常量C字符'a'创建string,并输出");
CString str7('a', -1);
cout << str7 << endl;
}
void Assignment() {
puts("空指针赋值给str1并输出");
CString str1 = nullptr;
cout << "str1" << str1 << endl;
puts("空C字符串赋值给str2并输出");
CString str2 = "\0";
cout << "str2" << str2 << endl;
puts("C字符串\"aaa\"赋值给str3并输出");
CString str3 = "aaa";
cout << "str3 = " << str3 << endl;
puts("C字符串\"bbbbbb\"赋值给str3 = \"aaa\"并输出");
str3 = "bbbbbb";
cout << "str3 = " << str3 << endl;
puts("空string赋值给str4并输出");
CString str4 = str1;
cout << "str4" << str4 << endl;
puts("str3 = \"bbbbbb\"赋值给str5并输出");
CString str5 = str3;
cout << "str5 = " << str5 << endl;
}
void AddSelfWithCStr() {
puts("str1 = null, str1 += \"aaa\"并输出");
CString str1;
str1 += "aaa";
cout << "str1 += \"aaa\" => " << str1 << endl;
puts("str1 = \"aaa\", str1 += nullptr 并输出");
str1 += nullptr;
cout << "str1 += nullptr => " << str1 << endl;
puts("str1 = \"aaa\", str1 += \"bbb\" 并输出");
str1 += "bbb";
cout << "str1 += \"bbb\" => " << str1 << endl;
}
void AddSelfWithStr() {
puts("str1 = nullptr, str2 = \"aaa\", str1 += str2并输出");
CString str1, str2 = "aaa";
str1 += str2;
cout << "str1 += str2 => " << str1 << endl;
puts("str1 = \"aaa\", str2 = nullptr, str1 += str2 并输出");
str2.clear();
str1 += str2;
cout << "str1 += str2 => " << str1 << endl;
puts("str1 = \"aaa\", str1 += \"bbb\" 并输出");
str1.clear(), str2.clear();
str1 = "aaa", str2 = "bbb";
str1 += str2;
cout << "str1 += str2 => " << str1 << endl;
}
void Add() {
CString str2 = "bbb";
CString str1 = nullptr + str2;
cout << "\"nullptr\" + \"bbb\" = " << str1 << endl;
str2 = nullptr;
str1.clear();
str1 = "bbb" + str2;
cout << "\"bbb\" + \"nullptr\" = " << str1 << endl;
str1 = "aaa" + str2;
cout << "\"aaa\" + \"bbb\" = " << str1 << endl;
}
void Equal() {
CString str1 = nullptr, str2 = nullptr;
if (str1 == str2) cout << "str1 equal to str2" << endl;
else cout << " str1 not equal to str2" << endl;
str1 = "aaa", str2 = "aa";
if (str1 == str2) cout << "str1 equal to str2" << endl;
else cout << "str1 not equal to str2" << endl;
}
void Not_Equal() {
CString str1 = nullptr, str2 = nullptr;
if (str1 != str2) cout << "str1 not equal to str2" << endl;
else cout << "str1 equal to str2" << endl;
str1 = "aaa", str2 = "aa";
if (str1 != str2) cout << "str1 not equal to str2" << endl;
else cout << "str1 equal to str2" << endl;
}
void Trim() {
puts("Trim left string = \"aaabbbaaaa\"");
CString str1 = "aaabbbaaaa";
str1.TrimLeft('a');
cout << str1 << endl;
puts("Trim right string = \"aaabbbaaaa\"");
str1 = "aaabbbaaaa";
str1.TrimRight('a');
cout << str1 << endl;
puts("Trim string = \"aaabbbaaaa\"");
str1 = "aaabbbaaaa";
str1.Trim('a');
cout << str1 << endl;
}
int main() {
CreateString();
Assignment();
AddSelfWithCStr();
AddSelfWithStr();
Add();
Equal();
Not_Equal();
Trim();
return 0;
}
前排膜拜Orz