Advertisement
Guest User

xstring.cpp

a guest
Mar 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <exception>
  4.  
  5. #include "xstring.h"
  6.  
  7. using namespace std;
  8. using namespace common;
  9.  
  10. xstring( const char* pchar )
  11. {
  12.     size_t cstrlen = strlen( pchar );
  13.  
  14.     this->m_len = cstrlen;
  15.     this->m_str = new char[this->m_len];
  16.  
  17.     for ( int i = 0; i < this->m_len; i++ )
  18.     {
  19.         this->m_str[i] = pchar[i];
  20.     }
  21. }
  22.  
  23. xstring( const xstring& rhs )
  24. {
  25.     this->m_len = rhs.len();
  26.     this->m_str = new char[this->m_len];
  27.  
  28.     for ( int i = 0; i < this->m_len; i++ )
  29.     {
  30.         this->m_str[i] = rhs.c_str()[i];
  31.     }
  32. }
  33.  
  34. xstring( xstring&& rhs )
  35. {
  36.     this->m_len = rhs.len();
  37.     this->m_str = new char[this->m_len];
  38.     this->m_str = move( rhs.c_str() );
  39. }
  40.  
  41. char xstring::operator[]( size_t idx )
  42. {
  43.     if ( idx >= this->m_len )
  44.     {
  45.         throw out_of_bounds( "Index supplied is beyond string's boundaries." );
  46.     }
  47.  
  48.     return this->m_str[idx];
  49. }
  50.  
  51. xstring& xstring::operator+( char c )
  52. {
  53.     this->m_str[this->m_len]     = c;
  54.     this->m_str[this->m_len + 1] = '\0';
  55.  
  56.     this->m_len++;
  57.  
  58.     return *this;
  59. }
  60.  
  61. xstring& xstring::operator+( const char* pchar )
  62. {
  63.     size_t cstrlen = strlen( pchar );
  64.     char*  tmp     = this->m_str;
  65.     size_t oldlen  = this->m_len;
  66.  
  67.     this->m_len += cstrlen;
  68.     this->m_str  = new char[this->m_len];
  69.  
  70.     for ( int i = 0; i < strlen( tmp ); i++ )
  71.     {
  72.         this->m_str[i] = tmp[i];
  73.     }
  74.     for ( int i = 0; i < this->m_len; i++ )
  75.     {
  76.         this->m_str[oldlen + i] = pchar[i];
  77.     }
  78.  
  79.     return *this;
  80. }
  81.  
  82. xstring& xstring::operator+( const xstring& rhs )
  83. {
  84.     return this->operator+( rhs.c_str() );
  85. }
  86.  
  87. xstring& xstring::operator=( char c )
  88. {
  89.     this->m_len = 1;
  90.     this->m_str = new char[1];
  91.  
  92.     this->m_str[0] = c;
  93.  
  94.     return *this;
  95. }
  96.  
  97. xstring& xstring::operator=( const char* pchar )
  98. {
  99.     size_t cstrlen = strlen( pchar );
  100.    
  101.     this->m_str = new char[cstrlen];
  102.     this->m_len = cstrlen;
  103.  
  104.     for ( int i = 0; i < cstrlen; i++ )
  105.     {
  106.         this->m_str[i] = pchar[i];
  107.     }
  108.  
  109.     return *this;
  110. }
  111.  
  112. xstring& xstring::operator=( const xstring& rhs )
  113. {
  114.     return this->operator=( rhs.c_str() );
  115. }
  116.  
  117. xstring& xstring::operator=( xstring&& rhs )
  118. {
  119.     this->m_len = rhs.len();
  120.     this->m_str = new char[this->m_len];
  121.     this->m_str = move( rhs.c_str() );
  122.  
  123.     return *this;
  124. }
  125.  
  126. bool xstring::operator==( const char* pchar ) const
  127. {
  128.     if ( &pchar == &( this->m_str ) )
  129.     {
  130.         return true;
  131.     }
  132.  
  133.     size_t cstrlen = strlen( pchar );
  134.  
  135.     if ( cstrlen != this->m_len )
  136.     {
  137.         return false;
  138.     }
  139.  
  140.     for ( int i = 0; i < cstrlen; i++ )
  141.     {
  142.         if ( this->m_str[i] != cstrlen[i] )
  143.         {
  144.             return false;
  145.         }
  146.     }
  147.  
  148.     return true;
  149. }
  150.  
  151. bool xstring::operator==( const xstring& rhs ) const
  152. {
  153.     return this->operator==( rhs.c_str() );
  154. }
  155.  
  156. xstring::operator bool() const
  157. {
  158.     return ( this->m_str != nullptr );
  159. }
  160.  
  161. void xstring::clear()
  162. {
  163.     this->m_len = 0;
  164.     this->m_str = nullptr;
  165. }
  166.  
  167. ostream& operator<<( ostream& stream, const xstring& rhs )
  168. {
  169.     return ( stream << rhs.c_str() );
  170. }
  171.  
  172. istream& operator>>( istream& stream, xstring& rhs )
  173. {
  174.     return ( stream >> rhs.c_str() );
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement