[cstdstring] adds wstring version of Replace to StringUtils
authorJonathan Marshall <jmarshall@xbmc.org>
Sun, 27 Oct 2013 05:27:28 +0000 (18:27 +1300)
committerJonathan Marshall <jmarshall@xbmc.org>
Wed, 13 Nov 2013 21:52:59 +0000 (10:52 +1300)
xbmc/utils/StringUtils.cpp
xbmc/utils/StringUtils.h

index 82e6ec0..f585404 100644 (file)
@@ -323,6 +323,24 @@ int StringUtils::Replace(std::string &str, const std::string &oldStr, const std:
   return replacedChars;
 }
 
+int StringUtils::Replace(std::wstring &str, const std::wstring &oldStr, const std::wstring &newStr)
+{
+  if (oldStr.empty())
+    return 0;
+
+  int replacedChars = 0;
+  size_t index = 0;
+
+  while (index < str.size() && (index = str.find(oldStr, index)) != string::npos)
+  {
+    str.replace(index, oldStr.size(), newStr);
+    index += newStr.size();
+    replacedChars++;
+  }
+
+  return replacedChars;
+}
+
 bool StringUtils::StartsWith(const std::string &str1, const std::string &str2)
 {
   return str1.compare(0, str2.size(), str2) == 0;
index 9b66a07..c407ecd 100644 (file)
@@ -72,6 +72,7 @@ public:
   static std::string& RemoveDuplicatedSpacesAndTabs(std::string& str);
   static int Replace(std::string &str, char oldChar, char newChar);
   static int Replace(std::string &str, const std::string &oldStr, const std::string &newStr);
+  static int Replace(std::wstring &str, const std::wstring &oldStr, const std::wstring &newStr);
   static bool StartsWith(const std::string &str1, const std::string &str2);
   static bool StartsWith(const std::string &str1, const char *s2);
   static bool StartsWith(const char *s1, const char *s2);