18#ifndef TRINITY_STRINGCONVERT_H
19#define TRINITY_STRINGCONVERT_H
33 template <
typename T,
typename =
void>
struct For
35 static_assert(Trinity::dependant_false_v<T>,
"Unsupported type used for ToString or StringTo");
43 struct For<T,
std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>>
66 char const*
const start = str.data();
67 char const*
const end = (start + str.length());
70 std::from_chars_result
const res = std::from_chars(start, end, val, base);
71 if ((res.ptr == end) && (res.ec == std::errc()))
79 using buffer_size = std::integral_constant<size_t,
sizeof(T) < 8 ? 11 : 20>;
81 std::string buf(buffer_size::value,
'\0');
82 char*
const start = buf.data();
83 char*
const end = (start + buf.length());
84 std::to_chars_result
const res = std::to_chars(start, end, val);
85 ASSERT(res.ec == std::errc());
86 buf.resize(res.ptr - start);
91#ifdef TRINITY_NEED_CHARCONV_WORKAROUND
109 uint64 val = std::stoull(std::string(str), &n, base);
110 if (n != str.length())
114 catch (...) {
return std::nullopt; }
119 return std::to_string(val);
124 struct For<
int64, void>
132 int64 val = std::stoll(std::string(str), &n, base);
133 if (n != str.length())
137 catch (...) {
return std::nullopt; }
142 return std::to_string(val);
172 return (val ?
"1" :
"0");
176#if TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT
177 template <
typename T>
178 struct For<T,
std::enable_if_t<std::is_floating_point_v<T>>>
185 if (
fmt == std::chars_format())
189 fmt = std::chars_format::hex;
190 str.remove_prefix(2);
193 fmt = std::chars_format::general;
199 char const*
const start = str.data();
200 char const*
const end = (start + str.length());
203 std::from_chars_result
const res = std::from_chars(start, end, val,
fmt);
204 if ((res.ptr == end) && (res.ec == std::errc()))
214 return FromString(str, std::chars_format::hex);
216 return FromString(str, std::chars_format::general);
218 return FromString(str, std::chars_format());
223 return std::to_string(val);
228 template <
typename T>
229 struct For<T,
std::enable_if_t<std::is_floating_point_v<T>>>
231 static Optional<T> FromString(std::string_view str,
int base = 0)
237 if ((base == 10) &&
StringEqualI(str.substr(0, 2),
"0x"))
246 T val =
static_cast<T
>(std::stold(tmp, &n));
247 if (n != tmp.length())
251 catch (...) {
return std::nullopt; }
256 return std::to_string(val);
264 template <
typename Result,
typename... Params>
270 template <
typename Type,
typename... Params>
std::unordered_set< uint32 > params[2]
std::optional< T > Optional
Optional helper class to wrap optional values within.
bool StringEqualI(std::string_view a, std::string_view b)
std::string ToString(Type &&val, Params &&... params)
Optional< Result > StringTo(std::string_view str, Params &&... params)
static Optional< T > FromString(std::string_view str, int base=10)
static std::string ToString(T val)
static Optional< T > FromString(std::string_view str, int base)
static Optional< T > FromString(std::string_view str, std::chars_format fmt=std::chars_format())
static std::string ToString(T val)
static Optional< bool > FromString(std::string_view str, int strict=0)
static std::string ToString(bool val)