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,30 +16,32 @@
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);
@ -49,14 +51,14 @@ c_iparse (cruft::view<char const*> &data)
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);
@ -66,6 +68,7 @@ c_fparse (cruft::view<char const*> &data)
data = data.consume (tail); data = data.consume (tail);
return val; return val;
}
} }