Fix string utility bugs

GetFileName returned null string if file is not within a path, now returns the input file name.
RemoveFileName returned file name if file is not within a path, now returns null string.
Added StringReplaceC (string replace with copy instead of modifying the reference object).
This commit is contained in:
Kawe Mazidjatari 2022-06-02 01:55:41 +02:00
parent 4d8d164df6
commit e6e4bd703c
2 changed files with 29 additions and 5 deletions

View File

@ -17,8 +17,8 @@ void HexDump(const char* szHeader, const char* szLogger, const void* pData, int
string GetExtension(const string& svInput);
string RemoveExtension(const string& svInput);
string GetFileName(const string& svInput, bool bRemoveExtension = false, bool bWindows = true);
string RemoveFileName(const string& svInput, bool bWindows = true);
string GetFileName(const string& svInput, bool bRemoveExtension, bool bWindows = false);
string RemoveFileName(const string& svInput, bool bWindows = false);
string CreateDirectories(string svInput);
string ConvertToWinPath(const string& svInput);
@ -35,6 +35,7 @@ bool CompareStringAlphabetically(const string& svA, const string& svB);
bool CompareStringLexicographically(const string& svA, const string& svB);
bool StringReplace(string& svInput, const string& svFrom, const string& svTo);
string StringReplaceC(const string& svInput, const string& svFrom, const string& svTo);
string StringEscape(const string& svInput);
string StringUnescape(const string& svInput);

View File

@ -277,7 +277,14 @@ string GetFileName(const string& svInput, bool bRemoveExtension, bool bWindows)
}
return svInput.substr(nPos + 1);
}
return "";
else // File name is not within path.
{
if (bRemoveExtension)
{
return RemoveExtension(svInput);
}
}
return svInput;
}
///////////////////////////////////////////////////////////////////////////////
@ -295,7 +302,7 @@ string RemoveFileName(const string& svInput, bool bWindows)
}
if (nPos == string::npos)
{
return svInput;
return "";
}
return svInput.substr(0, nPos);
}
@ -473,7 +480,7 @@ bool CompareStringLexicographically(const string& svA, const string& svB)
}
///////////////////////////////////////////////////////////////////////////////
// For replacing parts of a given string.
// For replacing parts of a given string by reference.
bool StringReplace(string& svInput, const string& svFrom, const string& svTo)
{
string::size_type nPos = svInput.find(svFrom);
@ -486,6 +493,22 @@ bool StringReplace(string& svInput, const string& svFrom, const string& svTo)
return true;
}
///////////////////////////////////////////////////////////////////////////////
// For replacing parts of a given string by value.
string StringReplaceC(const string& svInput, const string& svFrom, const string& svTo)
{
string results = svInput;
string::size_type nPos = results.find(svFrom);
if (nPos == string::npos)
{
return results;
}
results.replace(nPos, svFrom.length(), svTo);
return results;
}
///////////////////////////////////////////////////////////////////////////////
// For escaping special characters in a string.
string StringEscape(const string& svInput)