AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore::Impl::StringConvertImpl::For< T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool > > > Struct Template Reference

#include "StringConvert.h"

Static Public Member Functions

static Optional< T > FromString (std::string_view str, int base=10)
 
static std::string ToString (T val)
 

Detailed Description

template<typename T>
struct Acore::Impl::StringConvertImpl::For< T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool > > >

Member Function Documentation

◆ FromString()

template<typename T >
static Optional< T > Acore::Impl::StringConvertImpl::For< T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool > > >::FromString ( std::string_view  str,
int  base = 10 
)
inlinestatic
42 {
43 if (base == 0)
44 {
45 if (StringEqualI(str.substr(0, 2), "0x"))
46 {
47 base = 16;
48 str.remove_prefix(2);
49 }
50 else if (StringEqualI(str.substr(0, 2), "0b"))
51 {
52 base = 2;
53 str.remove_prefix(2);
54 }
55 else
56 {
57 base = 10;
58 }
59
60 if (str.empty())
61 {
62 return std::nullopt;
63 }
64 }
65
66 char const* const start = str.data();
67 char const* const end = (start + str.length());
68
69 T val;
70 std::from_chars_result const res = std::from_chars(start, end, val, base);
71 if ((res.ptr == end) && (res.ec == std::errc()))
72 {
73 return val;
74 }
75 else
76 {
77 return std::nullopt;
78 }
79 }
bool StringEqualI(std::string_view a, std::string_view b)
Definition: Util.cpp:592

References StringEqualI().

◆ ToString()

template<typename T >
static std::string Acore::Impl::StringConvertImpl::For< T, std::enable_if_t< std::is_integral_v< T > &&!std::is_same_v< T, bool > > >::ToString ( val)
inlinestatic
82 {
83 std::string buf(20, '\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
84 char* const start = buf.data();
85 char* const end = (start + buf.length());
86 std::to_chars_result const res = std::to_chars(start, end, val);
87 ASSERT(res.ec == std::errc());
88 buf.resize(res.ptr - start);
89 return buf;
90 }
#define ASSERT
Definition: Errors.h:68

References ASSERT.