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
|
// license:GPL-2.0+
// copyright-holders:Couriersud
#ifndef PSTONUM_H_
#define PSTONUM_H_
///
/// \file pstonum.h
///
#include "pconfig.h"
#include "pexception.h"
#include "pgsl.h"
#include "pmath.h" // for pstonum
#include "pstring.h"
#include <algorithm>
#include <initializer_list>
#include <iostream>
#include <locale>
#include <sstream>
namespace plib
{
// ----------------------------------------------------------------------------------------
// number conversions
// ----------------------------------------------------------------------------------------
template <typename T, typename S>
T pstonum_locale(const std::locale &loc, const S &arg, bool *err)
{
std::stringstream ss;
ss.imbue(loc);
ss << putf8string(arg);
auto len(ss.tellp());
T x(constants<T>::zero());
if (ss >> x)
{
auto pos(ss.tellg());
if (pos == decltype(pos)(-1))
pos = len;
*err = (pos != len);
}
else
*err = true;
return x;
}
template <typename T, typename E = void>
struct pstonum_helper;
template<typename T>
struct pstonum_helper<T, std::enable_if_t<plib::is_integral<T>::value && plib::is_signed<T>::value>>
{
template <typename S>
long long operator()(std::locale loc, const S &arg, bool *err)
{
//return std::stoll(arg, idx);
return pstonum_locale<long long>(loc, arg, err);
}
};
template<typename T>
struct pstonum_helper<T, std::enable_if_t<plib::is_integral<T>::value && !plib::is_signed<T>::value>>
{
template <typename S>
unsigned long long operator()(std::locale loc, const S &arg, bool *err)
{
//return std::stoll(arg, idx);
return pstonum_locale<unsigned long long>(loc, arg, err);
}
};
template<typename T>
struct pstonum_helper<T, std::enable_if_t<std::is_floating_point<T>::value>>
{
template <typename S>
long double operator()(std::locale loc, const S &arg, bool *err)
{
return pstonum_locale<long double>(loc, arg, err);
}
};
#if PUSE_FLOAT128
template<>
struct pstonum_helper<FLOAT128>
{
// FIXME: use strtoflt128 from quadmath.h
template <typename S>
FLOAT128 operator()(std::locale loc, const S &arg, bool *err)
{
return narrow_cast<FLOAT128>(pstonum_locale<long double>(loc, arg, err));
}
};
#endif
template<typename T, typename S>
T pstonum(const S &arg, const std::locale &loc = std::locale::classic()) noexcept(false)
{
bool err(false);
auto ret = pstonum_helper<T>()(loc, arg, &err);
if (err)
throw pexception(pstring("Error converting string to number: ") + pstring(arg));
using ret_type = decltype(ret);
if (ret >= narrow_cast<ret_type>(plib::numeric_limits<T>::lowest())
&& ret <= narrow_cast<ret_type>(plib::numeric_limits<T>::max()))
{
return narrow_cast<T>(ret);
}
throw pexception(pstring("Out of range: ") + pstring(arg));
}
template<typename R, typename T>
R pstonum_ne(const T &str, bool &err, std::locale loc = std::locale::classic()) noexcept
{
try
{
err = false;
return pstonum<R>(str, loc);
}
catch (...)
{
err = true;
return R(0);
}
}
template<typename R, typename T>
R pstonum_ne_def(const T &str, R def, std::locale loc = std::locale::classic()) noexcept
{
try
{
return pstonum<R>(str, loc);
}
catch (...)
{
return def;
}
}
} // namespace plib
#endif // PUTIL_H_
|