From 399131d57da9eb6e32c3f1c5facb58c21d053a05 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 30 May 2022 01:50:21 +0200 Subject: [PATCH] Add additional useful string utils --- r5dev/public/include/utility.h | 13 +++- r5dev/public/utility.cpp | 111 ++++++++++++++++++++++++++++++--- 2 files changed, 114 insertions(+), 10 deletions(-) diff --git a/r5dev/public/include/utility.h b/r5dev/public/include/utility.h index b0423a50..4bef33cb 100644 --- a/r5dev/public/include/utility.h +++ b/r5dev/public/include/utility.h @@ -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 StringToBytes(const string& svInput, bool bNullTerminator); vector PatternToBytes(const string& svInput); -vector IntToDigits(int value); +vector IntToDigits(int iValue); void PrintM128i8(__m128i in); void PrintM128i16(__m128i in); diff --git a/r5dev/public/utility.cpp b/r5dev/public/utility.cpp index b2d06093..7212d4f4 100644 --- a/r5dev/public/utility.cpp +++ b/r5dev/public/utility.cpp @@ -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 + //{ + // using std::codecvt::codecvt; + // ~destructible_codecvt() = default; + //}; + //std::wstring_convert 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 PatternToBytes(const string& svInput) /////////////////////////////////////////////////////////////////////////////// // For converting a integer into digits. -vector IntToDigits(int value) +vector IntToDigits(int iValue) { vector 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;