Implementing my own String in C++

Actually, this class has mixed together the basic functionalities of a String and those of a StringBuffer...

[size=medium][b]Header:[/b][/size]

#ifndef STRING_H_
#define STRING_H_
#include <iostream>
using namespace std;
class String{
friend ostream& operator<<(ostream& output, const String& str);
private:
char* buffer;
int initSize;
int incrementSize;
int strLength;
int bufferSize;
public:
String(const char* initStr = new char[128], int initSize = 128, int incrementSize = 128);
String(const String& str);
virtual ~String(){
delete[] buffer;
}
String operator+(const String& str) const;
String operator+(const char* str) const;
String& operator+=(const String& str);
String& operator+=(const char* str);
String operator=(const String& str) const;
String operator=(const char* str) const;
bool operator==(const String& str) const;
int length() {
if(strLength == -1)
strLength = strlen(buffer);
return strLength;
}
};

#endif

[size=medium]
[b]Implementation:[/b][/size]

#include "String.h"
using namespace std;

String::String(const char* initStr, int initSize, int incrementSize):
initSize(initSize), incrementSize(incrementSize){
int strLen = strlen(initStr) + 1;
if(strLen > 1)
initSize = (strLen / incrementSize + 1) * incrementSize;
buffer = new char[initSize];
memcpy(buffer, initStr, strLen);
strLength = -1;
bufferSize = initSize;
}

String::String(const String& str){
bufferSize = str.bufferSize;
buffer = new char[bufferSize];
strcpy(buffer, str.buffer);
initSize = str.initSize;
incrementSize = str.incrementSize;
strLength = -1;
}

String String::operator+(const String& str) const{
String tempStr = *this;
char* paramStr = str.buffer;
//传进来的String对象的长度
int strSize = strlen(paramStr);
//如果传进来的是一个空字符串
if(!strSize)
return tempStr;
//如果当前字符串缓存是buffer[128],之前保存了100个字符
//那(tempStr.bufferSize - tempStr.length())就表示当前缓存还剩下28个字符的空间
int excessSize = strSize - (tempStr.bufferSize - tempStr.length());
char* oldBuffer = tempStr.buffer;
if(excessSize > 0){
tempStr.bufferSize = tempStr.bufferSize + (excessSize / tempStr.incrementSize + 1) * tempStr.incrementSize;
char* newBuffer = new char[tempStr.bufferSize];
strcpy(newBuffer, oldBuffer);
strcat(newBuffer, paramStr);
delete [] oldBuffer;
oldBuffer = newBuffer;
}else{
strcat(oldBuffer, paramStr);
}
tempStr.strLength = -1;
return tempStr;
}

String String::operator+(const char* str) const{
String tempStr = *this;
//传进来的char*对象的长度
int strSize = strlen(str);
//如果传进来的是一个空字符串
if(!strSize)
return tempStr;
//如果当前字符串缓存是buffer[128],之前保存了100个字符
//那(tempStr.bufferSize - tempStr.length())就表示当前缓存还剩下28个字符的空间
int excessSize = strSize - (tempStr.bufferSize - tempStr.length());
char* oldBuffer = tempStr.buffer;
if(excessSize > 0){
tempStr.bufferSize = tempStr.bufferSize + (excessSize / tempStr.incrementSize + 1) * tempStr.incrementSize;
char* newBuffer = new char[tempStr.bufferSize];
strcpy(newBuffer, oldBuffer);
strcat(newBuffer, str);
delete [] oldBuffer;
oldBuffer = newBuffer;
}else{
strcat(oldBuffer, str);
}
tempStr.strLength = -1;
return tempStr;
}

String& String::operator+=(const String& str) {
char* temp = str.buffer;
//传进来的String对象的长度
int tempSize = strlen(temp);
//如果传进来的是一个空字符串,则返回原来的字符串
if(!tempSize)
return *this;
//如果当前字符串缓存是buffer[128],之保存了100个字符
//那(bufferSize - length())就表示当前缓存还剩下28个字符的空间
int excessSize = tempSize - (bufferSize - length());
if(excessSize > 0){
bufferSize = bufferSize + (excessSize / incrementSize + 1) * incrementSize;
char* newBuffer = new char[bufferSize];
strcpy(newBuffer, buffer);
strcat(newBuffer, temp);
delete [] buffer;
buffer = newBuffer;
}else{
strcat(buffer, temp);
}
this->strLength = -1;
return *this;
}

String& String::operator+=(const char* str) {
//传进来的char*的长度
int tempSize = strlen(str);
//如果传进来的是一个空字符串,则返回原来的字符串
if(!tempSize)
return *this;
//如果当前字符串缓存是buffer[128],之保存了100个字符,
//那(bufferSize - length())就表示当前缓存还剩下28个字符的空间
int excessSize = tempSize - (bufferSize - length());
cout << "excessSize:" << excessSize << endl;
if(excessSize > 0){
bufferSize = bufferSize + (excessSize / incrementSize + 1) * incrementSize;
char* newBuffer = new char[bufferSize];
strcpy(newBuffer, buffer);
strcat(newBuffer, str);
delete [] buffer;
buffer = newBuffer;
}else{
strcat(buffer, str);
}
cout << "buffer size:" << bufferSize << endl;
this->strLength = -1;
return *this;
}

String String::operator=(const String& str) const{
return str;
}
String String::operator=(const char* str) const{
return String(str);
}

bool String::operator==(const String& str) const{
return strcmp(buffer, str.buffer) == 0 ? true : false;
}

ostream& operator<<(ostream& output, const String& str) {
output << str.buffer;
return output;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值