aboutsummaryrefslogtreecommitdiff
path: root/libbutl/json/parser.ixx
blob: 3f02a1e93c2502f56c5fd7133125f520e707b625 (plain)
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
207
208
209
210
211
212
213
214
215
216
#include <cerrno>
#include <limits>      // numeric_limits
#include <utility>     // move()
#include <cassert>
#include <cstdlib>     // strto*()
#include <type_traits> // enable_if, is_*
#include <cstring>     // strlen()

namespace butl
{
  namespace json
  {
    inline invalid_json_input::
    invalid_json_input (std::string n,
                        std::uint64_t l,
                        std::uint64_t c,
                        std::uint64_t p,
                        const std::string& d)
        : invalid_json_input (move (n), l, c, p, d.c_str ())
    {
    }

    inline invalid_json_input::
    invalid_json_input (std::string n,
                        std::uint64_t l,
                        std::uint64_t c,
                        std::uint64_t p,
                        const char* d)
        : invalid_argument (d),
          name (std::move (n)),
          line (l), column (c), position (p)
    {
    }

    inline parser::
    parser (std::istream& is,
            const std::string& n,
            bool mv,
            const char* sep) noexcept
        : parser (is, n.c_str (), mv, sep)
    {
    }

    inline parser::
    parser (const void* t,
            std::size_t s,
            const std::string& n,
            bool mv,
            const char* sep) noexcept
        : parser (t, s, n.c_str (), mv, sep)
    {
    }

    inline parser::
    parser (const std::string& t,
            const std::string& n,
            bool mv,
            const char* sep) noexcept
        : parser (t.data (), t.size (), n.c_str (), mv, sep)
    {
    }

    inline parser::
    parser (const std::string& t,
            const char* n,
            bool mv,
            const char* sep) noexcept
        : parser (t.data (), t.size (), n, mv, sep)
    {
    }

    inline parser::
    parser (const char* t,
            const std::string& n,
            bool mv,
            const char* sep) noexcept
        : parser (t, std::strlen (t), n.c_str (), mv, sep)
    {
    }

    inline parser::
    parser (const char* t,
            const char* n,
            bool mv,
            const char* sep) noexcept
        : parser (t, std::strlen (t), n, mv, sep)
    {
    }

    inline const std::string& parser::
    name ()
    {
      if (!name_p_)
      {
        assert (parsed_ && !peeked_ && !value_p_);
        cache_parsed_data ();
        assert (name_p_);
      }
      return name_;
    }

    inline std::string& parser::
    value ()
    {
      if (!value_p_)
      {
        assert (parsed_ && !peeked_ && !name_p_);
        cache_parsed_data ();
        assert (value_p_);
      }
      return value_;
    }

    // Note: one day we will be able to use C++17 from_chars() which was made
    // exactly for this.
    //
    template <typename T>
    inline typename std::enable_if<std::is_same<T, bool>::value, T>::type
    parse_value (const char* b, size_t, const parser&)
    {
      return *b == 't';
    }

    template <typename T>
    inline typename std::enable_if<
      std::is_integral<T>::value &&
      std::is_signed<T>::value &&
      !std::is_same<T, bool>::value, T>::type
    parse_value (const char* b, size_t n, const parser& p)
    {
      char* e (nullptr);
      errno = 0; // We must clear it according to POSIX.
      std::int64_t v (strtoll (b, &e, 10)); // Can't throw.

      if (e == b || e != b + n || errno == ERANGE ||
          v < std::numeric_limits<T>::min () ||
          v > std::numeric_limits<T>::max ())
        p.throw_invalid_value ("signed integer", b, n);

      return static_cast<T> (v);
    }

    template <typename T>
    inline typename std::enable_if<
      std::is_integral<T>::value &&
      std::is_unsigned<T>::value &&
      !std::is_same<T, bool>::value, T>::type
    parse_value (const char* b, size_t n, const parser& p)
    {
      char* e (nullptr);
      errno = 0; // We must clear it according to POSIX.
      std::uint64_t v (strtoull (b, &e, 10)); // Can't throw.

      if (e == b || e != b + n || errno == ERANGE ||
          v > std::numeric_limits<T>::max ())
        p.throw_invalid_value ("unsigned integer", b, n);

      return static_cast<T> (v);
    }

    template <typename T>
    inline typename std::enable_if<std::is_same<T, float>::value, T>::type
    parse_value (const char* b, size_t n, const parser& p)
    {
      char* e (nullptr);
      errno = 0; // We must clear it according to POSIX.
      T r (std::strtof (b, &e));

      if (e == b || e != b + n || errno == ERANGE)
        p.throw_invalid_value ("float", b, n);

      return r;
    }

    template <typename T>
    inline typename std::enable_if<std::is_same<T, double>::value, T>::type
    parse_value (const char* b, size_t n, const parser& p)
    {
      char* e (nullptr);
      errno = 0; // We must clear it according to POSIX.
      T r (std::strtod (b, &e));

      if (e == b || e != b + n || errno == ERANGE)
        p.throw_invalid_value ("double", b, n);

      return r;
    }

    template <typename T>
    inline typename std::enable_if<std::is_same<T, long double>::value, T>::type
    parse_value (const char* b, size_t n, const parser& p)
    {
      char* e (nullptr);
      errno = 0; // We must clear it according to POSIX.
      T r (std::strtold (b, &e));

      if (e == b || e != b + n || errno == ERANGE)
        p.throw_invalid_value ("long double", b, n);

      return r;
    }

    template <typename T>
    inline T parser::
    value () const
    {
      if (!value_p_)
      {
        assert (parsed_ && !peeked_ && value_event (translate (*parsed_)));
        return parse_value<T> (raw_s_, raw_n_, *this);
      }

      return parse_value<T> (value_.data (), value_.size (), *this);
    }
  }
}