-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlexical_cast.cpp
199 lines (153 loc) · 4.9 KB
/
lexical_cast.cpp
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
// This file defines
//
// template<typename T> T lexical_cast(const std::string &x);
//
// which converts a string to type T. Currently the following types T are supported:
//
// string (trivial)
// long
// int
// double
// float
// uint16_t
//
// Note that a more general implementation of lexical_cast is already defined in boost, and considered
// for inclusion in C++ TR2, but it's not in C++11, so we're forced to define it here!
#include <cstdlib>
#include <climits>
#include "ch_frb_io_internals.hpp"
using namespace std;
namespace ch_frb_io {
#if 0
}; // pacify emacs c-mode!
#endif
template<> const char *typestr<string>() { return "string"; }
template<> const char *typestr<long>() { return "long"; }
template<> const char *typestr<int>() { return "int"; }
template<> const char *typestr<double>() { return "double"; }
template<> const char *typestr<float>() { return "float"; }
template<> const char *typestr<uint16_t>() { return "uint16_t"; }
template<> const char *typestr<bool>() { return "bool"; }
// trivial case: convert string -> string
template<> bool lexical_cast(const string &x, string &ret) { ret = x; return true;}
inline bool is_all_spaces(const char *s)
{
if (!s)
throw runtime_error("fatal: NULL pointer passed to is_all_spaces()");
for (;;) {
if (!*s)
return true;
if (!isspace(*s))
return false;
s++;
}
}
template<> bool lexical_cast(const string &x, long &ret)
{
const char *ptr = x.c_str();
char *endptr = NULL;
ret = strtol(ptr, &endptr, 10);
return (endptr != ptr) && (ret != LONG_MIN) && (ret != LONG_MAX) && is_all_spaces(endptr);
}
template<> bool lexical_cast(const string &x, int &ret)
{
long retl;
if (!lexical_cast(x, retl))
return false;
if ((sizeof(int) != sizeof(long)) && ((retl < INT_MIN) || (retl > INT_MAX)))
return false;
ret = retl;
return true;
}
template<> bool lexical_cast(const string &x, uint16_t &ret)
{
long retl;
if (!lexical_cast(x, retl))
return false;
if ((retl < 0) || (retl > 65535))
return false;
ret = retl;
return true;
}
template<> bool lexical_cast(const string &x, double &ret)
{
const char *ptr = x.c_str();
char *endptr = NULL;
ret = strtod(ptr, &endptr);
return (endptr != ptr) && (ret != -HUGE_VAL) && (ret != HUGE_VAL) && is_all_spaces(endptr);
}
template<> bool lexical_cast(const string &x, float &ret)
{
const char *ptr = x.c_str();
char *endptr = NULL;
ret = strtof(ptr, &endptr);
return (endptr != ptr) && (ret != -HUGE_VALF) && (ret != HUGE_VALF) && is_all_spaces(endptr);
}
template<> bool lexical_cast(const string &x, bool &ret)
{
const char *ptr = x.c_str();
if (!strcasecmp(ptr,"t") || !strcasecmp(ptr,"true"))
ret = true;
else if (!strcasecmp(ptr,"f") || !strcasecmp(ptr,"false"))
ret = false;
else
return false;
return true;
}
// -------------------------------------------------------------------------------------------------
//
// Unit test
template<typename T>
static void check_convert(const string &x, T y)
{
T ret;
if (!lexical_cast(x, ret))
throw runtime_error("test_lexical_cast(): didn't successfully convert");
if (fabs(double(ret) - double(y)) > 1.0e-5)
throw runtime_error("test_lexical_cast(): didn't correctly convert");
}
template<typename T>
static void check_convert_fails(const string &x)
{
T ret;
if (lexical_cast(x, ret))
throw runtime_error("test_lexical_cast(): conversion succeeded where it was expected to fail");
}
void test_lexical_cast()
{
check_convert<int>("0", 0);
check_convert<int>("-0", 0);
check_convert<int>("12", 12);
check_convert<int>("-123", -123);
check_convert<int>(" \t 1234 \n\t", 1234);
check_convert_fails<int>("");
check_convert_fails<int>(" ");
check_convert_fails<int>("oops");
check_convert_fails<int>(" oops ");
check_convert_fails<int>("1234abc");
check_convert_fails<int>("1234 abc");
check_convert_fails<int>("0.1");
check_convert<uint16_t>("0", 0);
check_convert<uint16_t> ("65535", 65535);
check_convert_fails<uint16_t> ("-1");
check_convert_fails<uint16_t> ("65536");
check_convert<double>("1.23", 1.23);
check_convert<double>("-1.23e-5", -1.23e-5);
check_convert<double>("-5", -5.0);
check_convert<double>(".23", 0.23);
check_convert<double>("-.034e3", -0.034e3);
check_convert<double>(" 0.03e20 ", 0.03e20);
check_convert_fails<double>("");
check_convert_fails<double>(" ");
check_convert_fails<double>("oops");
check_convert_fails<double>(" oops ");
check_convert_fails<double>("5x");
check_convert_fails<double>("-1.3e20x");
check_convert<bool>("t",true);
check_convert<bool>("TRUE",true);
check_convert<bool>("F",false);
check_convert<bool>("False",false);
check_convert_fails<bool>("False2");
cerr << "test_lexical_cast(): success\n";
}
} // namespace ch_frb_io