Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Kyoshiro's string class v2.3! This was made simply for fun/practice, and isnt
- suggested for actual usage. You're better off using the STL string class.
- */
- class STR
- {
- private:
- int len;
- char* string;
- public:
- STR() : string(NULL), len(NULL) {}
- STR(char* text)
- {
- len = NULL;
- string = NULL;
- this->Assign(text);
- }
- ~STR()
- {
- this->Delete();
- }
- char* c_str()
- {
- return string;
- }
- inline int Compare(char* text)
- {
- return strcmp(string, text);
- }
- inline int Length()
- {
- return len;
- }
- STR& Delete()
- {
- if(string)
- {
- len = 0;
- delete[] string;
- }
- return *this;
- }
- STR& Assign(char* text)
- {
- int size = strlen(text);
- if(size)
- {
- if(len)
- {
- if(len > size)
- {
- strcpy(string, text);
- return *this;
- }
- this->Delete();
- }
- len = size;
- string = new char[size+1];
- strcpy(string, text);
- }
- return *this;
- }
- STR& Resize(int length)
- {
- if(length < 0)
- {
- int l = len + length;
- length = (l > 0) ? (l) : (0);
- }
- if(length)
- {
- if(len)
- {
- char* tmp = new char[length+1];
- strncpy(tmp, string, length);
- tmp[length] = NULL;
- this->Delete();
- len = length;
- string = new char[length+1];
- strcpy(string, tmp);
- delete[] tmp;
- return *this;
- }
- string = new char[length+1];
- return *this;
- }
- this->Delete();
- return *this;
- }
- STR& Append(char* text)
- {
- int size = strlen(text);
- if(size)
- {
- if(len)
- {
- this->Resize((len+size));
- strcat(string, text);
- return *this;
- }
- this->Assign(text);
- }
- return *this;
- }
- int Find(char* text, int pos)
- {
- if(len)
- {
- int
- size = NULL,
- cpos = NULL,
- ccount = NULL;
- bool first;
- size = strlen(text);
- if(size)
- {
- if(pos < size && len >= size)
- {
- for(int i = pos; i < len; i++)
- {
- if(string[i] == text[ccount])
- {
- if(!first)
- {
- pos = i;
- first = true;
- }
- if(++ccount == size) return pos;
- }
- else
- {
- pos = NULL;
- first = false;
- ccount = NULL;
- }
- }
- }
- }
- return -1;
- }
- }
- STR& operator=(char* data) { this->Assign(data); return *this; }
- STR& operator+(char* data) { this->Append(data); return *this; }
- STR& operator+=(char* data) { this->Append(data); return *this; }
- char& operator[](int index)
- {
- if(len >= index)
- {
- char& ref = string[index];
- return ref;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement