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

@ -28,12 +28,12 @@ struct no_validator;
template <typename T> template <typename T>
struct no_validator<T, typename std::enable_if_t<has_m_ss_valid_t<T>>> { struct no_validator<T, typename std::enable_if_t<has_m_ss_valid_t<T>>> {
using type = typename member_wrapper<decltype(&T::ss_valid)>::arg_type; using type = typename member_wrapper<decltype(&T::ss_valid)>::arg_type;
}; };
template <typename T, typename U> template <typename T, typename U>
struct no_validator { struct no_validator {
using type = T; using type = T;
}; };
template <typename T> template <typename T>
@ -41,18 +41,17 @@ using no_validator_t = typename no_validator<T>::type;
template <typename... Ts> template <typename... Ts>
struct no_validator_tup { struct no_validator_tup {
using type = using type = typename apply_trait<no_validator, std::tuple<Ts...>>::type;
typename apply_trait<no_validator, std::tuple<Ts...>>::type;
}; };
template <typename... Ts> template <typename... Ts>
struct no_validator_tup<std::tuple<Ts...>> { struct no_validator_tup<std::tuple<Ts...>> {
using type = typename no_validator_tup<Ts...>::type; using type = typename no_validator_tup<Ts...>::type;
}; };
template <typename T> template <typename T>
struct no_validator_tup<std::tuple<T>> { struct no_validator_tup<std::tuple<T>> {
using type = no_validator_t<T>; using type = no_validator_t<T>;
}; };
template <typename... Ts> template <typename... Ts>
@ -64,8 +63,8 @@ using no_validator_tup_t = typename no_validator_tup<Ts...>::type;
template <typename... Ts> template <typename... Ts>
struct no_void_tup { struct no_void_tup {
using type = using type =
typename filter_not<std::is_void, no_validator_tup_t<Ts...>>::type; typename filter_not<std::is_void, no_validator_tup_t<Ts...>>::type;
}; };
template <typename... Ts> template <typename... Ts>
@ -78,12 +77,12 @@ using no_void_tup_t = filter_not_t<std::is_void, Ts...>;
// replace 'validators' and remove void from tuple // replace 'validators' and remove void from tuple
template <typename... Ts> template <typename... Ts>
struct no_void_validator_tup { struct no_void_validator_tup {
using type = no_validator_tup_t<no_void_tup_t<Ts...>>; using type = no_validator_tup_t<no_void_tup_t<Ts...>>;
}; };
template <typename... Ts> template <typename... Ts>
struct no_void_validator_tup<std::tuple<Ts...>> { struct no_void_validator_tup<std::tuple<Ts...>> {
using type = no_validator_tup_t<no_void_tup_t<Ts...>>; using type = no_validator_tup_t<no_void_tup_t<Ts...>>;
}; };
template <typename... Ts> template <typename... Ts>
@ -97,8 +96,8 @@ using no_void_validator_tup_t = typename no_void_validator_tup<Ts...>::type;
// the 'tied' method which is to be used for type deduction when converting // the 'tied' method which is to be used for type deduction when converting
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct tied_class { struct tied_class {
constexpr static bool value = constexpr static bool value =
(sizeof...(Ts) == 0 && std::is_class_v<T> && has_m_tied<T>::value); (sizeof...(Ts) == 0 && std::is_class_v<T> && has_m_tied<T>::value);
}; };
template <typename... Ts> template <typename... Ts>
@ -112,286 +111,277 @@ enum class error_mode { String, Bool };
//////////////// ////////////////
class converter { class converter {
using string_range = std::pair<const char*, const char*>; using string_range = std::pair<const char*, const char*>;
constexpr static auto default_delimiter = ','; constexpr static auto default_delimiter = ',';
public: public:
using split_input = std::vector<string_range>; using split_input = std::vector<string_range>;
// parses line with given delimiter, returns a 'T' object created with // parses line with given delimiter, returns a 'T' object created with
// extracted values of type 'Ts' // extracted values of type 'Ts'
template <typename T, typename... Ts> template <typename T, typename... Ts>
T convert_object(const char* const line, T convert_object(const char* const line, const std::string& delim = "") {
const std::string& delim = "") { return to_object<T>(convert<Ts...>(line, delim));
return to_object<T>(convert<Ts...>(line, delim)); }
// parses line with given delimiter, returns tuple of objects with
// extracted values of type 'Ts'
template <typename... Ts>
no_void_validator_tup_t<Ts...> convert(const char* const line,
const std::string& delim = "") {
input_ = split(line, delim);
return convert<Ts...>(input_);
}
// parses already split line, returns 'T' object with extracted values
template <typename T, typename... Ts>
T convert_object(const split_input& elems) {
return to_object<T>(convert<Ts...>(elems));
}
// parses already split line, returns either a tuple of objects with
// parsed values (returns raw element (no tuple) if Ts is empty), or if
// one argument is given which is a class which has a tied
// method which returns a tuple, returns that type
template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> convert(const split_input& elems) {
if constexpr (tied_class_v<T, Ts...>) {
using arg_ref_tuple =
typename std::result_of_t<decltype (&T::tied)(T)>;
using arg_tuple =
typename apply_trait<std::decay, arg_ref_tuple>::type;
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*){});
} else {
return convert_impl<T, Ts...>(elems);
}
}
bool valid() const {
return (error_mode_ == error_mode::String) ? string_error_.empty()
: bool_error_ == false;
}
const std::string& error_msg() const {
return string_error_;
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
}
// 'splits' string by given delimiter, returns vector of pairs which
// contain the beginings and the ends of each column of the string
const split_input& split(const char* const line,
const std::string& delim = "") {
input_.clear();
if (line[0] == '\0') {
return input_;
} }
// parses line with given delimiter, returns tuple of objects with switch (delim.size()) {
// extracted values of type 'Ts' case 0:
template <typename... Ts> return split_impl(line, ',');
no_void_validator_tup_t<Ts...> convert(const char* const line, case 1:
const std::string& delim = "") { return split_impl(line, delim[0]);
input_ = split(line, delim); default:
return convert<Ts...>(input_); return split_impl(line, delim, delim.size());
}
// parses already split line, returns 'T' object with extracted values
template <typename T, typename... Ts>
T convert_object(const split_input& elems) {
return to_object<T>(convert<Ts...>(elems));
}
// parses already split line, returns either a tuple of objects with
// parsed values (returns raw element (no tuple) if Ts is empty), or if
// one argument is given which is a class which has a tied
// method which returns a tuple, returns that type
template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> convert(const split_input& elems) {
if constexpr (tied_class_v<T, Ts...>) {
using arg_ref_tuple =
typename std::result_of_t<decltype (&T::tied)(T)>;
using arg_tuple =
typename apply_trait<std::decay,
arg_ref_tuple>::type;
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*){});
} else {
return convert_impl<T, Ts...>(elems);
}
}
bool valid() const {
return (error_mode_ == error_mode::String)
? string_error_.empty()
: bool_error_ == false;
}
const std::string& error_msg() const {
return string_error_;
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
}
// 'splits' string by given delimiter, returns vector of pairs which
// contain the beginings and the ends of each column of the string
const split_input& split(const char* const line,
const std::string& delim = "") {
input_.clear();
if (line[0] == '\0') {
return input_;
}
switch (delim.size()) {
case 0:
return split_impl(line, ',');
case 1:
return split_impl(line, delim[0]);
default:
return split_impl(line, delim, delim.size());
};
}
private:
////////////////
// error
////////////////
void clear_error() {
string_error_.clear();
bool_error_ = false;
}
std::string error_sufix(const string_range msg, size_t pos) const {
std::string error;
error.reserve(32);
error.append("at column ")
.append(std::to_string(pos + 1))
.append(": \'")
.append(msg.first, msg.second)
.append("\'");
return error;
}
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 ")
.append(error_sufix(msg, pos));
} else {
bool_error_ = true;
}
}
void set_error_validate(const char* const error, const string_range msg,
size_t pos) {
if (error_mode_ == error_mode::String) {
string_error_.clear();
string_error_.append(error).append(" ").append(
error_sufix(msg, pos));
} else {
bool_error_ = true;
}
}
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: ")
.append(std::to_string(expected_pos))
.append(", got: ")
.append(std::to_string(pos));
} else {
bool_error_ = true;
}
}
////////////////
// convert implementation
////////////////
template <typename... Ts>
no_void_validator_tup_t<Ts...> convert_impl(const split_input& elems) {
clear_error();
no_void_validator_tup_t<Ts...> ret{};
if (sizeof...(Ts) != elems.size()) {
set_error_number_of_colums(sizeof...(Ts), elems.size());
return ret;
}
return extract_tuple<Ts...>(elems);
}
// do not know how to specialize by return type :(
template <typename... Ts>
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
const split_input& elems, const std::tuple<Ts...>*) {
return convert_impl<Ts...>(elems);
}
////////////////
// substring
////////////////
template <typename Delim>
const split_input& split_impl(const char* const line, Delim delim,
size_t delim_size = 1) {
auto range = substring(line, delim);
input_.push_back(range);
while (range.second[0] != '\0') {
range = substring(range.second + delim_size, delim);
input_.push_back(range);
}
return input_;
}
bool no_match(const char* end, char delim) const {
return *end != delim;
}
bool no_match(const char* end, const std::string& delim) const {
return strncmp(end, delim.c_str(), delim.size()) != 0;
}
template <typename Delim>
string_range substring(const char* const begin, Delim delim) const {
const char* end;
for (end = begin; *end != '\0' && no_match(end, delim); ++end)
;
return string_range{begin, end};
}
////////////////
// conversion
////////////////
template <typename T>
void extract_one(no_validator_t<T>& dst, const string_range msg,
size_t pos) {
if (!valid()) {
return;
}
if (!extract(msg.first, msg.second, dst)) {
set_error_invalid_conversion(msg, pos);
return;
}
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);
} else {
set_error_validate("validation error",
msg, pos);
}
return;
}
}
}
template <size_t ArgN, size_t TupN, typename... Ts>
void extract_multiple(no_void_validator_tup_t<Ts...>& tup,
const split_input& elems) {
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;
if constexpr (not_void) {
if constexpr (one_element) {
extract_one<elem_t>(tup, elems[ArgN], ArgN);
} else {
auto& el = std::get<TupN>(tup);
extract_one<elem_t>(el, elems[ArgN], ArgN);
}
}
if constexpr (sizeof...(Ts) > ArgN + 1) {
constexpr size_t NewTupN = (not_void) ? TupN + 1 : TupN;
extract_multiple<ArgN + 1, NewTupN, Ts...>(tup, elems);
}
}
template <typename... Ts>
no_void_validator_tup_t<Ts...> extract_tuple(const split_input& elems) {
static_assert(!all_of<std::is_void, Ts...>::value,
"at least one parameter must be non void");
no_void_validator_tup_t<Ts...> ret;
extract_multiple<0, 0, Ts...>(ret, elems);
return ret;
}; };
}
//////////////// private:
// members ////////////////
//////////////// // error
////////////////
std::vector<string_range> input_; void clear_error() {
std::string string_error_; string_error_.clear();
bool bool_error_; bool_error_ = false;
enum error_mode error_mode_ { error_mode::String }; }
std::string error_sufix(const string_range msg, size_t pos) const {
std::string error;
error.reserve(32);
error.append("at column ")
.append(std::to_string(pos + 1))
.append(": \'")
.append(msg.first, msg.second)
.append("\'");
return error;
}
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 ")
.append(error_sufix(msg, pos));
} else {
bool_error_ = true;
}
}
void set_error_validate(const char* const error, const string_range msg,
size_t pos) {
if (error_mode_ == error_mode::String) {
string_error_.clear();
string_error_.append(error).append(" ").append(
error_sufix(msg, pos));
} else {
bool_error_ = true;
}
}
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: ")
.append(std::to_string(expected_pos))
.append(", got: ")
.append(std::to_string(pos));
} else {
bool_error_ = true;
}
}
////////////////
// convert implementation
////////////////
template <typename... Ts>
no_void_validator_tup_t<Ts...> convert_impl(const split_input& elems) {
clear_error();
no_void_validator_tup_t<Ts...> ret{};
if (sizeof...(Ts) != elems.size()) {
set_error_number_of_colums(sizeof...(Ts), elems.size());
return ret;
}
return extract_tuple<Ts...>(elems);
}
// do not know how to specialize by return type :(
template <typename... Ts>
no_void_validator_tup_t<std::tuple<Ts...>> convert_impl(
const split_input& elems, const std::tuple<Ts...>*) {
return convert_impl<Ts...>(elems);
}
////////////////
// substring
////////////////
template <typename Delim>
const split_input& split_impl(const char* const line, Delim delim,
size_t delim_size = 1) {
auto range = substring(line, delim);
input_.push_back(range);
while (range.second[0] != '\0') {
range = substring(range.second + delim_size, delim);
input_.push_back(range);
}
return input_;
}
bool no_match(const char* end, char delim) const {
return *end != delim;
}
bool no_match(const char* end, const std::string& delim) const {
return strncmp(end, delim.c_str(), delim.size()) != 0;
}
template <typename Delim>
string_range substring(const char* const begin, Delim delim) const {
const char* end;
for (end = begin; *end != '\0' && no_match(end, delim); ++end)
;
return string_range{begin, end};
}
////////////////
// conversion
////////////////
template <typename T>
void extract_one(no_validator_t<T>& dst, const string_range msg,
size_t pos) {
if (!valid()) {
return;
}
if (!extract(msg.first, msg.second, dst)) {
set_error_invalid_conversion(msg, pos);
return;
}
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);
} else {
set_error_validate("validation error", msg, pos);
}
return;
}
}
}
template <size_t ArgN, size_t TupN, typename... Ts>
void extract_multiple(no_void_validator_tup_t<Ts...>& tup,
const split_input& elems) {
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;
if constexpr (not_void) {
if constexpr (one_element) {
extract_one<elem_t>(tup, elems[ArgN], ArgN);
} else {
auto& el = std::get<TupN>(tup);
extract_one<elem_t>(el, elems[ArgN], ArgN);
}
}
if constexpr (sizeof...(Ts) > ArgN + 1) {
constexpr size_t NewTupN = (not_void) ? TupN + 1 : TupN;
extract_multiple<ArgN + 1, NewTupN, Ts...>(tup, elems);
}
}
template <typename... Ts>
no_void_validator_tup_t<Ts...> extract_tuple(const split_input& elems) {
static_assert(!all_of<std::is_void, Ts...>::value,
"at least one parameter must be non void");
no_void_validator_tup_t<Ts...> ret;
extract_multiple<0, 0, Ts...>(ret, elems);
return ret;
};
////////////////
// members
////////////////
std::vector<string_range> input_;
std::string string_error_;
bool bool_error_;
enum error_mode error_mode_ { error_mode::String };
}; };
template <> template <>
inline void converter::extract_one<std::string>(std::string& dst, inline void converter::extract_one<std::string>(std::string& dst,
const string_range msg, const string_range msg,
size_t) { size_t) {
if (!valid()) { if (!valid()) {
return; return;
} }
extract(msg.first, msg.second, dst); extract(msg.first, msg.second, dst);
} }
} /* ss */ } /* ss */

