From edab962f4183b599c4c7c5beb75947589443ccca Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 1 Dec 2015 22:58:51 +0300 Subject: Add string cut functions. --- src/utils/stringutils.cpp | 20 ++++++++++++++++++++ src/utils/stringutils.h | 6 ++++++ 2 files changed, 26 insertions(+) (limited to 'src/utils') diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 864f509fc..ee6b6fb07 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -638,6 +638,16 @@ bool findCutLast(std::string &restrict str1, const std::string &restrict str2) return false; } +void cutLast(std::string &restrict str1, const std::string &restrict str2) +{ + const size_t s1 = str1.size(); + const size_t s2 = str2.size(); + if (s1 < s2) + return; + if (str1.substr(s1 - s2) == str2) + str1 = str1.substr(0, s1 - s2); +} + bool findCutFirst(std::string &restrict str1, const std::string &restrict str2) { const size_t s1 = str1.size(); @@ -652,6 +662,16 @@ bool findCutFirst(std::string &restrict str1, const std::string &restrict str2) return false; } +void cutFirst(std::string &restrict str1, const std::string &restrict str2) +{ + const size_t s1 = str1.size(); + const size_t s2 = str2.size(); + if (s1 < s2) + return; + if (str1.substr(0, s2) == str2) + str1 = str1.substr(s2); +} + std::string &removeProtocol(std::string &url) { const size_t i = url.find("://"); diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 73e4b7697..31f562587 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -222,9 +222,15 @@ bool findFirst(const std::string &restrict str1, bool findCutLast(std::string &restrict str1, const std::string &restrict str2) A_WARN_UNUSED; +void cutLast(std::string &restrict str1, + const std::string &restrict str2); + bool findCutFirst(std::string &restrict str1, const std::string &restrict str2); +void cutFirst(std::string &restrict str1, + const std::string &restrict str2); + std::string &removeProtocol(std::string &url); bool strStartWith(const std::string &restrict str, -- cgit v1.2.3-70-g09d2