summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Wilson <chris+github@qwirx.com>2015-11-26 23:32:29 +0000
committerChris Wilson <chris+github@qwirx.com>2015-12-13 23:50:04 +0000
commite77de564aaacaed07075eaac1974d35d09dd2bce (patch)
tree88c94bc1401c834d1f63243999a31fc311f50cd6 /lib
parentcdd27f760290c27db7e3e1704e43eeceba07ea77 (diff)
Add some little string functions to Utils.cpp.
These functions check whether the beginning and end of a std::string match a supplied prefix or suffix, and remove that prefix or suffix, returning the remaining part. They are almost but not entirely trivial, and giving them names makes the code that uses them much more readable.
Diffstat (limited to 'lib')
-rw-r--r--lib/common/Utils.cpp36
-rw-r--r--lib/common/Utils.h4
2 files changed, 40 insertions, 0 deletions
diff --git a/lib/common/Utils.cpp b/lib/common/Utils.cpp
index a73a80da..4325dd88 100644
--- a/lib/common/Utils.cpp
+++ b/lib/common/Utils.cpp
@@ -95,6 +95,42 @@ void SplitString(std::string String, char SplitOn, std::vector<std::string> &rOu
#endif*/
}
+bool StartsWith(const std::string& prefix, const std::string& haystack)
+{
+ return haystack.size() >= prefix.size() &&
+ haystack.substr(0, prefix.size()) == prefix;
+}
+
+bool EndsWith(const std::string& suffix, const std::string& haystack)
+{
+ return haystack.size() >= suffix.size() &&
+ haystack.substr(haystack.size() - suffix.size()) == suffix;
+}
+
+std::string RemovePrefix(const std::string& prefix, const std::string& haystack)
+{
+ if(StartsWith(prefix, haystack))
+ {
+ return haystack.substr(prefix.size());
+ }
+ else
+ {
+ return "";
+ }
+}
+
+std::string RemoveSuffix(const std::string& suffix, const std::string& haystack)
+{
+ if(EndsWith(suffix, haystack))
+ {
+ return haystack.substr(0, haystack.size() - suffix.size());
+ }
+ else
+ {
+ return "";
+ }
+}
+
static std::string demangle(const std::string& mangled_name)
{
std::string demangled_name = mangled_name;
diff --git a/lib/common/Utils.h b/lib/common/Utils.h
index 3a5f0eb5..d306ce1c 100644
--- a/lib/common/Utils.h
+++ b/lib/common/Utils.h
@@ -18,6 +18,10 @@
std::string GetBoxBackupVersion();
void SplitString(std::string String, char SplitOn, std::vector<std::string> &rOutput);
+bool StartsWith(const std::string& prefix, const std::string& haystack);
+bool EndsWith(const std::string& prefix, const std::string& haystack);
+std::string RemovePrefix(const std::string& prefix, const std::string& haystack);
+std::string RemoveSuffix(const std::string& suffix, const std::string& haystack);
void DumpStackBacktrace();