`
kevintse
  • 浏览: 10934 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Implementing my own String in C++

    博客分类:
  • 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;
}

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

    You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that ...

    C.Programming.Professional.For.Beginners.B011A2PX30

    Were you aware that grade schools and high schools have begun implementing C Programming in their curriculum's? Are you wanting a simple way to understand a step by step action to learning C ...

    Visual C++ 编程资源大全(英文源码 其它)

    replaceinfiles.zip Search and Replace in multiple files Add-in for Visual C++ 6.0(83KB)&lt;END&gt;&lt;br&gt;76,RescueAgent.zip A Code Rescue Add-in for Visual C++ Developers(118KB)&lt;END&gt;&lt;br&gt;77,stripem.zip...

    Professional.MFC.with.VC6

    Implementing Automation in MFC .ODLay Hee-Hoo! About the Sample Other OLE Applications Debugging OLE Applications Automation and ActiveX Controls Custom COM Interfaces The Rubber Hits the Road ...

    Software.Application.Development.A.Visual.Cplusplus.MFC.and.STL

    All computer code is included, allowing developers to extend and reuse the software modules for their own project work. The book’s tutorial-like approach empowers students and practitioners with the ...

    Python程序设计(第二版).chm

    Rolling Your Own Servers in Python Part IV: Assorted Topics Chapter 16. Databases and Persistence Section 16.1. "Give Me an Order of Persistence, but Hold the Pickles" Section 16.2. ...

    VB编程资源大全(英文源码 其它)

    Its very well documented and you should be able to adapt it for writing your own addins&lt;END&gt;&lt;br&gt;45,msgbox32_dll.zip This code compiles to a DLL and when registered will install itself as a VB Add-...

    The way to go

    1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................

    [Go语言入门(含源码)] The Way to Go (with source code)

    1.2.8 Programming in Go......................................................................................10 1.2.9 Summary.............................................................................

Global site tag (gtag.js) - Google Analytics