aboutsummaryrefslogtreecommitdiff
path: root/xbmc/utils/StringUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/StringUtils.cpp')
-rw-r--r--xbmc/utils/StringUtils.cpp121
1 files changed, 103 insertions, 18 deletions
diff --git a/xbmc/utils/StringUtils.cpp b/xbmc/utils/StringUtils.cpp
index 6d82e48830..d316d715a0 100644
--- a/xbmc/utils/StringUtils.cpp
+++ b/xbmc/utils/StringUtils.cpp
@@ -114,12 +114,26 @@ void StringUtils::ToLower(string &str)
bool StringUtils::EqualsNoCase(const std::string &str1, const std::string &str2)
{
- string tmp1 = str1;
- string tmp2 = str2;
- ToLower(tmp1);
- ToLower(tmp2);
-
- return tmp1.compare(tmp2) == 0;
+ return EqualsNoCase(str1.c_str(), str2.c_str());
+}
+
+bool StringUtils::EqualsNoCase(const std::string &str1, const char *s2)
+{
+ return EqualsNoCase(str1.c_str(), s2);
+}
+
+bool StringUtils::EqualsNoCase(const char *s1, const char *s2)
+{
+ int c1, c2; // Yes, because the return type of tolower() is int.
+ // To make these chars would be to introduce an unnecesary extra bitmask/zero-extend (effectively caller-narowing) into the binary.
+ do
+ {
+ c1 = ::tolower(*s1++);
+ c2 = ::tolower(*s2++);
+ if (c1 != c2) // This includes the possibility that one of the characters is the null-terminator, which implies a string mismatch.
+ return false;
+ } while (c2 != '\0'); // At this point, we know c1 == c2, so there's no need to test them both.
+ return true;
}
string StringUtils::Left(const string &str, size_t count)
@@ -225,24 +239,95 @@ int StringUtils::Replace(std::string &str, const std::string &oldStr, const std:
return replacedChars;
}
-bool StringUtils::StartsWith(const std::string &str, const std::string &str2, bool useCase /* = false */)
+bool StringUtils::StartsWith(const std::string &str1, const std::string &str2)
{
- std::string left = StringUtils::Left(str, str2.size());
-
- if (useCase)
- return left.compare(str2) == 0;
+ return str1.compare(0, str2.size(), str2) == 0;
+}
- return StringUtils::EqualsNoCase(left, str2);
+bool StringUtils::StartsWith(const std::string &str1, const char *s2)
+{
+ return StartsWith(str1.c_str(), s2);
}
-bool StringUtils::EndsWith(const std::string &str, const std::string &str2, bool useCase /* = false */)
+bool StringUtils::StartsWith(const char *s1, const char *s2)
{
- std::string right = StringUtils::Right(str, str2.size());
-
- if (useCase)
- return right.compare(str2) == 0;
+ while (*s2 != '\0')
+ {
+ if (*s1 != *s2)
+ return false;
+ s1++;
+ s2++;
+ }
+ return true;
+}
- return StringUtils::EqualsNoCase(right, str2);
+bool StringUtils::StartsWithNoCase(const std::string &str1, const std::string &str2)
+{
+ return StartsWithNoCase(str1.c_str(), str2.c_str());
+}
+
+bool StringUtils::StartsWithNoCase(const std::string &str1, const char *s2)
+{
+ return StartsWithNoCase(str1.c_str(), s2);
+}
+
+bool StringUtils::StartsWithNoCase(const char *s1, const char *s2)
+{
+ while (*s2 != '\0')
+ {
+ if (::tolower(*s1) != ::tolower(*s2))
+ return false;
+ s1++;
+ s2++;
+ }
+ return true;
+}
+
+bool StringUtils::EndsWith(const std::string &str1, const std::string &str2)
+{
+ if (str1.size() < str2.size())
+ return false;
+ return str1.compare(str1.size() - str2.size(), str2.size(), str2) == 0;
+}
+
+bool StringUtils::EndsWith(const std::string &str1, const char *s2)
+{
+ size_t len2 = strlen(s2);
+ if (str1.size() < len2)
+ return false;
+ return str1.compare(str1.size() - len2, len2, s2) == 0;
+}
+
+bool StringUtils::EndsWithNoCase(const std::string &str1, const std::string &str2)
+{
+ if (str1.size() < str2.size())
+ return false;
+ const char *s1 = str1.c_str() + str1.size() - str2.size();
+ const char *s2 = str2.c_str();
+ while (*s2 != '\0')
+ {
+ if (::tolower(*s1) != ::tolower(*s2))
+ return false;
+ s1++;
+ s2++;
+ }
+ return true;
+}
+
+bool StringUtils::EndsWithNoCase(const std::string &str1, const char *s2)
+{
+ size_t len2 = strlen(s2);
+ if (str1.size() < len2)
+ return false;
+ const char *s1 = str1.c_str() + str1.size() - len2;
+ while (*s2 != '\0')
+ {
+ if (::tolower(*s1) != ::tolower(*s2))
+ return false;
+ s1++;
+ s2++;
+ }
+ return true;
}
void StringUtils::JoinString(const CStdStringArray &strings, const CStdString& delimiter, CStdString& result)