// CONSTRUCTOR (const AString &) //------------------------------------------------------------------------------ AString::AString( const AString & string ) { uint32_t len = string.GetLength(); m_Length = len; uint32_t reserved = Math::RoundUp( len, (uint32_t)2 ); m_Contents = (char *)ALLOC( reserved + 1 ); SetReserved( reserved, true ); Copy( string.Get(), m_Contents, len ); // copy handles terminator }
// CONSTRUCTOR (const char *) //------------------------------------------------------------------------------ AString::AString( const char * string ) { ASSERT( string ); uint32_t len = (uint32_t)StrLen( string ); m_Length = len; uint32_t reserved = Math::RoundUp( len, (uint32_t)2 ); m_Contents = (char *)ALLOC( reserved + 1 ); SetReserved( reserved, true ); Copy( string, m_Contents, len ); // copy handles terminator }
// GrowNoCopy //------------------------------------------------------------------------------ void AString::GrowNoCopy( uint32_t newLength ) { if ( MemoryMustBeFreed() ) { FREE( m_Contents ); } // allocate space, rounded up to multiple of 2 uint32_t reserve = Math::RoundUp( newLength, (uint32_t)2 ); m_Contents = (char *)ALLOC( reserve + 1 ); // also allocate for \0 terminator SetReserved( reserve, true ); }
// CONSTRUCTOR (uint32_t) //------------------------------------------------------------------------------ AString::AString( uint32_t reserve ) { char * mem = nullptr; if ( reserve > 0 ) { reserve = Math::RoundUp( reserve, (uint32_t)2 ); mem = (char *)ALLOC( reserve + 1 ); mem[ 0 ] = '\000'; } m_Contents = mem; m_Length = 0; SetReserved( reserve, true ); }
// Grow //------------------------------------------------------------------------------ void AString::Grow( uint32_t newLength ) { // allocate space, rounded up to multiple of 2 uint32_t reserve = Math::RoundUp( newLength, (uint32_t)2 ); char * newMem = (char *)ALLOC( reserve + 1 ); // also allocate for \0 terminator // transfer existing string data Copy( m_Contents, newMem, m_Length ); if ( MemoryMustBeFreed() ) { FREE( m_Contents ); } m_Contents = newMem; SetReserved( reserve, true ); }