diff options
| author | Michael Sartain <[email protected]> | 2014-10-02 08:25:55 -0700 |
|---|---|---|
| committer | Michael Sartain <[email protected]> | 2014-10-02 08:25:55 -0700 |
| commit | 55ed12f8d1eb6887d348be03aee5573d44177ffb (patch) | |
| tree | 3686f7ca78c780cd9a3d367b79a9d9250c1be7c0 /mp/src/public/tier1/utlstring.h | |
| parent | * Added support for Visual C++ 2013 Express to VPC (diff) | |
| download | source-sdk-2013-55ed12f8d1eb6887d348be03aee5573d44177ffb.tar.xz source-sdk-2013-55ed12f8d1eb6887d348be03aee5573d44177ffb.zip | |
Updated the SDK with the latest code from the TF and HL2 branches.
Diffstat (limited to 'mp/src/public/tier1/utlstring.h')
| -rw-r--r-- | mp/src/public/tier1/utlstring.h | 316 |
1 files changed, 187 insertions, 129 deletions
diff --git a/mp/src/public/tier1/utlstring.h b/mp/src/public/tier1/utlstring.h index c50765fa..32714e12 100644 --- a/mp/src/public/tier1/utlstring.h +++ b/mp/src/public/tier1/utlstring.h @@ -16,8 +16,7 @@ #include "limits.h" #if defined( OSX ) -#define wcsdup wcsdup_osx -inline wchar_t *wcsdup_osx(const wchar_t *pString) +inline wchar_t *wcsdup(const wchar_t *pString) { wchar_t *pMemory; @@ -32,97 +31,14 @@ inline wchar_t *wcsdup_osx(const wchar_t *pString) return NULL; } -#endif - -//----------------------------------------------------------------------------- -// Base class, containing simple memory management -//----------------------------------------------------------------------------- -class CUtlBinaryBlock -{ -public: - CUtlBinaryBlock( int growSize = 0, int initSize = 0 ); - - // NOTE: nInitialLength indicates how much of the buffer starts full - CUtlBinaryBlock( void* pMemory, int nSizeInBytes, int nInitialLength ); - CUtlBinaryBlock( const void* pMemory, int nSizeInBytes ); - CUtlBinaryBlock( const CUtlBinaryBlock& src ); - - void Get( void *pValue, int nMaxLen ) const; - void Set( const void *pValue, int nLen ); - const void *Get( ) const; - void *Get( ); - - unsigned char& operator[]( int i ); - const unsigned char& operator[]( int i ) const; - - int Length() const; - void SetLength( int nLength ); // Undefined memory will result - bool IsEmpty() const; - void Clear(); - void Purge(); - - bool IsReadOnly() const; - - CUtlBinaryBlock &operator=( const CUtlBinaryBlock &src ); - - // Test for equality - bool operator==( const CUtlBinaryBlock &src ) const; - -private: - CUtlMemory<unsigned char> m_Memory; - int m_nActualLength; -}; - - -//----------------------------------------------------------------------------- -// class inlines -//----------------------------------------------------------------------------- -inline const void *CUtlBinaryBlock::Get( ) const -{ - return m_Memory.Base(); -} - -inline void *CUtlBinaryBlock::Get( ) -{ - return m_Memory.Base(); -} - -inline int CUtlBinaryBlock::Length() const -{ - return m_nActualLength; -} -inline unsigned char& CUtlBinaryBlock::operator[]( int i ) +inline size_t strnlen(const char *s, size_t n) { - return m_Memory[i]; -} - -inline const unsigned char& CUtlBinaryBlock::operator[]( int i ) const -{ - return m_Memory[i]; -} - -inline bool CUtlBinaryBlock::IsReadOnly() const -{ - return m_Memory.IsReadOnly(); -} - -inline bool CUtlBinaryBlock::IsEmpty() const -{ - return Length() == 0; -} - -inline void CUtlBinaryBlock::Clear() -{ - SetLength( 0 ); -} - -inline void CUtlBinaryBlock::Purge() -{ - SetLength( 0 ); - m_Memory.Purge(); + const char *p = (const char *)memchr(s, 0, n); + return (p ? p - s : n); } +#endif //----------------------------------------------------------------------------- // Simple string class. @@ -140,95 +56,154 @@ public: public: CUtlString(); CUtlString( const char *pString ); + CUtlString( const char *pString, int length ); CUtlString( const CUtlString& string ); - // Attaches the string to external memory. Useful for avoiding a copy - CUtlString( void* pMemory, int nSizeInBytes, int nInitialLength ); - CUtlString( const void* pMemory, int nSizeInBytes ); +#ifdef MOVE_CONSTRUCTOR_SUPPORT + // Support moving of CUtlString objects. Long live C++11 + // This move constructor will get called when appropriate, such as when + // returning objects from functions, or otherwise copying from temporaries + // which are about to be destroyed. It can also be explicitly invoked with + // std::move(). + // Move constructor: + CUtlString( CUtlString&& rhs ) + { + // Move the string pointer from the source to this -- be sure to + // zero out the source to avoid double frees. + m_pString = rhs.m_pString; + rhs.m_pString = 0; + } + // Move assignment operator: + CUtlString& operator=( CUtlString&& rhs ) + { + // Move the string pointer from the source to this -- be sure to + // zero out the source to avoid double frees. + m_pString = rhs.m_pString; + rhs.m_pString = 0; + return *this; + } +#endif + + ~CUtlString(); const char *Get( ) const; void Set( const char *pValue ); - - void Clear() { Set( NULL ); } - - // Converts to c-strings operator const char*() const; + // Set directly and don't look for a null terminator in pValue. + // nChars does not include the nul and this will only copy + // at most nChars (even if pValue is longer). If nChars + // is >strlen(pValue) it will copy past the end, don't do it + // Does nothing if pValue == String() + void SetDirect( const char *pValue, int nChars ); + // for compatibility switching items from UtlSymbol const char *String() const { return Get(); } // Returns strlen int Length() const; + // IsEmpty() is more efficient than Length() == 0 bool IsEmpty() const; // Sets the length (used to serialize into the buffer ) // Note: If nLen != 0, then this adds an extra byte for a null-terminator. void SetLength( int nLen ); - char *Get(); + char *GetForModify(); + void Clear(); void Purge(); // Case Change void ToLower(); void ToUpper(); + void Append( const char *pAddition, int nChars ); void Append( const char *pchAddition ); - + void Append( const char chAddition ) { char temp[2] = { chAddition, 0 }; Append( temp ); } // Strips the trailing slash void StripTrailingSlash(); + void FixSlashes( char cSeparator = CORRECT_PATH_SEPARATOR ); + + // Trim whitespace + void TrimLeft( char cTarget ); + void TrimLeft( const char *szTargets = "\t\r\n " ); + void TrimRight( char cTarget ); + void TrimRight( const char *szTargets = "\t\r\n " ); + void Trim( char cTarget ); + void Trim( const char *szTargets = "\t\r\n " ); + + bool IsEqual_CaseSensitive( const char *src ) const; + bool IsEqual_CaseInsensitive( const char *src ) const; CUtlString &operator=( const CUtlString &src ); CUtlString &operator=( const char *src ); // Test for equality bool operator==( const CUtlString &src ) const; - bool operator==( const char *src ) const; bool operator!=( const CUtlString &src ) const { return !operator==( src ); } - bool operator!=( const char *src ) const { return !operator==( src ); } - - // If these are not defined, CUtlString as rhs will auto-convert - // to const char* and do logical operations on the raw pointers. Ugh. - inline friend bool operator==( const char *lhs, const CUtlString &rhs ) { return rhs.operator==( lhs ); } - inline friend bool operator!=( const char *lhs, const CUtlString &rhs ) { return rhs.operator!=( lhs ); } CUtlString &operator+=( const CUtlString &rhs ); CUtlString &operator+=( const char *rhs ); CUtlString &operator+=( char c ); CUtlString &operator+=( int rhs ); CUtlString &operator+=( double rhs ); - - // is valid? - bool IsValid() const; - bool MatchesPattern( const CUtlString &Pattern, int nFlags = 0 ); // case SENSITIVE, use * for wildcard in pattern string + CUtlString operator+( const char *pOther ) const; + CUtlString operator+( const CUtlString &other ) const; + CUtlString operator+( int rhs ) const; - int Format( PRINTF_FORMAT_STRING const char *pFormat, ... ); - void SetDirect( const char *pValue, int nChars ); + bool MatchesPattern( const CUtlString &Pattern, int nFlags = 0 ) const; // case SENSITIVE, use * for wildcard in pattern string + + char operator[]( int i ) const; + +#if ! defined(SWIG) + // Don't let SWIG see the PRINTF_FORMAT_STRING attribute or it will complain. + int Format( PRINTF_FORMAT_STRING const char *pFormat, ... ) FMTFUNCTION( 2, 3 ); + int FormatV( PRINTF_FORMAT_STRING const char *pFormat, va_list marker ); +#else + int Format( const char *pFormat, ... ); + int FormatV( const char *pFormat, va_list marker ); +#endif // Defining AltArgumentType_t hints that associative container classes should // also implement Find/Insert/Remove functions that take const char* params. typedef const char *AltArgumentType_t; - // Take a piece out of the string. + // Get a copy of part of the string. // If you only specify nStart, it'll go from nStart to the end. // You can use negative numbers and it'll wrap around to the start. - CUtlString Slice( int32 nStart=0, int32 nEnd=INT_MAX ); + CUtlString Slice( int32 nStart=0, int32 nEnd=INT_MAX ) const; + + // Get a substring starting from the left or the right side. + CUtlString Left( int32 nChars ) const; + CUtlString Right( int32 nChars ) const; - // Grab a substring starting from the left or the right side. - CUtlString Left( int32 nChars ); - CUtlString Right( int32 nChars ); + // Get a string with all instances of one character replaced with another. + CUtlString Replace( char cFrom, char cTo ) const; - // Replace all instances of one character with another. - CUtlString Replace( char cFrom, char cTo ); + // Replace all instances of specified string with another. + CUtlString Replace( const char *pszFrom, const char *pszTo ) const; - // Calls right through to V_MakeAbsolutePath. - CUtlString AbsPath( const char *pStartingDir=NULL ); + // Get this string as an absolute path (calls right through to V_MakeAbsolutePath). + CUtlString AbsPath( const char *pStartingDir=NULL ) const; // Gets the filename (everything except the path.. c:\a\b\c\somefile.txt -> somefile.txt). - CUtlString UnqualifiedFilename(); - - // Strips off one directory. Uses V_StripLastDir but strips the last slash also! - CUtlString DirName(); + CUtlString UnqualifiedFilename() const; + // Gets a string with one directory removed. Uses V_StripLastDir but strips the last slash also! + CUtlString DirName() const; + + // Get a string with the extension removed (with V_StripExtension). + CUtlString StripExtension() const; + + // Get a string with the filename removed (uses V_UnqualifiedFileName and also strips the last slash) + CUtlString StripFilename() const; + + // Get a string with the base filename (with V_FileBase). + CUtlString GetBaseFilename() const; + + // Get a string with the file extension (with V_FileBase). + CUtlString GetExtension() const; + // Works like V_ComposeFileName. static CUtlString PathJoin( const char *pStr1, const char *pStr2 ); @@ -236,21 +211,93 @@ public: static int __cdecl SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 ); static int __cdecl SortCaseSensitive( const CUtlString *pString1, const CUtlString *pString2 ); + // Empty string for those times when you need to return an empty string and + // either don't want to pay the construction cost, or are returning a + // const CUtlString& and cannot just return "". + static const CUtlString &GetEmptyString(); + private: - CUtlBinaryBlock m_Storage; + // INTERNALS + // AllocMemory allocates enough space for length characters plus a terminating zero. + // Previous characters are preserved, the buffer is null-terminated, but new characters + // are not touched. + void *AllocMemory( uint32 length ); + + // If m_pString is not NULL, it points to the start of the string, and the memory allocation. + char *m_pString; }; +// // If these are not defined, CUtlConstString as rhs will auto-convert +// // to const char* and do logical operations on the raw pointers. Ugh. +// inline friend bool operator<( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) > 0; } +// inline friend bool operator==( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) == 0; } +// inline friend bool operator!=( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) != 0; } + +inline bool operator==( const char *pString, const CUtlString &utlString ) +{ + return utlString.IsEqual_CaseSensitive( pString ); +} + +inline bool operator!=( const char *pString, const CUtlString &utlString ) +{ + return !utlString.IsEqual_CaseSensitive( pString ); +} + +inline bool operator==( const CUtlString &utlString, const char *pString ) +{ + return utlString.IsEqual_CaseSensitive( pString ); +} + +inline bool operator!=( const CUtlString &utlString, const char *pString ) +{ + return !utlString.IsEqual_CaseSensitive( pString ); +} + + + //----------------------------------------------------------------------------- // Inline methods //----------------------------------------------------------------------------- -inline bool CUtlString::IsEmpty() const +inline CUtlString::CUtlString() +: m_pString( NULL ) +{ +} + +inline CUtlString::CUtlString( const char *pString ) +: m_pString( NULL ) +{ + Set( pString ); +} + +inline CUtlString::CUtlString( const char *pString, int length ) +: m_pString( NULL ) +{ + SetDirect( pString, length ); +} + +inline CUtlString::CUtlString( const CUtlString& string ) +: m_pString( NULL ) { - return Length() == 0; + Set( string.Get() ); } -inline bool CUtlString::IsValid() const +inline CUtlString::~CUtlString() { - return ( String() != NULL ); + Purge(); +} + +inline int CUtlString::Length() const +{ + if (m_pString) + { + return V_strlen( m_pString ); + } + return 0; +} + +inline bool CUtlString::IsEmpty() const +{ + return !m_pString || m_pString[0] == 0; } inline int __cdecl CUtlString::SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 ) @@ -263,6 +310,14 @@ inline int __cdecl CUtlString::SortCaseSensitive( const CUtlString *pString1, co return V_strcmp( pString1->String(), pString2->String() ); } +// Converts to c-strings +inline CUtlString::operator const char*() const +{ + return Get(); +} + + + //----------------------------------------------------------------------------- // Purpose: Implementation of low-level string functionality for character types. //----------------------------------------------------------------------------- @@ -398,5 +453,8 @@ typedef CUtlConstStringBase<wchar_t> CUtlConstWideString; template < typename T > struct UTLConstStringCaselessStringLessFunctor { bool operator()( const CUtlConstStringBase<T>& a, const char *b ) const { return StringFuncs<T>::CaselessCompare( a.Get(), b ) < 0; } }; template < typename T > struct UTLConstStringCaselessStringEqualFunctor { bool operator()( const CUtlConstStringBase<T>& a, const char *b ) const { return StringFuncs<T>::CaselessCompare( a.Get(), b ) == 0; } }; +// Helper function for CUtlMaps with a CUtlString key +inline bool UtlStringLessFunc( const CUtlString &lhs, const CUtlString &rhs ) { return V_strcmp( lhs.Get(), rhs.Get() ) < 0; } +inline bool UtlStringCaseInsensitiveLessFunc( const CUtlString &lhs, const CUtlString &rhs ) { return V_stricmp( lhs.Get(), rhs.Get() ) < 0; } #endif // UTLSTRING_H |