CUtlString improvements

* Add method for finding a substring ('CUtlString::Find').
* Add method for appending a slash ('CUtlString::AppendSlash').
* Add 'CUtlString' overload for 'CUtlString::IsEqual_CaseSensitive'.
* Add 'CUtlString' overload for 'CUtlString::IsEqual_CaseInsensitive'.
* Add optional parameter to strip trailing slashes in 'CUtlString::DirName'.
* Add optional parameter to strip trailing slashes in 'CUtlString::StripFilename'.
This commit is contained in:
Kawe Mazidjatari 2023-05-29 21:35:12 +02:00
parent 04856caf05
commit 7e2b249ef6
2 changed files with 53 additions and 18 deletions

View File

@ -255,6 +255,8 @@ public:
// also implement Find/Insert/Remove functions that take const char* params.
typedef const char *AltArgumentType_t;
ptrdiff_t Find( const char* szTarget ) const;
// Take a piece out 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.
@ -264,7 +266,7 @@ public:
CUtlString Left( int64 nChars );
CUtlString Right( int64 nChars );
CUtlString Remove(char const *pTextToRemove, bool bCaseSensitive) const;
CUtlString Remove( char const *pTextToRemove, bool bCaseSensitive ) const;
// Replace all instances of one character with another.
CUtlString Replace( char cFrom, char cTo );
@ -298,14 +300,14 @@ public:
// Gets the filename (everything except the path.. c:\a\b\c\somefile.txt -> somefile.txt).
CUtlString UnqualifiedFilename() const;
// Strips off one directory. Uses V_StripLastDir but strips the last slash also!
CUtlString DirName();
// Strips off one directory. Uses V_StripLastDir but (optionally) strips the last slash also!
CUtlString DirName( bool bStripTrailingSlash = true ) 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 filename removed (uses V_UnqualifiedFileName and (optionally) also strips the last slash)
CUtlString StripFilename( bool bStripTrailingSlash = true ) const;
// Get a string with the base filename (with V_FileBase).
CUtlString GetBaseFilename() const;
@ -322,6 +324,15 @@ public:
// From Src2
void AppendSlash( char separator = CORRECT_PATH_SEPARATOR )
{
int64 nLength = Length() - 1;
if ( nLength > 0 && !PATHSEPARATOR( static_cast< char >( m_Storage[ nLength ] ) ) )
{
Append( separator );
}
}
void FixSlashes( char cSeparator = CORRECT_PATH_SEPARATOR )
{
for ( int64 nLength = Length() - 1; nLength >= 0; nLength-- )
@ -334,6 +345,10 @@ public:
}
}
inline bool IsEqual_CaseSensitive(const CUtlString& src) const
{
return IsEqual_CaseSensitive( src.Get() );
}
bool IsEqual_CaseSensitive( const char *src ) const
{
if ( !src )
@ -343,15 +358,18 @@ public:
return ( V_strcmp( Get(), src ) == 0 );
}
bool IsEqual_CaseInsensitive(const char *src) const
inline bool IsEqual_CaseInsensitive(const CUtlString& src) const
{
if (!src)
{
return (Length() == 0);
}
return (V_stricmp(Get(), src) == 0);
return IsEqual_CaseInsensitive( src.Get() );
}
bool IsEqual_CaseInsensitive( const char *src ) const
{
if ( !src )
{
return ( Length() == 0 );
}
return ( V_stricmp( Get(), src ) == 0 );
}
private:
CUtlBinaryBlock m_Storage;

View File

@ -102,7 +102,7 @@ void CUtlBinaryBlock::Get( void *pValue, int64 nLen ) const
}
}
void CUtlBinaryBlock::SetLength(int64 nLength )
void CUtlBinaryBlock::SetLength( int64 nLength )
{
//MEM_ALLOC_CREDIT();
Assert( !m_Memory.IsReadOnly() );
@ -437,6 +437,12 @@ void CUtlString::StripTrailingSlash()
}
}
// Find a substring
ptrdiff_t CUtlString::Find(const char* szTarget) const
{
return IndexOf( szTarget, Get() );
}
CUtlString CUtlString::Slice( int64 nStart, int64 nEnd )
{
if ( nStart < 0 )
@ -477,6 +483,7 @@ CUtlString CUtlString::Right( int64 nChars )
return Slice( -nChars );
}
// Get a string with the specified substring removed
CUtlString CUtlString::Remove( char const *pTextToRemove, bool bCaseSensitive ) const
@ -597,11 +604,18 @@ CUtlString CUtlString::UnqualifiedFilename() const
return CUtlString( pFilename );
}
CUtlString CUtlString::DirName()
CUtlString CUtlString::DirName( bool bStripTrailingSlash ) const
{
CUtlString ret( this->String() );
V_StripLastDir( (char*)ret.m_Storage.Get(), ret.m_Storage.Length() );
V_StripTrailingSlash( (char*)ret.m_Storage.Get() );
size_t len = 0;
V_StripLastDir( (char*)ret.m_Storage.Get(), ret.m_Storage.Length(), &len );
if (bStripTrailingSlash)
ret.StripTrailingSlash();
else
ret.SetLength(len); // StripTrailingSlash sets this, but if we skip it then nothing sets it.
return ret;
}
@ -612,13 +626,16 @@ CUtlString CUtlString::StripExtension() const
return CUtlString( szTemp );
}
CUtlString CUtlString::StripFilename() const
CUtlString CUtlString::StripFilename( bool bStripTrailingSlash ) const
{
const char *pFilename = V_UnqualifiedFileName( Get() ); // NOTE: returns 'Get()' on failure, never NULL
int64 nCharsToCopy = pFilename - Get();
CUtlString result;
result.SetDirect( Get(), nCharsToCopy );
result.StripTrailingSlash();
if ( bStripTrailingSlash )
result.StripTrailingSlash();
return result;
}