Expose getPointedThing to Lua
[oweals/minetest.git] / src / util / string.h
index c77c5a6f97bbb25ce814e9a64a90d74e249c8514..c0f4beefbdd70149ec68b469d12613447335df30 100644 (file)
@@ -27,7 +27,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <vector>
 #include <map>
 #include <sstream>
+#include <iomanip>
 #include <cctype>
+#include <unordered_map>
 
 #define STRINGIFY(x) #x
 #define TOSTRING(x) STRINGIFY(x)
@@ -53,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;
@@ -77,8 +79,8 @@ wchar_t *narrow_to_wide_c(const char *str);
 std::wstring narrow_to_wide(const std::string &mbs);
 std::string wide_to_narrow(const std::wstring &wcs);
 
-std::string urlencode(std::string str);
-std::string urldecode(std::string str);
+std::string urlencode(const std::string &str);
+std::string urldecode(const std::string &str);
 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask);
 size_t mystrlcpy(char *dst, const char *src, size_t size);
@@ -230,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());
 
@@ -350,23 +352,57 @@ inline T from_string(const std::string &str)
 /// Returns a 64-bit signed value represented by the string \p str (decimal).
 inline s64 stoi64(const std::string &str) { return from_string<s64>(str); }
 
-// TODO: Replace with C++11 std::to_string.
+#if __cplusplus < 201103L
+namespace std {
 
 /// Returns a string representing the value \p val.
 template <typename T>
-inline std::string to_string(T val)
+inline string to_string(T val)
 {
-       std::ostringstream oss;
+       ostringstream oss;
        oss << val;
        return oss.str();
 }
+#define DEFINE_STD_TOSTRING_FLOATINGPOINT(T)           \
+       template <>                                     \
+       inline string to_string<T>(T val)               \
+       {                                               \
+               ostringstream oss;                      \
+               oss << std::fixed                       \
+                       << std::setprecision(6)         \
+                       << val;                         \
+               return oss.str();                       \
+       }
+DEFINE_STD_TOSTRING_FLOATINGPOINT(float)
+DEFINE_STD_TOSTRING_FLOATINGPOINT(double)
+DEFINE_STD_TOSTRING_FLOATINGPOINT(long double)
+
+#undef DEFINE_STD_TOSTRING_FLOATINGPOINT
+
+/// Returns a wide string representing the value \p val
+template <typename T>
+inline wstring to_wstring(T val)
+{
+      return utf8_to_wide(to_string(val));
+}
+}
+#endif
 
 /// Returns a string representing the decimal value of the 32-bit value \p i.
-inline std::string itos(s32 i) { return to_string(i); }
+inline std::string itos(s32 i) { return std::to_string(i); }
 /// Returns a string representing the decimal value of the 64-bit value \p i.
-inline std::string i64tos(s64 i) { return to_string(i); }
+inline std::string i64tos(s64 i) { return std::to_string(i); }
+
+// std::to_string uses the '%.6f' conversion, which is inconsistent with
+// std::ostream::operator<<() and impractical too.  ftos() uses the
+// more generic and std::ostream::operator<<()-compatible '%G' format.
 /// Returns a string representing the decimal value of the float value \p f.
-inline std::string ftos(float f) { return to_string(f); }
+inline std::string ftos(float f)
+{
+       std::ostringstream oss;
+       oss << f;
+       return oss.str();
+}
 
 
 /**
@@ -386,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.
  *
@@ -578,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