View File

@ -20,108 +20,108 @@ namespace ss {
//////////////// ////////////////
template <typename T> template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, T> pow10(int n) { std::enable_if_t<std::is_floating_point_v<T>, T> pow10(int n) {
T ret = 1.0; T ret = 1.0;
T r = 10.0; T r = 10.0;
if (n < 0) { if (n < 0) {
n = -n; n = -n;
r = 0.1; r = 0.1;
} }
while (n) { while (n) {
if (n & 1) { if (n & 1) {
ret *= r; ret *= r;
}
r *= r;
n >>= 1;
} }
return ret; r *= r;
n >>= 1;
}
return ret;
} }
template <typename T> template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num( std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
const char* begin, const char* const end) { const char* begin, const char* const end) {
if (begin == end) { if (begin == end) {
return std::nullopt; return std::nullopt;
} }
int sign = 1; int sign = 1;
T int_part = 0.0; T int_part = 0.0;
T frac_part = 0.0; T frac_part = 0.0;
bool has_frac = false; bool has_frac = false;
bool has_exp = false; bool has_exp = false;
// +/- sign // +/- sign
if (*begin == '-') { if (*begin == '-') {
++begin; ++begin;
sign = -1; sign = -1;
}
while (begin != end) {
if (*begin >= '0' && *begin <= '9') {
int_part = int_part * 10 + (*begin - '0');
} else if (*begin == '.') {
has_frac = true;
++begin;
break;
} else if (*begin == 'e') {
has_exp = true;
++begin;
break;
} else {
return std::nullopt;
} }
++begin;
}
if (has_frac) {
T frac_exp = 0.1;
while (begin != end) { while (begin != end) {
if (*begin >= '0' && *begin <= '9') { if (*begin >= '0' && *begin <= '9') {
int_part = int_part * 10 + (*begin - '0'); frac_part += frac_exp * (*begin - '0');
} else if (*begin == '.') { frac_exp *= 0.1;
has_frac = true; } else if (*begin == 'e') {
++begin; has_exp = true;
break;
} else if (*begin == 'e') {
has_exp = true;
++begin;
break;
} else {
return std::nullopt;
}
++begin; ++begin;
} break;
} else {
if (has_frac) {
T frac_exp = 0.1;
while (begin != end) {
if (*begin >= '0' && *begin <= '9') {
frac_part += frac_exp * (*begin - '0');
frac_exp *= 0.1;
} else if (*begin == 'e') {
has_exp = true;
++begin;
break;
} else {
return std::nullopt;
}
++begin;
}
}
// parsing exponent part
T exp_part = 1.0;
if (begin != end && has_exp) {
int exp_sign = 1;
if (*begin == '-') {
exp_sign = -1;
++begin;
} else if (*begin == '+') {
++begin;
}
int e = 0;
while (begin != end && *begin >= '0' && *begin <= '9') {
e = e * 10 + *begin - '0';
++begin;
}
exp_part = pow10<T>(exp_sign * e);
}
if (begin != end) {
return std::nullopt; return std::nullopt;
}
++begin;
}
}
// parsing exponent part
T exp_part = 1.0;
if (begin != end && has_exp) {
int exp_sign = 1;
if (*begin == '-') {
exp_sign = -1;
++begin;
} else if (*begin == '+') {
++begin;
} }
return sign * (int_part + frac_part) * exp_part; int e = 0;
while (begin != end && *begin >= '0' && *begin <= '9') {
e = e * 10 + *begin - '0';
++begin;
}
exp_part = pow10<T>(exp_sign * e);
}
if (begin != end) {
return std::nullopt;
}
return sign * (int_part + frac_part) * exp_part;
} }
inline std::optional<short> from_char(char c) { inline std::optional<short> from_char(char c) {
if (c >= '0' && c <= '9') { if (c >= '0' && c <= '9') {
return c - '0'; return c - '0';
} }
return std::nullopt; return std::nullopt;
} }
#if defined(__clang__) || defined(__GNUC__) || defined(__GUNG__) #if defined(__clang__) || defined(__GNUC__) || defined(__GUNG__)
@ -130,38 +130,38 @@ inline std::optional<short> from_char(char c) {
//////////////// ////////////////
template <typename T> template <typename T>
bool mul_overflow(T& result, T operand) { bool mul_overflow(T& result, T operand) {
return __builtin_mul_overflow(result, operand, &result); return __builtin_mul_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(int& result, int operand) { inline bool mul_overflow(int& result, int operand) {
return __builtin_smul_overflow(result, operand, &result); return __builtin_smul_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(long& result, long operand) { inline bool mul_overflow(long& result, long operand) {
return __builtin_smull_overflow(result, operand, &result); return __builtin_smull_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(long long& result, long long operand) { inline bool mul_overflow(long long& result, long long operand) {
return __builtin_smulll_overflow(result, operand, &result); return __builtin_smulll_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(unsigned int& result, unsigned int operand) { inline bool mul_overflow(unsigned int& result, unsigned int operand) {
return __builtin_umul_overflow(result, operand, &result); return __builtin_umul_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(unsigned long& result, unsigned long operand) { inline bool mul_overflow(unsigned long& result, unsigned long operand) {
return __builtin_umull_overflow(result, operand, &result); return __builtin_umull_overflow(result, operand, &result);
} }
template <> template <>
inline bool mul_overflow(unsigned long long& result, inline bool mul_overflow(unsigned long long& result,
unsigned long long operand) { unsigned long long operand) {
return __builtin_umulll_overflow(result, operand, &result); return __builtin_umulll_overflow(result, operand, &result);
} }
//////////////// ////////////////
@ -170,38 +170,38 @@ inline bool mul_overflow(unsigned long long& result,
template <typename T> template <typename T>
inline bool add_overflow(T& result, T operand) { inline bool add_overflow(T& result, T operand) {
return __builtin_add_overflow(result, operand, &result); return __builtin_add_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(int& result, int operand) { inline bool add_overflow(int& result, int operand) {
return __builtin_sadd_overflow(result, operand, &result); return __builtin_sadd_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(long& result, long operand) { inline bool add_overflow(long& result, long operand) {
return __builtin_saddl_overflow(result, operand, &result); return __builtin_saddl_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(long long& result, long long operand) { inline bool add_overflow(long long& result, long long operand) {
return __builtin_saddll_overflow(result, operand, &result); return __builtin_saddll_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(unsigned int& result, unsigned int operand) { inline bool add_overflow(unsigned int& result, unsigned int operand) {
return __builtin_uadd_overflow(result, operand, &result); return __builtin_uadd_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(unsigned long& result, unsigned long operand) { inline bool add_overflow(unsigned long& result, unsigned long operand) {
return __builtin_uaddl_overflow(result, operand, &result); return __builtin_uaddl_overflow(result, operand, &result);
} }
template <> template <>
inline bool add_overflow(unsigned long long& result, inline bool add_overflow(unsigned long long& result,
unsigned long long operand) { unsigned long long operand) {
return __builtin_uaddll_overflow(result, operand, &result); return __builtin_uaddll_overflow(result, operand, &result);
} }
//////////////// ////////////////
@ -209,62 +209,61 @@ inline bool add_overflow(unsigned long long& result,
//////////////// ////////////////
template <typename T> template <typename T>
inline bool sub_overflow(T& result, T operand) { inline bool sub_overflow(T& result, T operand) {
return __builtin_sub_overflow(result, operand, &result); return __builtin_sub_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(int& result, int operand) { inline bool sub_overflow(int& result, int operand) {
return __builtin_ssub_overflow(result, operand, &result); return __builtin_ssub_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(long& result, long operand) { inline bool sub_overflow(long& result, long operand) {
return __builtin_ssubl_overflow(result, operand, &result); return __builtin_ssubl_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(long long& result, long long operand) { inline bool sub_overflow(long long& result, long long operand) {
return __builtin_ssubll_overflow(result, operand, &result); return __builtin_ssubll_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(unsigned int& result, unsigned int operand) { inline bool sub_overflow(unsigned int& result, unsigned int operand) {
return __builtin_usub_overflow(result, operand, &result); return __builtin_usub_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(unsigned long& result, unsigned long operand) { inline bool sub_overflow(unsigned long& result, unsigned long operand) {
return __builtin_usubl_overflow(result, operand, &result); return __builtin_usubl_overflow(result, operand, &result);
} }
template <> template <>
inline bool sub_overflow(unsigned long long& result, inline bool sub_overflow(unsigned long long& result,
unsigned long long operand) { unsigned long long operand) {
return __builtin_usubll_overflow(result, operand, &result); return __builtin_usubll_overflow(result, operand, &result);
} }
template <typename T, typename F> template <typename T, typename F>
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) { bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) {
if (mul_overflow<T>(value, 10) || if (mul_overflow<T>(value, 10) || add_last_digit_owerflow(value, digit)) {
add_last_digit_owerflow(value, digit)) { return true;
return true; }
} return false;
return false;
} }
#else #else
#warning "use clang or gcc!!!" #warning "use clang or gcc!!!"
template <typename T, typename U> template <typename T, typename U>
bool shift_and_add_overflow(T& value, T digit, U is_negative) { bool shift_and_add_overflow(T& value, T digit, U is_negative) {
digit = (is_negative) ? -digit : digit; digit = (is_negative) ? -digit : digit;
T old_value = value; T old_value = value;
value = 10 * value + digit; value = 10 * value + digit;
T expected_old_value = (value - digit) / 10; T expected_old_value = (value - digit) / 10;
if (old_value != expected_old_value) { if (old_value != expected_old_value) {
return true; return true;
} }
return false; return false;
} }
#endif #endif
@ -272,35 +271,34 @@ bool shift_and_add_overflow(T& value, T digit, U is_negative) {
template <typename T> template <typename T>
std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num( std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
const char* begin, const char* end) { const char* begin, const char* end) {
if (begin == end) { if (begin == end) {
return std::nullopt; return std::nullopt;
} }
bool is_negative = false; bool is_negative = false;
if constexpr (std::is_signed_v<T>) { if constexpr (std::is_signed_v<T>) {
is_negative = *begin == '-'; is_negative = *begin == '-';
if (is_negative) { if (is_negative) {
++begin; ++begin;
}
} }
}
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
auto add_last_digit_owerflow = auto add_last_digit_owerflow =
(is_negative) ? sub_overflow<T> : add_overflow<T>; (is_negative) ? sub_overflow<T> : add_overflow<T>;
#else #else
auto add_last_digit_owerflow = is_negative; auto add_last_digit_owerflow = is_negative;
#endif #endif
T value = 0; T value = 0;
for (auto i = begin; i != end; ++i) { for (auto i = begin; i != end; ++i) {
if (auto digit = from_char(*i); if (auto digit = from_char(*i);
!digit || !digit || shift_and_add_overflow<T>(value, digit.value(),
shift_and_add_overflow<T>(value, digit.value(), add_last_digit_owerflow)) {
add_last_digit_owerflow)) { return std::nullopt;
return std::nullopt;
}
} }
}
return value; return value;
} }
//////////////// ////////////////
@ -310,7 +308,7 @@ std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
namespace error { namespace error {
template <typename T> template <typename T>
struct unsupported_type { struct unsupported_type {
constexpr static bool value = false; constexpr static bool value = false;
}; };
} /* namespace */ } /* namespace */
@ -320,51 +318,51 @@ std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T> &&
!is_instance_of<T, std::variant>::value, !is_instance_of<T, std::variant>::value,
bool> bool>
extract(const char*, const char*, T&) { extract(const char*, const char*, T&) {
static_assert(error::unsupported_type<T>::value, static_assert(error::unsupported_type<T>::value,
"Conversion for given type is not defined, an " "Conversion for given type is not defined, an "
"\'extract\' function needs to be defined!"); "\'extract\' function needs to be defined!");
} }
template <typename T> template <typename T>
std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, bool> std::enable_if_t<std::is_integral_v<T> || std::is_floating_point_v<T>, bool>
extract(const char* begin, const char* end, T& value) { extract(const char* begin, const char* end, T& value) {
auto optional_value = to_num<T>(begin, end); auto optional_value = to_num<T>(begin, end);
if (!optional_value) { if (!optional_value) {
return false; return false;
} }
value = optional_value.value(); value = optional_value.value();
return true; return true;
} }
template <typename T> template <typename T>
std::enable_if_t<is_instance_of<T, std::optional>::value, bool> extract( std::enable_if_t<is_instance_of<T, std::optional>::value, bool> extract(
const char* begin, const char* end, T& value) { const char* begin, const char* end, T& value) {
typename T::value_type raw_value; typename T::value_type raw_value;
if (extract(begin, end, raw_value)) { if (extract(begin, end, raw_value)) {
value = raw_value; value = raw_value;
} else { } else {
value = std::nullopt; value = std::nullopt;
} }
return true; return true;
} }
template <typename T, size_t I> template <typename T, size_t I>
bool extract_variant(const char* begin, const char* end, T& value) { bool extract_variant(const char* begin, const char* end, T& value) {
using IthType = std::variant_alternative_t<I, std::decay_t<T>>; using IthType = std::variant_alternative_t<I, std::decay_t<T>>;
IthType ithValue; IthType ithValue;
if (extract<IthType>(begin, end, ithValue)) { if (extract<IthType>(begin, end, ithValue)) {
value = ithValue; value = ithValue;
return true; return true;
} else if constexpr (I + 1 < std::variant_size_v<T>) { } else if constexpr (I + 1 < std::variant_size_v<T>) {
return extract_variant<T, I + 1>(begin, end, value); return extract_variant<T, I + 1>(begin, end, value);
} }
return false; return false;
} }
template <typename T> template <typename T>
std::enable_if_t<is_instance_of<T, std::variant>::value, bool> extract( std::enable_if_t<is_instance_of<T, std::variant>::value, bool> extract(
const char* begin, const char* end, T& value) { const char* begin, const char* end, T& value) {
return extract_variant<T, 0>(begin, end, value); return extract_variant<T, 0>(begin, end, value);
} }
//////////////// ////////////////
@ -373,38 +371,38 @@ std::enable_if_t<is_instance_of<T, std::variant>::value, bool> extract(
template <> template <>
inline bool extract(const char* begin, const char* end, bool& value) { inline bool extract(const char* begin, const char* end, bool& value) {
if (end == begin + 1) { if (end == begin + 1) {
if (*begin == '1') { if (*begin == '1') {
value = true; value = true;
} else if (*begin == '0') { } else if (*begin == '0') {
value = false; value = false;
} else {
return false;
}
} else { } else {
size_t size = end - begin; return false;
if (size == 4 && strncmp(begin, "true", size) == 0) {
value = true;
} else if (size == 5 && strncmp(begin, "false", size) == 0) {
value = false;
} else {
return false;
}
} }
} else {
size_t size = end - begin;
if (size == 4 && strncmp(begin, "true", size) == 0) {
value = true;
} else if (size == 5 && strncmp(begin, "false", size) == 0) {
value = false;
} else {
return false;
}
}
return true; return true;
} }
template <> template <>
inline bool extract(const char* begin, const char* end, char& value) { inline bool extract(const char* begin, const char* end, char& value) {
value = *begin; value = *begin;
return (end == begin + 1); return (end == begin + 1);
} }
template <> template <>
inline bool extract(const char* begin, const char* end, std::string& value) { inline bool extract(const char* begin, const char* end, std::string& value) {
value = std::string(begin, end); value = std::string(begin, end);
return true; return true;
} }
} /* ss */ } /* ss */

View File

@ -12,13 +12,13 @@ namespace ss {
template <size_t N, typename T, typename... Ts> template <size_t N, typename T, typename... Ts>
struct decayed_arg_n { struct decayed_arg_n {
static_assert(N - 1 != sizeof...(Ts), "index out of range"); static_assert(N - 1 != sizeof...(Ts), "index out of range");
using type = typename decayed_arg_n<N - 1, Ts...>::type; using type = typename decayed_arg_n<N - 1, Ts...>::type;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct decayed_arg_n<0, T, Ts...> { struct decayed_arg_n<0, T, Ts...> {
using type = std::decay_t<T>; using type = std::decay_t<T>;
}; };
template <typename T> template <typename T>
@ -26,12 +26,12 @@ struct function_traits;
template <typename R, typename C, typename Arg> template <typename R, typename C, typename Arg>
struct function_traits<std::function<R(C&, const Arg&) const>> { struct function_traits<std::function<R(C&, const Arg&) const>> {
using arg_type = Arg; using arg_type = Arg;
}; };
template <typename R, typename... Ts> template <typename R, typename... Ts>
struct function_traits<R(Ts...)> { struct function_traits<R(Ts...)> {
using arg0 = typename decayed_arg_n<0, Ts...>::type; using arg0 = typename decayed_arg_n<0, Ts...>::type;
}; };
template <typename R, typename... Ts> template <typename R, typename... Ts>
@ -54,7 +54,7 @@ struct member_wrapper;
template <typename R, typename T> template <typename R, typename T>
struct member_wrapper<R T::*> { struct member_wrapper<R T::*> {
using arg_type = typename function_traits<R>::arg0; using arg_type = typename function_traits<R>::arg0;
}; };
//////////////// ////////////////
@ -62,19 +62,19 @@ struct member_wrapper<R T::*> {
//////////////// ////////////////
#define INIT_HAS_METHOD(method) \ #define INIT_HAS_METHOD(method) \
template <typename T> \ template <typename T> \
class has_m_##method { \ class has_m_##method { \
template <typename C> \ template <typename C> \
static std::true_type test(decltype(&C::method)); \ static std::true_type test(decltype(&C::method)); \
\ \
template <typename C> \ template <typename C> \
static std::false_type test(...); \ static std::false_type test(...); \
\ \
public: \ public: \
constexpr static bool value = decltype(test<T>(0))::value; \ constexpr static bool value = decltype(test<T>(0))::value; \
}; \ }; \
\ \
template <typename T> \ template <typename T> \
constexpr bool has_m_##method##_t = has_m_##method<T>::value; constexpr bool has_m_##method##_t = has_m_##method<T>::value;
} /* trait */ } /* trait */

View File

@ -16,344 +16,328 @@ template <typename...>
class composite; class composite;
class parser { class parser {
public:
parser(const std::string& file_name, const std::string& delimiter)
: file_name_{file_name}, delim_{delimiter},
file_{fopen(file_name_.c_str(), "rb")} {
if (file_) {
read_line();
} else {
set_error_file_not_open();
eof_ = true;
}
}
~parser() {
fclose(file_);
}
bool valid() const {
return (error_mode_ == error_mode::String) ? string_error_.empty()
: bool_error_ == false;
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
converter_.set_error_mode(mode);
}
const std::string& error_msg() const {
return string_error_;
}
bool eof() const {
return eof_;
}
bool ignore_next() {
return buff_.read(file_);
}
template <typename T, typename... Ts>
T get_object() {
return to_object<T>(get_next<Ts...>());
}
template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() {
buff_.update();
clear_error();
if (eof_) {
set_error_eof_reached();
return {};
}
split_input_ = converter_.split(buff_.get(), delim_);
auto value = converter_.convert<T, Ts...>(split_input_);
if (!converter_.valid()) {
set_error_invalid_conversion();
}
read_line();
return value;
}
////////////////
// composite conversion
////////////////
template <typename... Ts>
class composite {
public: public:
parser(const std::string& file_name, const std::string& delimiter) composite(std::tuple<Ts...>&& values, parser& parser)
: file_name_{file_name}, delim_{delimiter}, : values_{std::move(values)}, parser_{parser} {
file_{fopen(file_name_.c_str(), "rb")} {
if (file_) {
read_line();
} else {
set_error_file_not_open();
eof_ = true;
}
} }
~parser() { // tries to convert the same line with a different output type
fclose(file_); // only if the previous conversion was not successful,
} // returns composite containing itself and the new output
// as optional, additionally, if a parameter is passed, and
bool valid() const { // that parameter can be invoked using the converted value,
return (error_mode_ == error_mode::String) // than it will be invoked in the case of a valid conversion
? string_error_.empty() template <typename... Us, typename Fun = None>
: bool_error_ == false; composite<Ts..., std::optional<no_void_validator_tup_t<Us...>>> or_else(
}
void set_error_mode(error_mode mode) {
error_mode_ = mode;
converter_.set_error_mode(mode);
}
const std::string& error_msg() const {
return string_error_;
}
bool eof() const {
return eof_;
}
bool ignore_next() {
return buff_.read(file_);
}
template <typename T, typename... Ts>
T get_object() {
return to_object<T>(get_next<Ts...>());
}
template <typename T, typename... Ts>
no_void_validator_tup_t<T, Ts...> get_next() {
buff_.update();
clear_error();
if (eof_) {
set_error_eof_reached();
return {};
}
split_input_ = converter_.split(buff_.get(), delim_);
auto value = converter_.convert<T, Ts...>(split_input_);
if (!converter_.valid()) {
set_error_invalid_conversion();
}
read_line();
return value;
}
////////////////
// composite conversion
////////////////
template <typename... Ts>
class composite {
public:
composite(std::tuple<Ts...>&& values, parser& parser)
: values_{std::move(values)}, parser_{parser} {
}
// tries to convert the same line with a different output type
// only if the previous conversion was not successful,
// returns composite containing itself and the new output
// as optional, additionally, if a parameter is passed, and
// 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{}) {
using Value = no_void_validator_tup_t<Us...>;
std::optional<Value> value;
try_convert_and_invoke<Value, Us...>(value, fun);
return composite_with(std::move(value));
}
// 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{}) {
std::optional<U> value;
try_convert_and_invoke<U, Us...>(value, fun);
return composite_with(std::move(value));
}
std::tuple<Ts...> values() {
return values_;
}
template <typename Fun>
auto on_error(Fun&& fun) {
if (!parser_.valid()) {
if constexpr (std::is_invocable_v<Fun>) {
fun();
} else {
std::invoke(std::forward<Fun>(fun),
parser_.error_msg());
}
}
return *this;
}
private:
template <typename T>
composite<Ts..., T> composite_with(T&& new_value) {
auto merged_values =
std::tuple_cat(std::move(values_),
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) {
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));
} else {
new_value = std::move(tuple_output);
}
if (parser_.valid()) {
value = std::move(new_value);
parser_.try_invoke(*value,
std::forward<Fun>(
fun));
}
}
}
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_);
if (!parser_.converter_.valid()) {
parser_.set_error_invalid_conversion();
}
return value;
}
std::tuple<Ts...> values_;
parser& parser_;
};
// tries to convert a line and returns a composite which is
// able to try additional conversions in case of failure
template <typename... Ts, typename Fun = None>
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
Fun&& fun = None{}) { Fun&& fun = None{}) {
std::optional<no_void_validator_tup_t<Ts...>> value; using Value = no_void_validator_tup_t<Us...>;
auto new_value = get_next<Ts...>(); std::optional<Value> value;
if (valid()) { try_convert_and_invoke<Value, Us...>(value, fun);
value = std::move(new_value); return composite_with(std::move(value));
try_invoke(*value, std::forward<Fun>(fun)); }
// 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{}) {
std::optional<U> value;
try_convert_and_invoke<U, Us...>(value, fun);
return composite_with(std::move(value));
}
std::tuple<Ts...> values() {
return values_;
}
template <typename Fun>
auto on_error(Fun&& fun) {
if (!parser_.valid()) {
if constexpr (std::is_invocable_v<Fun>) {
fun();
} else {
std::invoke(std::forward<Fun>(fun), parser_.error_msg());
} }
return {std::move(value), *this}; }
}; return *this;
}
private: private:
template <typename...> template <typename T>
friend class composite; composite<Ts..., T> composite_with(T&& new_value) {
auto merged_values =
// tries to invoke the given function (see below), if the function std::tuple_cat(std::move(values_),
// returns a value which can be used as a conditional, and it returns std::tuple{std::forward<T>(new_value)});
// false, the function sets an error, and allows the invoke of the return {std::move(merged_values), parser_};
// next possible conversion as if the validation of the current one
// 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>;
if constexpr (!is_none) {
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))) {
set_error_failed_check();
}
} else {
try_invoke_impl(arg, std::forward<Fun>(fun));
}
}
} }
// tries to invoke the function if not None template <typename U, typename... Us, typename Fun = None>
// it first tries to invoke the function without arguments, void try_convert_and_invoke(std::optional<U>& value, Fun&& fun) {
// than with one argument if the function accepts the whole tuple if (!parser_.valid()) {
// as an argument, and finally tries to invoke it with the tuple std::optional<U> new_value;
// laid out as a parameter pack auto tuple_output = try_same<Us...>();
template <typename Arg, typename Fun = None> if constexpr (!std::is_same_v<U, decltype(tuple_output)>) {
auto try_invoke_impl(Arg&& arg, Fun&& fun) { new_value = to_object<U>(std::move(tuple_output));
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();
} else if constexpr (std::is_invocable_v<Fun, Arg>) {
return std::invoke(std::forward<Fun>(fun),
std::forward<Arg>(arg));
} else {
return std::apply(std::forward<Fun>(fun),
std::forward<Arg>(arg));
}
}
}
////////////////
// line reading
////////////////
class buffer {
char* buffer_{nullptr};
char* new_buffer_{nullptr};
size_t size_{0};
public:
~buffer() {
free(buffer_);
free(new_buffer_);
}
bool read(FILE* file) {
ssize_t size = getline(&new_buffer_, &size_, file);
size_t string_end = size - 1;
if (size == -1) {
return false;
}
if (size >= 2 && new_buffer_[size - 2] == '\r') {
string_end--;
}
new_buffer_[string_end] = '\0';
return true;
}
const char* get() const {
return buffer_;
}
void update() {
std::swap(buffer_, new_buffer_);
}
};
void read_line() {
eof_ = !buff_.read(file_);
++line_number_;
}
////////////////
// error
////////////////
void clear_error() {
string_error_.clear();
bool_error_ = false;
}
void set_error_failed_check() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" failed check.");
} else { } else {
bool_error_ = true; new_value = std::move(tuple_output);
} }
if (parser_.valid()) {
value = std::move(new_value);
parser_.try_invoke(*value, std::forward<Fun>(fun));
}
}
} }
void set_error_file_not_open() { template <typename U, typename... Us>
if (error_mode_ == error_mode::String) { no_void_validator_tup_t<U, Us...> try_same() {
string_error_.append(file_name_) parser_.clear_error();
.append(" could not be not open."); auto value =
} else { parser_.converter_.convert<U, Us...>(parser_.split_input_);
bool_error_ = true; if (!parser_.converter_.valid()) {
} parser_.set_error_invalid_conversion();
}
return value;
} }
void set_error_eof_reached() { std::tuple<Ts...> values_;
if (error_mode_ == error_mode::String) { parser& parser_;
string_error_.append(file_name_) };
.append(" reached end of file.");
} else { // tries to convert a line and returns a composite which is
bool_error_ = true; // able to try additional conversions in case of failure
template <typename... Ts, typename Fun = None>
composite<std::optional<no_void_validator_tup_t<Ts...>>> try_next(
Fun&& fun = None{}) {
std::optional<no_void_validator_tup_t<Ts...>> value;
auto new_value = get_next<Ts...>();
if (valid()) {
value = std::move(new_value);
try_invoke(*value, std::forward<Fun>(fun));
}
return {std::move(value), *this};
};
private:
template <typename...>
friend class composite;
// tries to invoke the given function (see below), if the function
// returns a value which can be used as a conditional, and it returns
// false, the function sets an error, and allows the invoke of the
// next possible conversion as if the validation of the current one
// 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>;
if constexpr (!is_none) {
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))) {
set_error_failed_check();
} }
} else {
try_invoke_impl(arg, std::forward<Fun>(fun));
}
}
}
// tries to invoke the function if not None
// it first tries to invoke the function without arguments,
// than with one argument if the function accepts the whole tuple
// as an argument, and finally tries to invoke it with the tuple
// 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>;
if constexpr (!is_none) {
if constexpr (std::is_invocable_v<Fun>) {
return fun();
} else if constexpr (std::is_invocable_v<Fun, Arg>) {
return std::invoke(std::forward<Fun>(fun),
std::forward<Arg>(arg));
} else {
return std::apply(std::forward<Fun>(fun),
std::forward<Arg>(arg));
}
}
}
////////////////
// line reading
////////////////
class buffer {
char* buffer_{nullptr};
char* new_buffer_{nullptr};
size_t size_{0};
public:
~buffer() {
free(buffer_);
free(new_buffer_);
} }
void set_error_invalid_conversion() { bool read(FILE* file) {
if (error_mode_ == error_mode::String) { ssize_t size = getline(&new_buffer_, &size_, file);
string_error_.append(file_name_) size_t string_end = size - 1;
.append(" ")
.append(std::to_string(line_number_)) if (size == -1) {
.append(": ") return false;
.append(converter_.error_msg()) }
.append(": \"")
.append(buff_.get()) if (size >= 2 && new_buffer_[size - 2] == '\r') {
.append("\""); string_end--;
} else { }
bool_error_ = true;
} new_buffer_[string_end] = '\0';
return true;
} }
//////////////// const char* get() const {
// members return buffer_;
//////////////// }
const std::string file_name_; void update() {
const std::string delim_; std::swap(buffer_, new_buffer_);
std::string string_error_; }
bool bool_error_; };
error_mode error_mode_{error_mode::String};
converter converter_; void read_line() {
converter::split_input split_input_; eof_ = !buff_.read(file_);
FILE* file_{nullptr}; ++line_number_;
buffer buff_; }
size_t line_number_{0};
bool eof_{false}; ////////////////
// error
////////////////
void clear_error() {
string_error_.clear();
bool_error_ = false;
}
void set_error_failed_check() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_).append(" failed check.");
} else {
bool_error_ = true;
}
}
void set_error_file_not_open() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_).append(" could not be not open.");
} else {
bool_error_ = true;
}
}
void set_error_eof_reached() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_).append(" reached end of file.");
} else {
bool_error_ = true;
}
}
void set_error_invalid_conversion() {
if (error_mode_ == error_mode::String) {
string_error_.append(file_name_)
.append(" ")
.append(std::to_string(line_number_))
.append(": ")
.append(converter_.error_msg())
.append(": \"")
.append(buff_.get())
.append("\"");
} else {
bool_error_ = true;
}
}
////////////////
// members
////////////////
const std::string file_name_;
const std::string delim_;
std::string string_error_;
bool bool_error_;
error_mode error_mode_{error_mode::String};
converter converter_;
converter::split_input split_input_;
FILE* file_{nullptr};
buffer buff_;
size_t line_number_{0};
bool eof_{false};
}; };
} /* ss */ } /* ss */

