doom3-bfg/neo/idlib/Str.h

1414 lines
32 KiB
C
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#ifndef __STR_H__
#define __STR_H__
/*
===============================================================================
Character string
===============================================================================
*/
#define ASSERT_ENUM_STRING( string, index ) ( 1 / (int)!( string - index ) ) ? #string : ""
enum utf8Encoding_t
{
2012-11-26 18:58:24 +00:00
UTF8_PURE_ASCII, // no characters with values > 127
UTF8_ENCODED_BOM, // characters > 128 encoded with UTF8, but no byte-order-marker at the beginning
UTF8_ENCODED_NO_BOM, // characters > 128 encoded with UTF8, with a byte-order-marker at the beginning
UTF8_INVALID, // has values > 127 but isn't valid UTF8
2012-11-26 18:58:24 +00:00
UTF8_INVALID_BOM // has a byte-order-marker at the beginning, but isn't valuid UTF8 -- it's messed up
};
// these library functions should not be used for cross platform compatibility
#define strcmp idStr::Cmp // use_idStr_Cmp
#define strncmp use_idStr_Cmpn
#if defined( StrCmpN )
#undef StrCmpN
#endif
#define StrCmpN use_idStr_Cmpn
#if defined( strcmpi )
#undef strcmpi
#endif
#define strcmpi use_idStr_Icmp
#if defined( StrCmpI )
#undef StrCmpI
#endif
#define StrCmpI use_idStr_Icmp
#if defined( StrCmpNI )
#undef StrCmpNI
#endif
#define StrCmpNI use_idStr_Icmpn
#define stricmp idStr::Icmp // use_idStr_Icmp
#undef strcasecmp // DG: redefining this without undefining it causes tons of compiler warnings
2012-11-26 18:58:24 +00:00
#define _stricmp use_idStr_Icmp
#define strcasecmp use_idStr_Icmp
#define strnicmp use_idStr_Icmpn
#define _strnicmp use_idStr_Icmpn
#define _memicmp use_idStr_Icmpn
#define snprintf use_idStr_snPrintf
#define _snprintf use_idStr_snPrintf
#define vsnprintf use_idStr_vsnPrintf
#define _vsnprintf use_idStr_vsnPrintf
class idVec4;
#ifndef FILE_HASH_SIZE
#define FILE_HASH_SIZE 1024
#endif
// color escape character
const int C_COLOR_ESCAPE = '^';
const int C_COLOR_DEFAULT = '0';
const int C_COLOR_RED = '1';
const int C_COLOR_GREEN = '2';
const int C_COLOR_YELLOW = '3';
const int C_COLOR_BLUE = '4';
const int C_COLOR_CYAN = '5';
const int C_COLOR_ORANGE = '6';
const int C_COLOR_WHITE = '7';
const int C_COLOR_GRAY = '8';
const int C_COLOR_BLACK = '9';
// color escape string
#define S_COLOR_DEFAULT "^0"
#define S_COLOR_RED "^1"
#define S_COLOR_GREEN "^2"
#define S_COLOR_YELLOW "^3"
#define S_COLOR_BLUE "^4"
#define S_COLOR_CYAN "^5"
#define S_COLOR_ORANGE "^6"
#define S_COLOR_WHITE "^7"
#define S_COLOR_GRAY "^8"
#define S_COLOR_BLACK "^9"
// make idStr a multiple of 16 bytes long
// don't make too large to keep memory requirements to a minimum
const int STR_ALLOC_BASE = 20;
const int STR_ALLOC_GRAN = 32;
typedef enum
{
2012-11-26 18:58:24 +00:00
MEASURE_SIZE = 0,
MEASURE_BANDWIDTH
} Measure_t;
class idStr
{
2012-11-26 18:58:24 +00:00
public:
idStr();
idStr( const idStr& text );
idStr( const idStr& text, int start, int end );
idStr( const char* text );
idStr( const char* text, int start, int end );
explicit idStr( const bool b );
explicit idStr( const char c );
explicit idStr( const int i );
explicit idStr( const unsigned u );
explicit idStr( const float f );
~idStr();
2012-11-26 18:58:24 +00:00
size_t Size() const;
const char* c_str() const;
operator const char* () const;
operator const char* ();
2012-11-26 18:58:24 +00:00
char operator[]( int index ) const;
char& operator[]( int index );
void operator=( const idStr& text );
void operator=( const char* text );
friend idStr operator+( const idStr& a, const idStr& b );
friend idStr operator+( const idStr& a, const char* b );
friend idStr operator+( const char* a, const idStr& b );
friend idStr operator+( const idStr& a, const float b );
friend idStr operator+( const idStr& a, const int b );
friend idStr operator+( const idStr& a, const unsigned b );
friend idStr operator+( const idStr& a, const bool b );
friend idStr operator+( const idStr& a, const char b );
idStr& operator+=( const idStr& a );
idStr& operator+=( const char* a );
idStr& operator+=( const float a );
idStr& operator+=( const char a );
idStr& operator+=( const int a );
idStr& operator+=( const unsigned a );
idStr& operator+=( const bool a );
// case sensitive compare
friend bool operator==( const idStr& a, const idStr& b );
friend bool operator==( const idStr& a, const char* b );
friend bool operator==( const char* a, const idStr& b );
// case sensitive compare
friend bool operator!=( const idStr& a, const idStr& b );
friend bool operator!=( const idStr& a, const char* b );
friend bool operator!=( const char* a, const idStr& b );
// case sensitive compare
int Cmp( const char* text ) const;
int Cmpn( const char* text, int n ) const;
int CmpPrefix( const char* text ) const;
// case insensitive compare
int Icmp( const char* text ) const;
int Icmpn( const char* text, int n ) const;
int IcmpPrefix( const char* text ) const;
// case insensitive compare ignoring color
int IcmpNoColor( const char* text ) const;
// compares paths and makes sure folders come first
int IcmpPath( const char* text ) const;
int IcmpnPath( const char* text, int n ) const;
int IcmpPrefixPath( const char* text ) const;
2012-11-26 18:58:24 +00:00
int Length() const;
int Allocated() const;
void Empty();
bool IsEmpty() const;
void Clear();
void Append( const char a );
void Append( const idStr& text );
void Append( const char* text );
void Append( const char* text, int len );
2012-11-26 18:58:24 +00:00
void Insert( const char a, int index );
void Insert( const char* text, int index );
2012-11-26 18:58:24 +00:00
void ToLower();
void ToUpper();
bool IsNumeric() const;
bool IsColor() const;
bool HasLower() const;
bool HasUpper() const;
int LengthWithoutColors() const;
idStr& RemoveColors();
2012-11-26 18:58:24 +00:00
void CapLength( int );
void Fill( const char ch, int newlen );
2012-11-26 18:58:24 +00:00
ID_INLINE int UTF8Length();
ID_INLINE uint32 UTF8Char( int& idx );
static int UTF8Length( const byte* s );
static ID_INLINE uint32 UTF8Char( const char* s, int& idx );
static uint32 UTF8Char( const byte* s, int& idx );
2012-11-26 18:58:24 +00:00
void AppendUTF8Char( uint32 c );
ID_INLINE void ConvertToUTF8();
static bool IsValidUTF8( const uint8* s, const int maxLen, utf8Encoding_t& encoding );
static ID_INLINE bool IsValidUTF8( const char* s, const int maxLen, utf8Encoding_t& encoding )
{
return IsValidUTF8( ( const uint8* )s, maxLen, encoding );
}
static ID_INLINE bool IsValidUTF8( const uint8* s, const int maxLen );
static ID_INLINE bool IsValidUTF8( const char* s, const int maxLen )
{
return IsValidUTF8( ( const uint8* )s, maxLen );
}
2012-11-26 18:58:24 +00:00
int Find( const char c, int start = 0, int end = -1 ) const;
int Find( const char* text, bool casesensitive = true, int start = 0, int end = -1 ) const;
bool Filter( const char* filter, bool casesensitive ) const;
2012-11-26 18:58:24 +00:00
int Last( const char c ) const; // return the index to the last occurance of 'c', returns -1 if not found
const char* Left( int len, idStr& result ) const; // store the leftmost 'len' characters in the result
const char* Right( int len, idStr& result ) const; // store the rightmost 'len' characters in the result
const char* Mid( int start, int len, idStr& result ) const; // store 'len' characters starting at 'start' in result
2012-11-26 18:58:24 +00:00
idStr Left( int len ) const; // return the leftmost 'len' characters
idStr Right( int len ) const; // return the rightmost 'len' characters
idStr Mid( int start, int len ) const; // return 'len' characters starting at 'start'
void Format( VERIFY_FORMAT_STRING const char* fmt, ... ); // perform a threadsafe sprintf to the string
2012-11-26 18:58:24 +00:00
static idStr FormatInt( const int num, bool isCash = false ); // formats an integer as a value with commas
static idStr FormatCash( const int num )
{
return FormatInt( num, true );
}
2012-11-26 18:58:24 +00:00
void StripLeading( const char c ); // strip char from front as many times as the char occurs
void StripLeading( const char* string ); // strip string from front as many times as the string occurs
bool StripLeadingOnce( const char* string ); // strip string from front just once if it occurs
2012-11-26 18:58:24 +00:00
void StripTrailing( const char c ); // strip char from end as many times as the char occurs
void StripTrailing( const char* string ); // strip string from end as many times as the string occurs
bool StripTrailingOnce( const char* string ); // strip string from end just once if it occurs
2012-11-26 18:58:24 +00:00
void Strip( const char c ); // strip char from front and end as many times as the char occurs
void Strip( const char* string ); // strip string from front and end as many times as the string occurs
2012-11-26 18:58:24 +00:00
void StripTrailingWhitespace(); // strip trailing white space characters
idStr& StripQuotes(); // strip quotes around string
bool Replace( const char* old, const char* nw );
2012-11-26 18:58:24 +00:00
bool ReplaceChar( const char old, const char nw );
ID_INLINE void CopyRange( const char* text, int start, int end );
2012-11-26 18:58:24 +00:00
// file name methods
int FileNameHash() const; // hash key for the filename (skips extension)
idStr& BackSlashesToSlashes(); // convert slashes
idStr& SlashesToBackSlashes(); // convert slashes
idStr& SetFileExtension( const char* extension ); // set the given file extension
idStr& StripFileExtension(); // remove any file extension
idStr& StripAbsoluteFileExtension(); // remove any file extension looking from front (useful if there are multiple .'s)
idStr& DefaultFileExtension( const char* extension ); // if there's no file extension use the default
idStr& DefaultPath( const char* basepath ); // if there's no path use the default
void AppendPath( const char* text ); // append a partial path
idStr& StripFilename(); // remove the filename from a path
idStr& StripPath(); // remove the path from the filename
void ExtractFilePath( idStr& dest ) const; // copy the file path to another string
void ExtractFileName( idStr& dest ) const; // copy the filename to another string
void ExtractFileBase( idStr& dest ) const; // copy the filename minus the extension to another string
void ExtractFileExtension( idStr& dest ) const; // copy the file extension to another string
bool CheckExtension( const char* ext );
2012-11-26 18:58:24 +00:00
// char * methods to replace library functions
static int Length( const char* s );
static char* ToLower( char* s );
static char* ToUpper( char* s );
static bool IsNumeric( const char* s );
static bool IsColor( const char* s );
static bool HasLower( const char* s );
static bool HasUpper( const char* s );
static int LengthWithoutColors( const char* s );
static char* RemoveColors( char* s );
static int Cmp( const char* s1, const char* s2 );
static int Cmpn( const char* s1, const char* s2, int n );
static int Icmp( const char* s1, const char* s2 );
static int Icmpn( const char* s1, const char* s2, int n );
static int IcmpNoColor( const char* s1, const char* s2 );
static int IcmpPath( const char* s1, const char* s2 ); // compares paths and makes sure folders come first
static int IcmpnPath( const char* s1, const char* s2, int n ); // compares paths and makes sure folders come first
static void Append( char* dest, int size, const char* src );
static void Copynz( char* dest, const char* src, int destsize );
static int snPrintf( char* dest, int size, VERIFY_FORMAT_STRING const char* fmt, ... );
static int vsnPrintf( char* dest, int size, const char* fmt, va_list argptr );
static int FindChar( const char* str, const char c, int start = 0, int end = -1 );
static int FindText( const char* str, const char* text, bool casesensitive = true, int start = 0, int end = -1 );
static bool Filter( const char* filter, const char* name, bool casesensitive );
static void StripMediaName( const char* name, idStr& mediaName );
static bool CheckExtension( const char* name, const char* ext );
static const char* FloatArrayToString( const float* array, const int length, const int precision );
static const char* CStyleQuote( const char* str );
static const char* CStyleUnQuote( const char* str );
2012-11-26 18:58:24 +00:00
// hash keys
static int Hash( const char* string );
static int Hash( const char* string, int length );
static int IHash( const char* string ); // case insensitive
static int IHash( const char* string, int length ); // case insensitive
2012-11-26 18:58:24 +00:00
// character methods
static char ToLower( char c );
static char ToUpper( char c );
static bool CharIsPrintable( int c );
static bool CharIsLower( int c );
static bool CharIsUpper( int c );
static bool CharIsAlpha( int c );
static bool CharIsNumeric( int c );
static bool CharIsNewLine( char c );
static bool CharIsTab( char c );
static int ColorIndex( int c );
static idVec4& ColorForIndex( int i );
friend int sprintf( idStr& dest, const char* fmt, ... );
friend int vsprintf( idStr& dest, const char* fmt, va_list ap );
2012-11-26 18:58:24 +00:00
void ReAllocate( int amount, bool keepold ); // reallocate string data buffer
void FreeData(); // free allocated string memory
// format value in the given measurement with the best unit, returns the best unit
int BestUnit( const char* format, float value, Measure_t measure );
// format value in the requested unit and measurement
void SetUnit( const char* format, float value, int unit, Measure_t measure );
2012-11-26 18:58:24 +00:00
static void InitMemory();
static void ShutdownMemory();
static void PurgeMemory();
static void ShowMemoryUsage_f( const idCmdArgs& args );
2012-11-26 18:58:24 +00:00
int DynamicMemoryUsed() const;
static idStr FormatNumber( int number );
2012-11-26 18:58:24 +00:00
protected:
int len;
char* data;
2012-11-26 18:58:24 +00:00
int allocedAndFlag; // top bit is used to store a flag that indicates if the string data is static or not
char baseBuffer[ STR_ALLOC_BASE ];
2012-11-26 18:58:24 +00:00
void EnsureAlloced( int amount, bool keepold = true ); // ensure string data buffer is large anough
2012-11-26 18:58:24 +00:00
// sets the data point to the specified buffer... note that this ignores makes the passed buffer empty and ignores
// anything currently in the idStr's dynamic buffer. This method is intended to be called only from a derived class's constructor.
ID_INLINE void SetStaticBuffer( char* buffer, const int bufferLength );
2012-11-26 18:58:24 +00:00
private:
// initialize string using base buffer... call ONLY FROM CONSTRUCTOR
ID_INLINE void Construct();
2012-11-26 18:58:24 +00:00
static const uint32 STATIC_BIT = 31;
static const uint32 STATIC_MASK = 1u << STATIC_BIT;
static const uint32 ALLOCED_MASK = STATIC_MASK - 1;
ID_INLINE int GetAlloced() const
{
return allocedAndFlag & ALLOCED_MASK;
}
ID_INLINE void SetAlloced( const int a )
{
allocedAndFlag = ( allocedAndFlag & STATIC_MASK ) | ( a & ALLOCED_MASK );
}
ID_INLINE bool IsStatic() const
{
return ( allocedAndFlag & STATIC_MASK ) != 0;
}
ID_INLINE void SetStatic( const bool isStatic )
{
allocedAndFlag = ( allocedAndFlag & ALLOCED_MASK ) | ( isStatic << STATIC_BIT );
}
2012-11-26 18:58:24 +00:00
public:
static const int INVALID_POSITION = -1;
};
char* va( VERIFY_FORMAT_STRING const char* fmt, ... ) ATTRIBUTE_PRINTF(1, 2);
2012-11-26 18:58:24 +00:00
/*
================================================================================================
Sort routines for sorting idList<idStr>
================================================================================================
*/
class idSort_Str : public idSort_Quick< idStr, idSort_Str >
{
2012-11-26 18:58:24 +00:00
public:
int Compare( const idStr& a, const idStr& b ) const
{
return a.Icmp( b );
}
2012-11-26 18:58:24 +00:00
};
class idSort_PathStr : public idSort_Quick< idStr, idSort_PathStr >
{
2012-11-26 18:58:24 +00:00
public:
int Compare( const idStr& a, const idStr& b ) const
{
return a.IcmpPath( b );
}
2012-11-26 18:58:24 +00:00
};
/*
========================
idStr::Construct
========================
*/
ID_INLINE void idStr::Construct()
{
2012-11-26 18:58:24 +00:00
SetStatic( false );
SetAlloced( STR_ALLOC_BASE );
data = baseBuffer;
len = 0;
data[ 0 ] = '\0';
#ifdef ID_DEBUG_UNINITIALIZED_MEMORY
memset( baseBuffer, 0, sizeof( baseBuffer ) );
#endif
}
ID_INLINE void idStr::EnsureAlloced( int amount, bool keepold )
{
2012-11-26 18:58:24 +00:00
// static string's can't reallocate
if( IsStatic() )
{
2012-11-26 18:58:24 +00:00
release_assert( amount <= GetAlloced() );
return;
}
if( amount > GetAlloced() )
{
2012-11-26 18:58:24 +00:00
ReAllocate( amount, keepold );
}
}
/*
========================
idStr::SetStaticBuffer
========================
*/
ID_INLINE void idStr::SetStaticBuffer( char* buffer, const int bufferLength )
{
2012-11-26 18:58:24 +00:00
// this should only be called on a freshly constructed idStr
assert( data == baseBuffer );
data = buffer;
len = 0;
SetAlloced( bufferLength );
SetStatic( true );
}
ID_INLINE idStr::idStr()
{
2012-11-26 18:58:24 +00:00
Construct();
}
ID_INLINE idStr::idStr( const idStr& text )
{
2012-11-26 18:58:24 +00:00
Construct();
int l;
2012-11-26 18:58:24 +00:00
l = text.Length();
EnsureAlloced( l + 1 );
strcpy( data, text.data );
len = l;
}
ID_INLINE idStr::idStr( const idStr& text, int start, int end )
{
2012-11-26 18:58:24 +00:00
Construct();
int i;
int l;
if( end > text.Length() )
{
2012-11-26 18:58:24 +00:00
end = text.Length();
}
if( start > text.Length() )
{
2012-11-26 18:58:24 +00:00
start = text.Length();
}
else if( start < 0 )
{
2012-11-26 18:58:24 +00:00
start = 0;
}
2012-11-26 18:58:24 +00:00
l = end - start;
if( l < 0 )
{
2012-11-26 18:58:24 +00:00
l = 0;
}
2012-11-26 18:58:24 +00:00
EnsureAlloced( l + 1 );
for( i = 0; i < l; i++ )
{
2012-11-26 18:58:24 +00:00
data[ i ] = text[ start + i ];
}
2012-11-26 18:58:24 +00:00
data[ l ] = '\0';
len = l;
}
ID_INLINE idStr::idStr( const char* text )
{
2012-11-26 18:58:24 +00:00
Construct();
int l;
if( text )
{
2012-11-26 18:58:24 +00:00
l = strlen( text );
EnsureAlloced( l + 1 );
strcpy( data, text );
len = l;
}
}
ID_INLINE idStr::idStr( const char* text, int start, int end )
{
2012-11-26 18:58:24 +00:00
Construct();
int i;
int l = strlen( text );
if( end > l )
{
2012-11-26 18:58:24 +00:00
end = l;
}
if( start > l )
{
2012-11-26 18:58:24 +00:00
start = l;
}
else if( start < 0 )
{
2012-11-26 18:58:24 +00:00
start = 0;
}
2012-11-26 18:58:24 +00:00
l = end - start;
if( l < 0 )
{
2012-11-26 18:58:24 +00:00
l = 0;
}
2012-11-26 18:58:24 +00:00
EnsureAlloced( l + 1 );
for( i = 0; i < l; i++ )
{
2012-11-26 18:58:24 +00:00
data[ i ] = text[ start + i ];
}
2012-11-26 18:58:24 +00:00
data[ l ] = '\0';
len = l;
}
ID_INLINE idStr::idStr( const bool b )
{
2012-11-26 18:58:24 +00:00
Construct();
EnsureAlloced( 2 );
data[ 0 ] = b ? '1' : '0';
data[ 1 ] = '\0';
len = 1;
}
ID_INLINE idStr::idStr( const char c )
{
2012-11-26 18:58:24 +00:00
Construct();
EnsureAlloced( 2 );
data[ 0 ] = c;
data[ 1 ] = '\0';
len = 1;
}
ID_INLINE idStr::idStr( const int i )
{
2012-11-26 18:58:24 +00:00
Construct();
char text[ 64 ];
int l;
2012-11-26 18:58:24 +00:00
l = sprintf( text, "%d", i );
EnsureAlloced( l + 1 );
strcpy( data, text );
len = l;
}
ID_INLINE idStr::idStr( const unsigned u )
{
2012-11-26 18:58:24 +00:00
Construct();
char text[ 64 ];
int l;
2012-11-26 18:58:24 +00:00
l = sprintf( text, "%u", u );
EnsureAlloced( l + 1 );
strcpy( data, text );
len = l;
}
ID_INLINE idStr::idStr( const float f )
{
2012-11-26 18:58:24 +00:00
Construct();
char text[ 64 ];
int l;
2012-11-26 18:58:24 +00:00
l = idStr::snPrintf( text, sizeof( text ), "%f", f );
while( l > 0 && text[l - 1] == '0' ) text[--l] = '\0';
while( l > 0 && text[l - 1] == '.' ) text[--l] = '\0';
2012-11-26 18:58:24 +00:00
EnsureAlloced( l + 1 );
strcpy( data, text );
len = l;
}
ID_INLINE idStr::~idStr()
{
2012-11-26 18:58:24 +00:00
FreeData();
}
ID_INLINE size_t idStr::Size() const
{
2012-11-26 18:58:24 +00:00
return sizeof( *this ) + Allocated();
}
ID_INLINE const char* idStr::c_str() const
{
2012-11-26 18:58:24 +00:00
return data;
}
ID_INLINE idStr::operator const char* ()
{
2012-11-26 18:58:24 +00:00
return c_str();
}
ID_INLINE idStr::operator const char* () const
{
2012-11-26 18:58:24 +00:00
return c_str();
}
ID_INLINE char idStr::operator[]( int index ) const
{
2012-11-26 18:58:24 +00:00
assert( ( index >= 0 ) && ( index <= len ) );
return data[ index ];
}
ID_INLINE char& idStr::operator[]( int index )
{
2012-11-26 18:58:24 +00:00
assert( ( index >= 0 ) && ( index <= len ) );
return data[ index ];
}
ID_INLINE void idStr::operator=( const idStr& text )
{
2012-11-26 18:58:24 +00:00
int l;
2012-11-26 18:58:24 +00:00
l = text.Length();
EnsureAlloced( l + 1, false );
memcpy( data, text.data, l );
data[l] = '\0';
len = l;
}
ID_INLINE idStr operator+( const idStr& a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
idStr result( a );
result.Append( b );
return result;
}
ID_INLINE idStr operator+( const idStr& a, const char* b )
{
2012-11-26 18:58:24 +00:00
idStr result( a );
result.Append( b );
return result;
}
ID_INLINE idStr operator+( const char* a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
idStr result( a );
result.Append( b );
return result;
}
ID_INLINE idStr operator+( const idStr& a, const bool b )
{
2012-11-26 18:58:24 +00:00
idStr result( a );
result.Append( b ? "true" : "false" );
return result;
}
ID_INLINE idStr operator+( const idStr& a, const char b )
{
2012-11-26 18:58:24 +00:00
idStr result( a );
result.Append( b );
return result;
}
ID_INLINE idStr operator+( const idStr& a, const float b )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
idStr result( a );
2012-11-26 18:58:24 +00:00
sprintf( text, "%f", b );
result.Append( text );
2012-11-26 18:58:24 +00:00
return result;
}
ID_INLINE idStr operator+( const idStr& a, const int b )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
idStr result( a );
2012-11-26 18:58:24 +00:00
sprintf( text, "%d", b );
result.Append( text );
2012-11-26 18:58:24 +00:00
return result;
}
ID_INLINE idStr operator+( const idStr& a, const unsigned b )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
idStr result( a );
2012-11-26 18:58:24 +00:00
sprintf( text, "%u", b );
result.Append( text );
2012-11-26 18:58:24 +00:00
return result;
}
ID_INLINE idStr& idStr::operator+=( const float a )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
2012-11-26 18:58:24 +00:00
sprintf( text, "%f", a );
Append( text );
2012-11-26 18:58:24 +00:00
return *this;
}
ID_INLINE idStr& idStr::operator+=( const int a )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
2012-11-26 18:58:24 +00:00
sprintf( text, "%d", a );
Append( text );
2012-11-26 18:58:24 +00:00
return *this;
}
ID_INLINE idStr& idStr::operator+=( const unsigned a )
{
2012-11-26 18:58:24 +00:00
char text[ 64 ];
2012-11-26 18:58:24 +00:00
sprintf( text, "%u", a );
Append( text );
2012-11-26 18:58:24 +00:00
return *this;
}
ID_INLINE idStr& idStr::operator+=( const idStr& a )
{
2012-11-26 18:58:24 +00:00
Append( a );
return *this;
}
ID_INLINE idStr& idStr::operator+=( const char* a )
{
2012-11-26 18:58:24 +00:00
Append( a );
return *this;
}
ID_INLINE idStr& idStr::operator+=( const char a )
{
2012-11-26 18:58:24 +00:00
Append( a );
return *this;
}
ID_INLINE idStr& idStr::operator+=( const bool a )
{
2012-11-26 18:58:24 +00:00
Append( a ? "true" : "false" );
return *this;
}
ID_INLINE bool operator==( const idStr& a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
return ( !idStr::Cmp( a.data, b.data ) );
}
ID_INLINE bool operator==( const idStr& a, const char* b )
{
2012-11-26 18:58:24 +00:00
assert( b );
return ( !idStr::Cmp( a.data, b ) );
}
ID_INLINE bool operator==( const char* a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
assert( a );
return ( !idStr::Cmp( a, b.data ) );
}
ID_INLINE bool operator!=( const idStr& a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
return !( a == b );
}
ID_INLINE bool operator!=( const idStr& a, const char* b )
{
2012-11-26 18:58:24 +00:00
return !( a == b );
}
ID_INLINE bool operator!=( const char* a, const idStr& b )
{
2012-11-26 18:58:24 +00:00
return !( a == b );
}
ID_INLINE int idStr::Cmp( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Cmp( data, text );
}
ID_INLINE int idStr::Cmpn( const char* text, int n ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Cmpn( data, text, n );
}
ID_INLINE int idStr::CmpPrefix( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Cmpn( data, text, strlen( text ) );
}
ID_INLINE int idStr::Icmp( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Icmp( data, text );
}
ID_INLINE int idStr::Icmpn( const char* text, int n ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Icmpn( data, text, n );
}
ID_INLINE int idStr::IcmpPrefix( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::Icmpn( data, text, strlen( text ) );
}
ID_INLINE int idStr::IcmpNoColor( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::IcmpNoColor( data, text );
}
ID_INLINE int idStr::IcmpPath( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::IcmpPath( data, text );
}
ID_INLINE int idStr::IcmpnPath( const char* text, int n ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::IcmpnPath( data, text, n );
}
ID_INLINE int idStr::IcmpPrefixPath( const char* text ) const
{
2012-11-26 18:58:24 +00:00
assert( text );
return idStr::IcmpnPath( data, text, strlen( text ) );
}
ID_INLINE int idStr::Length() const
{
2012-11-26 18:58:24 +00:00
return len;
}
ID_INLINE int idStr::Allocated() const
{
if( data != baseBuffer )
{
2012-11-26 18:58:24 +00:00
return GetAlloced();
}
else
{
2012-11-26 18:58:24 +00:00
return 0;
}
}
ID_INLINE void idStr::Empty()
{
2012-11-26 18:58:24 +00:00
EnsureAlloced( 1 );
data[ 0 ] = '\0';
len = 0;
}
ID_INLINE bool idStr::IsEmpty() const
{
2012-11-26 18:58:24 +00:00
return ( idStr::Cmp( data, "" ) == 0 );
}
ID_INLINE void idStr::Clear()
{
if( IsStatic() )
{
2012-11-26 18:58:24 +00:00
len = 0;
data[ 0 ] = '\0';
return;
}
FreeData();
Construct();
}
ID_INLINE void idStr::Append( const char a )
{
2012-11-26 18:58:24 +00:00
EnsureAlloced( len + 2 );
data[ len ] = a;
len++;
data[ len ] = '\0';
}
ID_INLINE void idStr::Append( const idStr& text )
{
2012-11-26 18:58:24 +00:00
int newLen;
int i;
2012-11-26 18:58:24 +00:00
newLen = len + text.Length();
EnsureAlloced( newLen + 1 );
for( i = 0; i < text.len; i++ )
{
2012-11-26 18:58:24 +00:00
data[ len + i ] = text[ i ];
}
len = newLen;
data[ len ] = '\0';
}
ID_INLINE void idStr::Append( const char* text )
{
2012-11-26 18:58:24 +00:00
int newLen;
int i;
if( text )
{
2012-11-26 18:58:24 +00:00
newLen = len + strlen( text );
EnsureAlloced( newLen + 1 );
for( i = 0; text[ i ]; i++ )
{
2012-11-26 18:58:24 +00:00
data[ len + i ] = text[ i ];
}
len = newLen;
data[ len ] = '\0';
}
}
ID_INLINE void idStr::Append( const char* text, int l )
{
2012-11-26 18:58:24 +00:00
int newLen;
int i;
if( text && l )
{
2012-11-26 18:58:24 +00:00
newLen = len + l;
EnsureAlloced( newLen + 1 );
for( i = 0; text[ i ] && i < l; i++ )
{
2012-11-26 18:58:24 +00:00
data[ len + i ] = text[ i ];
}
len = newLen;
data[ len ] = '\0';
}
}
ID_INLINE void idStr::Insert( const char a, int index )
{
2012-11-26 18:58:24 +00:00
int i, l;
if( index < 0 )
{
2012-11-26 18:58:24 +00:00
index = 0;
}
else if( index > len )
{
2012-11-26 18:58:24 +00:00
index = len;
}
2012-11-26 18:58:24 +00:00
l = 1;
EnsureAlloced( len + l + 1 );
for( i = len; i >= index; i-- )
{
data[i + l] = data[i];
2012-11-26 18:58:24 +00:00
}
data[index] = a;
len++;
}
ID_INLINE void idStr::Insert( const char* text, int index )
{
2012-11-26 18:58:24 +00:00
int i, l;
if( index < 0 )
{
2012-11-26 18:58:24 +00:00
index = 0;
}
else if( index > len )
{
2012-11-26 18:58:24 +00:00
index = len;
}
2012-11-26 18:58:24 +00:00
l = strlen( text );
EnsureAlloced( len + l + 1 );
for( i = len; i >= index; i-- )
{
data[i + l] = data[i];
2012-11-26 18:58:24 +00:00
}
for( i = 0; i < l; i++ )
{
data[index + i] = text[i];
2012-11-26 18:58:24 +00:00
}
len += l;
}
ID_INLINE void idStr::ToLower()
{
for( int i = 0; data[i]; i++ )
{
if( CharIsUpper( data[i] ) )
{
2012-11-26 18:58:24 +00:00
data[i] += ( 'a' - 'A' );
}
}
}
ID_INLINE void idStr::ToUpper()
{
for( int i = 0; data[i]; i++ )
{
if( CharIsLower( data[i] ) )
{
2012-11-26 18:58:24 +00:00
data[i] -= ( 'a' - 'A' );
}
}
}
ID_INLINE bool idStr::IsNumeric() const
{
2012-11-26 18:58:24 +00:00
return idStr::IsNumeric( data );
}
ID_INLINE bool idStr::IsColor() const
{
2012-11-26 18:58:24 +00:00
return idStr::IsColor( data );
}
ID_INLINE bool idStr::HasLower() const
{
2012-11-26 18:58:24 +00:00
return idStr::HasLower( data );
}
ID_INLINE bool idStr::HasUpper() const
{
2012-11-26 18:58:24 +00:00
return idStr::HasUpper( data );
}
ID_INLINE idStr& idStr::RemoveColors()
{
2012-11-26 18:58:24 +00:00
idStr::RemoveColors( data );
len = Length( data );
return *this;
}
ID_INLINE int idStr::LengthWithoutColors() const
{
2012-11-26 18:58:24 +00:00
return idStr::LengthWithoutColors( data );
}
ID_INLINE void idStr::CapLength( int newlen )
{
if( len <= newlen )
{
2012-11-26 18:58:24 +00:00
return;
}
data[ newlen ] = 0;
len = newlen;
}
ID_INLINE void idStr::Fill( const char ch, int newlen )
{
2012-11-26 18:58:24 +00:00
EnsureAlloced( newlen + 1 );
len = newlen;
memset( data, ch, len );
data[ len ] = 0;
}
/*
========================
idStr::UTF8Length
========================
*/
ID_INLINE int idStr::UTF8Length()
{
return UTF8Length( ( byte* )data );
2012-11-26 18:58:24 +00:00
}
/*
========================
idStr::UTF8Char
========================
*/
ID_INLINE uint32 idStr::UTF8Char( int& idx )
{
return UTF8Char( ( byte* )data, idx );
2012-11-26 18:58:24 +00:00
}
/*
========================
idStr::ConvertToUTF8
========================
*/
ID_INLINE void idStr::ConvertToUTF8()
{
2012-11-26 18:58:24 +00:00
idStr temp( *this );
Clear();
for( int index = 0; index < temp.Length(); ++index )
{
2012-11-26 18:58:24 +00:00
AppendUTF8Char( temp[index] );
}
}
/*
========================
idStr::UTF8Char
========================
*/
ID_INLINE uint32 idStr::UTF8Char( const char* s, int& idx )
{
return UTF8Char( ( byte* )s, idx );
2012-11-26 18:58:24 +00:00
}
/*
========================
idStr::IsValidUTF8
========================
*/
ID_INLINE bool idStr::IsValidUTF8( const uint8* s, const int maxLen )
{
2012-11-26 18:58:24 +00:00
utf8Encoding_t encoding;
return IsValidUTF8( s, maxLen, encoding );
}
ID_INLINE int idStr::Find( const char c, int start, int end ) const
{
if( end == -1 )
{
2012-11-26 18:58:24 +00:00
end = len;
}
return idStr::FindChar( data, c, start, end );
}
ID_INLINE int idStr::Find( const char* text, bool casesensitive, int start, int end ) const
{
if( end == -1 )
{
2012-11-26 18:58:24 +00:00
end = len;
}
return idStr::FindText( data, text, casesensitive, start, end );
}
ID_INLINE bool idStr::Filter( const char* filter, bool casesensitive ) const
{
2012-11-26 18:58:24 +00:00
return idStr::Filter( filter, data, casesensitive );
}
ID_INLINE const char* idStr::Left( int len, idStr& result ) const
{
2012-11-26 18:58:24 +00:00
return Mid( 0, len, result );
}
ID_INLINE const char* idStr::Right( int len, idStr& result ) const
{
if( len >= Length() )
{
2012-11-26 18:58:24 +00:00
result = *this;
return result;
}
return Mid( Length() - len, len, result );
}
ID_INLINE idStr idStr::Left( int len ) const
{
2012-11-26 18:58:24 +00:00
return Mid( 0, len );
}
ID_INLINE idStr idStr::Right( int len ) const
{
if( len >= Length() )
{
2012-11-26 18:58:24 +00:00
return *this;
}
return Mid( Length() - len, len );
}
ID_INLINE void idStr::Strip( const char c )
{
2012-11-26 18:58:24 +00:00
StripLeading( c );
StripTrailing( c );
}
ID_INLINE void idStr::Strip( const char* string )
{
2012-11-26 18:58:24 +00:00
StripLeading( string );
StripTrailing( string );
}
ID_INLINE bool idStr::CheckExtension( const char* ext )
{
2012-11-26 18:58:24 +00:00
return idStr::CheckExtension( data, ext );
}
ID_INLINE int idStr::Length( const char* s )
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; s[i]; i++ ) {}
2012-11-26 18:58:24 +00:00
return i;
}
ID_INLINE char* idStr::ToLower( char* s )
{
for( int i = 0; s[i]; i++ )
{
if( CharIsUpper( s[i] ) )
{
2012-11-26 18:58:24 +00:00
s[i] += ( 'a' - 'A' );
}
}
return s;
}
ID_INLINE char* idStr::ToUpper( char* s )
{
for( int i = 0; s[i]; i++ )
{
if( CharIsLower( s[i] ) )
{
2012-11-26 18:58:24 +00:00
s[i] -= ( 'a' - 'A' );
}
}
return s;
}
ID_INLINE int idStr::Hash( const char* string )
{
2012-11-26 18:58:24 +00:00
int i, hash = 0;
for( i = 0; *string != '\0'; i++ )
{
2012-11-26 18:58:24 +00:00
hash += ( *string++ ) * ( i + 119 );
}
return hash;
}
ID_INLINE int idStr::Hash( const char* string, int length )
{
2012-11-26 18:58:24 +00:00
int i, hash = 0;
for( i = 0; i < length; i++ )
{
2012-11-26 18:58:24 +00:00
hash += ( *string++ ) * ( i + 119 );
}
return hash;
}
ID_INLINE int idStr::IHash( const char* string )
{
2012-11-26 18:58:24 +00:00
int i, hash = 0;
for( i = 0; *string != '\0'; i++ )
{
2012-11-26 18:58:24 +00:00
hash += ToLower( *string++ ) * ( i + 119 );
}
return hash;
}
ID_INLINE int idStr::IHash( const char* string, int length )
{
2012-11-26 18:58:24 +00:00
int i, hash = 0;
for( i = 0; i < length; i++ )
{
2012-11-26 18:58:24 +00:00
hash += ToLower( *string++ ) * ( i + 119 );
}
return hash;
}
ID_INLINE bool idStr::IsColor( const char* s )
{
2012-11-26 18:58:24 +00:00
return ( s[0] == C_COLOR_ESCAPE && s[1] != '\0' && s[1] != ' ' );
}
ID_INLINE char idStr::ToLower( char c )
{
if( c <= 'Z' && c >= 'A' )
{
2012-11-26 18:58:24 +00:00
return ( c + ( 'a' - 'A' ) );
}
return c;
}
ID_INLINE char idStr::ToUpper( char c )
{
if( c >= 'a' && c <= 'z' )
{
2012-11-26 18:58:24 +00:00
return ( c - ( 'a' - 'A' ) );
}
return c;
}
ID_INLINE bool idStr::CharIsPrintable( int c )
{
2012-11-26 18:58:24 +00:00
// test for regular ascii and western European high-ascii chars
return ( c >= 0x20 && c <= 0x7E ) || ( c >= 0xA1 && c <= 0xFF );
}
ID_INLINE bool idStr::CharIsLower( int c )
{
2012-11-26 18:58:24 +00:00
// test for regular ascii and western European high-ascii chars
return ( c >= 'a' && c <= 'z' ) || ( c >= 0xE0 && c <= 0xFF );
}
ID_INLINE bool idStr::CharIsUpper( int c )
{
2012-11-26 18:58:24 +00:00
// test for regular ascii and western European high-ascii chars
return ( c <= 'Z' && c >= 'A' ) || ( c >= 0xC0 && c <= 0xDF );
}
ID_INLINE bool idStr::CharIsAlpha( int c )
{
2012-11-26 18:58:24 +00:00
// test for regular ascii and western European high-ascii chars
return ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ||
( c >= 0xC0 && c <= 0xFF ) );
}
ID_INLINE bool idStr::CharIsNumeric( int c )
{
2012-11-26 18:58:24 +00:00
return ( c <= '9' && c >= '0' );
}
ID_INLINE bool idStr::CharIsNewLine( char c )
{
2012-11-26 18:58:24 +00:00
return ( c == '\n' || c == '\r' || c == '\v' );
}
ID_INLINE bool idStr::CharIsTab( char c )
{
2012-11-26 18:58:24 +00:00
return ( c == '\t' );
}
ID_INLINE int idStr::ColorIndex( int c )
{
2012-11-26 18:58:24 +00:00
return ( c & 15 );
}
ID_INLINE int idStr::DynamicMemoryUsed() const
{
2012-11-26 18:58:24 +00:00
return ( data == baseBuffer ) ? 0 : GetAlloced();
}
/*
========================
idStr::CopyRange
========================
*/
ID_INLINE void idStr::CopyRange( const char* text, int start, int end )
{
2012-11-26 18:58:24 +00:00
int l = end - start;
if( l < 0 )
{
2012-11-26 18:58:24 +00:00
l = 0;
}
2012-11-26 18:58:24 +00:00
EnsureAlloced( l + 1 );
for( int i = 0; i < l; i++ )
{
2012-11-26 18:58:24 +00:00
data[ i ] = text[ start + i ];
}
2012-11-26 18:58:24 +00:00
data[ l ] = '\0';
len = l;
}
#endif /* !__STR_H__ */