parse: move internal helpers into an anonymous namespace
This commit is contained in:
parent
ba01794d0b
commit
8c934fbd2c
81
parse.cpp
81
parse.cpp
@ -16,56 +16,59 @@
|
|||||||
|
|
||||||
using cruft::parse;
|
using cruft::parse;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
namespace {
|
||||||
/// A trait class that records the C string parsing function for a given type.
|
///////////////////////////////////////////////////////////////////////////
|
||||||
///
|
/// A trait class that records the C string parsing function for a given
|
||||||
/// Not all types will necessarily have a corresponding parsing function. eg,
|
/// type.
|
||||||
template <typename ValueT> struct parse_traits { };
|
///
|
||||||
|
/// 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> { static constexpr auto value = strtol; };
|
||||||
template <> struct parse_traits<long long> { static constexpr auto value = strtoll; };
|
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> { static constexpr auto value = strtoul; };
|
||||||
template <> struct parse_traits<unsigned long long> { static constexpr auto value = strtoull; };
|
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<float> { static constexpr auto value = strtof; };
|
||||||
template <> struct parse_traits<double> { static constexpr auto value = strtod; };
|
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<long double> { static constexpr auto value = strtold; };
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
template <typename ValueT>
|
template <typename ValueT>
|
||||||
ValueT
|
ValueT
|
||||||
c_iparse (cruft::view<char const*> &data)
|
c_iparse (cruft::view<char const*> &data)
|
||||||
{
|
{
|
||||||
auto head = const_cast<char *> (data.begin ());
|
auto head = const_cast<char *> (data.begin ());
|
||||||
auto tail = head;
|
auto tail = head;
|
||||||
auto val = parse_traits<ValueT>::value (head, &tail, 0);
|
auto val = parse_traits<ValueT>::value (head, &tail, 0);
|
||||||
|
|
||||||
if (tail == head)
|
if (tail == head)
|
||||||
throw std::invalid_argument ("unable to parse integer");
|
throw std::invalid_argument ("unable to parse integer");
|
||||||
|
|
||||||
data = data.consume (tail);
|
data = data.consume (tail);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-------------------------------------------------------------------------
|
||||||
template <typename ValueT>
|
template <typename ValueT>
|
||||||
ValueT
|
ValueT
|
||||||
c_fparse (cruft::view<char const*> &data)
|
c_fparse (cruft::view<char const*> &data)
|
||||||
{
|
{
|
||||||
auto head = const_cast<char *> (data.begin ());
|
auto head = const_cast<char *> (data.begin ());
|
||||||
auto tail = head;
|
auto tail = head;
|
||||||
auto val = parse_traits<ValueT>::value (head, &tail);
|
auto val = parse_traits<ValueT>::value (head, &tail);
|
||||||
|
|
||||||
if (tail == head)
|
if (tail == head)
|
||||||
throw std::invalid_argument ("unable to parse float");
|
throw std::invalid_argument ("unable to parse float");
|
||||||
|
|
||||||
data = data.consume (tail);
|
data = data.consume (tail);
|
||||||
return val;
|
return val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user