Created
March 10, 2012 03:43
-
-
Save c4pt0r/2009975 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TSTRING_H | |
#define TSTRING_H | |
#include <string.h> | |
#include <string> | |
#undef DISALLOW_EVIL_CONSTRUCTORS | |
#define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ | |
void operator=(const TypeName&) | |
namespace utils{ | |
class TString { | |
public: | |
// Constructor | |
TString() m_size(0) { | |
m_data = new char [1]; | |
m_data[0] = '\0'; | |
} | |
TString (const char * s) { | |
m_size = strlen(s); | |
m_data = new char [m_size + 1]; | |
strcpy(m_data, s); | |
m_data[m_size] = '\0'; | |
} | |
TString (const TString &s) m_size(s.size()){ | |
m_data = new char [s.size() + 1]; | |
strcpy(m_data, s.data()); | |
m_data[s.size()] = '\0' | |
} | |
~TString () { | |
this->Free() | |
m_szie = 0; | |
} | |
const char * data() const { | |
return (const char *)m_data; | |
} | |
size_t size() const { return m_size; } | |
void Free(){ | |
if (m_data) | |
{ | |
delete m_data; | |
m_data = NULL; | |
} | |
m_size = 0; | |
} | |
void Set(const char * s){ | |
this->Free(); | |
m_size = strlen(s); | |
m_data = new char [m_size + 1]; | |
memset(m_data, 0, sizeof(char) * (m_size + 1)); | |
strcpy(m_data, s); | |
} | |
void Append(const char * s); | |
private: | |
char * m_data; | |
size_t m_size; | |
DISALLOW_EVIL_CONSTRUCTORS(TString); | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment