1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITY_STRINGCONVERT_H
#define TRINITY_STRINGCONVERT_H
#include "Define.h"
#include "Errors.h"
#include "Optional.h"
#include "Types.h"
#include "Util.h"
#include <charconv>
#include <string>
#include <string_view>
#include <type_traits>
namespace Trinity::Impl::StringConvertImpl
{
template <typename T, typename = void> struct For
{
static_assert(Trinity::dependant_false_v<T>, "Unsupported type used for ToString or StringTo");
/*
static Optional<T> FromString(std::string_view str, ...);
static std::string ToString(T&& val, ...);
*/
};
// @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>>>
{
static Optional<T> FromString(std::string_view str, int base = 10)
{
if (base == 0)
{
if (StringEqualI(str.substr(0, 2), "0x"))
{
base = 16;
str.remove_prefix(2);
}
else if (StringEqualI(str.substr(0, 2), "0b"))
{
base = 2;
str.remove_prefix(2);
}
else
base = 10;
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, base);
if (res.ptr == end)
return val;
else
return std::nullopt;
}
static std::string ToString(T val)
{
std::string buf(20,'\0'); /* 2^64 is 20 decimal characters, -(2^63) is 20 including the sign */
char* const start = buf.data();
char* const end = (start + buf.length());
std::to_chars_result const res = std::to_chars(start, end, val);
ASSERT(res.ec == std::errc());
buf.resize(res.ptr - start);
return buf;
}
};
#ifdef TRINITY_NEED_CHARCONV_WORKAROUND
/*
If this is defined, std::from_chars will cause linkage errors for 64-bit types.
(This is a bug in clang-7.)
If the clang requirement is bumped to >= clang-8, remove this ifdef block and its
associated check in cmake/compiler/clang/settings.cmake
*/
template <>
struct For<uint64, void>
{
static Optional<uint64> FromString(std::string_view str, int base = 10)
{
if (str.empty())
return std::nullopt;
try
{
size_t n;
uint64 val = std::stoull(std::string(str), &n, base);
if (n != str.length())
return std::nullopt;
return val;
}
catch (...) { return std::nullopt; }
}
static std::string ToString(uint64 val)
{
return std::to_string(val);
}
};
template <>
struct For<int64, void>
{
static Optional<int64> FromString(std::string_view str, int base = 10)
{
try {
if (str.empty())
return std::nullopt;
size_t n;
int64 val = std::stoll(std::string(str), &n, base);
if (n != str.length())
return std::nullopt;
return val;
}
catch (...) { return std::nullopt; }
}
static std::string ToString(int64 val)
{
return std::to_string(val);
}
};
#endif
template <>
struct For<bool, void>
{
static Optional<bool> FromString(std::string_view str)
{
if ((str == "1") || StringEqualI(str, "y") || StringEqualI(str, "on") || StringEqualI(str, "yes") || StringEqualI(str, "true"))
return true;
if ((str == "0") || StringEqualI(str, "n") || StringEqualI(str, "off") || StringEqualI(str, "no") || StringEqualI(str, "false"))
return false;
return std::nullopt;
}
static std::string ToString(bool val)
{
return (val ? "1" : "0");
}
};
}
namespace Trinity
{
template <typename Result, typename... Params>
Optional<Result> StringTo(std::string_view str, Params&&... params)
{
return Trinity::Impl::StringConvertImpl::For<Result>::FromString(str, std::forward<Params>(params)...);
}
template <typename Type, typename... Params>
std::string ToString(Type&& val, Params&&... params)
{
return Trinity::Impl::StringConvertImpl::For<std::decay_t<Type>>::ToString(std::forward<Type>(val), std::forward<Params>(params)...);
}
}
#endif
|