diff --git a/parse/value.cpp b/parse/value.cpp index eac67da7..20d88938 100644 --- a/parse/value.cpp +++ b/parse/value.cpp @@ -152,3 +152,34 @@ C_PARSE_WITH_CAST(short, long) C_PARSE_WITH_CAST(int, long) C_PARSE_WITH_CAST(unsigned short, unsigned long) C_PARSE_WITH_CAST(unsigned int, unsigned long) + + +/////////////////////////////////////////////////////////////////////////////// +template <> +bool +cruft::parse::value (cruft::view &str) +{ + static constexpr struct { + char const *str; + bool res; + } VALUES[] = { + { "yes", true }, + { "y", true }, + { "true",true }, + { "t", true }, + + { "no", false, }, + { "n", false, }, + { "false", false, }, + { "f", false, }, + }; + + for (auto [key,val]: VALUES) { + if (equal (key, str)) { + str = str.consume (strlen (key)); + return val; + } + } + + throw std::invalid_argument ("invalid boolean string"); +} diff --git a/test/parse/value.cpp b/test/parse/value.cpp index 53618d98..8c6b7362 100644 --- a/test/parse/value.cpp +++ b/test/parse/value.cpp @@ -14,5 +14,8 @@ main (void) tap.expect_eq (cruft::parse::from_string ("1"), 1.f, "parsing float '1'"); tap.expect_throw ([] () { cruft::parse::from_string ("a"); }, "parsing float 'a'"); + tap.expect_eq (cruft::parse::from_string ("true"), true, "parsing true"); + tap.expect_eq (cruft::parse::from_string ("false"), false, "parsing false"); + return tap.status (); } \ No newline at end of file