33// Distributed under the MIT software license, see the accompanying
44// file COPYING or https://opensource.org/licenses/mit-license.php.
55
6- #include < stdint .h>
7- # include < errno.h >
8- #include < string.h >
9- #include < stdlib.h >
10- #include < stdexcept >
11- #include < vector >
6+ #include < univalue .h>
7+
8+ #include < cerrno >
9+ #include < cstdint >
10+ #include < cstdlib >
11+ #include < cstring >
1212#include < limits>
13- #include < string >
13+ #include < locale >
1414#include < sstream>
15-
16- #include " univalue.h"
15+ #include < stdexcept>
16+ #include < string>
17+ #include < vector>
1718
1819namespace
1920{
@@ -28,37 +29,6 @@ static bool ParsePrechecks(const std::string& str)
2829 return true ;
2930}
3031
31- bool ParseInt32 (const std::string& str, int32_t *out)
32- {
33- if (!ParsePrechecks (str))
34- return false ;
35- char *endp = nullptr ;
36- errno = 0 ; // strtol will not set errno if valid
37- long int n = strtol (str.c_str (), &endp, 10 );
38- if (out) *out = (int32_t )n;
39- // Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
40- // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
41- // platforms the size of these types may be different.
42- return endp && *endp == 0 && !errno &&
43- n >= std::numeric_limits<int32_t >::min () &&
44- n <= std::numeric_limits<int32_t >::max ();
45- }
46-
47- bool ParseInt64 (const std::string& str, int64_t *out)
48- {
49- if (!ParsePrechecks (str))
50- return false ;
51- char *endp = nullptr ;
52- errno = 0 ; // strtoll will not set errno if valid
53- long long int n = strtoll (str.c_str (), &endp, 10 );
54- if (out) *out = (int64_t )n;
55- // Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
56- // we still have to check that the returned value is within the range of an *int64_t*.
57- return endp && *endp == 0 && !errno &&
58- n >= std::numeric_limits<int64_t >::min () &&
59- n <= std::numeric_limits<int64_t >::max ();
60- }
61-
6232bool ParseDouble (const std::string& str, double *out)
6333{
6434 if (!ParsePrechecks (str))
@@ -102,26 +72,6 @@ const std::string& UniValue::get_str() const
10272 return getValStr ();
10373}
10474
105- int UniValue::get_int () const
106- {
107- if (typ != VNUM)
108- throw std::runtime_error (" JSON value is not an integer as expected" );
109- int32_t retval;
110- if (!ParseInt32 (getValStr (), &retval))
111- throw std::runtime_error (" JSON integer out of range" );
112- return retval;
113- }
114-
115- int64_t UniValue::get_int64 () const
116- {
117- if (typ != VNUM)
118- throw std::runtime_error (" JSON value is not an integer as expected" );
119- int64_t retval;
120- if (!ParseInt64 (getValStr (), &retval))
121- throw std::runtime_error (" JSON integer out of range" );
122- return retval;
123- }
124-
12575double UniValue::get_real () const
12676{
12777 if (typ != VNUM)
0 commit comments