Expose getPointedThing to Lua
[oweals/minetest.git] / src / util / string.h
index 724543a365f5804a7e3ebeba58384ffd223c5d9c..c0f4beefbdd70149ec68b469d12613447335df30 100644 (file)
@@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <sstream>
 #include <iomanip>
 #include <cctype>
+#include <unordered_map>
 
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
@@ -54,7 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
        (((unsigned char)(x) < 0xe0) ? 2 :     \
        (((unsigned char)(x) < 0xf0) ? 3 : 4))
 
-typedef std::map<std::string, std::string> StringMap;
+typedef std::unordered_map<std::string, std::string> StringMap;
 
 struct FlagDesc {
        const char *name;
@@ -231,7 +232,7 @@ 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());
 
@@ -421,6 +422,18 @@ inline void str_replace(std::string &str, const std::string &pattern,
        }
 }
 
+/**
+ * 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.
  *
@@ -613,4 +626,28 @@ inline const char *bool_to_cstr(bool val)
        return val ? "true" : "false";
 }
 
+inline const std::string duration_to_string(int sec)
+{
+       int min = sec / 60;
+       sec %= 60;
+       int hour = min / 60;
+       min %= 60;
+
+       std::stringstream ss;
+       if (hour > 0) {
+               ss << hour << "h ";
+       }
+
+       if (min > 0) {
+               ss << min << "m ";
+       }
+
+       if (sec > 0) {
+               ss << sec << "s ";
+       }
+
+       return ss.str();
+}
+
+
 #endif