2017-07-26 15:25:29 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2017-07-26 15:25:29 +10:00
|
|
|
*
|
2018-12-16 13:26:48 +11:00
|
|
|
* Copyright 2017-2018 Danny Robson <danny@nerdcruft.net>
|
2017-07-26 15:25:29 +10:00
|
|
|
*/
|
|
|
|
|
2018-01-10 17:19:39 +11:00
|
|
|
#include "parse.hpp"
|
|
|
|
|
|
|
|
#include "cast.hpp"
|
2017-07-26 15:25:29 +10:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::parse;
|
2017-07-26 15:25:29 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T> struct c_traits;
|
|
|
|
template <> struct c_traits<long> { static constexpr auto func = strtol; };
|
|
|
|
template <> struct c_traits<long long> { static constexpr auto func = strtoll; };
|
|
|
|
template <> struct c_traits<unsigned long> { static constexpr auto func = strtoul; };
|
|
|
|
template <> struct c_traits<unsigned long long> { static constexpr auto func = strtoull; };
|
|
|
|
|
|
|
|
template <> struct c_traits<float> { static constexpr auto func = strtof; };
|
|
|
|
template <> struct c_traits<double> { static constexpr auto func = strtod; };
|
|
|
|
template <> struct c_traits<long double> { static constexpr auto func = strtold; };
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
T
|
2018-12-16 13:26:48 +11:00
|
|
|
c_iparse (cruft::view<char const*> &data)
|
2017-07-26 15:25:29 +10:00
|
|
|
{
|
2018-12-16 13:26:48 +11:00
|
|
|
auto head = const_cast<char *> (data.begin ());
|
|
|
|
auto tail = head;
|
|
|
|
auto val = c_traits<T>::func (head, &tail, 0);
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
if (tail == head)
|
|
|
|
throw std::invalid_argument ("unable to parse integer");
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
data = data.consume (tail);
|
2017-07-26 15:25:29 +10:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
T
|
2018-12-16 13:26:48 +11:00
|
|
|
c_fparse (cruft::view<char const*> &data)
|
2017-07-26 15:25:29 +10:00
|
|
|
{
|
2018-12-16 13:26:48 +11:00
|
|
|
auto head = const_cast<char *> (data.begin ());
|
|
|
|
auto tail = head;
|
|
|
|
auto val = c_traits<T>::func (head, &tail);
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
if (tail == head)
|
|
|
|
throw std::invalid_argument ("unable to parse float");
|
2017-07-26 15:25:29 +10:00
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
data = data.consume (tail);
|
2017-07-26 15:25:29 +10:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2018-12-16 13:26:48 +11:00
|
|
|
#define C_PARSE(T, KLASS) \
|
|
|
|
template <> \
|
|
|
|
T \
|
|
|
|
cruft::parse<T> ( \
|
|
|
|
cruft::view<const char *> &str \
|
|
|
|
) { \
|
|
|
|
return c_ ## KLASS ## parse<T> (str); \
|
2017-07-26 15:25:29 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2018-12-16 13:26:48 +11:00
|
|
|
C_PARSE(long, i)
|
|
|
|
C_PARSE(long long, i)
|
|
|
|
C_PARSE(unsigned long, i)
|
2017-07-26 15:25:29 +10:00
|
|
|
C_PARSE(unsigned long long, i)
|
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
C_PARSE(float, f)
|
|
|
|
C_PARSE(double, f)
|
2017-07-26 15:25:29 +10:00
|
|
|
C_PARSE(long double, f)
|
2017-12-19 18:12:35 +11:00
|
|
|
|
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
#define C_PARSE_WITH_CAST(FINAL, USED) \
|
|
|
|
template<> \
|
|
|
|
FINAL \
|
|
|
|
cruft::parse<FINAL> (cruft::view<char const*> &str) { \
|
|
|
|
auto remain = str; \
|
|
|
|
auto res = parse<USED> (remain); \
|
|
|
|
if (res > std::numeric_limits<FINAL>::max ()) \
|
|
|
|
throw std::invalid_argument ("overflow during parse"); \
|
|
|
|
\
|
|
|
|
str = remain; \
|
|
|
|
return cruft::cast::narrow<FINAL> (res); \
|
2018-03-27 16:11:04 +11:00
|
|
|
}
|
2018-07-27 18:28:30 +10:00
|
|
|
|
2018-12-16 13:26:48 +11:00
|
|
|
C_PARSE_WITH_CAST(unsigned int, unsigned long)
|