From 24a4097cef0d00179491dfcee569347f50e607e2 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Fri, 11 Nov 2022 01:17:11 +0100 Subject: [PATCH] Improve string trimming logic Add option to trim anything before/after the criteria. --- r5dev/public/utility/utility.cpp | 44 +++++++++++++++++++++++++++----- r5dev/public/utility/utility.h | 6 ++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/r5dev/public/utility/utility.cpp b/r5dev/public/utility/utility.cpp index 305e63a6..7cbfbffc 100644 --- a/r5dev/public/utility/utility.cpp +++ b/r5dev/public/utility/utility.cpp @@ -748,25 +748,57 @@ vector StringSplit(string svInput, char cDelim, size_t nMax) /////////////////////////////////////////////////////////////////////////////// // For trimming leading characters from input. -string& StringLTrim(string& svInput, const char* pszToTrim) +string& StringLTrim(string& svInput, const char* pszToTrim, bool bTrimBefore) { - svInput.erase(0, svInput.find_first_not_of(pszToTrim)); + size_t n = 0; + if (!bTrimBefore) + { + n = svInput.find_first_not_of(pszToTrim); + } + else // Remove everything before criteria as well. + { + n = svInput.find_first_of(pszToTrim); + n = svInput.find_first_not_of(pszToTrim, n); + } + + if (n != string::npos) + { + svInput.erase(0, n); + } + return svInput; } /////////////////////////////////////////////////////////////////////////////// // For trimming trailing characters from input. -string& StringRTrim(string& svInput, const char* pszToTrim) +string& StringRTrim(string& svInput, const char* pszToTrim, bool bTrimAfter) { - svInput.erase(svInput.find_last_not_of(pszToTrim) + 1); + size_t n = 0; + if (!bTrimAfter) + { + n = svInput.find_last_not_of(pszToTrim) + 1; + } + else // Remove everything after criteria as well. + { + n = svInput.find_first_of(pszToTrim) + 1; + } + + if (n > 0) + { + svInput.erase(n); + if (bTrimAfter) + { + svInput.at(svInput.size() - 1) = '\0'; + } + } return svInput; } /////////////////////////////////////////////////////////////////////////////// // For trimming leading and trailing characters from input. -string& StringTrim(string& svInput, const char* pszToTrim) +string& StringTrim(string& svInput, const char* pszToTrim, bool bTrimAll) { - return StringLTrim(StringRTrim(svInput, pszToTrim), pszToTrim); + return StringRTrim(StringLTrim(svInput, pszToTrim, bTrimAll), pszToTrim, bTrimAll); } /////////////////////////////////////////////////////////////////////////////// diff --git a/r5dev/public/utility/utility.h b/r5dev/public/utility/utility.h index 181f4f09..0caedc93 100644 --- a/r5dev/public/utility/utility.h +++ b/r5dev/public/utility/utility.h @@ -52,9 +52,9 @@ string StringUnescape(const string& svInput); size_t StringCount(const string& svInput, char cDelim); vector StringSplit(string svInput, char cDelim, size_t nMax = SIZE_MAX); -string& StringLTrim(string& svInput, const char* pszToTrim); -string& StringRTrim(string& svInput, const char* pszToTrim); -string& StringTrim(string& svInput, const char* pszToTrim); +string& StringLTrim(string& svInput, const char* pszToTrim, bool bTrimBefore = false); +string& StringRTrim(string& svInput, const char* pszToTrim, bool bTrimAfter = false); +string& StringTrim(string& svInput, const char* pszToTrim, bool bTrimAll = false); ///////////////////////////////////////////////////////////////////////////// // Bytes