]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/util/string.h
Hacked Client
[dragonfireclient.git] / src / util / string.h
index 584ffb73a07fffa8ec7dfc29393b2a048f9d8f6f..0d2a6bdb2cbcf6e70008da71ad0380bc5c53a2fd 100644 (file)
@@ -20,7 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #pragma once
 
 #include "irrlichttypes_bloated.h"
-#include <stdlib.h>
+#include "irrString.h"
+#include <cstdlib>
 #include <string>
 #include <cstring>
 #include <vector>
@@ -85,7 +86,8 @@ std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
 size_t mystrlcpy(char *dst, const char *src, size_t size);
 char *mystrtok_r(char *s, const char *sep, char **lasts);
 u64 read_seed(const char *str);
-bool parseColorString(const std::string &value, video::SColor &color, bool quiet);
+bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
+               unsigned char default_alpha = 0xff);
 
 
 /**
@@ -203,6 +205,56 @@ inline bool str_starts_with(const std::basic_string<T> &str,
                        case_insensitive);
 }
 
+
+/**
+ * Check whether \p str ends with the string suffix. If \p case_insensitive
+ * is true then the check is case insensitve (default is false; i.e. case is
+ * significant).
+ *
+ * @param str
+ * @param suffix
+ * @param case_insensitive
+ * @return true if the str begins with suffix
+ */
+template <typename T>
+inline bool str_ends_with(const std::basic_string<T> &str,
+               const std::basic_string<T> &suffix,
+               bool case_insensitive = false)
+{
+       if (str.size() < suffix.size())
+               return false;
+
+       size_t start = str.size() - suffix.size();
+       if (!case_insensitive)
+               return str.compare(start, suffix.size(), suffix) == 0;
+
+       for (size_t i = 0; i < suffix.size(); ++i)
+               if (tolower(str[start + i]) != tolower(suffix[i]))
+                       return false;
+       return true;
+}
+
+
+/**
+ * Check whether \p str ends with the string suffix. If \p case_insensitive
+ * is true then the check is case insensitve (default is false; i.e. case is
+ * significant).
+ *
+ * @param str
+ * @param suffix
+ * @param case_insensitive
+ * @return true if the str begins with suffix
+ */
+template <typename T>
+inline bool str_ends_with(const std::basic_string<T> &str,
+               const T *suffix,
+               bool case_insensitive = false)
+{
+       return str_ends_with(str, std::basic_string<T>(suffix),
+                       case_insensitive);
+}
+
+
 /**
  * Splits a string into its component parts separated by the character
  * \p delimiter.
@@ -231,12 +283,12 @@ inline std::vector<std::basic_string<T> > str_split(
  */
 inline std::string lowercase(const std::string &str)
 {
-       std::string s2 = "";
+       std::string s2;
 
        s2.reserve(str.size());
 
-       for (size_t i = 0; i < str.size(); i++)
-               s2 += tolower(str[i]);
+       for (char i : str)
+               s2 += tolower(i);
 
        return s2;
 }
@@ -598,6 +650,12 @@ std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
        return tokens;
 }
 
+std::wstring translate_string(const std::wstring &s);
+
+inline std::wstring unescape_translate(const std::wstring &s) {
+       return unescape_enriched(translate_string(s));
+}
+
 /**
  * Checks that all characters in \p to_check are a decimal digits.
  *
@@ -607,8 +665,8 @@ std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
  */
 inline bool is_number(const std::string &to_check)
 {
-       for (size_t i = 0; i < to_check.size(); i++)
-               if (!std::isdigit(to_check[i]))
+       for (char i : to_check)
+               if (!std::isdigit(i))
                        return false;
 
        return !to_check.empty();
@@ -647,3 +705,40 @@ inline const std::string duration_to_string(int sec)
 
        return ss.str();
 }
+
+/**
+ * Joins a vector of strings by the string \p delimiter.
+ *
+ * @return A std::string
+ */
+inline std::string str_join(const std::vector<std::string> &list,
+               const std::string &delimiter)
+{
+       std::ostringstream oss;
+       bool first = true;
+       for (const auto &part : list) {
+               if (!first)
+                       oss << delimiter;
+               oss << part;
+               first = false;
+       }
+       return oss.str();
+}
+
+/**
+ * Create a UTF8 std::string from a irr::core::stringw.
+ */
+inline std::string stringw_to_utf8(const irr::core::stringw &input)
+{
+       std::wstring str(input.c_str());
+       return wide_to_utf8(str);
+}
+
+ /**
+  * Create a irr::core:stringw from a UTF8 std::string.
+  */
+inline irr::core::stringw utf8_to_stringw(const std::string &input)
+{
+       std::wstring str = utf8_to_wide(input);
+       return irr::core::stringw(str.c_str());
+}