parse: move si and value parsing into common namespace

This commit is contained in:
Danny Robson 2019-03-19 12:38:22 +11:00
parent 340d873d3a
commit e5e47ffb24
9 changed files with 37 additions and 38 deletions

View File

@ -388,8 +388,10 @@ list (
nocopy.hpp
parallel/queue.cpp
parallel/queue.hpp
parse.cpp
parse.hpp
parse/value.cpp
parse/value.hpp
parse/si.cpp
parse/si.hpp
platform.hpp
point.cpp
point.hpp
@ -419,10 +421,8 @@ list (
region.hpp
roots/bisection.hpp
scoped.hpp
si.cpp
signal.cpp
signal.hpp
si.hpp
singleton.hpp
stats.cpp
stats.hpp

View File

@ -9,7 +9,7 @@
#include "colour.hpp"
#include "ascii.hpp"
#include "parse.hpp"
#include "parse/value.hpp"
///////////////////////////////////////////////////////////////////////////////
@ -45,7 +45,7 @@ parse_hex (cruft::view<const char*> &str)
///////////////////////////////////////////////////////////////////////////////
template <>
cruft::srgba4f
cruft::parse<cruft::srgba4f> (cruft::view<const char*> &str)
cruft::parse::value<cruft::srgba4f> (cruft::view<const char*> &str)
{
return parse_hex (str);
}

View File

@ -8,7 +8,7 @@
#include "queue.hpp"
#include "../parse.hpp"
#include "../parse/value.hpp"
#include "../scoped.hpp"
using cruft::job::queue;
@ -19,7 +19,7 @@ unsigned
queue::default_parallelism (void) noexcept
{
if (auto var = getenv ("JOB_THREADS")) {
return cruft::from_string<unsigned> (var);
return cruft::parse::from_string<unsigned> (var);
}
return std::thread::hardware_concurrency ();
@ -31,7 +31,7 @@ static unsigned
default_depth (void)
{
if (auto var = getenv ("JOB_DEPTH")) {
return cruft::from_string<unsigned> (var);
return cruft::parse::from_string<unsigned> (var);
}
return 1024;

View File

@ -8,21 +8,20 @@
#include "si.hpp"
#include "std.hpp"
#include "parse.hpp"
#include "value.hpp"
#include "../std.hpp"
#include <cruft/util/preprocessor.hpp>
#include <charconv>
///////////////////////////////////////////////////////////////////////////////
template <typename ValueT>
cruft::expected<ValueT, std::errc>
cruft::si::parse (cruft::view<char const*> const src)
cruft::parse::si (cruft::view<char const*> const src)
{
auto remain = src;
ValueT dst = cruft::parse<ValueT> (remain);
ValueT dst = cruft::parse::value<ValueT> (remain);
switch (remain.size ()) {
case 0:
@ -43,7 +42,7 @@ cruft::si::parse (cruft::view<char const*> const src)
///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE(KLASS) template cruft::expected<KLASS,std::errc> cruft::si::parse (cruft::view<char const*>);
#define INSTANTIATE(KLASS) template cruft::expected<KLASS,std::errc> cruft::parse::si (cruft::view<char const*>);
MAP0 (INSTANTIATE,

View File

@ -8,14 +8,14 @@
#pragma once
#include "expected.hpp"
#include "view.hpp"
#include "../expected.hpp"
#include "../view.hpp"
#include <system_error>
namespace cruft::si {
namespace cruft::parse {
template <typename ValueT>
expected<ValueT, std::errc>
parse (cruft::view<char const*>);
si (cruft::view<char const*>);
}

View File

@ -6,15 +6,15 @@
* Copyright 2017-2018 Danny Robson <danny@nerdcruft.net>
*/
#include "parse.hpp"
#include "value.hpp"
#include "cast.hpp"
#include "../cast.hpp"
#include <cruft/util/preprocessor.hpp>
#include <cstdlib>
#include <stdexcept>
using cruft::parse;
using cruft::parse::value;
namespace {
///////////////////////////////////////////////////////////////////////////
@ -96,7 +96,7 @@ namespace {
#define C_PARSE_ITERATOR(ITERATOR,PREFIX, KLASS)\
template <> \
KLASS \
cruft::parse<KLASS> ( \
cruft::parse::value<KLASS> ( \
cruft::view<ITERATOR> &str \
) { \
return c_ ## PREFIX ## parse<KLASS> (str); \
@ -137,9 +137,9 @@ MAP1(C_PARSE, f,
#define C_PARSE_WITH_CAST(FINAL, USED) \
template<> \
FINAL \
cruft::parse<FINAL> (cruft::view<char const*> &str) { \
cruft::parse::value<FINAL> (cruft::view<char const*> &str) { \
auto remain = str; \
auto res = parse<USED> (remain); \
auto res = value<USED> (remain); \
if (res > std::numeric_limits<FINAL>::max ()) \
throw std::invalid_argument ("overflow during parse"); \
\

View File

@ -8,17 +8,17 @@
#pragma once
#include "view.hpp"
#include "../view.hpp"
namespace cruft {
namespace cruft::parse {
///////////////////////////////////////////////////////////////////////////
/// Extracts an instance of a native type T from the string [first, last).
///
/// Throws std::invalid_argument when the type cannot be parsed.
///
/// The view is modified in place to reflect the unused data.
template <typename T> T parse (cruft::view<const char *> &);
template <typename T> T parse (cruft::view< char *> &);
template <typename T> T value (cruft::view<const char *> &);
template <typename T> T value (cruft::view< char *> &);
/// Parses a prefix string to obtain an instance of T.
@ -30,7 +30,7 @@ namespace cruft {
T
from_string (cruft::view<const char*> data)
{
T res = parse<T> (data);
T res = value<T> (data);
if (!data.empty ())
throw std::invalid_argument ("unable to parse");
return std::move (res);

View File

@ -1,4 +1,4 @@
#include "parse.hpp"
#include "parse/value.hpp"
#include "tap.hpp"
@ -8,11 +8,11 @@ main (void)
{
cruft::TAP::logger tap;
tap.expect_eq (cruft::from_string<long> ("1"), 1L, "parsing long '1'");
tap.expect_throw<std::invalid_argument> ([] () { cruft::from_string<long> ("a"); }, "parsing long 'a'");
tap.expect_eq (cruft::parse::from_string<long> ("1"), 1L, "parsing long '1'");
tap.expect_throw<std::invalid_argument> ([] () { cruft::parse::from_string<long> ("a"); }, "parsing long 'a'");
tap.expect_eq (cruft::from_string<float> ("1"), 1.f, "parsing float '1'");
tap.expect_throw<std::invalid_argument> ([] () { cruft::from_string<float> ("a"); }, "parsing float 'a'");
tap.expect_eq (cruft::parse::from_string<float> ("1"), 1.f, "parsing float '1'");
tap.expect_throw<std::invalid_argument> ([] () { cruft::parse::from_string<float> ("a"); }, "parsing float 'a'");
return tap.status ();
}

View File

@ -1,5 +1,5 @@
#include "tap.hpp"
#include "si.hpp"
#include "parse/si.hpp"
int main ()
@ -21,7 +21,7 @@ int main ()
};
for (auto const &t: TESTS) {
auto res = cruft::si::parse<std::size_t> (t.str);
auto res = cruft::parse::si<std::size_t> (t.str);
if (!res) {
tap.fail ("SI parsing %!", t.str);
} else {