"Martin M. Pedersen" <noname> wrote in message news:<40*********************@dread11.news.tele.dk >...
"YinTat" <pr******@hotmail.com> wrote in message
news:4c**************************@posting.google.c om... Have any bug or harmful code in the "CString" class?
Why the "CString" class faster than STL string class?
Besides the problems already mentioned, you are missing a destructor, copy
constructor and copy assignment.
Regards,
Martin
#pragma once
#include <cstring>
/************************************Class********* ****************************/
class CString
{
public:
/***********************Construction/Destruction***************************/
inline CString():m_capacity(1), m_size(0)
{
m_str = new char[1];
}
inline CString(const CString& rhs)
{
m_size = rhs.m_size;
m_capacity = rhs.m_capacity;
m_str = new char[m_capacity];
memmove(m_str, rhs.m_str, m_size+1);
}
inline CString(const char *rhs)
{
m_size = strlen(rhs);
m_capacity = m_size * 2 + 1;
m_str = new char[m_capacity];
memmove(m_str, rhs, m_size+1);
}
inline ~CString()
{
delete[] m_str;
}
/*******************************Operator*********** ************************/
inline CString& operator= (const CString& rhs)
{
if (this == &rhs)
return *this;
m_size = rhs.m_size;
if (m_capacity <= m_size)
{
m_capacity = rhs.m_capacity;
delete[] m_str;
m_str = new char[m_capacity];
}
memmove(m_str, rhs.m_str, m_size+1);
return *this;
}
inline CString& operator= (const char *rhs)
{
if (m_str == rhs)
return *this;
m_size = strlen(rhs);
if (m_capacity <= m_size)
{
m_capacity = m_size * 2 + 1;
delete[] m_str;
m_str = new char[m_capacity];
}
memmove(m_str, rhs, m_size+1);
return *this;
}
inline bool operator== (const CString& rhs)
{
if (m_size == rhs.m_size)
return (memcmp(m_str, rhs.m_str, m_size)==0);
else
return false;
}
inline bool operator== (const char *rhs)
{
return (memcmp(m_str, rhs, m_size)==0);
}
inline CString& operator+= (const CString& rhs)
{
unsigned int tmp_size = m_size + rhs.m_size;
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, m_size);
delete[] m_str;
m_str = tmp_char;
}
memmove(m_str+m_size, rhs.m_str, rhs.m_size);
m_size = tmp_size;
return *this;
}
inline CString& operator+= (const char *rhs)
{
unsigned int rhs_size = strlen(rhs);
unsigned int tmp_size = m_size + rhs_size;
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, m_size);
delete[] m_str;
m_str = tmp_char;
}
memmove(m_str+m_size, rhs, rhs_size);
m_size = tmp_size;
return *this;
}
inline CString* operator& ()
{
return this;
}
inline const CString* operator& () const
{
return this;
}
/****************************Class
Function********************************/
//"123456789".size = "123456789"
inline const char* c_str()
{
return m_str;
}
//"123456789".size = 9
inline unsigned int size()
{
return m_size;
}
//"123456789".clearmem = ""
inline CString& clearmem()
{
delete[] m_str;
m_str = new char[1];
m_capacity = 1;
m_size = 0;
return *this;
}
//"123456789".insert(3,"xxx")="123xxx456789"
inline CString& insert(unsigned int pos, const char *rhs)
{
unsigned int rhs_size = strlen(rhs);
unsigned int tmp_size = m_size + rhs_size;
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, pos);
memmove(tmp_char+pos+rhs_size, m_str+pos, m_size-pos+1); //1 for
'/0'
delete[] m_str;
m_str = tmp_char;
}
else
{
memmove(m_str+pos+rhs_size, m_str+pos, m_size-pos+1);
}
memmove(m_str+pos, rhs, rhs_size);
m_size = tmp_size;
return *this;
}
//"123456789".insert(3,"xxx")="123xxx456789"
inline CString& insert(unsigned int pos, const CString& rhs)
{
unsigned int tmp_size = m_size + rhs.m_size;
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, pos);
memmove(tmp_char+pos+rhs.m_size, m_str+pos, m_size-pos+1);//1 for
'/0'
delete[] m_str;
m_str = tmp_char;
}
else
{
memmove(m_str+pos+rhs.m_size, m_str+pos, m_size-pos+1);
}
memmove(m_str+pos, rhs.m_str, rhs.m_size);
m_size = tmp_size;
return *this;
}
//"123456789".erase(3,3)="123789"
inline CString& erase(unsigned int pos, unsigned int len)
{
memmove(m_str+pos, m_str+pos+len, m_size-pos+1);
m_size = m_size - len;
return *this;
}
//"123456789".assign(3,3)="456"
inline CString& assign(unsigned int pos, unsigned int len)
{
memmove(m_str, m_str+pos, len);
m_str[len] = '\0';
m_size = len;
return *this;
}
//"123456789".fill(5,'1') = "11111"
inline CString& fill(unsigned int len, char fillch)
{
if (m_capacity <= m_size)
{
m_capacity = len * 2 + 1;
delete[] m_str;
m_str = new char[m_capacity];
}
memset(m_str, fillch, len);
m_str[len] = '0';
return *this;
}
//"123456789".replace(3,3,"xxxxxx") = "123xxxxxx789"
inline CString& replace(unsigned int pos, unsigned int len,
const char *rhs)
{
if ((pos + len) > m_size)
throw 0xC000008CL;
unsigned int rhs_size = strlen(rhs);
unsigned int tmp_size = m_size + rhs_size - len;
if (len != rhs_size)
{
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, pos);
memmove(tmp_char+pos+rhs_size, m_str+pos+len,
tmp_size-pos-rhs_size+1); //1 for '/0'
delete[] m_str;
m_str = tmp_char;
}
else
{
memmove(m_str+pos+rhs_size, m_str+pos+len,
tmp_size-pos-rhs_size+1); //1 for '/0'
}
}
memmove(m_str+pos, rhs, rhs_size);
m_size = tmp_size;
return *this;
}
//"123456789".replace(3,3,"xxxxxx") = "123xxxxxx789"
inline CString& replace(unsigned int pos, unsigned int len,
CString& rhs)
{
if ((pos + len) > m_size)
throw 0xC000008CL;
unsigned int tmp_size = m_size + rhs.m_size - len;
if (len != rhs.m_size)
{
if (m_capacity <= tmp_size)
{
m_capacity = tmp_size * 2 + 1;
char *tmp_char = new char[m_capacity];
memmove(tmp_char, m_str, pos);
memmove(tmp_char+pos+rhs.m_size, m_str+pos+len,
tmp_size-pos-rhs.m_size+1); //1 for '/0'
delete[] m_str;
m_str = tmp_char;
}
else
{
memmove(m_str+pos+rhs.m_size, m_str+pos+len,
tmp_size-pos-rhs.m_size+1); //1 for '/0'
}
}
memmove(m_str+pos, rhs.m_str, rhs.m_size);
m_size = tmp_size;
return *this;
}
private:
char *m_str;
unsigned int m_capacity;
unsigned int m_size;
};