//------------------------------------------------------------------
//FileName : String.h
//Author : Younger
//description : Over write the string class
//Date : 2016/07/13
//Referent : http://blog.youkuaiyun.com/moxiaomomo/article/details/6411584
//-------------------------------------------------------------------
#ifndef _MyString_String_H_
#define _MyString_String_H_
#include <iostream>
#include <iomanip>
using namespace std;
namespace Str
{
class string
{
friend ostream& operator<<(ostream&,string&);//overload operator<<
friend ostream& operator>>(iostream&,string&);//overload operator>>
public:
string(const char* pData = NULL);//default construction
string(const string& str);//copy construction
~string(){delete[] m_pData;}
string& operator+(const string& str);//overload operator+
string& operator=(const string& str);//overload operator=
string& operator+=(const string& str);//overload operator+=
bool operator==(const string& );//overload operator==
char& operator[](unsigned int)const;//overload operator[]
size_t size(){return strlen(m_pData);}
const char* c_str()const;
void append(const string& str);
private:
char* m_pData;
};
inline void string::append(const string& str)
{
if (&str != NULL)
{
if (str.m_pData != NULL)
{
char* Tp = m_pData;
m_pData = new char[strlen(Tp)+strlen(str.m_pData)+1];
strcpy(m_pData,Tp);
strcat(m_pData,str.m_pData);
delete Tp;
Tp = NULL;
}
}
}
inline const char* string::c_str()const
{
return m_pData;
}
inline string& string::operator+=(const string& str)
{
if (str.m_pData != NULL)
{
if (m_pData == NULL)
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
else
{
char* tp = m_pData;
m_pData = new char[strlen(tp)+strlen(str.m_pData)+1];
strcpy(m_pData,tp);
strcat(m_pData,str.m_pData);
delete tp;
tp = NULL;
}
}
return *this;
}
inline string::string(const char* pData)
{
if (!pData)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(pData)+1];
strcpy(m_pData,pData);
}
}
inline string::string(const string& str)
{
if (str.m_pData == NULL)
{
m_pData = NULL;
}
else
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
}
inline string& string::operator=(const string& str)
{
if (this != &str)
{
delete m_pData;
if (str.m_pData != NULL)
{
m_pData = new char[strlen(str.m_pData)+1];
strcpy(m_pData,str.m_pData);
}
else
{
m_pData = NULL;
}
}
return *this;
}
inline bool string::operator==(const string& str)
{
if(strlen(str.m_pData) != strlen(m_pData))
{
return false;
}
else if (strcmp(m_pData,str.m_pData))
{
return false;
}
return true;
}
inline string& string::operator+(const string& str)
{
static string Rstring;
if (str.m_pData == NULL)
{
Rstring.m_pData = m_pData;
}
else if (m_pData == NULL)
{
Rstring = str;
}
else
{
Rstring.m_pData = new char[strlen(m_pData)+strlen(str.m_pData)+1];
strcpy(Rstring.m_pData,m_pData);
strcat(Rstring.m_pData,str.m_pData);
}
return Rstring;
}
inline char& string::operator[](unsigned int num)const
{
if (num >= 0 && num <= strlen(m_pData))
{
return m_pData[num];
}
}
ostream& operator<<(ostream& os,string& str)
{
os<<str.m_pData;
return os;
}
istream& operator>>(istream& io,string& str)
{
char IoData[255];
io>>setw(255)>>IoData;
str = IoData;
return io;
}
}
#endif