From f45aa5cac1579e87cbc599ffb58e10e662066792 Mon Sep 17 00:00:00 2001 From: Treeston Date: Wed, 2 Sep 2020 22:04:45 +0200 Subject: Common/Util: Trinity::StringTo support (PR #25364) --- src/common/Utilities/StringConvert.h | 86 +++++++++++++++++++++- .../game/Chat/ChatCommands/ChatCommandArgs.h | 30 ++------ 2 files changed, 90 insertions(+), 26 deletions(-) (limited to 'src') 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 struct For && !std::is_same_v>> { @@ -171,6 +170,91 @@ namespace Trinity::Impl::StringConvertImpl return (val ? "1" : "0"); } }; + +#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT + template + struct For>> + { + static Optional 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 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 + struct For>> + { + static Optional 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(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 struct ArgInfo { static_assert(!std::is_same_v, "Invalid command parameter type - see ChatCommandArgs.h for possible types"); }; -// catch-all for integral types +// catch-all for number types template -struct ArgInfo>> +struct ArgInfo || std::is_floating_point_v>> { static Optional TryConsume(T& val, std::string_view args) { @@ -59,34 +59,14 @@ struct ArgInfo>> val = *v; else return std::nullopt; - return tail; - } -}; - -// catch-all for floating point types -template -struct ArgInfo>> -{ - static Optional 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) { - // @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; } }; -- cgit v1.2.3