1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-01-04 00:23:25 +08:00

Update CSplitString

This commit is contained in:
GAMMACASE 2024-10-07 03:40:38 +03:00
parent f8b8fe219b
commit e311e59189
2 changed files with 23 additions and 10 deletions

View File

@ -502,7 +502,7 @@ PLATFORM_INTERFACE bool _V_StrSubst( const char *pIn, const char *pMatch, const
// Split the specified string on the specified separator. // Split the specified string on the specified separator.
// Returns a list of strings separated by pSeparator. // Returns a list of strings separated by pSeparator.
// You are responsible for freeing the contents of outStrings (call outStrings.PurgeAndDeleteElements). // You are responsible for freeing the contents of outStrings (call outStrings.PurgeAndDeleteElements).
PLATFORM_OVERLOAD void V_SplitString( const char *pString, const char *pSeparator, CUtlVector<CUtlString, CUtlMemory<CUtlString, int>> &outStrings, bool ); PLATFORM_OVERLOAD void V_SplitString( const char *pString, const char *pSeparator, CUtlVector<CUtlString, CUtlMemory<CUtlString, int>> &outStrings, bool include_empty = false );
// Just like V_SplitString, but it can use multiple possible separators. // Just like V_SplitString, but it can use multiple possible separators.
PLATFORM_OVERLOAD void V_SplitStringInPlace( char *pString, const char *pSeparator, CUtlVector<const char *, CUtlMemory<const char *, int>> &outStrings ); PLATFORM_OVERLOAD void V_SplitStringInPlace( char *pString, const char *pSeparator, CUtlVector<const char *, CUtlMemory<const char *, int>> &outStrings );

View File

@ -1191,17 +1191,27 @@ public:
// <Sergiy> placing it here a few days before Cert to minimize disruption to the rest of codebase // <Sergiy> placing it here a few days before Cert to minimize disruption to the rest of codebase
class CSplitString: public CUtlVector<char*, CUtlMemory<char*, int> > class CSplitString : public CUtlVector<char *, CUtlMemory<char *, int>>
{ {
public: 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() ~CSplitString()
@ -1210,11 +1220,13 @@ public:
delete[] m_szBuffer; delete[] m_szBuffer;
} }
// // Works the same way as CSplitString::Split with the only exception that it allows you
// NOTE: If you want to make Construct() public and implement Purge() here, you'll have to free m_szBuffer there // 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: 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() void PurgeAndDeleteElements()
{ {
@ -1223,6 +1235,7 @@ private:
private: private:
char *m_szBuffer; // a copy of original string, with '\0' instead of separators char *m_szBuffer; // a copy of original string, with '\0' instead of separators
int m_nBufferSize;
}; };