-
Notifications
You must be signed in to change notification settings - Fork 456
/
lexical_cast.hpp
207 lines (178 loc) · 3.43 KB
/
lexical_cast.hpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#pragma once
#include <type_traits>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <stdexcept>
#include <cctype>
#include <cstring>
using namespace std;
namespace detail
{
const char* strue = "true";
const char* sfalse = "false";
template <typename To, typename From>
struct Converter
{
};
//to numeric
template <typename From>
struct Converter<int, From>
{
static int convert(const string& from)
{
return std::atoi(from.c_str());
}
static int convert(const char* from)
{
return std::atoi(from);
}
};
template <typename From>
struct Converter<long, From>
{
static long convert(const string& from)
{
return std::atol(from.c_str());
}
static long convert(const char* from)
{
return std::atol(from);
}
};
template <typename From>
struct Converter<long long, From>
{
static long long convert(const string& from)
{
return std::atoll(from.c_str());
}
static long long convert(const char* from)
{
return std::atoll(from);
}
};
template <typename From>
struct Converter<double, From>
{
static double convert(const string& from)
{
return std::atof(from.c_str());
}
static double convert(const char* from)
{
return std::atof(from);
}
};
template <typename From>
struct Converter<float, From>
{
static float convert(const string& from)
{
return (float)std::atof(from.c_str());
}
static float convert(const char* from)
{
return (float)std::atof(from);
}
};
//to bool
template <typename From>
struct Converter<bool, From>
{
static typename std::enable_if<std::is_integral<From>::value, bool>::type convert(From from)
{
return !!from;
}
};
bool checkbool(const char* from, const size_t len, const char* s)
{
for (size_t i = 0; i < len; i++)
{
if (from[i] != s[i])
{
return false;
}
}
return true;
}
static bool convert(const char* from)
{
const unsigned int len = strlen(from);
if (len != 4 && len != 5)
throw std::invalid_argument("argument is invalid");
bool r = true;
if (len == 4)
{
r = checkbool(from, len, strue);
if (r)
return true;
}
else
{
r = checkbool(from, len, sfalse);
if (r)
return false;
}
throw std::invalid_argument("argument is invalid");
}
template <>
struct Converter<bool, string>
{
static bool convert(const string& from)
{
return detail::convert(from.c_str());
}
};
template <>
struct Converter<bool, const char*>
{
static bool convert(const char* from)
{
return detail::convert(from);
}
};
template <>
struct Converter<bool, char*>
{
static bool convert(char* from)
{
return detail::convert(from);
}
};
template <unsigned N>
struct Converter<bool, const char[N]>
{
static bool convert(const char(&from)[N])
{
return detail::convert(from);
}
};
template <unsigned N>
struct Converter<bool, char[N]>
{
static bool convert(const char(&from)[N])
{
return detail::convert(from);
}
};
//to string
template <typename From>
struct Converter<string, From>
{
static string convert(const From& from)
{
return std::to_string(from);
}
};
}
template <typename To, typename From>
typename std::enable_if<!std::is_same<To, From>::value, To>::type lexical_cast(const From& from)
{
return detail::Converter<To, From>::convert(from);
}
template <typename To, typename From>
typename std::enable_if<std::is_same<To, From>::value, To>::type lexical_cast(const From& from)
{
return from;
}