summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/corestr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/corestr.cpp')
-rw-r--r--src/lib/util/corestr.cpp44
1 files changed, 29 insertions, 15 deletions
diff --git a/src/lib/util/corestr.cpp b/src/lib/util/corestr.cpp
index 66b12f6ee04..c8775f1cec0 100644
--- a/src/lib/util/corestr.cpp
+++ b/src/lib/util/corestr.cpp
@@ -171,27 +171,41 @@ void strreplacechr(std::string& str, char ch, char newch)
}
}
-std::string strtrimspace(std::string& str)
+static std::string internal_strtrimspace(std::string& str, bool right_only)
{
- int start = 0;
- for (auto & elem : str)
- {
- if (!isspace(uint8_t(elem))) break;
- start++;
- }
- int end = str.length();
- if (end > 0)
+ // identify the start
+ std::string::iterator start = str.begin();
+ if (!right_only)
{
- for (size_t i = str.length() - 1; i > 0; i--)
- {
- if (!isspace(uint8_t(str[i]))) break;
- end--;
- }
+ start = std::find_if(
+ str.begin(),
+ str.end(),
+ [](char c) { return !isspace(uint8_t(c)); });
}
- str = str.substr(start, end-start);
+
+ // identify the end
+ std::string::iterator end = std::find_if(
+ str.rbegin(),
+ std::string::reverse_iterator(start),
+ [](char c) { return !isspace(uint8_t(c)); }).base();
+
+ // extract the string
+ str = end > start
+ ? str.substr(start - str.begin(), end - start)
+ : "";
return str;
}
+std::string strtrimspace(std::string& str)
+{
+ return internal_strtrimspace(str, false);
+}
+
+std::string strtrimrightspace(std::string& str)
+{
+ return internal_strtrimspace(str, true);
+}
+
std::string strmakeupper(std::string& str)
{
std::transform(str.begin(), str.end(), str.begin(), ::toupper);