Add additional useful string utils

This commit is contained in:
Kawe Mazidjatari 2022-05-30 01:50:21 +02:00
parent 148a6f424c
commit 399131d57d
2 changed files with 114 additions and 10 deletions

View File

@ -14,13 +14,22 @@ void DbgPrint(LPCSTR sFormat, ...);
void PrintLastError(void);
void HexDump(const char* szHeader, const char* szLogger, const void* pData, int nSize);
string CreateDirectories(string svFilePath);
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 CreateDirectories(string svInput);
string ConvertToWinPath(const string& svInput);
string ConvertToUnixPath(const string& svInput);
string Base64Encode(const string& svInput);
string Base64Decode(const string& svInput);
string UTF8Encode(const wstring& wsvInput);
u32string UTF8Decode(const string& svInput);
bool StringIsDigit(const string& svInput);
bool CompareStringAlphabetically(const string& svA, const string& svB);
bool CompareStringLexicographically(const string& svA, const string& svB);
@ -31,7 +40,7 @@ string StringUnescape(const string& svInput);
vector<int> StringToBytes(const string& svInput, bool bNullTerminator);
vector<int> PatternToBytes(const string& svInput);
vector<int> IntToDigits(int value);
vector<int> IntToDigits(int iValue);
void PrintM128i8(__m128i in);
void PrintM128i16(__m128i in);

View File

@ -232,6 +232,74 @@ void HexDump(const char* szHeader, const char* szLogger, const void* pData, int
///////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
// For removing file names from the extension.
string GetExtension(const string& svInput)
{
string::size_type nPos = svInput.rfind('.');
if (nPos != std::string::npos)
{
return svInput.substr(nPos + 1);
}
return "";
}
///////////////////////////////////////////////////////////////////////////////
// For removing extensions from file names.
string RemoveExtension(const string& svInput)
{
string::size_type nPos = svInput.find_last_of(".");
if (nPos == string::npos)
{
return svInput;
}
return svInput.substr(0, nPos);
}
///////////////////////////////////////////////////////////////////////////////
// For removing the path from file names.
string GetFileName(const string& svInput, bool bRemoveExtension, bool bWindows)
{
string::size_type nPos;
if (bWindows)
{
nPos = svInput.rfind('\\');
}
else
{
nPos = svInput.rfind('/');
}
if (nPos != std::string::npos)
{
if (bRemoveExtension)
{
return RemoveExtension(svInput.substr(nPos + 1));
}
return svInput.substr(nPos + 1);
}
return "";
}
///////////////////////////////////////////////////////////////////////////////
// For removing file names from the path.
string RemoveFileName(const string& svInput, bool bWindows)
{
string::size_type nPos;
if (bWindows)
{
nPos = svInput.find_last_of("\\");
}
else
{
nPos = svInput.find_last_of("/");
}
if (nPos == string::npos)
{
return svInput;
}
return svInput.substr(0, nPos);
}
///////////////////////////////////////////////////////////////////////////////
// For creating directories for output streams.
string CreateDirectories(string svInput)
@ -286,7 +354,7 @@ string ConvertToUnixPath(const string& svInput)
}
///////////////////////////////////////////////////////////////////////////////
// For encoding data in base64.
// For encoding data in Base64.
string Base64Encode(const string& svInput)
{
string results;
@ -314,7 +382,7 @@ string Base64Encode(const string& svInput)
}
///////////////////////////////////////////////////////////////////////////////
// For decoding data in base64.
// For decoding data in Base64.
string Base64Decode(const string& svInput)
{
string results;
@ -343,6 +411,33 @@ string Base64Decode(const string& svInput)
return results;
}
///////////////////////////////////////////////////////////////////////////////
// For encoding data in UTF8.
string UTF8Encode(const wstring& wsvInput)
{
string results;
int nLen = WideCharToMultiByte(CP_UTF8, 0, wsvInput.c_str(), wsvInput.length(), NULL, 0, NULL, NULL);
if (nLen > 0)
{
results.resize(nLen);
WideCharToMultiByte(CP_UTF8, 0, wsvInput.c_str(), wsvInput.length(), &results[0], nLen, NULL, NULL);
}
return results;
}
///////////////////////////////////////////////////////////////////////////////
// For decoding data in UTF8.
u32string UTF8Decode(const string& svInput)
{
//struct destructible_codecvt : public std::codecvt<char32_t, char, std::mbstate_t>
//{
// using std::codecvt<char32_t, char, std::mbstate_t>::codecvt;
// ~destructible_codecvt() = default;
//};
//std::wstring_convert<destructible_codecvt, char32_t> utf32_converter;
//return utf32_converter.from_bytes(svInput);
}
///////////////////////////////////////////////////////////////////////////////
// For checking if a string is a number.
bool StringIsDigit(const string& svInput)
@ -381,13 +476,13 @@ bool CompareStringLexicographically(const string& svA, const string& svB)
// For replacing parts of a given string.
bool StringReplace(string& svInput, const string& svFrom, const string& svTo)
{
size_t start_pos = svInput.find(svFrom);
if (start_pos == string::npos)
string::size_type nPos = svInput.find(svFrom);
if (nPos == string::npos)
{
return false;
}
svInput.replace(start_pos, svFrom.length(), svTo);
svInput.replace(nPos, svFrom.length(), svTo);
return true;
}
@ -491,12 +586,12 @@ vector<int> PatternToBytes(const string& svInput)
///////////////////////////////////////////////////////////////////////////////
// For converting a integer into digits.
vector<int> IntToDigits(int value)
vector<int> IntToDigits(int iValue)
{
vector<int> vDigits;
for (; value > 0; value /= 10)
for (; iValue > 0; iValue /= 10)
{
vDigits.push_back(value % 10);
vDigits.push_back(iValue % 10);
}
std::reverse(vDigits.begin(), vDigits.end());
return vDigits;