summaryrefslogtreecommitdiff
path: root/src/util/string.h
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2016-12-22 23:16:00 +0100
committerest31 <MTest31@outlook.com>2016-12-22 23:16:00 +0100
commit81d56b94919dceb7b2e51d70b21a7ca22f852bd5 (patch)
tree1e9ef1be1b3295a8673d6e4f0bdeb4c2d3a6015f /src/util/string.h
parent8077612dcb48221281e726a60eb97bf73fde462b (diff)
parent231ac33d34dfaaddf292c5f31b1eae43eeefba2d (diff)
downloadminetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.tar.gz
minetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.tar.bz2
minetest-81d56b94919dceb7b2e51d70b21a7ca22f852bd5.zip
Merge 0.4.15 changes into stable-0.4
0.4.15 release!
Diffstat (limited to 'src/util/string.h')
-rw-r--r--src/util/string.h86
1 files changed, 77 insertions, 9 deletions
diff --git a/src/util/string.h b/src/util/string.h
index 40ef3e4d3..572c37150 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -21,12 +21,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define UTIL_STRING_HEADER
#include "irrlichttypes_bloated.h"
+#include "cpp11_container.h"
#include <stdlib.h>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <sstream>
+#include <iomanip>
#include <cctype>
#define STRINGIFY(x) #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 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);
@@ -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();
+}
/**
@@ -519,6 +555,38 @@ std::basic_string<T> unescape_enriched(const std::basic_string<T> &s)
return output;
}
+template <typename T>
+std::vector<std::basic_string<T> > split(const std::basic_string<T> &s, T delim)
+{
+ std::vector<std::basic_string<T> > tokens;
+
+ std::basic_string<T> current;
+ bool last_was_escape = false;
+ for (size_t i = 0; i < s.length(); i++) {
+ T si = s[i];
+ if (last_was_escape) {
+ current += '\\';
+ current += si;
+ last_was_escape = false;
+ } else {
+ if (si == delim) {
+ tokens.push_back(current);
+ current = std::basic_string<T>();
+ last_was_escape = false;
+ } else if (si == '\\') {
+ last_was_escape = true;
+ } else {
+ current += si;
+ last_was_escape = false;
+ }
+ }
+ }
+ //push last element
+ tokens.push_back(current);
+
+ return tokens;
+}
+
/**
* Checks that all characters in \p to_check are a decimal digits.
*