diff --git a/public/tier1/strtools.h b/public/tier1/strtools.h index 40730607..95ad8ddb 100644 --- a/public/tier1/strtools.h +++ b/public/tier1/strtools.h @@ -502,7 +502,7 @@ PLATFORM_INTERFACE bool _V_StrSubst( const char *pIn, const char *pMatch, const // Split the specified string on the specified separator. // Returns a list of strings separated by pSeparator. // You are responsible for freeing the contents of outStrings (call outStrings.PurgeAndDeleteElements). -PLATFORM_OVERLOAD void V_SplitString( const char *pString, const char *pSeparator, CUtlVector> &outStrings, bool ); +PLATFORM_OVERLOAD void V_SplitString( const char *pString, const char *pSeparator, CUtlVector> &outStrings, bool include_empty = false ); // Just like V_SplitString, but it can use multiple possible separators. PLATFORM_OVERLOAD void V_SplitStringInPlace( char *pString, const char *pSeparator, CUtlVector> &outStrings ); diff --git a/public/tier1/utlvector.h b/public/tier1/utlvector.h index 3b6ff2e1..8d8bbe59 100644 --- a/public/tier1/utlvector.h +++ b/public/tier1/utlvector.h @@ -1191,17 +1191,27 @@ public: // placing it here a few days before Cert to minimize disruption to the rest of codebase -class CSplitString: public CUtlVector > +class CSplitString : public CUtlVector> { public: - CSplitString(const char *pString, const char *pSeparator, bool bIncludeSeparators = false) : m_szBuffer(nullptr) + // Splits the string based on separator provided + // Example: string "test;string string2;here" with a separator ";" + // results in [ "test", "string string2", "here" ] array entries. + // Separator can be multicharacter, i.e. "-;" would split string like + // "test-;string" into [ "test", "string" ] + CSplitString( const char *pString, const char *pSeparator, bool include_empty = false ) : m_szBuffer( nullptr ), m_nBufferSize( 0 ) { - Split( pString, -1, &pSeparator, 1, bIncludeSeparators); + Split( pString, -1, &pSeparator, 1, include_empty ); } - CSplitString(const char *pString, const char **pSeparators, int nSeparators, bool bIncludeSeparators = false) : m_szBuffer(nullptr) + // Splits the string based on array of separators provided + // Example: string "test;string,string2;here" with a separators [ ";", "," ] + // results in [ "test", "string", "string2", "here" ] array entries. + // Separator can be multicharacter, i.e. "-;" would split string like + // "test-;string" into [ "test", "string" ] + CSplitString( const char *pString, const char **pSeparators, int nSeparators, bool include_empty = false ) : m_szBuffer( nullptr ), m_nBufferSize( 0 ) { - Split(pString, -1, pSeparators, nSeparators, bIncludeSeparators); + Split( pString, -1, pSeparators, nSeparators, include_empty ); } ~CSplitString() @@ -1210,11 +1220,13 @@ public: delete[] m_szBuffer; } - // - // NOTE: If you want to make Construct() public and implement Purge() here, you'll have to free m_szBuffer there - // + // Works the same way as CSplitString::Split with the only exception that it allows you + // to provide single string of separators like ";,:" which will then get used to separate string + // instead of providing an array of separators, but doesn't support multicharacter separators cuz of that! + DLL_CLASS_IMPORT void SetPacked( const char *pString, const char *pSeparators, bool include_empty = false, int stringSize = -1 ); + private: - DLL_CLASS_IMPORT void Split(const char *pString, int stringSize, const char **pSeparators, int nSeparators, bool bIncludeSeparators); + DLL_CLASS_IMPORT void Split(const char *pString, int stringSize, const char **pSeparators, int nSeparators, bool include_empty = false ); void PurgeAndDeleteElements() { @@ -1223,6 +1235,7 @@ private: private: char *m_szBuffer; // a copy of original string, with '\0' instead of separators + int m_nBufferSize; };