parse: move internal helpers into an anonymous namespace

This commit is contained in:
Danny Robson 2019-02-02 13:48:48 +11:00
parent ba01794d0b
commit 8c934fbd2c

View File

@ -16,56 +16,59 @@
using cruft::parse;
///////////////////////////////////////////////////////////////////////////////
/// A trait class that records the C string parsing function for a given type.
///
/// Not all types will necessarily have a corresponding parsing function. eg,
template <typename ValueT> struct parse_traits { };
namespace {
///////////////////////////////////////////////////////////////////////////
/// A trait class that records the C string parsing function for a given
/// type.
///
/// Not all types will necessarily have a corresponding parsing function.
template <typename ValueT> struct parse_traits { };
//-----------------------------------------------------------------------------
template <> struct parse_traits<long> { static constexpr auto value = strtol; };
template <> struct parse_traits<long long> { static constexpr auto value = strtoll; };
template <> struct parse_traits<unsigned long> { static constexpr auto value = strtoul; };
template <> struct parse_traits<unsigned long long> { static constexpr auto value = strtoull; };
//-----------------------------------------------------------------------------
template <> struct parse_traits<long> { static constexpr auto value = strtol; };
template <> struct parse_traits<long long> { static constexpr auto value = strtoll; };
template <> struct parse_traits<unsigned long> { static constexpr auto value = strtoul; };
template <> struct parse_traits<unsigned long long> { static constexpr auto value = strtoull; };
//-----------------------------------------------------------------------------
template <> struct parse_traits<float> { static constexpr auto value = strtof; };
template <> struct parse_traits<double> { static constexpr auto value = strtod; };
template <> struct parse_traits<long double> { static constexpr auto value = strtold; };
//-----------------------------------------------------------------------------
template <> struct parse_traits<float> { static constexpr auto value = strtof; };
template <> struct parse_traits<double> { static constexpr auto value = strtod; };
template <> struct parse_traits<long double> { static constexpr auto value = strtold; };
///////////////////////////////////////////////////////////////////////////////
template <typename ValueT>
ValueT
c_iparse (cruft::view<char const*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
auto val = parse_traits<ValueT>::value (head, &tail, 0);
///////////////////////////////////////////////////////////////////////////
template <typename ValueT>
ValueT
c_iparse (cruft::view<char const*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
auto val = parse_traits<ValueT>::value (head, &tail, 0);
if (tail == head)
throw std::invalid_argument ("unable to parse integer");
if (tail == head)
throw std::invalid_argument ("unable to parse integer");
data = data.consume (tail);
return val;
}
data = data.consume (tail);
return val;
}
//-----------------------------------------------------------------------------
template <typename ValueT>
ValueT
c_fparse (cruft::view<char const*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
auto val = parse_traits<ValueT>::value (head, &tail);
//-------------------------------------------------------------------------
template <typename ValueT>
ValueT
c_fparse (cruft::view<char const*> &data)
{
auto head = const_cast<char *> (data.begin ());
auto tail = head;
auto val = parse_traits<ValueT>::value (head, &tail);
if (tail == head)
throw std::invalid_argument ("unable to parse float");
if (tail == head)
throw std::invalid_argument ("unable to parse float");
data = data.consume (tail);
return val;
data = data.consume (tail);
return val;
}
}