[8cce80b4] | 1 | /*
|
---|
[b57a3ee] | 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
[8cce80b4] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[7bbf91e] | 29 | #ifndef LIBCPP_BITS_LOCALE_NUM_GET
|
---|
| 30 | #define LIBCPP_BITS_LOCALE_NUM_GET
|
---|
[8cce80b4] | 31 |
|
---|
[b57a3ee] | 32 | #include <__bits/locale/locale.hpp>
|
---|
| 33 | #include <__bits/locale/numpunct.hpp>
|
---|
[a1c35cc] | 34 | #include <cerrno>
|
---|
| 35 | #include <cstring>
|
---|
[8cce80b4] | 36 | #include <ios>
|
---|
| 37 | #include <iterator>
|
---|
[e8c4c59] | 38 | #include <limits>
|
---|
[8cce80b4] | 39 |
|
---|
| 40 | namespace std
|
---|
| 41 | {
|
---|
| 42 | /**
|
---|
| 43 | * 22.4.2.1, class template num_get:
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | template<class Char, class InputIterator = istreambuf_iterator<Char>>
|
---|
| 47 | class num_get: public locale::facet
|
---|
| 48 | {
|
---|
| 49 | public:
|
---|
| 50 | using char_type = Char;
|
---|
| 51 | using iter_type = InputIterator;
|
---|
| 52 |
|
---|
[e8c4c59] | 53 | explicit num_get(size_t refs = 0)
|
---|
| 54 | { /* DUMMY BODY */ }
|
---|
[8cce80b4] | 55 |
|
---|
| 56 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 57 | ios_base::iostate& err, bool& v) const
|
---|
| 58 | {
|
---|
| 59 | return do_get(in, end, base, err, v);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 63 | ios_base::iostate& err, long& v) const
|
---|
| 64 | {
|
---|
| 65 | return do_get(in, end, base, err, v);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 69 | ios_base::iostate& err, long long& v) const
|
---|
| 70 | {
|
---|
| 71 | return do_get(in, end, base, err, v);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 75 | ios_base::iostate& err, unsigned short& v) const
|
---|
| 76 | {
|
---|
| 77 | return do_get(in, end, base, err, v);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 81 | ios_base::iostate& err, unsigned int& v) const
|
---|
| 82 | {
|
---|
| 83 | return do_get(in, end, base, err, v);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 87 | ios_base::iostate& err, unsigned long& v) const
|
---|
| 88 | {
|
---|
| 89 | return do_get(in, end, base, err, v);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 93 | ios_base::iostate& err, unsigned long long& v) const
|
---|
| 94 | {
|
---|
| 95 | return do_get(in, end, base, err, v);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 99 | ios_base::iostate& err, float& v) const
|
---|
| 100 | {
|
---|
| 101 | return do_get(in, end, base, err, v);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 105 | ios_base::iostate& err, double& v) const
|
---|
| 106 | {
|
---|
| 107 | return do_get(in, end, base, err, v);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 111 | ios_base::iostate& err, long double& v) const
|
---|
| 112 | {
|
---|
| 113 | return do_get(in, end, base, err, v);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | iter_type get(iter_type in, iter_type end, ios_base& base,
|
---|
| 117 | ios_base::iostate& err, void*& v) const
|
---|
| 118 | {
|
---|
| 119 | return do_get(in, end, base, err, v);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | static locale::id id;
|
---|
| 123 |
|
---|
[e8c4c59] | 124 | ~num_get()
|
---|
| 125 | { /* DUMMY BODY */ }
|
---|
[8cce80b4] | 126 |
|
---|
| 127 | protected:
|
---|
| 128 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 129 | ios_base::iostate& err, bool& v) const
|
---|
| 130 | {
|
---|
[e8c4c59] | 131 | if (in == end)
|
---|
| 132 | {
|
---|
| 133 | err = ios_base::eofbit;
|
---|
| 134 | return in;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if ((base.flags() & ios_base::boolalpha) == 0)
|
---|
| 138 | {
|
---|
| 139 | int8_t tmp{};
|
---|
| 140 | in = get_integral_<int64_t>(in, end, base, err, tmp);
|
---|
| 141 |
|
---|
| 142 | if (tmp == 0)
|
---|
| 143 | v = false;
|
---|
| 144 | else if (tmp == 1)
|
---|
| 145 | v = true;
|
---|
| 146 | else
|
---|
| 147 | {
|
---|
| 148 | v = true;
|
---|
| 149 | err = ios_base::failbit;
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | else
|
---|
| 153 | {
|
---|
| 154 | /**
|
---|
| 155 | * We track both truename() and falsename()
|
---|
| 156 | * at the same time, once either is matched
|
---|
| 157 | * or the input is too big without a match
|
---|
| 158 | * or the input ends the conversion ends
|
---|
| 159 | * and the result is deduced.
|
---|
| 160 | */
|
---|
| 161 |
|
---|
| 162 | auto loc = base.getloc();
|
---|
| 163 | const auto& nt = use_facet<numpunct<char_type>>(loc);
|
---|
| 164 |
|
---|
| 165 | auto true_target = nt.truename();
|
---|
| 166 | auto false_target = nt.falsename();
|
---|
| 167 |
|
---|
| 168 | if (true_target == "" || false_target == "")
|
---|
| 169 | {
|
---|
| 170 | v = (err == ios_base::failbit);
|
---|
| 171 | return in;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | auto true_str = true_target;
|
---|
| 175 | auto false_str = false_target;
|
---|
| 176 |
|
---|
| 177 | size_t i{};
|
---|
| 178 | while (true)
|
---|
| 179 | {
|
---|
| 180 | if (true_str.size() <= i && false_str.size() <= i)
|
---|
| 181 | break;
|
---|
| 182 | auto c = *in++;
|
---|
| 183 |
|
---|
| 184 | if (i < true_str.size())
|
---|
| 185 | true_str[i] = c;
|
---|
| 186 | if (i < false_str.size())
|
---|
| 187 | false_str[i] = c;
|
---|
| 188 | ++i;
|
---|
| 189 |
|
---|
| 190 | if (in == end || *in == '\n')
|
---|
| 191 | {
|
---|
| 192 | err = ios_base::eofbit;
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (i == true_str.size() && true_str == true_target)
|
---|
| 198 | {
|
---|
| 199 | v = true;
|
---|
| 200 |
|
---|
| 201 | if (++in == end)
|
---|
| 202 | err = ios_base::eofbit;
|
---|
| 203 | else
|
---|
| 204 | err = ios_base::goodbit;
|
---|
| 205 | }
|
---|
| 206 | else if (i == false_str.size() && false_str == false_target)
|
---|
| 207 | {
|
---|
| 208 | v = false;
|
---|
| 209 |
|
---|
| 210 | if (in == end)
|
---|
| 211 | err = (ios_base::failbit | ios_base::eofbit);
|
---|
| 212 | else
|
---|
| 213 | err = ios_base::failbit;
|
---|
| 214 | }
|
---|
| 215 | else
|
---|
| 216 | err = ios_base::failbit;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return in;
|
---|
[8cce80b4] | 220 | }
|
---|
| 221 |
|
---|
| 222 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 223 | ios_base::iostate& err, long& v) const
|
---|
| 224 | {
|
---|
[e8c4c59] | 225 | return get_integral_<int64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 226 | }
|
---|
| 227 |
|
---|
| 228 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 229 | ios_base::iostate& err, long long& v) const
|
---|
| 230 | {
|
---|
[e8c4c59] | 231 | return get_integral_<int64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 232 | }
|
---|
| 233 |
|
---|
| 234 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 235 | ios_base::iostate& err, unsigned short& v) const
|
---|
| 236 | {
|
---|
[e8c4c59] | 237 | return get_integral_<uint64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 238 | }
|
---|
| 239 |
|
---|
| 240 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 241 | ios_base::iostate& err, unsigned int& v) const
|
---|
| 242 | {
|
---|
[e8c4c59] | 243 | return get_integral_<uint64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 244 | }
|
---|
| 245 |
|
---|
| 246 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 247 | ios_base::iostate& err, unsigned long& v) const
|
---|
| 248 | {
|
---|
[e8c4c59] | 249 | return get_integral_<uint64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 250 | }
|
---|
| 251 |
|
---|
| 252 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 253 | ios_base::iostate& err, unsigned long long& v) const
|
---|
| 254 | {
|
---|
[e8c4c59] | 255 | return get_integral_<uint64_t>(in, end, base, err, v);
|
---|
[8cce80b4] | 256 | }
|
---|
| 257 |
|
---|
| 258 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 259 | ios_base::iostate& err, float& v) const
|
---|
| 260 | {
|
---|
| 261 | // TODO: implement
|
---|
[a1c35cc] | 262 | return in;
|
---|
[8cce80b4] | 263 | }
|
---|
| 264 |
|
---|
| 265 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 266 | ios_base::iostate& err, double& v) const
|
---|
| 267 | {
|
---|
| 268 | // TODO: implement
|
---|
[a1c35cc] | 269 | return in;
|
---|
[8cce80b4] | 270 | }
|
---|
| 271 |
|
---|
| 272 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 273 | ios_base::iostate& err, long double& v) const
|
---|
| 274 | {
|
---|
| 275 | // TODO: implement
|
---|
[a1c35cc] | 276 | return in;
|
---|
[8cce80b4] | 277 | }
|
---|
| 278 |
|
---|
| 279 | iter_type do_get(iter_type in, iter_type end, ios_base& base,
|
---|
| 280 | ios_base::iostate& err, void*& v) const
|
---|
| 281 | {
|
---|
| 282 | // TODO: implement
|
---|
[a1c35cc] | 283 | return in;
|
---|
[8cce80b4] | 284 | }
|
---|
[e8c4c59] | 285 |
|
---|
| 286 | private:
|
---|
| 287 | template<class BaseType, class T>
|
---|
| 288 | iter_type get_integral_(iter_type in, iter_type end, ios_base& base,
|
---|
[c5b2b05] | 289 | ios_base::iostate& err, T& v) const
|
---|
[e8c4c59] | 290 | {
|
---|
| 291 | BaseType res{};
|
---|
| 292 | unsigned int num_base{10};
|
---|
| 293 |
|
---|
| 294 | auto basefield = (base.flags() & ios_base::basefield);
|
---|
| 295 | if (basefield == ios_base::oct)
|
---|
| 296 | num_base = 8;
|
---|
| 297 | else if (basefield == ios_base::hex)
|
---|
| 298 | num_base = 16;
|
---|
| 299 |
|
---|
| 300 | auto size = fill_buffer_integral_(in, end, base);
|
---|
| 301 | if (size > 0)
|
---|
| 302 | {
|
---|
[bc56f30] | 303 | int olderrno{errno};
|
---|
| 304 | errno = EOK;
|
---|
[0d14c25] | 305 | char *endptr = NULL;
|
---|
[bc56f30] | 306 |
|
---|
[e8c4c59] | 307 | if constexpr (is_signed<BaseType>::value)
|
---|
[0d14c25] | 308 | res = ::strtoll(base.buffer_, &endptr, num_base);
|
---|
[e8c4c59] | 309 | else
|
---|
[0d14c25] | 310 | res = ::strtoull(base.buffer_, &endptr, num_base);
|
---|
[e8c4c59] | 311 |
|
---|
[0d14c25] | 312 | if (errno != EOK || endptr == base.buffer_)
|
---|
[c5b2b05] | 313 | err |= ios_base::failbit;
|
---|
[bc56f30] | 314 |
|
---|
| 315 | errno = olderrno;
|
---|
| 316 |
|
---|
| 317 | if (res > static_cast<BaseType>(numeric_limits<T>::max()))
|
---|
[e8c4c59] | 318 | {
|
---|
| 319 | err |= ios_base::failbit;
|
---|
| 320 | v = numeric_limits<T>::max();
|
---|
| 321 | }
|
---|
| 322 | else if (res < static_cast<BaseType>(numeric_limits<T>::min()))
|
---|
| 323 | {
|
---|
| 324 | err |= ios_base::failbit;
|
---|
| 325 | v = numeric_limits<T>::min();
|
---|
| 326 | }
|
---|
| 327 | else
|
---|
| 328 | v = static_cast<T>(res);
|
---|
| 329 | }
|
---|
| 330 | else
|
---|
| 331 | {
|
---|
| 332 | err |= ios_base::failbit;
|
---|
| 333 | v = 0;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | return in;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | size_t fill_buffer_integral_(iter_type& in, iter_type end, ios_base& base) const
|
---|
| 340 | {
|
---|
| 341 | if (in == end)
|
---|
| 342 | return 0;
|
---|
| 343 |
|
---|
| 344 | auto loc = base.getloc();
|
---|
| 345 | const auto& ct = use_facet<ctype<char_type>>(loc);
|
---|
[c1e11d32] | 346 | auto hex = ((base.flags() & ios_base::hex) != 0);
|
---|
[e8c4c59] | 347 |
|
---|
| 348 | size_t i{};
|
---|
| 349 | if (*in == '+' || *in == '-')
|
---|
| 350 | base.buffer_[i++] = *in++;
|
---|
| 351 |
|
---|
| 352 | while (in != end && i < ios_base::buffer_size_ - 1)
|
---|
| 353 | {
|
---|
| 354 | auto c = *in;
|
---|
[c1e11d32] | 355 | if (ct.is(ctype_base::digit, c) || (hex &&
|
---|
| 356 | ((c >= ct.widen('A') && c <= ct.widen('F')) ||
|
---|
| 357 | (c >= ct.widen('a') && c <= ct.widen('f')))))
|
---|
[e8c4c59] | 358 | {
|
---|
| 359 | ++in;
|
---|
| 360 | base.buffer_[i++] = c;
|
---|
| 361 | }
|
---|
| 362 | else
|
---|
| 363 | break;
|
---|
| 364 | }
|
---|
[c1e11d32] | 365 | base.buffer_[i] = char_type{};
|
---|
[e8c4c59] | 366 |
|
---|
| 367 | return i;
|
---|
| 368 | }
|
---|
[8cce80b4] | 369 | };
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | #endif
|
---|