aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTreeston <treeston.mmoc@gmail.com>2020-09-02 22:04:45 +0200
committerGitHub <noreply@github.com>2020-09-02 22:04:45 +0200
commitf45aa5cac1579e87cbc599ffb58e10e662066792 (patch)
tree250812f5a1608c21f3e04cbc460e8be0cece147c /src
parenteaf8fa75a1c76131ecbf2585db6d6236b7334b8e (diff)
Common/Util: Trinity::StringTo<double> support (PR #25364)
Diffstat (limited to 'src')
-rw-r--r--src/common/Utilities/StringConvert.h86
-rw-r--r--src/server/game/Chat/ChatCommands/ChatCommandArgs.h30
2 files changed, 90 insertions, 26 deletions
diff --git a/src/common/Utilities/StringConvert.h b/src/common/Utilities/StringConvert.h
index f264c0222fa..4ec3324fb33 100644
--- a/src/common/Utilities/StringConvert.h
+++ b/src/common/Utilities/StringConvert.h
@@ -39,7 +39,6 @@ namespace Trinity::Impl::StringConvertImpl
*/
};
- // @todo relax this once proper std::from_chars support exists
template <typename T>
struct For<T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
{
@@ -171,6 +170,91 @@ namespace Trinity::Impl::StringConvertImpl
return (val ? "1" : "0");
}
};
+
+#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT
+ template <typename T>
+ struct For<T, std::enable_if_t<std::is_floating_point_v<T>>>
+ {
+ static Optional<T> FromString(std::string_view str, std::chars_format fmt = std::chars_format())
+ {
+ if (str.empty())
+ return std::nullopt;
+
+ if (fmt == std::chars_format())
+ {
+ if (StringEqualI(str.substr(0, 2), "0x"))
+ {
+ fmt = std::chars_format::hex;
+ str.remove_prefix(2);
+ }
+ else
+ fmt = std::chars_format::general;
+
+ if (str.empty())
+ return std::nullopt;
+ }
+
+ char const* const start = str.data();
+ char const* const end = (start + str.length());
+
+ T val;
+ std::from_chars_result const res = std::from_chars(start, end, val, fmt);
+ if ((res.ptr == end) && (res.ec == std::errc()))
+ return val;
+ else
+ return std::nullopt;
+ }
+
+ // this allows generic converters for all numeric types (easier templating!)
+ static Optional<T> FromString(std::string_view str, int base)
+ {
+ if (base == 16)
+ return FromString(str, std::chars_format::hex);
+ else if (base == 10)
+ return FromString(str, std::chars_format::general);
+ else
+ return FromString(str, std::chars_format());
+ }
+
+ static std::string ToString(T val)
+ {
+ return std::to_string(val);
+ }
+ };
+#else
+ // @todo replace this once libc++ supports double args to from_chars
+ template <typename T>
+ struct For<T, std::enable_if_t<std::is_floating_point_v<T>>>
+ {
+ static Optional<T> FromString(std::string_view str, int base = 0)
+ {
+ try {
+ if (str.empty())
+ return std::nullopt;
+
+ if ((base == 10) && StringEqualI(str.substr(0, 2), "0x"))
+ return std::nullopt;
+
+ std::string tmp;
+ if (base == 16)
+ tmp.append("0x");
+ tmp.append(str);
+
+ size_t n;
+ T val = static_cast<T>(std::stold(tmp, &n));
+ if (n != tmp.length())
+ return std::nullopt;
+ return val;
+ }
+ catch (...) { return std::nullopt; }
+ }
+
+ static std::string ToString(T val)
+ {
+ return std::to_string(val);
+ }
+ };
+#endif
}
namespace Trinity
diff --git a/src/server/game/Chat/ChatCommands/ChatCommandArgs.h b/src/server/game/Chat/ChatCommands/ChatCommandArgs.h
index 583edcd40e2..9f10013989c 100644
--- a/src/server/game/Chat/ChatCommands/ChatCommandArgs.h
+++ b/src/server/game/Chat/ChatCommands/ChatCommandArgs.h
@@ -45,9 +45,9 @@ namespace Trinity::Impl::ChatCommands
template <typename T, typename = void>
struct ArgInfo { static_assert(!std::is_same_v<T,T>, "Invalid command parameter type - see ChatCommandArgs.h for possible types"); };
-// catch-all for integral types
+// catch-all for number types
template <typename T>
-struct ArgInfo<T, std::enable_if_t<std::is_integral_v<T>>>
+struct ArgInfo<T, std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>>>
{
static Optional<std::string_view> TryConsume(T& val, std::string_view args)
{
@@ -59,34 +59,14 @@ struct ArgInfo<T, std::enable_if_t<std::is_integral_v<T>>>
val = *v;
else
return std::nullopt;
- return tail;
- }
-};
-
-// catch-all for floating point types
-template <typename T>
-struct ArgInfo<T, std::enable_if_t<std::is_floating_point_v<T>>>
-{
- static Optional<std::string_view> TryConsume(T& val, std::string_view args)
- {
- auto [token, tail] = tokenize(args);
- if (token.empty())
- return std::nullopt;
- try
+ if constexpr (std::is_floating_point_v<T>)
{
- // @todo replace this once libc++ supports double args to from_chars for required minimum
- size_t processedChars = 0;
- val = std::stold(std::string(token), &processedChars);
- if (processedChars != token.length())
+ if (!std::isfinite(val))
return std::nullopt;
}
- catch (...) { return std::nullopt; }
- if (std::isfinite(val))
- return tail;
- else
- return std::nullopt;
+ return tail;
}
};