View File

@ -8,23 +8,23 @@ namespace ss {
template <typename T, auto... Values> template <typename T, auto... Values>
struct ax { struct ax {
private: private:
template <auto X, auto... Xs> template <auto X, auto... Xs>
bool ss_valid_impl(const T& x) const { bool ss_valid_impl(const T& x) const {
if constexpr (sizeof...(Xs) != 0) { if constexpr (sizeof...(Xs) != 0) {
return x != X && ss_valid_impl<Xs...>(x); return x != X && ss_valid_impl<Xs...>(x);
}
return x != X;
} }
return x != X;
}
public: public:
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return ss_valid_impl<Values...>(value); return ss_valid_impl<Values...>(value);
} }
const char* error() const { const char* error() const {
return "value excluded"; return "value excluded";
} }
}; };
//////////////// ////////////////
@ -33,23 +33,23 @@ struct ax {
template <typename T, auto... Values> template <typename T, auto... Values>
struct nx { struct nx {
private: private:
template <auto X, auto... Xs> template <auto X, auto... Xs>
bool ss_valid_impl(const T& x) const { bool ss_valid_impl(const T& x) const {
if constexpr (sizeof...(Xs) != 0) { if constexpr (sizeof...(Xs) != 0) {
return x == X || ss_valid_impl<Xs...>(x); return x == X || ss_valid_impl<Xs...>(x);
}
return x == X;
} }
return x == X;
}
public: public:
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return ss_valid_impl<Values...>(value); return ss_valid_impl<Values...>(value);
} }
const char* error() const { const char* error() const {
return "value excluded"; return "value excluded";
} }
}; };
//////////////// ////////////////
@ -58,13 +58,13 @@ struct nx {
template <typename T, auto Min, auto Max> template <typename T, auto Min, auto Max>
struct ir { struct ir {
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return value >= Min && value <= Max; return value >= Min && value <= Max;
} }
const char* error() const { const char* error() const {
return "out of range"; return "out of range";
} }
}; };
//////////////// ////////////////
@ -73,13 +73,13 @@ struct ir {
template <typename T, auto Min, auto Max> template <typename T, auto Min, auto Max>
struct oor { struct oor {
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return value < Min || value > Max; return value < Min || value > Max;
} }
const char* error() const { const char* error() const {
return "in restricted range"; return "in restricted range";
} }
}; };
//////////////// ////////////////
@ -88,13 +88,13 @@ struct oor {
template <typename T> template <typename T>
struct ne { struct ne {
bool ss_valid(const T& value) const { bool ss_valid(const T& value) const {
return !value.empty(); return !value.empty();
} }
const char* error() const { const char* error() const {
return "empty field"; return "empty field";
} }
}; };
} /* ss */ } /* ss */

View File

@ -14,12 +14,12 @@ struct tup_cat;
template <typename... Ts, typename... Us> template <typename... Ts, typename... Us>
struct tup_cat<std::tuple<Ts...>, std::tuple<Us...>> { struct tup_cat<std::tuple<Ts...>, std::tuple<Us...>> {
using type = std::tuple<Ts..., Us...>; using type = std::tuple<Ts..., Us...>;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct tup_cat<T, std::tuple<Ts...>> { struct tup_cat<T, std::tuple<Ts...>> {
using type = std::tuple<T, Ts...>; using type = std::tuple<T, Ts...>;
}; };
template <typename... Ts> template <typename... Ts>
@ -34,14 +34,14 @@ struct left_of_impl;
template <size_t N, typename T, typename... Ts> template <size_t N, typename T, typename... Ts>
struct left_of_impl { struct left_of_impl {
static_assert(N < 128, "recursion limit reached"); static_assert(N < 128, "recursion limit reached");
static_assert(N != 0, "cannot take the whole tuple"); static_assert(N != 0, "cannot take the whole tuple");
using type = tup_cat_t<T, typename left_of_impl<N - 1, Ts...>::type>; using type = tup_cat_t<T, typename left_of_impl<N - 1, Ts...>::type>;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct left_of_impl<0, T, Ts...> { struct left_of_impl<0, T, Ts...> {
using type = std::tuple<T>; using type = std::tuple<T>;
}; };
template <size_t N, typename... Ts> template <size_t N, typename... Ts>
@ -62,12 +62,12 @@ struct right_of_impl;
template <size_t N, typename T, typename... Ts> template <size_t N, typename T, typename... Ts>
struct right_of_impl { struct right_of_impl {
using type = typename right_of_impl<N - 1, Ts...>::type; using type = typename right_of_impl<N - 1, Ts...>::type;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct right_of_impl<0, T, Ts...> { struct right_of_impl<0, T, Ts...> {
using type = std::tuple<T, Ts...>; using type = std::tuple<T, Ts...>;
}; };
template <size_t N, typename... Ts> template <size_t N, typename... Ts>
@ -88,19 +88,19 @@ struct apply_trait;
template <template <typename...> class Trait, typename T, typename... Ts> template <template <typename...> class Trait, typename T, typename... Ts>
struct apply_trait<Trait, std::tuple<T, Ts...>> { struct apply_trait<Trait, std::tuple<T, Ts...>> {
using type = using type =
tup_cat_t<typename Trait<T>::type, tup_cat_t<typename Trait<T>::type,
typename apply_trait<Trait, std::tuple<Ts...>>::type>; typename apply_trait<Trait, std::tuple<Ts...>>::type>;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_trait { struct apply_trait {
using type = std::tuple<typename Trait<T>::type>; using type = std::tuple<typename Trait<T>::type>;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_trait<Trait, std::tuple<T>> { struct apply_trait<Trait, std::tuple<T>> {
using type = std::tuple<typename Trait<T>::type>; using type = std::tuple<typename Trait<T>::type>;
}; };
//////////////// ////////////////
@ -113,12 +113,12 @@ struct optional_trait;
template <typename U> template <typename U>
struct optional_trait<std::true_type, U> { struct optional_trait<std::true_type, U> {
using type = U; using type = U;
}; };
template <typename U> template <typename U>
struct optional_trait<std::false_type, U> { struct optional_trait<std::false_type, U> {
using type = std::false_type; using type = std::false_type;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
@ -126,21 +126,21 @@ struct apply_optional_trait;
template <template <typename...> class Trait, typename T, typename... Ts> template <template <typename...> class Trait, typename T, typename... Ts>
struct apply_optional_trait<Trait, std::tuple<T, Ts...>> { struct apply_optional_trait<Trait, std::tuple<T, Ts...>> {
using type = tup_cat_t< using type = tup_cat_t<
typename optional_trait<typename Trait<T>::type, T>::type, typename optional_trait<typename Trait<T>::type, T>::type,
typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>; typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_optional_trait { struct apply_optional_trait {
using type = std::tuple< using type =
typename optional_trait<typename Trait<T>::type, T>::type>; std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct apply_optional_trait<Trait, std::tuple<T>> { struct apply_optional_trait<Trait, std::tuple<T>> {
using type = std::tuple< using type =
typename optional_trait<typename Trait<T>::type, T>::type>; std::tuple<typename optional_trait<typename Trait<T>::type, T>::type>;
}; };
//////////////// ////////////////
@ -149,37 +149,37 @@ struct apply_optional_trait<Trait, std::tuple<T>> {
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct remove_false { struct remove_false {
using type = tup_cat_t<T, typename remove_false<Ts...>::type>; using type = tup_cat_t<T, typename remove_false<Ts...>::type>;
}; };
template <typename... Ts> template <typename... Ts>
struct remove_false<std::false_type, Ts...> { struct remove_false<std::false_type, Ts...> {
using type = typename remove_false<Ts...>::type; using type = typename remove_false<Ts...>::type;
}; };
template <typename T, typename... Ts> template <typename T, typename... Ts>
struct remove_false<std::tuple<T, Ts...>> { struct remove_false<std::tuple<T, Ts...>> {
using type = tup_cat_t<T, typename remove_false<Ts...>::type>; using type = tup_cat_t<T, typename remove_false<Ts...>::type>;
}; };
template <typename... Ts> template <typename... Ts>
struct remove_false<std::tuple<std::false_type, Ts...>> { struct remove_false<std::tuple<std::false_type, Ts...>> {
using type = typename remove_false<Ts...>::type; using type = typename remove_false<Ts...>::type;
}; };
template <typename T> template <typename T>
struct remove_false<T> { struct remove_false<T> {
using type = std::tuple<T>; using type = std::tuple<T>;
}; };
template <typename T> template <typename T>
struct remove_false<std::tuple<T>> { struct remove_false<std::tuple<T>> {
using type = std::tuple<T>; using type = std::tuple<T>;
}; };
template <> template <>
struct remove_false<std::false_type> { struct remove_false<std::false_type> {
using type = std::tuple<>; using type = std::tuple<>;
}; };
//////////////// ////////////////
@ -188,8 +188,8 @@ struct remove_false<std::false_type> {
template <template <typename...> class Trait> template <template <typename...> class Trait>
struct negate_impl { struct negate_impl {
template <typename... Ts> template <typename... Ts>
using type = std::integral_constant<bool, !Trait<Ts...>::value>; using type = std::integral_constant<bool, !Trait<Ts...>::value>;
}; };
//////////////// ////////////////
@ -198,14 +198,14 @@ struct negate_impl {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct filter_if { struct filter_if {
using type = typename filter_if<Trait, std::tuple<Ts...>>::type; using type = typename filter_if<Trait, std::tuple<Ts...>>::type;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct filter_if<Trait, std::tuple<Ts...>> { struct filter_if<Trait, std::tuple<Ts...>> {
using type = typename remove_false<typename apply_optional_trait< using type = typename remove_false<
Trait, std::tuple<Ts...>>::type>::type; typename apply_optional_trait<Trait, std::tuple<Ts...>>::type>::type;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
@ -213,14 +213,14 @@ using filter_if_t = typename filter_if<Trait, Ts...>::type;
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct filter_not { struct filter_not {
using type = typename filter_not<Trait, std::tuple<Ts...>>::type; using type = typename filter_not<Trait, std::tuple<Ts...>>::type;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct filter_not<Trait, std::tuple<Ts...>> { struct filter_not<Trait, std::tuple<Ts...>> {
using type = typename remove_false<typename apply_optional_trait< using type = typename remove_false<typename apply_optional_trait<
negate_impl<Trait>::template type, std::tuple<Ts...>>::type>::type; negate_impl<Trait>::template type, std::tuple<Ts...>>::type>::type;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
@ -235,13 +235,13 @@ struct count;
template <template <typename...> class Trait, typename T, typename... Ts> template <template <typename...> class Trait, typename T, typename... Ts>
struct count { struct count {
static constexpr size_t size = static constexpr size_t size =
std::tuple_size<filter_if_t<Trait, T, Ts...>>::value; std::tuple_size<filter_if_t<Trait, T, Ts...>>::value;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct count<Trait, T> { struct count<Trait, T> {
static constexpr size_t size = Trait<T>::value; static constexpr size_t size = Trait<T>::value;
}; };
//////////////// ////////////////
@ -253,13 +253,13 @@ struct count;
template <template <typename...> class Trait, typename T, typename... Ts> template <template <typename...> class Trait, typename T, typename... Ts>
struct count_not { struct count_not {
static constexpr size_t size = static constexpr size_t size =
std::tuple_size<filter_not_t<Trait, T, Ts...>>::value; std::tuple_size<filter_not_t<Trait, T, Ts...>>::value;
}; };
template <template <typename...> class Trait, typename T> template <template <typename...> class Trait, typename T>
struct count_not<Trait, T> { struct count_not<Trait, T> {
static constexpr size_t size = !Trait<T>::value; static constexpr size_t size = !Trait<T>::value;
}; };
//////////////// ////////////////
@ -268,14 +268,12 @@ struct count_not<Trait, T> {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct all_of { struct all_of {
static constexpr bool value = static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
count<Trait, Ts...>::size == sizeof...(Ts);
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct all_of<Trait, std::tuple<Ts...>> { struct all_of<Trait, std::tuple<Ts...>> {
static constexpr bool value = static constexpr bool value = count<Trait, Ts...>::size == sizeof...(Ts);
count<Trait, Ts...>::size == sizeof...(Ts);
}; };
//////////////// ////////////////
@ -284,14 +282,14 @@ struct all_of<Trait, std::tuple<Ts...>> {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct any_of { struct any_of {
static_assert(sizeof...(Ts) > 0); static_assert(sizeof...(Ts) > 0);
static constexpr bool value = count<Trait, Ts...>::size > 0; static constexpr bool value = count<Trait, Ts...>::size > 0;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct any_of<Trait, std::tuple<Ts...>> { struct any_of<Trait, std::tuple<Ts...>> {
static_assert(sizeof...(Ts) > 0); static_assert(sizeof...(Ts) > 0);
static constexpr bool value = count<Trait, Ts...>::size > 0; static constexpr bool value = count<Trait, Ts...>::size > 0;
}; };
//////////////// ////////////////
@ -300,12 +298,12 @@ struct any_of<Trait, std::tuple<Ts...>> {
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct none_of { struct none_of {
static constexpr bool value = count<Trait, Ts...>::size == 0; static constexpr bool value = count<Trait, Ts...>::size == 0;
}; };
template <template <typename...> class Trait, typename... Ts> template <template <typename...> class Trait, typename... Ts>
struct none_of<Trait, std::tuple<Ts...>> { struct none_of<Trait, std::tuple<Ts...>> {
static constexpr bool value = count<Trait, Ts...>::size == 0; static constexpr bool value = count<Trait, Ts...>::size == 0;
}; };
//////////////// ////////////////
@ -314,12 +312,12 @@ struct none_of<Trait, std::tuple<Ts...>> {
template <typename T, template <typename...> class Template> template <typename T, template <typename...> class Template>
struct is_instance_of { struct is_instance_of {
constexpr static bool value = false; constexpr static bool value = false;
}; };
template <typename ...Ts, template <typename...> class Template> template <typename... Ts, template <typename...> class Template>
struct is_instance_of<Template<Ts...>, Template> { struct is_instance_of<Template<Ts...>, Template> {
constexpr static bool value = true; constexpr static bool value = true;
}; };
//////////////// ////////////////
@ -328,14 +326,14 @@ struct is_instance_of<Template<Ts...>, Template> {
template <class S, std::size_t... Is, class Tup> template <class S, std::size_t... Is, class Tup>
S to_object(std::index_sequence<Is...>, Tup&& tup) { S to_object(std::index_sequence<Is...>, Tup&& tup) {
return {std::get<Is>(std::forward<Tup>(tup))...}; return {std::get<Is>(std::forward<Tup>(tup))...};
} }
template <class S, class Tup> template <class S, class Tup>
S to_object(Tup&& tup) { S to_object(Tup&& tup) {
using T = std::remove_reference_t<Tup>; using T = std::remove_reference_t<Tup>;
return to_object<S>(std::make_index_sequence<std::tuple_size<T>{}>{}, return to_object<S>(std::make_index_sequence<std::tuple_size<T>{}>{},
std::forward<Tup>(tup)); std::forward<Tup>(tup));
} }
} /* trait */ } /* trait */

View File

@ -4,9 +4,9 @@
#include <algorithm> #include <algorithm>
TEST_CASE("testing split") { TEST_CASE("testing split") {
ss::converter c; ss::converter c;
for (const auto& [s, expected, delim] : for (const auto& [s, expected, delim] :
// clang-format off // clang-format off
{std::tuple{"a,b,c,d", std::vector{"a", "b", "c", "d"}, ","}, {std::tuple{"a,b,c,d", std::vector{"a", "b", "c", "d"}, ","},
{"", {}, " "}, {"", {}, " "},
{"a,b,c", {"a", "b", "c"}, ""}, {"a,b,c", {"a", "b", "c"}, ""},
@ -14,270 +14,265 @@ TEST_CASE("testing split") {
{"a::b::c::d", {"a", "b", "c", "d"}, "::"}, {"a::b::c::d", {"a", "b", "c", "d"}, "::"},
{"x\t-\ty", {"x", "y"}, "\t-\t"}, {"x\t-\ty", {"x", "y"}, "\t-\t"},
{"x", {"x"}, ","}} // clang-format on {"x", {"x"}, ","}} // clang-format on
) { ) {
auto split = c.split(s, delim); auto split = c.split(s, delim);
CHECK(split.size() == expected.size()); CHECK(split.size() == expected.size());
for (size_t i = 0; i < split.size(); ++i) { for (size_t i = 0; i < split.size(); ++i) {
auto s = std::string(split[i].first, split[i].second); auto s = std::string(split[i].first, split[i].second);
CHECK(s == expected[i]); CHECK(s == expected[i]);
}
} }
}
} }
TEST_CASE("testing valid conversions") { TEST_CASE("testing valid conversions") {
ss::converter c; ss::converter c;
{ {
auto tup = c.convert<int>("5"); auto tup = c.convert<int>("5");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = c.convert<int, void>("5,junk"); auto tup = c.convert<int, void>("5,junk");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = c.convert<void, int>("junk,5"); auto tup = c.convert<void, int>("junk,5");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n"); auto tup = c.convert<int, void, void>("5\njunk\njunk", "\n");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = c.convert<void, int, void>("junk 5 junk", " "); auto tup = c.convert<void, int, void>("junk 5 junk", " ");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t"); auto tup = c.convert<void, void, int>("junk\tjunk\t5", "\t");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 5); CHECK(tup == 5);
} }
{ {
auto tup = auto tup =
c.convert<void, void, std::optional<int>>("junk\tjunk\t5", c.convert<void, void, std::optional<int>>("junk\tjunk\t5", "\t");
"\t"); REQUIRE(c.valid());
REQUIRE(c.valid()); REQUIRE(tup.has_value());
REQUIRE(tup.has_value()); CHECK(tup == 5);
CHECK(tup == 5); }
} {
{ auto tup = c.convert<int, double, void>("5,6.6,junk");
auto tup = c.convert<int, double, void>("5,6.6,junk"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{5, 6.6});
CHECK(tup == std::tuple{5, 6.6}); }
} {
{ auto tup = c.convert<int, void, double>("5,junk,6.6");
auto tup = c.convert<int, void, double>("5,junk,6.6"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{5, 6.6});
CHECK(tup == std::tuple{5, 6.6}); }
} {
{ auto tup = c.convert<void, int, double>("junk;5;6.6", ";");
auto tup = c.convert<void, int, double>("junk;5;6.6", ";"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{5, 6.6});
CHECK(tup == std::tuple{5, 6.6}); }
} {
{ auto tup =
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());
REQUIRE(c.valid()); CHECK(tup == std::tuple{5, 6.6});
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", ";");
auto tup = REQUIRE(c.valid());
c.convert<void, std::optional<int>, double>("junk;5.4;6.6", REQUIRE(!std::get<0>(tup).has_value());
";"); CHECK(tup == std::tuple{std::nullopt, 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>, REQUIRE(c.valid());
double>("junk;5;6.6", ";"); REQUIRE(std::holds_alternative<int>(std::get<0>(tup)));
REQUIRE(c.valid()); CHECK(tup == std::tuple{std::variant<int, double>{5}, 6.6});
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(c.valid()); REQUIRE(std::holds_alternative<double>(std::get<0>(tup)));
REQUIRE(std::holds_alternative<double>(std::get<0>(tup))); CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6});
CHECK(tup == std::tuple{std::variant<int, double>{5.5}, 6.6}); }
}
} }
TEST_CASE("testing invalid conversions") { TEST_CASE("testing invalid conversions") {
ss::converter c; ss::converter c;
c.convert<int>(""); c.convert<int>("");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int, void>(""); c.convert<int, void>("");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int, void>(",junk"); c.convert<int, void>(",junk");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, int>("junk,"); c.convert<void, int>("junk,");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int>("x"); c.convert<int>("x");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int, void>("x"); c.convert<int, void>("x");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int, void>("x,junk"); c.convert<int, void>("x,junk");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, int>("junk,x"); c.convert<void, int>("junk,x");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";"); c.convert<void, std::variant<int, double>, double>("junk;.5.5;6", ";");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
} }
TEST_CASE("testing ss:ax restriction (all except)") { TEST_CASE("testing ss:ax restriction (all except)") {
ss::converter c; ss::converter c;
c.convert<ss::ax<int, 0>>("0"); c.convert<ss::ax<int, 0>>("0");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::ax<int, 0, 1, 2>>("1"); c.convert<ss::ax<int, 0, 1, 2>>("1");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1"); c.convert<void, char, ss::ax<int, 0, 1, 2>>("junk,c,1");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::ax<int, 1>, char>("1,c"); c.convert<ss::ax<int, 1>, char>("1,c");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
{ {
int tup = c.convert<ss::ax<int, 1>>("3"); int tup = c.convert<ss::ax<int, 1>>("3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 3); CHECK(tup == 3);
} }
{ {
std::tuple<char, int> tup = std::tuple<char, int> tup = c.convert<char, ss::ax<int, 1>>("c,3");
c.convert<char, ss::ax<int, 1>>("c,3"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{'c', 3});
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 = REQUIRE(c.valid());
c.convert<ss::ax<int, 1>, char>("3,c"); CHECK(tup == std::tuple{3, 'c'});
REQUIRE(c.valid()); }
CHECK(tup == std::tuple{3, 'c'});
}
} }
TEST_CASE("testing ss:nx restriction (none except)") { TEST_CASE("testing ss:nx restriction (none except)") {
ss::converter c; ss::converter c;
c.convert<ss::nx<int, 1>>("3"); c.convert<ss::nx<int, 1>>("3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<char, ss::nx<int, 1, 2, 69>>("c,3"); c.convert<char, ss::nx<int, 1, 2, 69>>("c,3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::nx<int, 1>, char>("3,c"); c.convert<ss::nx<int, 1>, char>("3,c");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
{ {
auto tup = c.convert<ss::nx<int, 3>>("3"); auto tup = c.convert<ss::nx<int, 3>>("3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 3); CHECK(tup == 3);
} }
{ {
auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2"); auto tup = c.convert<ss::nx<int, 0, 1, 2>>("2");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 2); CHECK(tup == 2);
} }
{ {
auto tup = auto tup = c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1");
c.convert<char, void, ss::nx<int, 0, 1, 2>>("c,junk,1"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{'c', 1});
CHECK(tup == std::tuple{'c', 1}); }
} {
{ auto tup = c.convert<ss::nx<int, 1>, char>("1,c");
auto tup = c.convert<ss::nx<int, 1>, char>("1,c"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{1, 'c'});
CHECK(tup == std::tuple{1, 'c'}); }
}
} }
TEST_CASE("testing ss:ir restriction (in range)") { TEST_CASE("testing ss:ir restriction (in range)") {
ss::converter c; ss::converter c;
c.convert<ss::ir<int, 0, 2>>("3"); c.convert<ss::ir<int, 0, 2>>("3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<char, ss::ir<int, 4, 69>>("c,3"); c.convert<char, ss::ir<int, 4, 69>>("c,3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::ir<int, 1, 2>, char>("3,c"); c.convert<ss::ir<int, 1, 2>, char>("3,c");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
{ {
auto tup = c.convert<ss::ir<int, 1, 5>>("3"); auto tup = c.convert<ss::ir<int, 1, 5>>("3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 3); CHECK(tup == 3);
} }
{ {
auto tup = c.convert<ss::ir<int, 0, 2>>("2"); auto tup = c.convert<ss::ir<int, 0, 2>>("2");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 2); CHECK(tup == 2);
} }
{ {
auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1"); auto tup = c.convert<char, void, ss::ir<int, 0, 1>>("c,junk,1");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{'c', 1}); CHECK(tup == std::tuple{'c', 1});
} }
{ {
auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c"); auto tup = c.convert<ss::ir<int, 1, 20>, char>("1,c");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{1, 'c'}); CHECK(tup == std::tuple{1, 'c'});
} }
} }
TEST_CASE("testing ss:oor restriction (out of range)") { TEST_CASE("testing ss:oor restriction (out of range)") {
ss::converter c; ss::converter c;
c.convert<ss::oor<int, 1, 5>>("3"); c.convert<ss::oor<int, 1, 5>>("3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::oor<int, 0, 2>>("2"); c.convert<ss::oor<int, 0, 2>>("2");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk"); c.convert<char, ss::oor<int, 0, 1>, void>("c,1,junk");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::oor<int, 1, 20>, char>("1,c"); c.convert<ss::oor<int, 1, 20>, char>("1,c");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
{ {
auto tup = c.convert<ss::oor<int, 0, 2>>("3"); auto tup = c.convert<ss::oor<int, 0, 2>>("3");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == 3); CHECK(tup == 3);
} }
{ {
auto tup = auto tup = c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3");
c.convert<char, void, ss::oor<int, 4, 69>>("c,junk,3"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{'c', 3});
CHECK(tup == std::tuple{'c', 3}); }
}
{ {
auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c"); auto tup = c.convert<ss::oor<int, 1, 2>, char>("3,c");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == std::tuple{3, 'c'}); CHECK(tup == std::tuple{3, 'c'});
} }
} }
const std::vector<int> extracted_vector = {1, 2, 3}; const std::vector<int> extracted_vector = {1, 2, 3};
@ -286,45 +281,44 @@ const std::vector<int> extracted_vector = {1, 2, 3};
template <> template <>
inline bool ss::extract(const char* begin, const char* end, inline bool ss::extract(const char* begin, const char* end,
std::vector<int>& value) { std::vector<int>& value) {
if (begin == end) { if (begin == end) {
return false; return false;
} }
value = extracted_vector; value = extracted_vector;
return true; return true;
} }
TEST_CASE("testing ss:ne restriction (not empty)") { TEST_CASE("testing ss:ne restriction (not empty)") {
ss::converter c; ss::converter c;
c.convert<ss::ne<std::string>>(""); c.convert<ss::ne<std::string>>("");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<int, ss::ne<std::string>>("3,"); c.convert<int, ss::ne<std::string>>("3,");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::ne<std::string>, int>(",3"); c.convert<ss::ne<std::string>, int>(",3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<void, ss::ne<std::string>, int>("junk,,3"); c.convert<void, ss::ne<std::string>, int>("junk,,3");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
c.convert<ss::ne<std::vector<int>>>(""); c.convert<ss::ne<std::vector<int>>>("");
REQUIRE(!c.valid()); REQUIRE(!c.valid());
{ {
auto tup = c.convert<ss::ne<std::string>>("s"); auto tup = c.convert<ss::ne<std::string>>("s");
REQUIRE(c.valid()); REQUIRE(c.valid());
CHECK(tup == "s"); CHECK(tup == "s");
} }
{ {
auto tup = auto tup = c.convert<std::optional<int>, ss::ne<std::string>>("1,s");
c.convert<std::optional<int>, ss::ne<std::string>>("1,s"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == std::tuple{1, "s"});
CHECK(tup == std::tuple{1, "s"}); }
} {
{ auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}");
auto tup = c.convert<ss::ne<std::vector<int>>>("{1 2 3}"); REQUIRE(c.valid());
REQUIRE(c.valid()); CHECK(tup == extracted_vector);
CHECK(tup == extracted_vector); }
}
} }

View File

@ -7,59 +7,58 @@ constexpr auto eps = 0.000001;
using ld = long double; using ld = long double;
#define CHECK_FLOATING_CONVERSION(input, type) \ #define CHECK_FLOATING_CONVERSION(input, type) \
{ \ { \
std::string s = #input; \ std::string s = #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()); \ REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(input)) < eps); \ CHECK(std::abs(t.value() - type(input)) < eps); \
} \ } \
{ \ { \
/* check negative too */ \ /* check negative too */ \
auto s = std::string("-") + #input; \ 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()); \ REQUIRE(t.has_value()); \
CHECK(std::abs(t.value() - type(-input)) < eps); \ CHECK(std::abs(t.value() - type(-input)) < eps); \
} }
TEST_CASE("testing extract functions for floating point values") { TEST_CASE("testing extract functions for floating point values") {
CHECK_FLOATING_CONVERSION(123.456, float); CHECK_FLOATING_CONVERSION(123.456, float);
CHECK_FLOATING_CONVERSION(123.456, double); CHECK_FLOATING_CONVERSION(123.456, double);
CHECK_FLOATING_CONVERSION(123.456, ld); CHECK_FLOATING_CONVERSION(123.456, ld);
CHECK_FLOATING_CONVERSION(69, float); CHECK_FLOATING_CONVERSION(69, float);
CHECK_FLOATING_CONVERSION(69, double); CHECK_FLOATING_CONVERSION(69, double);
CHECK_FLOATING_CONVERSION(69, ld); CHECK_FLOATING_CONVERSION(69, ld);
CHECK_FLOATING_CONVERSION(420., float); CHECK_FLOATING_CONVERSION(420., float);
CHECK_FLOATING_CONVERSION(420., double); CHECK_FLOATING_CONVERSION(420., double);
CHECK_FLOATING_CONVERSION(420., ld); CHECK_FLOATING_CONVERSION(420., ld);
CHECK_FLOATING_CONVERSION(0.123, float); CHECK_FLOATING_CONVERSION(0.123, float);
CHECK_FLOATING_CONVERSION(0.123, double); CHECK_FLOATING_CONVERSION(0.123, double);
CHECK_FLOATING_CONVERSION(0.123, ld); CHECK_FLOATING_CONVERSION(0.123, ld);
CHECK_FLOATING_CONVERSION(123e4, float); CHECK_FLOATING_CONVERSION(123e4, float);
CHECK_FLOATING_CONVERSION(123e4, double); CHECK_FLOATING_CONVERSION(123e4, double);
CHECK_FLOATING_CONVERSION(123e4, ld); CHECK_FLOATING_CONVERSION(123e4, ld);
} }
#define CHECK_DECIMAL_CONVERSION(input, type) \ #define CHECK_DECIMAL_CONVERSION(input, type) \
{ \ { \
std::string s = #input; \ std::string s = #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()); \ REQUIRE(t.has_value()); \
CHECK(t.value() == type(input)); \ CHECK(t.value() == type(input)); \
} \
{ \
/* 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()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \ } \
{ \ }
/* 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()); \
REQUIRE(t.has_value()); \
CHECK(t.value() == type(-input)); \
} \
}
using us = unsigned short; using us = unsigned short;
using ui = unsigned int; using ui = unsigned int;
@ -68,266 +67,251 @@ using ll = long long;
using ull = unsigned long long; using ull = unsigned long long;
TEST_CASE("testing extract functions for decimal values") { TEST_CASE("testing extract functions for decimal values") {
CHECK_DECIMAL_CONVERSION(1234, short); CHECK_DECIMAL_CONVERSION(1234, short);
CHECK_DECIMAL_CONVERSION(1234, us); CHECK_DECIMAL_CONVERSION(1234, us);
CHECK_DECIMAL_CONVERSION(1234, int); CHECK_DECIMAL_CONVERSION(1234, int);
CHECK_DECIMAL_CONVERSION(1234, ui); CHECK_DECIMAL_CONVERSION(1234, ui);
CHECK_DECIMAL_CONVERSION(1234, long); CHECK_DECIMAL_CONVERSION(1234, long);
CHECK_DECIMAL_CONVERSION(1234, ul); CHECK_DECIMAL_CONVERSION(1234, ul);
CHECK_DECIMAL_CONVERSION(1234, ll); CHECK_DECIMAL_CONVERSION(1234, ll);
CHECK_DECIMAL_CONVERSION(1234567891011, ull); CHECK_DECIMAL_CONVERSION(1234567891011, ull);
} }
#define CHECK_INVALID_CONVERSION(input, type) \ #define CHECK_INVALID_CONVERSION(input, type) \
{ \ { \
std::string s = input; \ std::string s = 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()); \
CHECK(!t.has_value()); \ CHECK(!t.has_value()); \
} }
TEST_CASE("testing extract functions for numbers with invalid inputs") { TEST_CASE("testing extract functions for numbers with invalid inputs") {
// negative unsigned value // negative unsigned value
CHECK_INVALID_CONVERSION("-1234", ul); CHECK_INVALID_CONVERSION("-1234", ul);
// floating pint for int // floating pint for int
CHECK_INVALID_CONVERSION("123.4", int); CHECK_INVALID_CONVERSION("123.4", int);
// random input for float // random input for float
CHECK_INVALID_CONVERSION("xxx1", float); CHECK_INVALID_CONVERSION("xxx1", float);
// random input for int // random input for int
CHECK_INVALID_CONVERSION("xxx1", int); CHECK_INVALID_CONVERSION("xxx1", int);
// empty field for int // empty field for int
CHECK_INVALID_CONVERSION("", int); CHECK_INVALID_CONVERSION("", int);
} }
#define CHECK_OUT_OF_RANGE_CONVERSION(type) \ #define CHECK_OUT_OF_RANGE_CONVERSION(type) \
{ \ { \
std::string s = \ std::string s = std::to_string(std::numeric_limits<type>::max()); \
std::to_string(std::numeric_limits<type>::max()); \ 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()); \ CHECK(t.has_value()); \
CHECK(t.has_value()); \ for (auto& i : s) { \
for (auto& i : s) { \ if (i != '9' && i != '.') { \
if (i != '9' && i != '.') { \ i = '9'; \
i = '9'; \ break; \
break; \ } \
} \
} \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
} \ } \
{ \ t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
std::string s = \ CHECK(!t.has_value()); \
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()); \ std::string s = std::to_string(std::numeric_limits<type>::min()); \
for (auto& i : s) { \ auto t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
if (std::is_signed_v<type> && i != '9' && i != '.') { \ CHECK(t.has_value()); \
i = '9'; \ for (auto& i : s) { \
break; \ if (std::is_signed_v<type> && i != '9' && i != '.') { \
} else if (std::is_unsigned_v<type>) { \ i = '9'; \
s = "-1"; \ break; \
break; \ } else if (std::is_unsigned_v<type>) { \
} \ s = "-1"; \
} \ break; \
t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \ } \
CHECK(!t.has_value()); \ } \
} t = ss::to_num<type>(s.c_str(), s.c_str() + s.size()); \
CHECK(!t.has_value()); \
}
TEST_CASE("testing extract functions for numbers with out of range inputs") { TEST_CASE("testing extract functions for numbers with out of range inputs") {
CHECK_OUT_OF_RANGE_CONVERSION(short); CHECK_OUT_OF_RANGE_CONVERSION(short);
CHECK_OUT_OF_RANGE_CONVERSION(us); CHECK_OUT_OF_RANGE_CONVERSION(us);
CHECK_OUT_OF_RANGE_CONVERSION(int); CHECK_OUT_OF_RANGE_CONVERSION(int);
CHECK_OUT_OF_RANGE_CONVERSION(ui); CHECK_OUT_OF_RANGE_CONVERSION(ui);
CHECK_OUT_OF_RANGE_CONVERSION(long); CHECK_OUT_OF_RANGE_CONVERSION(long);
CHECK_OUT_OF_RANGE_CONVERSION(ul); CHECK_OUT_OF_RANGE_CONVERSION(ul);
CHECK_OUT_OF_RANGE_CONVERSION(ll); CHECK_OUT_OF_RANGE_CONVERSION(ll);
CHECK_OUT_OF_RANGE_CONVERSION(ull); CHECK_OUT_OF_RANGE_CONVERSION(ull);
} }
TEST_CASE("testing extract functions for boolean values") { TEST_CASE("testing extract functions for boolean values") {
for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"}, for (const auto& [b, s] : {std::pair<bool, std::string>{true, "1"},
{false, "0"}, {false, "0"},
{true, "true"}, {true, "true"},
{false, "false"}}) { {false, "false"}}) {
bool v; bool v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == b); CHECK(v == b);
} }
for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) { for (const std::string& s : {"2", "tru", "truee", "xxx", ""}) {
bool v; bool v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
} }
} }
TEST_CASE("testing extract functions for char values") { TEST_CASE("testing extract functions for char values") {
for (const auto& [c, s] : for (const auto& [c, s] :
{std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) { {std::pair<char, std::string>{'a', "a"}, {'x', "x"}, {' ', " "}}) {
char v; char v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
CHECK(v == c); CHECK(v == c);
} }
for (const std::string& s : {"aa", "xxx", ""}) { for (const std::string& s : {"aa", "xxx", ""}) {
char v; char v;
REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(!ss::extract(s.c_str(), s.c_str() + s.size(), v));
} }
} }
TEST_CASE("testing extract functions for std::optional") { TEST_CASE("testing extract functions for std::optional") {
for (const auto& [i, s] : for (const auto& [i, s] :
{std::pair<std::optional<int>, std::string>{1, "1"}, {std::pair<std::optional<int>, std::string>{1, "1"},
{69, "69"}, {69, "69"},
{-4, "-4"}}) { {-4, "-4"}}) {
std::optional<int> v; std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value()); REQUIRE(v.has_value());
CHECK(*v == i); CHECK(*v == i);
} }
for (const auto& [c, s] : for (const auto& [c, s] :
{std::pair<std::optional<char>, std::string>{'a', "a"}, {std::pair<std::optional<char>, std::string>{'a', "a"},
{'x', "x"}, {'x', "x"},
{' ', " "}}) { {' ', " "}}) {
std::optional<char> v; std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(v.has_value()); REQUIRE(v.has_value());
CHECK(*v == c); CHECK(*v == c);
} }
for (const std::string& s : {"aa", "xxx", ""}) { for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<int> v; std::optional<int> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value()); REQUIRE(!v.has_value());
} }
for (const std::string& s : {"aa", "xxx", ""}) { for (const std::string& s : {"aa", "xxx", ""}) {
std::optional<char> v; std::optional<char> v;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v)); REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), v));
REQUIRE(!v.has_value()); REQUIRE(!v.has_value());
} }
} }
#define REQUIRE_VARIANT(var, el, type) \ #define REQUIRE_VARIANT(var, el, type) \
{ \ { \
auto ptr = std::get_if<type>(&var); \ auto ptr = std::get_if<type>(&var); \
REQUIRE(ptr); \ REQUIRE(ptr); \
REQUIRE(el == *ptr); \ REQUIRE(el == *ptr); \
} }
#define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative<type>(var)); #define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative<type>(var));
TEST_CASE("testing extract functions for std::variant") { TEST_CASE("testing extract functions for std::variant") {
{
std::string s = "22";
{ {
std::string s = "22"; std::variant<int, double, std::string> var;
{ REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
std::variant<int, double, std::string> var; CHECK_NOT_VARIANT(var, double);
REQUIRE( CHECK_NOT_VARIANT(var, std::string);
ss::extract(s.c_str(), s.c_str() + s.size(), var)); REQUIRE_VARIANT(var, 22, int);
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));
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));
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_VARIANT(var, 22, int);
}
} }
{ {
std::string s = "22.2"; std::variant<double, int, std::string> var;
{ REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
std::variant<int, double, std::string> var; CHECK_NOT_VARIANT(var, int);
REQUIRE( CHECK_NOT_VARIANT(var, std::string);
ss::extract(s.c_str(), s.c_str() + s.size(), var)); REQUIRE_VARIANT(var, 22, double);
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));
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));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
}
} }
{ {
std::string s = "2.2.2"; std::variant<std::string, double, int> var;
{ REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
std::variant<int, double, std::string> var; CHECK_NOT_VARIANT(var, int);
REQUIRE( CHECK_NOT_VARIANT(var, double);
ss::extract(s.c_str(), s.c_str() + s.size(), var)); REQUIRE_VARIANT(var, "22", std::string);
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));
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));
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_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_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_VARIANT(var, int{}, int);
}
} }
{
std::variant<int> var;
REQUIRE(ss::extract(s.c_str(), s.c_str() + s.size(), var));
REQUIRE_VARIANT(var, 22, int);
}
}
{
std::string s = "22.2";
{
std::variant<int, double, std::string> 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));
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));
CHECK_NOT_VARIANT(var, int);
CHECK_NOT_VARIANT(var, double);
REQUIRE_VARIANT(var, "22.2", std::string);
}
}
{
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));
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));
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));
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_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_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_VARIANT(var, int{}, int);
}
}
} }

View File

@ -5,172 +5,170 @@
#include <filesystem> #include <filesystem>
struct unique_file_name { struct unique_file_name {
const std::string name; const std::string name;
unique_file_name() : name{std::tmpnam(nullptr)} { unique_file_name() : name{std::tmpnam(nullptr)} {
} }
~unique_file_name() { ~unique_file_name() {
std::filesystem::remove(name); std::filesystem::remove(name);
} }
}; };
struct X { struct X {
constexpr static auto delim = ","; constexpr static auto delim = ",";
int i; int i;
double d; double d;
std::string s; std::string s;
std::string to_string() const { std::string to_string() const {
return std::to_string(i) return std::to_string(i)
.append(delim) .append(delim)
.append(std::to_string(d)) .append(std::to_string(d))
.append(delim) .append(delim)
.append(s); .append(s);
} }
auto tied() const { auto tied() const {
return std::tie(i, d, s); return std::tie(i, d, s);
} }
}; };
template <typename T> template <typename T>
std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs, std::enable_if_t<ss::has_m_tied_t<T>, bool> operator==(const T& lhs,
const T& rhs) { const T& rhs) {
return lhs.tied() == rhs.tied(); return lhs.tied() == rhs.tied();
} }
template <typename T> template <typename T>
static void make_and_write(const std::string& file_name, static void make_and_write(const std::string& file_name,
const std::vector<T>& data) { const std::vector<T>& data) {
std::ofstream out{file_name}; std::ofstream out{file_name};
std::vector<const char*> new_lines = {"\n", "\r\n"}; std::vector<const char*> new_lines = {"\n", "\r\n"};
for (size_t i = 0; i < data.size(); ++i) { for (size_t i = 0; i < data.size(); ++i) {
out << data[i].to_string() << new_lines[i % new_lines.size()]; out << data[i].to_string() << new_lines[i % new_lines.size()];
} }
} }
TEST_CASE("testing parser") { TEST_CASE("testing parser") {
unique_file_name f; unique_file_name f;
std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"}, std::vector<X> data = {{1, 2, "x"}, {3, 4, "y"}, {5, 6, "z"},
{7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}}; {7, 8, "u"}, {9, 10, "v"}, {11, 12, "w"}};
make_and_write(f.name, data); make_and_write(f.name, data);
{ {
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
auto a = p.get_next<int, double, std::string>(); auto a = p.get_next<int, double, std::string>();
i.emplace_back(ss::to_object<X>(a)); i.emplace_back(ss::to_object<X>(a));
}
CHECK(std::equal(i.begin(), i.end(), data.begin()));
} }
{ CHECK(std::equal(i.begin(), i.end(), data.begin()));
ss::parser p{f.name, ","}; }
std::vector<X> i;
p.ignore_next(); {
while (!p.eof()) { ss::parser p{f.name, ","};
using tup = std::tuple<int, double, std::string>; std::vector<X> i;
auto a = p.get_next<tup>();
i.emplace_back(ss::to_object<X>(a));
}
CHECK(std::equal(i.begin(), i.end(), data.begin() + 1)); p.ignore_next();
while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
auto a = p.get_next<tup>();
i.emplace_back(ss::to_object<X>(a));
} }
{ CHECK(std::equal(i.begin(), i.end(), data.begin() + 1));
ss::parser p{f.name, ","}; }
std::vector<X> i;
while (!p.eof()) { {
i.push_back( ss::parser p{f.name, ","};
p.get_object<X, int, double, std::string>()); std::vector<X> i;
}
CHECK(std::equal(i.begin(), i.end(), data.begin())); while (!p.eof()) {
i.push_back(p.get_object<X, int, double, std::string>());
} }
{ CHECK(std::equal(i.begin(), i.end(), data.begin()));
ss::parser p{f.name, ","}; }
std::vector<X> i;
while (!p.eof()) { {
using tup = std::tuple<int, double, std::string>; ss::parser p{f.name, ","};
i.push_back(p.get_object<X, tup>()); std::vector<X> i;
}
CHECK(std::equal(i.begin(), i.end(), data.begin())); while (!p.eof()) {
using tup = std::tuple<int, double, std::string>;
i.push_back(p.get_object<X, tup>());
} }
{ CHECK(std::equal(i.begin(), i.end(), data.begin()));
ss::parser p{f.name, ","}; }
std::vector<X> i;
while (!p.eof()) { {
i.push_back(p.get_next<X>()); ss::parser p{f.name, ","};
} std::vector<X> i;
CHECK(std::equal(i.begin(), i.end(), data.begin())); while (!p.eof()) {
i.push_back(p.get_next<X>());
} }
{ CHECK(std::equal(i.begin(), i.end(), data.begin()));
constexpr int excluded = 3; }
ss::parser p{f.name, ","};
std::vector<X> i;
while (!p.eof()) { {
auto a = p.get_object<X, ss::ax<int, excluded>, double, constexpr int excluded = 3;
std::string>(); ss::parser p{f.name, ","};
if (p.valid()) { std::vector<X> i;
i.push_back(a);
} while (!p.eof()) {
} auto a =
std::vector<X> expected = data; p.get_object<X, ss::ax<int, excluded>, double, std::string>();
std::remove_if(expected.begin(), expected.end(), if (p.valid()) {
[](const X& x) { return x.i == excluded; }); i.push_back(a);
CHECK(std::equal(i.begin(), i.end(), expected.begin())); }
} }
std::vector<X> expected = data;
std::remove_if(expected.begin(), expected.end(),
[](const X& x) { return x.i == excluded; });
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{ {
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
auto a = p.get_object<X, ss::nx<int, 3>, double, auto a = p.get_object<X, ss::nx<int, 3>, double, std::string>();
std::string>(); if (p.valid()) {
if (p.valid()) { i.push_back(a);
i.push_back(a); }
}
}
std::vector<X> expected = {{3, 4, "y"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
} }
std::vector<X> expected = {{3, 4, "y"}};
CHECK(std::equal(i.begin(), i.end(), expected.begin()));
}
{ {
unique_file_name empty_f; unique_file_name empty_f;
std::vector<X> empty_data = {}; std::vector<X> empty_data = {};
make_and_write(empty_f.name, empty_data); make_and_write(empty_f.name, empty_data);
ss::parser p{empty_f.name, ","}; ss::parser p{empty_f.name, ","};
std::vector<X> i; std::vector<X> i;
while (!p.eof()) { while (!p.eof()) {
i.push_back(p.get_next<X>()); i.push_back(p.get_next<X>());
}
CHECK(i.empty());
} }
CHECK(i.empty());
}
} }
using test_tuple = std::tuple<double, char, double>; using test_tuple = std::tuple<double, char, double>;
struct test_struct { struct test_struct {
int i; int i;
double d; double d;
char c; char c;
auto tied() { auto tied() {
return std::tie(i, d, c); return std::tie(i, d, c);
} }
}; };
void expect_test_struct(const test_struct&) { void expect_test_struct(const test_struct&) {
@ -178,269 +176,265 @@ void expect_test_struct(const test_struct&) {
// various scenarios // various scenarios
TEST_CASE("testing composite conversion") { TEST_CASE("testing composite conversion") {
unique_file_name f; unique_file_name f;
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
for (auto& i : {"10,a,11.1", "10,20,11.1", "junk", "10,11.1", for (auto& i : {"10,a,11.1", "10,20,11.1", "junk", "10,11.1",
"1,11.1,a", "junk", "10,junk", "11,junk"}) { "1,11.1,a", "junk", "10,junk", "11,junk"}) {
out << i << std::endl; out << i << std::endl;
}
} }
}
ss::parser p{f.name, ","}; ss::parser p{f.name, ","};
auto fail = [] { FAIL(""); }; auto fail = [] { FAIL(""); };
auto expect_error = [](auto error) { CHECK(!error.empty()); }; auto expect_error = [](auto error) { CHECK(!error.empty()); };
REQUIRE(p.valid());
REQUIRE(!p.eof());
{
constexpr static auto expectedData = std::tuple{10, 'a', 11.1};
auto [d1, d2, d3, d4] =
p.try_next<int, int, double>(fail)
.or_else<test_struct>(fail)
.or_else<int, char, double>(
[](auto&& data) { CHECK(data == expectedData); })
.on_error(fail)
.or_else<test_tuple>(fail)
.values();
REQUIRE(p.valid()); REQUIRE(p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(d3);
REQUIRE(!d4);
CHECK(*d3 == expectedData);
}
{
REQUIRE(!p.eof());
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) {
CHECK(std::tie(i1, i2, d) == expectedData);
})
.on_error(fail)
.or_else_object<test_struct, int, double, char>(fail)
.on_error(fail)
.or_else<test_tuple>(fail)
.on_error(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
CHECK(*d1 == expectedData);
}
{
REQUIRE(!p.eof()); REQUIRE(!p.eof());
{ auto [d1, d2, d3, d4, d5] =
constexpr static auto expectedData = std::tuple{10, 'a', 11.1}; p.try_next<int, int, double>(fail)
.on_error(expect_error)
.or_else_object<test_struct, int, double, char>(fail)
.or_else<test_struct>(fail)
.or_else<test_tuple>(fail)
.or_else<int, char, double>(fail)
.values();
auto [d1, d2, d3, d4] = REQUIRE(!p.valid());
p.try_next<int, int, double>(fail) REQUIRE(!d1);
.or_else<test_struct>(fail) REQUIRE(!d2);
.or_else<int, char, double>( REQUIRE(!d3);
[](auto&& data) { CHECK(data == expectedData); }) REQUIRE(!d4);
.on_error(fail) REQUIRE(!d5);
.or_else<test_tuple>(fail) }
.values();
REQUIRE(p.valid()); {
REQUIRE(!d1); REQUIRE(!p.eof());
REQUIRE(!d2);
REQUIRE(d3);
REQUIRE(!d4);
CHECK(*d3 == expectedData);
}
{ auto [d1, d2] =
REQUIRE(!p.eof()); p.try_next<int, double>([](auto& i, auto& d) {
constexpr static auto expectedData = std::tuple{10, 20, 11.1}; REQUIRE(std::tie(i, d) == std::tuple{10, 11.1});
})
.or_else<int, double>([](auto&, auto&) { FAIL(""); })
.values();
auto [d1, d2, d3, d4] = REQUIRE(p.valid());
p.try_next<int, int, double>( REQUIRE(d1);
[](auto& i1, auto i2, double d) { REQUIRE(!d2);
CHECK(std::tie(i1, i2, d) == expectedData); }
})
.on_error(fail)
.or_else_object<test_struct, int, double, char>(fail)
.on_error(fail)
.or_else<test_tuple>(fail)
.on_error(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(p.valid()); {
REQUIRE(d1); REQUIRE(!p.eof());
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
CHECK(*d1 == expectedData);
}
{ auto [d1, d2] = p.try_next<int, double>([](auto&, auto&) { FAIL(""); })
REQUIRE(!p.eof()); .or_else<test_struct>(expect_test_struct)
.values();
auto [d1, d2, d3, d4, d5] = REQUIRE(p.valid());
p.try_next<int, int, double>(fail) REQUIRE(!d1);
.on_error(expect_error) REQUIRE(d2);
.or_else_object<test_struct, int, double, char>(fail) CHECK(d2->tied() == std::tuple{1, 11.1, 'a'});
.or_else<test_struct>(fail) }
.or_else<test_tuple>(fail)
.or_else<int, char, double>(fail)
.values();
REQUIRE(!p.valid()); {
REQUIRE(!d1); REQUIRE(!p.eof());
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
{ auto [d1, d2, d3, d4, d5] =
REQUIRE(!p.eof()); p.try_next<int, int, double>(fail)
.or_else_object<test_struct, int, double, char>()
.or_else<test_struct>(expect_test_struct)
.or_else<test_tuple>(fail)
.or_else<std::tuple<int, double>>(fail)
.on_error(expect_error)
.values();
auto [d1, d2] = REQUIRE(!p.valid());
p.try_next<int, double>([](auto& i, auto& d) { REQUIRE(!d1);
REQUIRE(std::tie(i, d) == std::tuple{10, 11.1}); REQUIRE(!d2);
}) REQUIRE(!d3);
.or_else<int, double>([](auto&, auto&) { FAIL(""); }) REQUIRE(!d4);
.values(); REQUIRE(!d5);
}
REQUIRE(p.valid()); {
REQUIRE(d1); REQUIRE(!p.eof());
REQUIRE(!d2);
}
{ auto [d1, d2] = p.try_next<int, std::optional<int>>()
REQUIRE(!p.eof()); .on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
auto [d1, d2] = REQUIRE(p.valid());
p.try_next<int, double>([](auto&, auto&) { FAIL(""); }) REQUIRE(d1);
.or_else<test_struct>(expect_test_struct) REQUIRE(!d2);
.values(); CHECK(*d1 == std::tuple{10, std::nullopt});
}
REQUIRE(p.valid()); {
REQUIRE(!d1); REQUIRE(!p.eof());
REQUIRE(d2);
CHECK(d2->tied() == std::tuple{1, 11.1, 'a'});
}
{ auto [d1, d2] = p.try_next<int, std::variant<int, std::string>>()
REQUIRE(!p.eof()); .on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
auto [d1, d2, d3, d4, d5] = REQUIRE(p.valid());
p.try_next<int, int, double>(fail) REQUIRE(d1);
.or_else_object<test_struct, int, double, char>() REQUIRE(!d2);
.or_else<test_struct>(expect_test_struct) CHECK(*d1 == std::tuple{11, std::variant<int, std::string>{"junk"}});
.or_else<test_tuple>(fail) }
.or_else<std::tuple<int, double>>(fail)
.on_error(expect_error)
.values();
REQUIRE(!p.valid());
REQUIRE(!d1);
REQUIRE(!d2);
REQUIRE(!d3);
REQUIRE(!d4);
REQUIRE(!d5);
}
{
REQUIRE(!p.eof());
auto [d1, d2] = p.try_next<int, std::optional<int>>()
.on_error(fail)
.or_else<std::tuple<int, std::string>>(fail)
.on_error(fail)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 == std::tuple{10, std::nullopt});
}
{
REQUIRE(!p.eof());
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)
.values();
REQUIRE(p.valid());
REQUIRE(d1);
REQUIRE(!d2);
CHECK(*d1 ==
std::tuple{11, std::variant<int, std::string>{"junk"}});
}
} }
size_t move_called = 0; size_t move_called = 0;
struct my_string { struct my_string {
char* data{nullptr}; char* data{nullptr};
my_string() = default; my_string() = default;
~my_string() { ~my_string() {
delete[] data; delete[] data;
} }
// make sure no object is copied // make sure no object is copied
my_string(const my_string&) = delete; my_string(const my_string&) = delete;
my_string& operator=(const my_string&) = delete; my_string& operator=(const my_string&) = delete;
my_string(my_string&& other) : data{other.data} { my_string(my_string&& other) : data{other.data} {
move_called++; move_called++;
other.data = nullptr; other.data = nullptr;
} }
my_string& operator=(my_string&& other) { my_string& operator=(my_string&& other) {
move_called++; move_called++;
data = other.data; data = other.data;
return *this; return *this;
} }
}; };
template <> template <>
inline bool ss::extract(const char* begin, const char* end, my_string& s) { inline bool ss::extract(const char* begin, const char* end, my_string& s) {
size_t size = end - begin; size_t size = end - begin;
s.data = new char[size + 1]; s.data = new char[size + 1];
strncpy(s.data, begin, size); strncpy(s.data, begin, size);
s.data[size] = '\0'; s.data[size] = '\0';
return true; return true;
} }
struct xyz { struct xyz {
my_string x; my_string x;
my_string y; my_string y;
my_string z; my_string z;
auto tied() { auto tied() {
return std::tie(x, y, z); return std::tie(x, y, z);
} }
}; };
TEST_CASE("testing the moving of parsed values") { TEST_CASE("testing the moving of parsed values") {
size_t move_called_one_col; size_t move_called_one_col;
{
unique_file_name f;
{
std::ofstream out{f.name};
out << "x" << std::endl;
}
ss::parser p{f.name, ","};
auto x = p.get_next<my_string>();
CHECK(move_called < 3);
move_called_one_col = move_called;
move_called = 0;
}
{
unique_file_name f; unique_file_name f;
{ {
std::ofstream out{f.name}; std::ofstream out{f.name};
out << "a,b,c" << std::endl; out << "x" << std::endl;
} }
{ ss::parser p{f.name, ","};
auto x = p.get_next<my_string>();
CHECK(move_called < 3);
move_called_one_col = move_called;
move_called = 0;
}
ss::parser p{f.name, ","}; unique_file_name f;
auto x = p.get_next<my_string, my_string, my_string>(); {
CHECK(move_called <= 3 * move_called_one_col); std::ofstream out{f.name};
move_called = 0; out << "a,b,c" << std::endl;
} }
{ {
ss::parser p{f.name, ","};
auto x = p.get_object<xyz, my_string, my_string, my_string>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
{ ss::parser p{f.name, ","};
ss::parser p{f.name, ","}; auto x = p.get_next<my_string, my_string, my_string>();
auto x = p.get_next<xyz>(); CHECK(move_called <= 3 * move_called_one_col);
CHECK(move_called <= 6 * move_called_one_col); move_called = 0;
move_called = 0; }
}
{
ss::parser p{f.name, ","};
auto x = p.get_object<xyz, my_string, my_string, my_string>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
{
ss::parser p{f.name, ","};
auto x = p.get_next<xyz>();
CHECK(move_called <= 6 * move_called_one_col);
move_called = 0;
}
} }
TEST_CASE("testing the moving of parsed composite values") { TEST_CASE("testing the moving of parsed composite values") {
// to compile is enough // to compile is enough
return; return;
ss::parser* p; ss::parser* p;
p->try_next<my_string, my_string, my_string>() p->try_next<my_string, my_string, my_string>()
.or_else<my_string, my_string, my_string, my_string>([](auto&&) {}) .or_else<my_string, my_string, my_string, my_string>([](auto&&) {})
.or_else<my_string>([](auto&) {}) .or_else<my_string>([](auto&) {})
.or_else<xyz>([](auto&&) {}) .or_else<xyz>([](auto&&) {})
.or_else_object<xyz, my_string, my_string, my_string>([](auto&&) {}) .or_else_object<xyz, my_string, my_string, my_string>([](auto&&) {})
.or_else<std::tuple<my_string, my_string, my_string>>( .or_else<std::tuple<my_string, my_string, my_string>>(
[](auto&, auto&, auto&) {}); [](auto&, auto&, auto&) {});
} }