aboutsummaryrefslogtreecommitdiff
path: root/src/util/string.h
diff options
context:
space:
mode:
authorRogier-5 <rogier777@gmail.com>2016-08-11 19:22:40 +0200
committerest31 <est31@users.noreply.github.com>2016-08-11 19:22:40 +0200
commitb11720af459d44a553cb5d23ef776a632fb30a65 (patch)
tree78618a26db2ee19e892961b73a26db66fd889e4e /src/util/string.h
parent35f47e5461042b3d190f8ea583aec2aa26f1c484 (diff)
downloadminetest-b11720af459d44a553cb5d23ef776a632fb30a65.tar.gz
minetest-b11720af459d44a553cb5d23ef776a632fb30a65.tar.bz2
minetest-b11720af459d44a553cb5d23ef776a632fb30a65.zip
Use the standard to_string() functions for C++11 (#4279)
If compiling according to a C++ version before C++11, then define std::to_string ourselves. Add a to_wstring version as well As std::to_string() for floating point types uses %.6f as floating point format converter, instead of %G, it needs special care. To preserve ftos() behavior (which is expected to use the %G format converter), it no longer uses to_string().
Diffstat (limited to 'src/util/string.h')
-rw-r--r--src/util/string.h47
1 files changed, 41 insertions, 6 deletions
diff --git a/src/util/string.h b/src/util/string.h
index 8f4ef4711..724543a36 100644
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include <map>
#include <sstream>
+#include <iomanip>
#include <cctype>
#define STRINGIFY(x) #x
@@ -350,23 +351,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();
+}
/**