Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef BC_STRING_H
- #define BC_STRING_H
- #include "stdafx.h"
- #define BC_STRING_MINIMUM 16 // Initial and minimal string data size
- #define BC_STRING_SLICE 8 // Allocated string data size is always a multiple of this
- #define BC_BOOL_TRUE_STRING "true" // Written when writing a true boolean to the string
- #define BC_BOOL_FALSE_STRING "false" // false
- namespace LibBC {
- class bcString {
- public:
- bcString( void );
- bcString( const bcString &sText );
- bcString( const bcString &sText, int nStart, int nEnd );
- bcString( const char *szText );
- bcString( const char *szText, int nStart, int nEnd );
- explicit bcString( const bool bFlag );
- explicit bcString( const char nCharacter );
- explicit bcString( const int nNumber );
- explicit bcString( const unsigned nNumber );
- explicit bcString( const float fNumber );
- explicit bcString( const double fNumber );
- ~bcString( void );
- const char *CString( void ) const;
- char *PChar( void ) const;
- operator const char *( void ) const;
- char operator[]( int nChar ) const;
- char &operator[]( int nChar );
- void operator=( const bcString &sText );
- void operator=( const char *szText );
- friend bcString operator+( const bcString &sTextA, const bcString &sTextB );
- friend bcString operator+( const char *szTextA, const bcString &sTextB );
- friend bcString operator+( const bcString &sTextA, const char *szTextB );
- friend bcString operator+( const bcString &sText, const bool bFlag );
- friend bcString operator+( const bcString &sText, const char nCharacter );
- friend bcString operator+( const bcString &sText, const int nNumber );
- friend bcString operator+( const bcString &sText, const unsigned nNumber );
- friend bcString operator+( const bcString &sText, const float fNumber );
- friend bcString operator+( const bcString &sText, const double fNumber );
- bcString &operator+=( const bcString &sText );
- bcString &operator+=( const char *szText );
- bcString &operator+=( const bool bFlag );
- bcString &operator+=( const char nCharacter );
- bcString &operator+=( const int nNumber );
- bcString &operator+=( const unsigned nNumber );
- bcString &operator+=( const float fNumber );
- bcString &operator+=( const double fNumber );
- friend bool operator==( const bcString &sTextA, const bcString &sTextB );
- friend bool operator==( const bcString &sTextA, const char *szTextB );
- friend bool operator==( const char *szTextA, const bcString &sTextB );
- friend bool operator!=( const bcString &sTextA, const bcString &sTextB );
- friend bool operator!=( const bcString &sTextA, const char *szTextB );
- friend bool operator!=( const char *szTextA, const bcString &sTextB );
- bool Compare( const char *szText ) const;
- bool CompareUntil( const char *szText, int nEnd ) const;
- bool ComparePrefix( const char *szPrefix ) const;
- bool CompareIns( const char *szText ) const;
- bool CompareUntilIns( const char *szText, int nEnd ) const;
- bool ComparePrefixIns( const char *szPrefix ) const;
- int Length( void ) const;
- int Allocated( void ) const;
- bcString &Clear( void );
- bcString &Empty( void );
- bool IsEmpty( void ) const;
- bcString &Append( const char nCharacter );
- bcString &Append( const bcString &sText );
- bcString &Append( const char *szText );
- bcString &Append( const char *szText, int nLength );
- bcString &Insert( const char nCharacter, const int nIndex );
- bcString &Insert( const bcString &sText, const int nIndex );
- bcString &Insert( const char *szText, const int nIndex );
- bcString &Insert( const char *szText, const int nLength, const int nIndex );
- bcString &ToLower( void );
- bcString &ToUpper( void );
- bcString &Cut( const int nLength );
- bcString &Fill( const char nCharacter, const int nNewLength );
- int Find( const char nCharacter, int nStart = 0, int nEnd = -1 ) const;
- int Find( const char *szText, bool bCaseSensitive = true, int nStart = 0, int nEnd = -1 ) const;
- bool Filter( const char *szFilter, bool bCaseSensitive ) const;
- int Last( const char nCharacter ) const;
- int Last( const char *szText, bool bCaseSensitive = true ) const;
- bcString Left( const int nLength ) const;
- bcString Right( const int nLength ) const;
- bcString Mid( const int nStart, const int nLength ) const;
- bcString &Remove( int nStart, int nLength );
- bcString &Replace( const char *szOld, const char *szNew );
- static bool Istrcmp( const char *szTextA, const char *szTextB );
- static bool Istrncmp( const char *szTextA, const char *szTextB, size_t nLength );
- static bool Filter( const char *szFilter, const char *szText, bool bCaseSensitive );
- static void Remove( char *szText, int nStart, int nEnd );
- void Reallocate( const int nAmount, bool bPreserveContent );
- void FreeData( void );
- // STATIC IMPLEMENTATIONS:
- static bcString Clear( const bcString &String );
- static bcString Empty( const bcString &String );
- static bcString Append( const bcString &String, const char nCharacter );
- static bcString Append( const bcString &String, const bcString &sAppendix );
- static bcString Append( const bcString &String, const char *szText );
- static bcString Append( const bcString &String, const char *szText, const int nLength );
- static bcString Insert( const bcString &String, const char nCharacter, const int nIndex );
- static bcString Insert( const bcString &String, const bcString &sInsertion, const int nIndex );
- static bcString Insert( const bcString &String, const char *szText, const int nIndex );
- static bcString Insert( const bcString &String, const char *szText, const int nLength, const int nIndex );
- static bcString ToLower( const bcString &String );
- static bcString ToUpper( const bcString &String );
- static bcString Cut( const bcString &String, const int nLength );
- static bcString Remove( const bcString &String, int nStart, int nLength );
- static bcString Replace( const bcString &String, const char *szOld, const char *szNew );
- protected:
- int _nLength;
- char *_data;
- int _nAllocated;
- char _base[ BC_STRING_MINIMUM ];
- void _Init( void );
- void _EnsureAlloc( const int nAmount, bool bPreserveContent = true );
- };
- /*
- ====================
- bcString::_Init
- Initializes the bcString.
- ====================
- */
- BC_INLINE void bcString::_Init( void ) {
- _nLength = 0;
- _data = _base;
- _data[ 0 ] = '\0';
- _nAllocated = BC_STRING_MINIMUM;
- }
- /*
- ====================
- bcString::_EnsureAlloc
- Makes sure stuff is allocated.
- ====================
- */
- BC_INLINE void bcString::_EnsureAlloc( const int nAmount, bool bPreserveContent ) {
- if( nAmount > _nAllocated ) {
- Reallocate( nAmount, bPreserveContent );
- };
- }
- /*
- ====================
- bcString::bcString
- Creates the bcString.
- ====================
- */
- BC_INLINE bcString::bcString( void ) {
- _Init();
- }
- BC_INLINE bcString::bcString( const bcString &sText ) {
- int nLength;
- _Init();
- nLength = sText.Length();
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, sText._data );
- _nLength = nLength;
- }
- BC_INLINE bcString::bcString( const bcString &sText, int nStart, int nEnd ) {
- int nLength, i;
- _Init();
- ValueRange( nStart, 0, sText.Length() - 1 );
- ValueRange( nEnd, 0, sText.Length() - 1 );
- nLength = nEnd - nStart;
- if( nLength < 0 ) {
- nLength = 0;
- }
- _EnsureAlloc( nLength + 1, false );
- for( i = 0; i < nLength; i++ ) {
- _data[ i ] = sText[ nStart + i ];
- }
- _data[ i + 1 ] = '\0';
- _nLength = nLength;
- }
- BC_INLINE bcString::bcString( const char *szText ) {
- int nLength;
- _Init();
- if( szText ) {
- nLength = strlen( szText );
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, szText );
- _nLength = nLength;
- }
- }
- BC_INLINE bcString::bcString( const char *szText, int nStart, int nEnd ) {
- int nPartLength, nStringLength, i;
- _Init();
- if( szText ) {
- nStringLength = strlen( szText );
- ValueRange( nStart, 0, nStringLength - 1 );
- ValueRange( nEnd, 0, nStringLength - 1 );
- nPartLength = nEnd - nStart + 1;
- if( nPartLength < 0 ) {
- nPartLength = 0;
- }
- _EnsureAlloc( nPartLength + 1, false );
- for( i = 0; i < nPartLength; i++ ) {
- _data[ i ] = szText[ nStart + i ];
- }
- _data[ i ] = '\0';
- _nLength = nPartLength;
- }
- }
- BC_INLINE bcString::bcString( const bool bFlag ) {
- int nLength;
- _Init();
- if( bFlag ) {
- nLength = strlen( BC_BOOL_TRUE_STRING );
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, BC_BOOL_TRUE_STRING );
- //_data[ nLength ] = '\0';
- _nLength = nLength;
- } else {
- nLength = strlen( BC_BOOL_FALSE_STRING );
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, BC_BOOL_FALSE_STRING );
- //_data[ nLength ] = '\0';
- _nLength = nLength;
- }
- }
- BC_INLINE bcString::bcString( const char nCharacter ) {
- _Init();
- _EnsureAlloc( 2, false );
- _data[ 0 ] = nCharacter;
- _data[ 1 ] = '\0';
- _nLength = 1;
- }
- BC_INLINE bcString::bcString( const int nNumber ) {
- char szNumber[ 12 ];
- int nLength;
- _Init();
- nLength = sprintf( szNumber, "%i", nNumber );
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, szNumber );
- _nLength = nLength;
- }
- BC_INLINE bcString::bcString( const unsigned nNumber ) {
- char szNumber[ 12 ];
- int nLength;
- _Init();
- nLength = sprintf( szNumber, "%i", nNumber );
- _EnsureAlloc( nLength + 1, false );
- strcpy( _data, szNumber );
- _nLength = nLength;
- }
- BC_INLINE bcString::bcString( const float fNumber ) {
- char szNumber[ 32 ];
- int nLength;
- _Init();
- //TODO: _snprintf_s might not work for non-VC compilers(?)
- nLength = bcMath::Minimumi( _snprintf_s( szNumber, sizeof( szNumber ), "%f", fNumber ), 32 );
- while( nLength > 0 && szNumber[ nLength - 1 ] == '0' ) szNumber[ --nLength ] = '\0';
- while( nLength > 0 && szNumber[ nLength - 1 ] == '.' ) szNumber[ --nLength ] = '\0';
- _EnsureAlloc( nLength + 1 );
- strcpy( _data, szNumber );
- _nLength = nLength;
- }
- BC_INLINE bcString::bcString( const double fNumber ) {
- char szNumber[ 64 ];
- int nLength;
- _Init();
- //TODO: _snprintf_s might not work for non-VC compilers(?)
- nLength = bcMath::Minimumi( _snprintf_s( szNumber, sizeof( szNumber ), "%f", fNumber ), 64 );
- while( nLength > 0 && szNumber[ nLength - 1 ] == '0' ) szNumber[ --nLength ] = '\0';
- while( nLength > 0 && szNumber[ nLength - 1 ] == '.' ) szNumber[ --nLength ] = '\0';
- _EnsureAlloc( nLength + 1 );
- strcpy( _data, szNumber );
- _nLength = nLength;
- }
- /*
- ====================
- bcString::~bcString
- Destroys the bcString.
- ====================
- */
- BC_INLINE bcString::~bcString( void ) {
- FreeData();
- }
- /*
- ====================
- bcString::CString
- Returns a constant pointer to the text.
- ====================
- */
- BC_INLINE const char *bcString::CString( void ) const {
- return _data;
- }
- /*
- ====================
- bcString::PChar
- Returns a pointer to the text. Since it allocates new memory, it should be avoided as long as CString is an alternative.
- ====================
- */
- BC_INLINE char *bcString::PChar( void ) const {
- char *pText = new char[ _nLength + 1 ];
- strcpy( pText, _data );
- return pText;
- }
- /*
- ====================
- bcString::operator const char *
- Returns a constant pointer to the text.
- ====================
- */
- BC_INLINE bcString::operator const char *( void ) const {
- return CString();
- }
- /*
- ====================
- bcString::operator[]
- Returns the value of the char at the desired position, rvalue.
- ====================
- */
- BC_INLINE char bcString::operator[]( int nChar ) const {
- return _data[ nChar ];
- }
- /*
- ====================
- bcString::operator[]
- Returns the reference to the char at the desired position, lvalue.
- ====================
- */
- BC_INLINE char &bcString::operator[]( int nChar ) {
- return _data[ nChar ];
- }
- /*
- ====================
- bcString::operator=
- Assigns another string to this instance.
- ====================
- */
- BC_INLINE void bcString::operator=( const bcString &sText ) {
- int nLength;
- nLength = sText.Length();
- _EnsureAlloc( nLength + 1, false );
- memcpy( _data, sText._data, nLength );
- _data[ nLength ] = '\0';
- _nLength = nLength;
- }
- /*
- ====================
- bcString::operator=
- Assigns another string to this instance.
- ====================
- */
- BC_INLINE void bcString::operator=( const char *szText ) {
- if( szText ) {
- int nLength;
- nLength = strlen( szText );
- _EnsureAlloc( nLength + 1, false );
- memcpy( _data, szText, nLength );
- _data[ nLength ] = '\0';
- _nLength = nLength;
- }
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated strings.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sTextA, const bcString &sTextB ) {
- bcString sResult( sTextA );
- sResult.Append( sTextB );
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated strings.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sTextA, const char *szTextB ) {
- bcString sResult( sTextA );
- sResult.Append( szTextB );
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated strings.
- ====================
- */
- BC_INLINE bcString operator+( const char *szTextA, const bcString &sTextB ) {
- bcString sResult( szTextA );
- sResult.Append( sTextB );
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const bool bFlag ) {
- bcString sResult( sText );
- sResult += bFlag;
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const char nCharacter ) {
- bcString sResult( sText );
- sResult.Append( nCharacter );
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const int nNumber ) {
- bcString sResult( sText );
- sResult += nNumber;
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const unsigned nNumber ) {
- bcString sResult( sText );
- sResult += nNumber;
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const float fNumber ) {
- bcString sResult( sText );
- sResult += fNumber;
- return sResult;
- }
- /*
- ====================
- bcString::operator+
- Returns the two concatenated arguments.
- ====================
- */
- BC_INLINE bcString operator+( const bcString &sText, const double fNumber ) {
- bcString sResult( sText );
- sResult += fNumber;
- return sResult;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const bcString &sText ) {
- Append( sText );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const char *szText ) {
- Append( szText );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const bool bFlag ) {
- Append( bFlag ? BC_BOOL_TRUE_STRING : BC_BOOL_FALSE_STRING );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const char nCharacter ) {
- Append( nCharacter );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const int nNumber ) {
- char pBuffer[ 12 ];
- sprintf( pBuffer, "%i", nNumber );
- Append( pBuffer );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const unsigned nNumber ) {
- char pBuffer[ 12 ];
- sprintf( pBuffer, "%u", nNumber );
- Append( pBuffer );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const float fNumber ) {
- char szNumber[ 32 ];
- int nLength;
- //TODO: _snprintf_s might not work for non-VC compilers(?)
- nLength = bcMath::Minimumi( _snprintf_s( szNumber, sizeof( szNumber ), "%f", fNumber ), 32 );
- while( nLength > 0 && szNumber[ nLength - 1 ] == '0' ) szNumber[ --nLength ] = '\0';
- while( nLength > 0 && szNumber[ nLength - 1 ] == '.' ) szNumber[ --nLength ] = '\0';
- Append( szNumber );
- return *this;
- }
- /*
- ====================
- bcString::operator+=
- Appends to this instance the argument given.
- ====================
- */
- BC_INLINE bcString &bcString::operator+=( const double fNumber ) {
- char szNumber[ 64 ];
- int nLength;
- //TODO: _snprintf_s might not work for non-VC compilers(?)
- nLength = bcMath::Minimumi( _snprintf_s( szNumber, sizeof( szNumber ), "%f", fNumber ), 64 );
- while( nLength > 0 && szNumber[ nLength - 1 ] == '0' ) szNumber[ --nLength ] = '\0';
- while( nLength > 0 && szNumber[ nLength - 1 ] == '.' ) szNumber[ --nLength ] = '\0';
- Append( szNumber );
- return *this;
- }
- /*
- ====================
- operator!=( bcString&, bcString& )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator==( const bcString &sTextA, const bcString &sTextB ) {
- return !strcmp( sTextA._data, sTextA._data );
- }
- /*
- ====================
- operator!=( bcString&, char* )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator==( const bcString &sTextA, const char *szTextB ) {
- assert( szTextB );
- return !strcmp( sTextA._data, szTextB );
- }
- /*
- ====================
- operator==( char*, bcString& )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator==( const char *szTextA, const bcString &sTextB ) {
- assert( szTextA );
- return !strcmp( szTextA, sTextB._data );
- }
- /*
- ====================
- operator!=( bcString&, bcString& )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator!=( const bcString &sTextA, const bcString &sTextB ) {
- return strcmp( sTextA._data, sTextB._data );
- }
- /*
- ====================
- operator!=( bcString&, char* )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator!=( const bcString &sTextA, const char *szTextB ) {
- assert( szTextB );
- return strcmp( sTextA._data, szTextB );
- }
- /*
- ====================
- operator!=( char*, bcString& )
- Compares the two strings given case-sensitively.
- ====================
- */
- BC_INLINE bool operator!=( const char *szTextA, const bcString &sTextB ) {
- assert( szTextA );
- return strcmp( szTextA, sTextB._data );
- }
- /*
- ====================
- bcString::Compare
- Compares this intance with the string given case-sensitively.
- ====================
- */
- BC_INLINE bool bcString::Compare( const char *szText ) const {
- assert( szText );
- return !strcmp( _data, szText );
- }
- /*
- ====================
- bcString::CompareUntil
- Compares this intance with the string given case-sensitively until it reaches the position specified by nEnd.
- ====================
- */
- BC_INLINE bool bcString::CompareUntil( const char *szText, int nEnd ) const {
- assert( szText );
- if( nEnd < 0 ) {
- nEnd = 0;
- }
- return !strncmp( _data, szText, nEnd + 1 );
- }
- /*
- ====================
- bcString::ComparePrefix
- Compares this intance with the string given case-sensitively until it reaches the end of the given string.
- ====================
- */
- BC_INLINE bool bcString::ComparePrefix( const char *szText ) const {
- assert( szText );
- return !strncmp( _data, szText, strlen( szText ) );
- }
- /*
- ====================
- bcString::CompareIns
- Compares this intance with the string given case-insensitively.
- ====================
- */
- BC_INLINE bool bcString::CompareIns( const char *szText ) const {
- assert( szText );
- return Istrcmp( _data, szText );
- }
- /*
- ====================
- bcString::CompareUntilIns
- Compares this intance with the string given case-sensitively until it reaches nEnd.
- ====================
- */
- BC_INLINE bool bcString::CompareUntilIns( const char *szText, int nEnd ) const {
- assert( szText );
- if( nEnd < 0 ) {
- nEnd = 0;
- }
- return Istrncmp( _data, szText, nEnd + 1 );
- }
- /*
- ====================
- bcString::ComparePrefixIns
- Compares this intance with the string given case-sensitively until it reaches the end of the given string.
- ====================
- */
- BC_INLINE bool bcString::ComparePrefixIns( const char *szText ) const {
- assert( szText );
- return Istrncmp( _data, szText, strlen( szText ) );
- }
- /*
- ====================
- bcString::Length
- Returns the length of this instance.
- ====================
- */
- BC_INLINE int bcString::Length( void ) const {
- return _nLength;
- }
- /*
- ====================
- bcString::Allocated
- Returns the amount of bytes this instance has allocated for the data.
- ====================
- */
- BC_INLINE int bcString::Allocated( void ) const {
- return _nAllocated;
- }
- /*
- ====================
- bcString::Clear
- Clears the string.
- ====================
- */
- BC_INLINE bcString &bcString::Clear( void ) {
- delete[] _data;
- _data = _base;
- _data[ 0 ] = '\0';
- _nAllocated = BC_STRING_MINIMUM;
- _nLength = 0;
- return *this;
- }
- /*
- ====================
- bcString::Empty
- Removes all characters the string.
- ====================
- */
- BC_INLINE bcString &bcString::Empty( void ) {
- _data[ 0 ] = '\0';
- _nLength = 0;
- return *this;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to this instance.
- ====================
- */
- BC_INLINE bcString &bcString::Append( const char nCharacter ) {
- _EnsureAlloc( ++_nLength );
- _data[ _nLength - 1 ] = nCharacter;
- _data[ _nLength ] = '\0';
- return *this;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to this instance.
- ====================
- */
- BC_INLINE bcString &bcString::Append( const bcString &sText ) {
- _EnsureAlloc( _nLength + sText._nLength + 1 );
- strcpy( _data + _nLength, sText._data );
- _nLength += sText._nLength;
- return *this;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to this instance.
- ====================
- */
- BC_INLINE bcString &bcString::Append( const char *szText ) {
- assert( szText );
- int nLength;
- nLength = strlen( szText );
- _EnsureAlloc( _nLength + nLength + 1 );
- strcpy( _data + _nLength, szText );
- _nLength += nLength;
- return *this;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to this instance.
- ====================
- */
- BC_INLINE bcString &bcString::Append( const char *szText, int nLength ) {
- assert( szText );
- ValueRange( nLength, 0, static_cast<signed>( strlen( szText ) ) - 1 );
- _EnsureAlloc( _nLength + nLength + 1 );
- strcpy( _data + _nLength, szText );
- _nLength += nLength;
- return *this;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to this instance at nIndex.
- ====================
- */
- BC_INLINE bcString &bcString::Insert( const char nCharacter, const int nIndex ) {
- assert( nIndex < _nLength );
- char *pCurrent;
- _EnsureAlloc( ++_nLength );
- pCurrent = _data + _nLength;
- while( true ) {
- *pCurrent-- = *( pCurrent - 1 );
- if( pCurrent == _data + nIndex ) break;
- }
- *( _data + nIndex ) = nCharacter;
- return *this;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to this instance at nIndex.
- ====================
- */
- BC_INLINE bcString &bcString::Insert( const bcString &sText, const int nIndex ) {
- assert( nIndex < _nLength );
- char *pCurrent;
- int nLength;
- nLength = sText._nLength;
- _EnsureAlloc( _nLength + nLength + 1 );
- pCurrent = _data + _nLength + nLength;
- while( true ) {
- *pCurrent-- = *( pCurrent - nLength );
- if( pCurrent == _data + nIndex ) break;
- }
- memcpy( _data + nIndex, sText._data, nLength );
- _nLength += nLength;
- return *this;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to this instance at nIndex.
- ====================
- */
- BC_INLINE bcString &bcString::Insert( const char *szText, const int nIndex ) {
- assert( szText );
- assert( nIndex < _nLength );
- char *pCurrent;
- int nLength;
- nLength = strlen( szText );
- _EnsureAlloc( _nLength + nLength + 1 );
- pCurrent = _data + _nLength + nLength;
- while( true ) {
- *pCurrent-- = *( pCurrent - nLength );
- if( pCurrent == _data + nIndex ) break;
- }
- memcpy( _data + nIndex, szText, nLength );
- _nLength += nLength;
- return *this;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to this instance at nIndex.
- ====================
- */
- BC_INLINE bcString &bcString::Insert( const char *szText, const int nLength, const int nIndex ) {
- assert( szText );
- assert( nIndex < _nLength );
- char *pCurrent;
- _EnsureAlloc( _nLength + nLength + 1 );
- pCurrent = _data + _nLength + nLength;
- while( true ) {
- *pCurrent-- = *( pCurrent - nLength );
- if( pCurrent == _data + nIndex ) break;
- }
- memcpy( _data + nIndex, szText, nLength );
- _nLength += nLength;
- return *this;
- }
- /*
- ====================
- bcString::ToLower
- Converts this string to lower case.
- ====================
- */
- BC_INLINE bcString &bcString::ToLower( void ) {
- char *nChar;
- int i;
- nChar = _data;
- for( i = 0; i < _nLength; i++ ) {
- if( ( *nChar >= 'A' && *nChar <= 'Z' ) || ( *nChar >= 0xC0 && *nChar <= 0xDE && *nChar != 0xD7 ) ) {
- *nChar += 0x20;
- }
- ++nChar;
- }
- return *this;
- }
- /*
- ====================
- bcString::ToUpper
- Converts this string to upper case.
- ====================
- */
- BC_INLINE bcString &bcString::ToUpper( void ) {
- char *nChar;
- int i;
- nChar = _data;
- for( i = 0; i < _nLength; i++ ) {
- if( ( *nChar >= 'a' && *nChar <= 'z' ) || ( *nChar >= 0xE0 && *nChar <= 0xFE && *nChar != 0xF7 ) ) {
- *nChar -= 0x20;
- }
- ++nChar;
- }
- return *this;
- }
- /*
- ====================
- bcString::Cut
- Cuts this string down to the length given.
- ====================
- */
- BC_INLINE bcString &bcString::Cut( const int nLength ) {
- assert( nLength <= _nLength );
- _data[ nLength ] = '\0';
- _nLength = nLength;
- return *this;
- }
- /*
- ====================
- bcString::Fill
- Fills this string with nNewLength nCharacters.
- ====================
- */
- BC_INLINE bcString &bcString::Fill( const char nCharacter, const int nNewLength ) {
- assert( nNewLength >= 0 );
- _EnsureAlloc( nNewLength + 1, false );
- memset( _data, static_cast<int>( nCharacter ), nNewLength );
- _nLength = nNewLength;
- _data[ _nLength ] = '\0';
- return *this;
- }
- /*
- ====================
- bcString::Find
- Finds the first occasion of nCharacter in this string, starting to look at nStart and stopping at nEnd.
- Returns -1 on fail.
- ====================
- */
- BC_INLINE int bcString::Find( const char nCharacter, int nStart, int nEnd ) const {
- ValueRange( nStart, 0, _nLength - 1 );
- ValueRange( nEnd, 0, _nLength - 1 );
- do {
- if( _data[ nStart ] = nCharacter ) return nStart;
- } while( nStart++ != nEnd );
- return -1;
- }
- /*
- ====================
- bcString::Find
- Finds the first occasion of szText in this string, starting to look at nStart and stopping at nEnd.
- Returns -1 on fail.
- ====================
- */
- BC_INLINE int bcString::Find( const char *szText, bool bCaseSensitive, int nStart, int nEnd ) const {
- assert( szText );
- ValueRange( nStart, 0, _nLength - 1 );
- ValueRange( nEnd, 0, _nLength - 1 );
- int nLength;
- nLength = strlen( szText );
- do {
- if( bCaseSensitive ) {
- if( !strncmp( _data + nStart, szText, nLength ) ) return nStart;
- } else {
- if( Istrncmp( _data + nStart, szText, nLength ) ) return nStart;
- }
- } while( nStart++ != nEnd );
- return -1;
- }
- /*
- ====================
- bcString::Filter
- Sees if this string conforms to the filter. Use:
- * as a wildcard
- ? as a single-character wildcard
- [a-z] as a character range wildcard
- ====================
- */
- BC_INLINE bool bcString::Filter( const char *szFilter, bool bCaseSensitive ) const {
- assert( szFilter );
- return Filter( szFilter, _data, bCaseSensitive );
- }
- /*
- ====================
- bcString::Last
- Finds the last occasion of nCharacter in this string.
- Returns -1 on fail.
- ====================
- */
- BC_INLINE int bcString::Last( const char nCharacter ) const {
- int nPosition;
- nPosition = _nLength - 1;
- do {
- if( _data[ nPosition ] == nCharacter ) return nPosition;
- } while( nPosition-- >= 0 );
- return -1;
- }
- /*
- ====================
- bcString::Last
- Finds the last occasion of szText in this string.
- Returns -1 on fail.
- ====================
- */
- BC_INLINE int bcString::Last( const char *szText, bool bCaseSensitive ) const {
- assert( szText );
- int nPosition, nLength;
- nLength = strlen( szText );
- nPosition = _nLength - nLength;
- do {
- if( !strncmp( _data + nPosition, szText, nLength ) ) return nPosition;
- } while( nPosition-- >= 0 );
- return -1;
- }
- /*
- ====================
- bcString::Left
- Returns the first nLength characters in a new bcString.
- ====================
- */
- BC_INLINE bcString bcString::Left( const int nLength ) const {
- return bcString( _data, 0, nLength - 1 );
- }
- /*
- ====================
- bcString::Right
- Returns the last nLength characters in a new bcString.
- ====================
- */
- BC_INLINE bcString bcString::Right( const int nLength ) const {
- return bcString( _data, _nLength - nLength, _nLength - 1 );
- }
- /*
- ====================
- bcString::Mid
- Returns the nLength characters starting from nStart in a new bcString.
- ====================
- */
- BC_INLINE bcString bcString::Mid( const int nStart, const int nLength ) const {
- assert( nStart + nLength <= _nLength );
- return bcString( _data, nStart, nStart + nLength - 1 );
- }
- /*
- ====================
- bcString::Remove
- Removes the portion of this string starting at nStart with a length of nLength.
- ====================
- */
- BC_INLINE bcString &bcString::Remove( int nStart, int nLength ) {
- ValueRange( nStart, 0, _nLength - 1 );
- ValueRange( nLength, 0, _nLength - 1 );
- Remove( _data, nStart, nStart + nLength - 1 );
- _nLength -= nLength;
- return *this;
- }
- /*
- ====================
- bcString::Replace
- Replaces all occasions of szOld with szNew.
- ====================
- */
- BC_INLINE bcString &bcString::Replace( const char *szOld, const char *szNew ) {
- assert( szOld );
- assert( szNew );
- int i, nOldLength, nNewLength;
- nOldLength = strlen( szOld );
- nNewLength = strlen( szNew );
- for( i = 0; i < _nLength - nOldLength; i++ ) {
- if( !strncmp( _data + i, szOld, nOldLength ) ) {
- Remove( i, nOldLength );
- Insert( szNew, i );
- i += nNewLength - 1;
- }
- }
- return *this;
- }
- // STATIC VERSIONS
- /*
- ====================
- bcString::Clear
- Clears the string.
- ====================
- */
- BC_INLINE bcString bcString::Clear( const bcString &String ) {
- bcString Result( String );
- Result.Clear();
- return Result;
- }
- /*
- ====================
- bcString::Empty
- Removes all characters the string.
- ====================
- */
- BC_INLINE bcString bcString::Empty( const bcString &String ) {
- bcString Result( String );
- Result.Empty();
- return Result;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to the string.
- ====================
- */
- BC_INLINE bcString bcString::Append( const bcString &String, const char nCharacter ) {
- bcString Result( String );
- Result.Append( nCharacter );
- return Result;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to the string.
- ====================
- */
- BC_INLINE bcString bcString::Append( const bcString &String, const bcString &sAppendix ) {
- bcString Result( String );
- Result.Append( sAppendix );
- return Result;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to the string.
- ====================
- */
- BC_INLINE bcString bcString::Append( const bcString &String, const char *szText ) {
- bcString Result( String );
- Result.Append( szText );
- return Result;
- }
- /*
- ====================
- bcString::Append
- Appends the variable passed to the string.
- ====================
- */
- BC_INLINE bcString bcString::Append( const bcString &String, const char *szText, const int nLength ) {
- bcString Result( String );
- Result.Append( szText, nLength );
- return Result;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to the string passed at nIndex.
- ====================
- */
- BC_INLINE bcString bcString::Insert( const bcString &String, const char nCharacter, const int nIndex ) {
- bcString Result( String );
- Result.Insert( nCharacter, nIndex );
- return Result;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to the string passed at nIndex.
- ====================
- */
- BC_INLINE bcString bcString::Insert( const bcString &String, const bcString &sInsertion, const int nIndex ) {
- bcString Result( String );
- Result.Insert( sInsertion, nIndex );
- return Result;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to the string passed at nIndex.
- ====================
- */
- BC_INLINE bcString bcString::Insert( const bcString &String, const char *szText, const int nIndex ) {
- bcString Result( String );
- Result.Insert( szText, nIndex );
- return Result;
- }
- /*
- ====================
- bcString::Insert
- Inserts the variable passed to the string passed at nIndex.
- ====================
- */
- BC_INLINE bcString bcString::Insert( const bcString &String, const char *szText, const int nLength, const int nIndex ) {
- bcString Result( String );
- Result.Insert( szText, nLength, nIndex );
- return Result;
- }
- /*
- ====================
- bcString::ToLower
- Converts the string passed to lower case.
- ====================
- */
- BC_INLINE bcString bcString::ToLower( const bcString &String ) {
- bcString Result( String );
- Result.ToLower();
- return Result;
- }
- /*
- ====================
- bcString::ToUpper
- Converts the string passed to upper case.
- ====================
- */
- BC_INLINE bcString bcString::ToUpper( const bcString &String ) {
- bcString Result( String );
- Result.ToUpper();
- return Result;
- }
- /*
- ====================
- bcString::Cut
- Cuts the string passed down to the length given.
- ====================
- */
- BC_INLINE bcString bcString::Cut( const bcString &String, const int nLength ) {
- bcString Result( String );
- Result.Cut( nLength );
- return Result;
- }
- /*
- ====================
- bcString::Remove
- Removes the portion of the string passed starting at nStart with a length of nLength.
- ====================
- */
- BC_INLINE bcString bcString::Remove( const bcString &String, int nStart, int nLength ) {
- bcString Result( String );
- Result.Remove( nStart, nLength );
- return Result;
- }
- /*
- ====================
- bcString::Replace
- Replaces all occasions of szOld with szNew.
- ====================
- */
- BC_INLINE bcString bcString::Replace( const bcString &String, const char *szOld, const char *szNew ) {
- bcString Result( String );
- Result.Replace( szOld, szNew );
- return Result;
- }
- }
- #endif //BC_STRING_H
- // FUCKIN HEADER END
- /*********************************************************************************/
- // FUCKIN CPP BEGIN
- #include "stdafx.h"
- /*
- ====================
- bcString::Reallocate
- Allocates new place for the bcString.
- ====================
- */
- void LibBC::bcString::Reallocate( const int nAmount, bool bPreserveContent ) {
- assert( nAmount >= 0 );
- int nNewSize;
- int nMod = nAmount % BC_STRING_SLICE;
- char *pNewBuffer;
- if( !nMod ) {
- nNewSize = nAmount;
- } else {
- nNewSize = nAmount + BC_STRING_SLICE - nMod;
- }
- pNewBuffer = new char[ nNewSize ];
- if( bPreserveContent ) {
- _data[ _nLength ] = '\0';
- strcpy( pNewBuffer, _data );
- }
- if( _data && _data != _base ) {
- delete[] _data;
- }
- _data = pNewBuffer;
- _nAllocated = nNewSize;
- }
- /*
- ====================
- bcString::Istrcmp
- Compares the two strings case-insensitively.
- ====================
- */
- bool LibBC::bcString::Istrcmp( const char *szTextA, const char *szTextB ) {
- char nCharA, nCharB, nDifference, nMinimum;
- do {
- nCharA = *szTextA++;
- nCharB = *szTextB++;
- nDifference = nCharA - nCharB;
- if( bcMath::Absolute8i( nDifference ) == 0x20 ) {
- nMinimum = bcMath::Minimum8i( nCharA, nCharB );
- if( nDifference > 0 ) {
- if( ( nCharB <= 'A' && nCharB >= 'Z' && nCharB <= 0xC0 && nCharB >= 0xDE ) || nCharB == 0xD7 ) {
- return false;
- }
- } else {
- if( ( nCharA <= 'A' && nCharA >= 'Z' && nCharA <= 0xC0 && nCharA >= 0xDE ) || nCharA == 0xD7 ) {
- return false;
- }
- }
- } else if( nDifference ) return false;
- } while( nCharA && nCharB );
- return true;
- }
- /*
- ====================
- bcString::Istrncmp
- Compares the two strings case-insensitively until it reaches nLength.
- ====================
- */
- bool LibBC::bcString::Istrncmp( const char *szTextA, const char *szTextB, size_t nLength ) {
- char nCharA, nCharB, nDifference, nMinimum;
- dword nPosition;
- for( nPosition = 0; nPosition < nLength; ++nPosition ) {
- nCharA = *szTextA++;
- nCharB = *szTextB++;
- nDifference = nCharA - nCharB;
- if( bcMath::Absolute8i( nDifference ) == 0x20 ) {
- nMinimum = bcMath::Minimum8i( nCharA, nCharB );
- if( nDifference > 0 ) {
- if( ( nCharB <= 'A' && nCharB >= 'Z' && nCharB <= 0xC0 && nCharB >= 0xDE ) || nCharB == 0xD7 ) {
- return false;
- }
- } else {
- if( ( nCharA <= 'A' && nCharA >= 'Z' && nCharA <= 0xC0 && nCharA >= 0xDE ) || nCharA == 0xD7 ) {
- return false;
- }
- }
- } else if( nDifference ) return false;
- };
- return true;
- }
- /*
- ====================
- bcString::FreeData
- Frees the data used by the string.
- ====================
- */
- void LibBC::bcString::FreeData( void ) {
- if( _data && _data != _base ) {
- delete[] _data;
- _data = _base;
- }
- }
- /*
- ====================
- bcString::Filter
- Sees if the string given conforms to the filter. Use:
- * as a wildcard
- ? as a single-character wildcard
- [a-z] as a character range wildcard
- ====================
- */
- bool LibBC::bcString::Filter( const char *szFilter, const char *szText, bool bCaseSensitive ) {
- bcString sBuffer;
- bool bFound;
- int i, nIndex;
- while( *szFilter ) {
- if ( *szFilter == '*' ) {
- szFilter++;
- sBuffer.Empty();
- for ( i = 0; *szFilter; i++ ) {
- if( *szFilter == '*' || *szFilter == '?' || ( *szFilter == '[' && *( szFilter + 1 ) != '[' ) ) {
- break;
- }
- sBuffer += *szFilter;
- if ( *szFilter == '[' ) {
- szFilter++;
- }
- szFilter++;
- }
- if ( sBuffer.Length() ) {
- nIndex = bcString( szText ).Find( sBuffer.CString(), bCaseSensitive );
- if ( nIndex == -1 ) {
- return false;
- }
- szText += nIndex + strlen( sBuffer );
- }
- } else if ( *szFilter == '?' ) {
- szFilter++;
- szText++;
- } else if ( *szFilter == '[' ) {
- if ( *( szFilter + 1 ) == '[' ) {
- if ( *szText != '[' ) {
- return false;
- }
- szFilter += 2;
- szText++;
- } else {
- szFilter++;
- bFound = false;
- while( *szFilter && !bFound ) {
- if ( *szFilter == ']' && *( szFilter + 1 ) != ']' ) {
- break;
- }
- if ( *( szFilter + 1 ) == '-' && *( szFilter + 2 ) && ( *(szFilter + 2 ) != ']' || *( szFilter + 3 ) == ']' ) ) {
- if ( bCaseSensitive ) {
- if ( *szText >= *szFilter && *szText <= *( szFilter + 2 ) ) {
- bFound = true;
- }
- }
- else {
- if ( toupper( *szText ) >= toupper( *szFilter ) && toupper( *szText ) <= toupper( *( szFilter + 2 ) ) ) {
- bFound = true;
- }
- }
- szFilter += 3;
- }
- else {
- if ( bCaseSensitive ) {
- if ( *szFilter == *szText ) {
- bFound = true;
- }
- }
- else {
- if ( toupper(*szFilter) == toupper( *szText ) ) {
- bFound = true;
- }
- }
- szFilter++;
- }
- }
- if ( !bFound ) {
- return false;
- }
- while( *szFilter ) {
- if ( *szFilter == ']' && *(szFilter+1) != ']' ) {
- break;
- }
- szFilter++;
- }
- szFilter++;
- szText++;
- }
- } else {
- if ( bCaseSensitive ) {
- if ( *szFilter != *szText ) {
- return false;
- }
- } else {
- if ( toupper( *szFilter ) != toupper( *szText ) ) {
- return false;
- }
- }
- szFilter++;
- szText++;
- }
- }
- return true;
- }
- void LibBC::bcString::Remove( char *szText, int nStart, int nEnd ) {
- assert( szText );
- int i, nLength;
- nLength = strlen( szText );
- if( nStart >= nLength ) nStart = nLength - 1;
- if( nEnd >= nLength ) nEnd = nLength - 1;
- for( i = nStart; i <= nLength - 1; i++ ) {
- szText[ i ] = szText[ i + nEnd - nStart + 1 ];
- }
- szText[ i ] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment