Advertisement
Kyosaur

C++ String class

Mar 10th, 2011
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /*
  2.         Kyoshiro's string class v2.3! This was made simply for fun/practice, and isnt
  3.         suggested for actual usage. You're better off using the STL string class.
  4. */
  5.  
  6. class STR
  7. {
  8.  
  9. private:
  10.  
  11.     int len;
  12.     char* string;
  13.  
  14. public:
  15.  
  16.     STR() : string(NULL), len(NULL) {}
  17.  
  18.     STR(char* text)
  19.     {
  20.         len     = NULL;
  21.         string  = NULL;
  22.  
  23.         this->Assign(text);
  24.     }
  25.  
  26.     ~STR()
  27.     {
  28.         this->Delete();
  29.     }
  30.  
  31.     char* c_str()
  32.     {
  33.         return string;
  34.     }
  35.  
  36.  
  37.     inline int Compare(char* text)
  38.     {
  39.         return strcmp(string, text);
  40.     }
  41.  
  42.     inline int Length()
  43.     {
  44.         return len;
  45.     }
  46.  
  47.     STR& Delete()
  48.     {
  49.         if(string)
  50.         {
  51.             len = 0;
  52.             delete[] string;
  53.         }
  54.         return *this;
  55.     }
  56.  
  57.     STR& Assign(char* text)
  58.     {
  59.         int size = strlen(text);
  60.  
  61.         if(size)
  62.         {
  63.             if(len)
  64.             {
  65.                 if(len > size)
  66.                 {
  67.                     strcpy(string, text);
  68.  
  69.                     return *this;
  70.                 }
  71.                 this->Delete();
  72.             }
  73.             len = size;
  74.  
  75.             string = new char[size+1];
  76.             strcpy(string, text);
  77.         }
  78.         return *this;
  79.     }
  80.  
  81.     STR& Resize(int length)
  82.     {
  83.         if(length < 0)
  84.         {
  85.             int l = len + length;
  86.             length = (l > 0) ? (l) : (0);
  87.         }
  88.  
  89.         if(length)
  90.         {  
  91.             if(len)
  92.             {
  93.                 char* tmp = new char[length+1];
  94.  
  95.                 strncpy(tmp, string, length);
  96.                 tmp[length] = NULL;
  97.  
  98.                 this->Delete();
  99.            
  100.                 len = length;
  101.  
  102.                 string = new char[length+1];
  103.                 strcpy(string, tmp);
  104.  
  105.                 delete[] tmp;
  106.                 return *this;
  107.             }
  108.             string = new char[length+1];
  109.             return *this;
  110.         }
  111.         this->Delete();
  112.         return *this;
  113.     }
  114.  
  115.     STR& Append(char* text)
  116.     {
  117.         int size = strlen(text);
  118.  
  119.         if(size)
  120.         {
  121.             if(len)
  122.             {
  123.                 this->Resize((len+size));
  124.                 strcat(string, text);
  125.  
  126.                 return *this;
  127.             }
  128.             this->Assign(text);
  129.         }
  130.         return *this;
  131.     }
  132.  
  133.  
  134.     int Find(char* text, int pos)
  135.     {
  136.         if(len)
  137.         {
  138.             int
  139.                 size    = NULL,
  140.                 cpos    = NULL,
  141.                 ccount  = NULL;
  142.  
  143.             bool first;
  144.  
  145.             size = strlen(text);
  146.  
  147.             if(size)
  148.             {
  149.                 if(pos < size && len >= size)
  150.                 {
  151.                     for(int i = pos; i < len; i++)
  152.                     {
  153.                         if(string[i] == text[ccount])
  154.                         {
  155.                             if(!first)
  156.                             {
  157.                                 pos = i;
  158.                                 first = true;
  159.                             }
  160.  
  161.                             if(++ccount == size) return pos;
  162.                         }
  163.                         else
  164.                         {
  165.                             pos     = NULL;
  166.                             first   = false;
  167.                             ccount  = NULL;
  168.                         }
  169.                     }
  170.                 }
  171.             }
  172.             return -1;
  173.         }
  174.     }
  175.  
  176.     STR& operator=(char* data) { this->Assign(data); return *this; }
  177.     STR& operator+(char* data) { this->Append(data); return *this; }
  178.     STR& operator+=(char* data) { this->Append(data); return *this; }
  179.  
  180.  
  181.     char& operator[](int index)
  182.     {
  183.         if(len >= index)
  184.         {
  185.             char& ref = string[index];
  186.             return ref;
  187.         }
  188.     }
  189.  
  190. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement