make indent with 4 caracters instead of 8

This commit is contained in:
ado 2020-12-26 00:46:42 +01:00
parent 16f4648cb3
commit e406253694
9 changed files with 1688 additions and 1746 deletions

View File

@ -41,8 +41,7 @@ using no_validator_t = typename no_validator<T>::type;
template <typename... Ts>
struct no_validator_tup {
using type =
typename apply_trait<no_validator, std::tuple<Ts...>>::type;
using type = typename apply_trait<no_validator, std::tuple<Ts...>>::type;
};
template <typename... Ts>
@ -121,8 +120,7 @@ class converter {
// parses line with given delimiter, returns a 'T' object created with
// extracted values of type 'Ts'
template <typename T, typename... Ts>
T convert_object(const char* const line,
const std::string& delim = "") {
T convert_object(const char* const line, const std::string& delim = "") {
return to_object<T>(convert<Ts...>(line, delim));
}
@ -152,11 +150,9 @@ class converter {
typename std::result_of_t<decltype (&T::tied)(T)>;
using arg_tuple =
typename apply_trait<std::decay,
arg_ref_tuple>::type;
typename apply_trait<std::decay, arg_ref_tuple>::type;
return to_object<T>(
convert_impl(elems, (arg_tuple*){}));
return to_object<T>(convert_impl(elems, (arg_tuple*){}));
} else if constexpr (sizeof...(Ts) == 0 &&
is_instance_of<T, std::tuple>::value) {
return convert_impl(elems, (T*){});
@ -167,8 +163,7 @@ class converter {
}
bool valid() const {
return (error_mode_ == error_mode::String)
? string_error_.empty()
return (error_mode_ == error_mode::String) ? string_error_.empty()
: bool_error_ == false;
}
@ -223,8 +218,7 @@ class converter {
void set_error_invalid_conversion(const string_range msg, size_t pos) {
if (error_mode_ == error_mode::String) {
string_error_.clear();
string_error_
.append("invalid conversion for parameter ")
string_error_.append("invalid conversion for parameter ")
.append(error_sufix(msg, pos));
} else {
bool_error_ = true;
@ -245,8 +239,7 @@ class converter {
void set_error_number_of_colums(size_t expected_pos, size_t pos) {
if (error_mode_ == error_mode::String) {
string_error_.clear();
string_error_
.append("invalid number of columns, expected: ")
string_error_.append("invalid number of columns, expected: ")
.append(std::to_string(expected_pos))
.append(", got: ")
.append(std::to_string(pos));
@ -329,11 +322,9 @@ class converter {
if constexpr (has_m_ss_valid_t<T>) {
if (T validator; !validator.ss_valid(dst)) {
if constexpr (has_m_error_t<T>) {
set_error_validate(validator.error(),
msg, pos);
set_error_validate(validator.error(), msg, pos);
} else {
set_error_validate("validation error",
msg, pos);
set_error_validate("validation error", msg, pos);
}
return;
}
@ -346,8 +337,7 @@ class converter {
using elem_t = std::tuple_element_t<ArgN, std::tuple<Ts...>>;
constexpr bool not_void = !std::is_void_v<elem_t>;
constexpr bool one_element =
count_not<std::is_void, Ts...>::size == 1;
constexpr bool one_element = count_not<std::is_void, Ts...>::size == 1;
if constexpr (not_void) {
if constexpr (one_element) {

View File

@ -245,8 +245,7 @@ inline bool sub_overflow(unsigned long long& result,
template <typename T, typename F>
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) {
if (mul_overflow<T>(value, 10) ||
add_last_digit_owerflow(value, digit)) {
if (mul_overflow<T>(value, 10) || add_last_digit_owerflow(value, digit)) {
return true;
}
return false;
@ -293,8 +292,7 @@ std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
T value = 0;
for (auto i = begin; i != end; ++i) {
if (auto digit = from_char(*i);
!digit ||
shift_and_add_overflow<T>(value, digit.value(),
!digit || shift_and_add_overflow<T>(value, digit.value(),
add_last_digit_owerflow)) {
return std::nullopt;
}

View File

@ -33,8 +33,7 @@ class parser {
}
bool valid() const {
return (error_mode_ == error_mode::String)
? string_error_.empty()
return (error_mode_ == error_mode::String) ? string_error_.empty()
: bool_error_ == false;
}
@ -97,8 +96,8 @@ class parser {
// that parameter can be invoked using the converted value,
// than it will be invoked in the case of a valid conversion
template <typename... Us, typename Fun = None>
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>>
or_else(Fun&& fun = None{}) {
composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>> or_else(
Fun&& fun = None{}) {
using Value = no_void_validator_tup_t<Us...>;
std::optional<Value> value;
try_convert_and_invoke<Value, Us...>(value, fun);
@ -108,8 +107,7 @@ class parser {
// same as or_else, but saves the result into a 'U' object
// instead of a tuple
template <typename U, typename... Us, typename Fun = None>
composite<Ts..., std::optional<U>> or_else_object(
Fun&& fun = None{}) {
composite<Ts..., std::optional<U>> or_else_object(Fun&& fun = None{}) {
std::optional<U> value;
try_convert_and_invoke<U, Us...>(value, fun);
return composite_with(std::move(value));
@ -125,8 +123,7 @@ class parser {
if constexpr (std::is_invocable_v<Fun>) {
fun();
} else {
std::invoke(std::forward<Fun>(fun),
parser_.error_msg());
std::invoke(std::forward<Fun>(fun), parser_.error_msg());
}
}
return *this;
@ -137,29 +134,23 @@ class parser {
composite<Ts..., T> composite_with(T&& new_value) {
auto merged_values =
std::tuple_cat(std::move(values_),
std::tuple{
std::forward<T>(new_value)});
std::tuple{std::forward<T>(new_value)});
return {std::move(merged_values), parser_};
}
template <typename U, typename... Us, typename Fun = None>
void try_convert_and_invoke(std::optional<U>& value,
Fun&& fun) {
void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
if (!parser_.valid()) {
std::optional<U> new_value;
auto tuple_output = try_same<Us...>();
if constexpr (!std::is_same_v<
U, decltype(tuple_output)>) {
new_value = to_object<U>(
std::move(tuple_output));
if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
new_value = to_object<U>(std::move(tuple_output));
} else {
new_value = std::move(tuple_output);
}
if (parser_.valid()) {
value = std::move(new_value);
parser_.try_invoke(*value,
std::forward<Fun>(
fun));
parser_.try_invoke(*value, std::forward<Fun>(fun));
}
}
}
@ -167,8 +158,8 @@ class parser {
template <typename U, typename... Us>
no_void_validator_tup_t<U, Us...> try_same() {
parser_.clear_error();
auto value = parser_.converter_.convert<U, Us...>(
parser_.split_input_);
auto value =
parser_.converter_.convert<U, Us...>(parser_.split_input_);
if (!parser_.converter_.valid()) {
parser_.set_error_invalid_conversion();
}
@ -204,15 +195,12 @@ class parser {
// failed
template <typename Arg, typename Fun = None>
void try_invoke(Arg&& arg, Fun&& fun) {
constexpr bool is_none =
std::is_same_v<std::decay_t<Fun>, None>;
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
if constexpr (!is_none) {
using Ret = decltype(
try_invoke_impl(arg, std::forward<Fun>(fun)));
using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun)));
constexpr bool returns_void = std::is_same_v<Ret, void>;
if constexpr (!returns_void) {
if (!try_invoke_impl(arg,
std::forward<Fun>(fun))) {
if (!try_invoke_impl(arg, std::forward<Fun>(fun))) {
set_error_failed_check();
}
} else {
@ -228,8 +216,7 @@ class parser {
// laid out as a parameter pack
template <typename Arg, typename Fun = None>
auto try_invoke_impl(Arg&& arg, Fun&& fun) {
constexpr bool is_none =
std::is_same_v<std::decay_t<Fun>, None>;
constexpr bool is_none = std::is_same_v<std::decay_t<Fun>, None>;
if constexpr (!is_none) {
if constexpr (std::is_invocable_v<Fun>) {
return fun();
@ -299,8 +286,7 @@ class parser {
void set_error_failed_check() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" failed check.");
string_error_.append(file_name_).append(" failed check.");
} else {
bool_error_ = true;
}
@ -308,8 +294,7 @@ class parser {
void set_error_file_not_open() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" could not be not open.");
string_error_.append(file_name_).append(" could not be not open.");
} else {
bool_error_ = true;
}
@ -317,8 +302,7 @@ class parser {
void set_error_eof_reached() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" reached end of file.");
string_error_.append(file_name_).append(" reached end of file.");
} else {
bool_error_ = true;
}

View File

@ -133,14 +133,14 @@ struct apply_optional_trait<Trait, std::tuple<T, Ts...>> {
template <template <typename...> class Trait, typename T>
struct apply_optional_trait {
using type = std::tuple<
typename optional_trait<typename Trait<T>::type, T>::type>;
using type =
std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
};
template <template <typename...> class Trait, typename T>
struct apply_optional_trait<Trait, std::tuple<T>> {
using type = std::tuple<
typename optional_trait<typename Trait<T>::type, T>::type>;
using type =
std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
};
////////////////
@ -204,8 +204,8 @@ struct filter_if {
template <template <typename...> class Trait, typename... Ts>
struct filter_if<Trait, std::tuple<Ts...>> {
using type = typename remove_false<typename apply_optional_trait<
Trait, std::tuple<Ts...>>::type>::type;
using type = typename remove_false<
typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>::type;
};
template <template <typename...> class Trait, typename... Ts>
@ -268,14 +268,12 @@ struct count_not<Trait, T> {
template <template <typename...> class Trait, typename... Ts>
struct all_of {
static constexpr bool value =
count<Trait, Ts...>::size == sizeof...(Ts);
static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
};
template <template <typename...> class Trait, typename... Ts>
struct all_of<Trait, std::tuple<Ts...>> {
static constexpr bool value =
count<Trait, Ts...>::size == sizeof...(Ts);
static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
};
////////////////

View File

@ -59,8 +59,7 @@ TEST_CASE("testing valid conversions") {
}
{
auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5",
"\t");
c.convert<void, void, std::optional<int>>("junk\tjunk\t5", "\t");
REQUIRE(c.valid());
REQUIRE(tup.has_value());
CHECK(tup == 5);
@ -82,30 +81,30 @@ TEST_CASE("testing valid conversions") {
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5;6.6",
";");
c.convert<void, std::optional<int>, double>("junk;5;6.6", ";");
REQUIRE(c.valid());
REQUIRE(std::get<0>(tup).has_value());
CHECK(tup == std::tuple{5, 6.6});
}
{
auto tup =
c.convert<void, std::optional<int>, double>("junk;5.4;6.6",
";");
c.convert<void, std::optional<int>, double>("junk;5.4;6.6", ";");
REQUIRE(c.valid());
REQUIRE(!std::get<0>(tup).has_value());
CHECK(tup == std::tuple{std::nullopt, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5;6.6", ";");
auto tup =
c.convert<void, std::variant<int, double>, double>("junk;5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
}
{
auto tup = c.convert<void, std::variant<int, double>,
double>("junk;5.5;6.6", ";");
auto tup =
c.convert<void, std::variant<int, double>, double>("junk;5.5;6.6",
";");
REQUIRE(c.valid());
REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
@ -163,14 +162,12 @@ TEST_CASE("testing ss:ax restriction (all except)") {
CHECK(tup == 3);
}
{
std::tuple<char, int> tup =
c.convert<char, ss::ax<int, 1>>("c,3");
std::tuple<char, int> tup = c.convert<char, ss::ax<int, 1>>("c,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
{
std::tuple<int, char> tup =
c.convert<ss::ax<int, 1>, char>("3,c");
std::tuple<int, char> tup = c.convert<ss::ax<int, 1>, char>("3,c");
REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'});
}
@ -199,8 +196,7 @@ TEST_CASE("testing ss:nx restriction (none except)") {
CHECK(tup == 2);
}
{
auto tup =
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
auto tup = c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1});
}
@ -267,8 +263,7 @@ TEST_CASE("testing ss:oor restriction (out of range)") {
}
{
auto tup =
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
auto tup = c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 3});
}
@ -317,8 +312,7 @@ TEST_CASE("testing ss:ne restriction (not empty)") {
CHECK(tup == "s");
}
{
auto tup =
c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
auto tup = c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
REQUIRE(c.valid());
CHECK(tup == std::tuple{1, "s"});
}

View File

@ -54,8 +54,7 @@ TEST_CASE("testing extract functions for floating point values") {
/* check negative too */ \
if (std::is_signed_v<type>) { \
auto s = std::string("-") + #input; \
auto t = \
ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \
@ -104,8 +103,7 @@ TEST_CASE("testing extract functions for numbers with invalid inputs") {
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::max()); \
std::string s = std::to_string(std::numeric_limits<type>::max()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
@ -118,8 +116,7 @@ TEST_CASE("testing extract functions for numbers with invalid inputs") {
CHECK(!t.has_value()); \
} \
{ \
std::string s = \
std::to_string(std::numeric_limits<type>::min()); \
std::string s = std::to_string(std::numeric_limits<type>::min()); \
auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(t.has_value()); \
for (auto& i : s) { \
@ -224,32 +221,28 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "22";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, double);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, int);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22", std::string);
}
{
std::variant<int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int);
}
}
@ -257,24 +250,21 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "22.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<double, int, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, std::string);
REQUIRE_VARIANT(var, 22.2, double);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
@ -284,48 +274,42 @@ TEST_CASE("testing extract functions for std::variant") {
std::string s = "2.2.2";
{
std::variant<int, double, std::string> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<double, std::string, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<std::string, double, int> var;
REQUIRE(
ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "2.2.2", std::string);
}
{
std::variant<int, double> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
CHECK_NOT_VARIANT(var, double);
}
{
std::variant<double, int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, double{}, double);
CHECK_NOT_VARIANT(var, int);
}
{
std::variant<int> var;
REQUIRE(
!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, int{}, int);
}

View File

@ -85,8 +85,7 @@ TEST_CASE("testing parser") {
std::vector<X> i;
while (!p.eof()) {
i.push_back(
p.get_object<X, int, double, std::string>());
i.push_back(p.get_object<X, int, double, std::string>());
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
@ -121,8 +120,8 @@ TEST_CASE("testing parser") {
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_object<X, ss::ax<int, excluded>, double,
std::string>();
auto a =
p.get_object<X, ss::ax<int, excluded>, double, std::string>();
if (p.valid()) {
i.push_back(a);
}
@ -138,8 +137,7 @@ TEST_CASE("testing parser") {
std::vector<X> i;
while (!p.eof()) {
auto a = p.get_object<X, ss::nx<int, 3>, double,
std::string>();
auto a = p.get_object<X, ss::nx<int, 3>, double, std::string>();
if (p.valid()) {
i.push_back(a);
}
@ -219,8 +217,7 @@ TEST_CASE("testing composite conversion") {
constexpr static auto expectedData = std::tuple{10, 20, 11.1};
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>(
[](auto& i1, auto i2, double d) {
p.try_next<int, int, double>([](auto& i1, auto i2, double d) {
CHECK(std::tie(i1, i2, d) == expectedData);
})
.on_error(fail)
@ -277,8 +274,7 @@ TEST_CASE("testing composite conversion") {
{
REQUIRE(!p.eof());
auto [d1, d2] =
p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
auto [d1, d2] = p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
.or_else<test_struct>(expect_test_struct)
.values();
@ -326,8 +322,7 @@ TEST_CASE("testing composite conversion") {
{
REQUIRE(!p.eof());
auto [d1, d2] =
p.try_next<int, std::variant<int, std::string>>()
auto [d1, d2] = p.try_next<int, std::variant<int, std::string>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
@ -336,8 +331,7 @@ TEST_CASE("testing composite conversion") {
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 ==
std::tuple{11, std::variant<int, std::string>{"junk"}});
CHECK(*d1 == std::tuple{11, std::variant<int, std::string>{"junk"}});
}
}