51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef UTIL_STRING_HEADER
-#define UTIL_STRING_HEADER
+#pragma once
#include "irrlichttypes_bloated.h"
-#include "cpp11_container.h"
-#include <stdlib.h>
+#include "irrString.h"
+#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cctype>
+#include <unordered_map>
+
+class Translations;
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
(((unsigned char)(x) < 0xe0) ? 2 : \
(((unsigned char)(x) < 0xf0) ? 3 : 4))
-typedef UNORDERED_MAP<std::string, std::string> StringMap;
+typedef std::unordered_map<std::string, std::string> StringMap;
struct FlagDesc {
const char *name;
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);
/**
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.
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;
}
}
}
+/**
+ * Escapes characters [ ] \ , ; that can not be used in formspecs
+ */
+inline void str_formspec_escape(std::string &str)
+{
+ str_replace(str, "\\", "\\\\");
+ str_replace(str, "]", "\\]");
+ str_replace(str, "[", "\\[");
+ str_replace(str, ";", "\\;");
+ str_replace(str, ",", "\\,");
+}
+
/**
* Replace all occurrences of the character \p from in \p str with \p to.
*
return tokens;
}
+std::wstring translate_string(const std::wstring &s, Translations *translations);
+
+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.
*
*/
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();
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();
+}
-#endif
+/**
+ * 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());
+}
+
+/**
+ * Sanitize the name of a new directory. This consists of two stages:
+ * 1. Check for 'reserved filenames' that can't be used on some filesystems
+ * and prefix them
+ * 2. Remove 'unsafe' characters from the name by replacing them with '_'
+ */
+std::string sanitizeDirName(const std::string &str, const std::string &optional_prefix);