hi, everyone, i am new to C++, i tried implementing a String. and i need your help, i want to know what to improve for this class regarding performance, or everything else you think i should pay more attention to when implementing such a class for general use(of course, i think i may have to choose std::string instead...)
Thank you in advance!
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;
int strSize = strlen(paramStr);
if(!strSize)
return tempStr;
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;
int strSize = strlen(str);
if(!strSize)
return tempStr;
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;
int tempSize = strlen(temp);
if(!tempSize)
return *this;
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) {
int tempSize = strlen(str);
if(!tempSize)
return *this;
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;
}