#include "string.h" ////////////////////////////////////////////////////////////////////////// string::string() : m_sizeLength( 0 ), m_sizeCapacity( 1 ), m_szString( nullptr ) { m_szString = new char[m_sizeCapacity]; if( !m_szString ) { return; } m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// string::string( const string &strString ) : m_sizeLength( strString.m_sizeLength ), m_sizeCapacity( strString.m_sizeCapacity ), m_szString( nullptr ) { m_szString = new char[m_sizeCapacity]; if( !m_szString ) { return; } memcpy( m_szString, strString.m_szString, m_sizeCapacity * sizeof( char ) ); } ////////////////////////////////////////////////////////////////////////// string::string( const char* szString ) : m_sizeLength( strlen( szString ) ), m_sizeCapacity( strlen( szString ) + 1 ), m_szString( nullptr ) { m_szString = new char[m_sizeCapacity]; if( !m_szString ) { return; } memcpy( m_szString, szString, m_sizeLength * sizeof( char ) ); m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// string::string( char chCharacter ) : m_sizeLength( 1 ), m_sizeCapacity( 2 ), m_szString( nullptr ) { m_szString = new char[m_sizeCapacity]; if( !m_szString ) { return; } m_szString[0] = chCharacter; m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// string::string( size_t sizeLength, char chCharacter ) : m_sizeLength( sizeLength ), m_sizeCapacity( sizeLength + 1 ), m_szString( nullptr ) { m_szString = new char[m_sizeCapacity]; if( !m_szString ) { return; } for( size_t i = 0; i < sizeLength; ++i ) { m_szString[i] = chCharacter; } m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// string::~string() { delete [] m_szString; } ////////////////////////////////////////////////////////////////////////// string &string::append( const string& strString ) { if( m_sizeLength + strString.m_sizeLength >= m_sizeCapacity ) { char* szCopyString = new char[m_sizeLength + strString.m_sizeLength + 1]; if( !szCopyString ) { return *this; } memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) ); delete [] m_szString; m_sizeCapacity = m_sizeLength + strString.m_sizeLength + 1; m_szString = szCopyString; } memcpy( m_szString + m_sizeLength, strString.m_szString, strString.m_sizeLength * sizeof( char ) ); m_sizeLength = m_sizeLength + strString.m_sizeLength; m_szString[m_sizeLength] = '\0'; return *this; } ////////////////////////////////////////////////////////////////////////// char& string::at( size_t sizeIndex ) { if( sizeIndex >= m_sizeLength ) { return m_szString[m_sizeLength - 1]; } return m_szString[sizeIndex]; } ////////////////////////////////////////////////////////////////////////// const char& string::at( size_t sizeIndex ) const { if( sizeIndex >= m_sizeLength ) { return m_szString[m_sizeLength - 1]; } return m_szString[sizeIndex]; } ////////////////////////////////////////////////////////////////////////// size_t string::capacity() const { return m_sizeCapacity; } ////////////////////////////////////////////////////////////////////////// void string::clear() { m_sizeLength = 0; m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// const char* string::c_str() const { return m_szString; } ////////////////////////////////////////////////////////////////////////// const char* string::data() const { return m_szString; } ////////////////////////////////////////////////////////////////////////// bool string::empty() const { if( m_sizeLength == 0 ) { return true; } return false; } ////////////////////////////////////////////////////////////////////////// size_t string::find( const string &strString, size_t sizePos /* = 0 */ ) const { if( strString.empty() || sizePos >= m_sizeLength ) { return npos; } for( size_t i = sizePos; i < m_sizeLength; ++i ) { if( i + strString.length() > m_sizeLength ) { return npos; } if( m_szString[i] == strString[0] ) { size_t sizeFound; for( sizeFound = 1; sizeFound < strString.size(); ++sizeFound ) { if( m_szString[i + sizeFound] != strString[sizeFound] ) { break; } } if( sizeFound == strString.size() ) { return i; } } } return npos; } ////////////////////////////////////////////////////////////////////////// size_t string::length() const { return m_sizeLength; } ////////////////////////////////////////////////////////////////////////// void string::push_back( char chCharacter ) { if( m_sizeLength + 1 >= m_sizeCapacity ) { char* szCopyString = new char[m_sizeLength + 2]; if( !szCopyString ) { return; } memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) ); delete [] m_szString; m_sizeCapacity = m_sizeLength + 2; m_szString = szCopyString; } m_szString[m_sizeLength++] = chCharacter; m_szString[m_sizeLength] = '\0'; } ////////////////////////////////////////////////////////////////////////// void string::reserve( size_t sizeCapacity /* = 0 */ ) { if( sizeCapacity > m_sizeCapacity ) { char* szCopyString = new char[sizeCapacity]; if( !szCopyString ) { return; } memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) ); delete [] m_szString; szCopyString[m_sizeLength] = '\0'; m_sizeCapacity = sizeCapacity; m_szString = szCopyString; } } ////////////////////////////////////////////////////////////////////////// void string::resize( size_t sizeLength, char chCharacter ) { if( sizeLength < m_sizeLength ) { m_sizeLength = sizeLength; m_szString[m_sizeLength] = '\0'; } else if( sizeLength > m_sizeLength ) { if( sizeLength + 1 > m_sizeCapacity ) { char* szCopyString = new char[sizeLength + 1]; if( !szCopyString ) { return; } memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) ); delete [] m_szString; for( size_t i = m_sizeLength; i < sizeLength; ++i ) { szCopyString[i] = chCharacter; } szCopyString[sizeLength] = '\0'; m_sizeLength = sizeLength; m_sizeCapacity = m_sizeLength + 1; m_szString = szCopyString; } else { for( size_t i = m_sizeLength; i < sizeLength; ++i ) { m_szString[i] = chCharacter; } m_szString[sizeLength] = '\0'; m_sizeLength = sizeLength; } } } ////////////////////////////////////////////////////////////////////////// size_t string::rfind( const string &strString, size_t sizePos /* = npos */ ) const { if( sizePos == npos ) { sizePos = m_sizeLength - 1; } if( strString.empty() || sizePos >= m_sizeLength ) { return npos; } for( size_t i = sizePos + 1; i-- > 0; ) { if( i - ( strString.length() - 1 ) > m_sizeLength ) { return npos; } if( m_szString[i] == strString[strString.length() - 1] ) { size_t sizeFound; for( sizeFound = strString.length() - 1; sizeFound-- > 0; ) { if( m_szString[i - ( strString.length() - ( sizeFound + 1 ) )] != strString[sizeFound] ) { break; } } if( sizeFound == npos ) { return i - ( strString.length() - 1 ); } } } return npos; } ////////////////////////////////////////////////////////////////////////// size_t string::size() const { return m_sizeLength; } ////////////////////////////////////////////////////////////////////////// string string::substr( size_t sizePos /* = 0 */, size_t sizeLength /* = npos */ ) const { if( sizePos >= m_sizeLength ) { return string(); } string strSubString; for( size_t i = sizePos; i < sizePos + sizeLength; ++i ) { if( i >= m_sizeLength ) { break; } strSubString += m_szString[i]; } return strSubString; } ////////////////////////////////////////////////////////////////////////// string& string::operator=( const string &strRHS ) { char* szCopyString = new char[strRHS.m_sizeCapacity]; if( !szCopyString ) { return *this; } m_sizeLength = strRHS.m_sizeLength; m_sizeCapacity = strRHS.m_sizeCapacity; memcpy( szCopyString, strRHS.m_szString, m_sizeCapacity * sizeof( char ) ); delete [] m_szString; m_szString = szCopyString; return *this; } ////////////////////////////////////////////////////////////////////////// char &string::operator[]( size_t sizeIndex ) { return m_szString[sizeIndex]; } ////////////////////////////////////////////////////////////////////////// const char &string::operator[]( size_t sizeIndex ) const { return m_szString[sizeIndex]; } ////////////////////////////////////////////////////////////////////////// string string::operator+( const string &strRHS ) const { string strReturn = *this; strReturn.append( strRHS ); return strReturn; } ////////////////////////////////////////////////////////////////////////// string &string::operator+=( const string &strRHS ) { append( strRHS ); return *this; } ////////////////////////////////////////////////////////////////////////// bool string::operator==( const string &strRHS ) const { if( size() != strRHS.size() ) { return false; } for( size_t i = 0; i < size(); ++i ) { if( m_szString[i] != strRHS.m_szString[i] ) { return false; } } return true; } ////////////////////////////////////////////////////////////////////////// bool string::operator!=( const string &strRHS ) const { return !( *this == strRHS ); }