Implementing my own String in C++

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

Header:
#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


Implementation:

#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;
}

你可能感兴趣的:(Implementing my own String in C++)