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 nocopy.hpp
parallel/queue.cpp parallel/queue.cpp
parallel/queue.hpp parallel/queue.hpp
parse.cpp parse/value.cpp
parse.hpp parse/value.hpp
parse/si.cpp
parse/si.hpp
platform.hpp platform.hpp
point.cpp point.cpp
point.hpp point.hpp
@ -419,10 +421,8 @@ list (
region.hpp region.hpp
roots/bisection.hpp roots/bisection.hpp
scoped.hpp scoped.hpp
si.cpp
signal.cpp signal.cpp
signal.hpp signal.hpp
si.hpp
singleton.hpp singleton.hpp
stats.cpp stats.cpp
stats.hpp stats.hpp

View File

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

View File

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

View File

@ -8,21 +8,20 @@
#include "si.hpp" #include "si.hpp"
#include "std.hpp" #include "value.hpp"
#include "parse.hpp"
#include "../std.hpp"
#include <cruft/util/preprocessor.hpp> #include <cruft/util/preprocessor.hpp>
#include <charconv>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename ValueT> template <typename ValueT>
cruft::expected<ValueT, std::errc> 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; auto remain = src;
ValueT dst = cruft::parse<ValueT> (remain); ValueT dst = cruft::parse::value<ValueT> (remain);
switch (remain.size ()) { switch (remain.size ()) {
case 0: 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, MAP0 (INSTANTIATE,

View File

@ -8,14 +8,14 @@
#pragma once #pragma once
#include "expected.hpp" #include "../expected.hpp"
#include "view.hpp" #include "../view.hpp"
#include <system_error> #include <system_error>
namespace cruft::si { namespace cruft::parse {
template <typename ValueT> template <typename ValueT>
expected<ValueT, std::errc> 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> * 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 <cruft/util/preprocessor.hpp>
#include <cstdlib> #include <cstdlib>
#include <stdexcept> #include <stdexcept>
using cruft::parse; using cruft::parse::value;
namespace { namespace {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -96,7 +96,7 @@ namespace {
#define C_PARSE_ITERATOR(ITERATOR,PREFIX, KLASS)\ #define C_PARSE_ITERATOR(ITERATOR,PREFIX, KLASS)\
template <> \ template <> \
KLASS \ KLASS \
cruft::parse<KLASS> ( \ cruft::parse::value<KLASS> ( \
cruft::view<ITERATOR> &str \ cruft::view<ITERATOR> &str \
) { \ ) { \
return c_ ## PREFIX ## parse<KLASS> (str); \ return c_ ## PREFIX ## parse<KLASS> (str); \
@ -137,9 +137,9 @@ MAP1(C_PARSE, f,
#define C_PARSE_WITH_CAST(FINAL, USED) \ #define C_PARSE_WITH_CAST(FINAL, USED) \
template<> \ template<> \
FINAL \ FINAL \
cruft::parse<FINAL> (cruft::view<char const*> &str) { \ cruft::parse::value<FINAL> (cruft::view<char const*> &str) { \
auto remain = str; \ auto remain = str; \
auto res = parse<USED> (remain); \ auto res = value<USED> (remain); \
if (res > std::numeric_limits<FINAL>::max ()) \ if (res > std::numeric_limits<FINAL>::max ()) \
throw std::invalid_argument ("overflow during parse"); \ throw std::invalid_argument ("overflow during parse"); \
\ \

View File

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

View File

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

View File

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