Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <exception>
- #include "xstring.h"
- using namespace std;
- using namespace common;
- xstring( const char* pchar )
- {
- size_t cstrlen = strlen( pchar );
- this->m_len = cstrlen;
- this->m_str = new char[this->m_len];
- for ( int i = 0; i < this->m_len; i++ )
- {
- this->m_str[i] = pchar[i];
- }
- }
- xstring( const xstring& rhs )
- {
- this->m_len = rhs.len();
- this->m_str = new char[this->m_len];
- for ( int i = 0; i < this->m_len; i++ )
- {
- this->m_str[i] = rhs.c_str()[i];
- }
- }
- xstring( xstring&& rhs )
- {
- this->m_len = rhs.len();
- this->m_str = new char[this->m_len];
- this->m_str = move( rhs.c_str() );
- }
- char xstring::operator[]( size_t idx )
- {
- if ( idx >= this->m_len )
- {
- throw out_of_bounds( "Index supplied is beyond string's boundaries." );
- }
- return this->m_str[idx];
- }
- xstring& xstring::operator+( char c )
- {
- this->m_str[this->m_len] = c;
- this->m_str[this->m_len + 1] = '\0';
- this->m_len++;
- return *this;
- }
- xstring& xstring::operator+( const char* pchar )
- {
- size_t cstrlen = strlen( pchar );
- char* tmp = this->m_str;
- size_t oldlen = this->m_len;
- this->m_len += cstrlen;
- this->m_str = new char[this->m_len];
- for ( int i = 0; i < strlen( tmp ); i++ )
- {
- this->m_str[i] = tmp[i];
- }
- for ( int i = 0; i < this->m_len; i++ )
- {
- this->m_str[oldlen + i] = pchar[i];
- }
- return *this;
- }
- xstring& xstring::operator+( const xstring& rhs )
- {
- return this->operator+( rhs.c_str() );
- }
- xstring& xstring::operator=( char c )
- {
- this->m_len = 1;
- this->m_str = new char[1];
- this->m_str[0] = c;
- return *this;
- }
- xstring& xstring::operator=( const char* pchar )
- {
- size_t cstrlen = strlen( pchar );
- this->m_str = new char[cstrlen];
- this->m_len = cstrlen;
- for ( int i = 0; i < cstrlen; i++ )
- {
- this->m_str[i] = pchar[i];
- }
- return *this;
- }
- xstring& xstring::operator=( const xstring& rhs )
- {
- return this->operator=( rhs.c_str() );
- }
- xstring& xstring::operator=( xstring&& rhs )
- {
- this->m_len = rhs.len();
- this->m_str = new char[this->m_len];
- this->m_str = move( rhs.c_str() );
- return *this;
- }
- bool xstring::operator==( const char* pchar ) const
- {
- if ( &pchar == &( this->m_str ) )
- {
- return true;
- }
- size_t cstrlen = strlen( pchar );
- if ( cstrlen != this->m_len )
- {
- return false;
- }
- for ( int i = 0; i < cstrlen; i++ )
- {
- if ( this->m_str[i] != cstrlen[i] )
- {
- return false;
- }
- }
- return true;
- }
- bool xstring::operator==( const xstring& rhs ) const
- {
- return this->operator==( rhs.c_str() );
- }
- xstring::operator bool() const
- {
- return ( this->m_str != nullptr );
- }
- void xstring::clear()
- {
- this->m_len = 0;
- this->m_str = nullptr;
- }
- ostream& operator<<( ostream& stream, const xstring& rhs )
- {
- return ( stream << rhs.c_str() );
- }
- istream& operator>>( istream& stream, xstring& rhs )
- {
- return ( stream >> rhs.c_str() );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement