2020-12-10 19:26:56 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-11 21:17:07 +01:00
|
|
|
#include "type_traits.hpp"
|
2020-12-10 19:26:56 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
|
|
|
#include <limits>
|
|
|
|
#include <optional>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
2020-12-20 03:16:17 +01:00
|
|
|
#include <variant>
|
2020-12-10 19:26:56 +01:00
|
|
|
|
|
|
|
namespace ss {
|
|
|
|
|
|
|
|
// todo
|
|
|
|
// taken from
|
|
|
|
// https://gist.github.com/oschonrock/67fc870ba067ebf0f369897a9d52c2dd
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// number converters
|
|
|
|
////////////////
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_floating_point_v<T>, T> pow10(int n) {
|
2020-12-26 00:46:42 +01:00
|
|
|
T ret = 1.0;
|
|
|
|
T r = 10.0;
|
|
|
|
if (n < 0) {
|
|
|
|
n = -n;
|
|
|
|
r = 0.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n) {
|
|
|
|
if (n & 1) {
|
|
|
|
ret *= r;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
r *= r;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
return ret;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
|
|
|
|
const char* begin, const char* const end) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (begin == end) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
int sign = 1;
|
|
|
|
T int_part = 0.0;
|
|
|
|
T frac_part = 0.0;
|
|
|
|
bool has_frac = false;
|
|
|
|
bool has_exp = false;
|
|
|
|
|
|
|
|
// +/- sign
|
|
|
|
if (*begin == '-') {
|
|
|
|
++begin;
|
|
|
|
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;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
++begin;
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
if (has_frac) {
|
|
|
|
T frac_exp = 0.1;
|
2020-12-10 19:26:56 +01:00
|
|
|
|
|
|
|
while (begin != end) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (*begin >= '0' && *begin <= '9') {
|
|
|
|
frac_part += frac_exp * (*begin - '0');
|
|
|
|
frac_exp *= 0.1;
|
|
|
|
} else if (*begin == 'e') {
|
|
|
|
has_exp = true;
|
2020-12-10 19:26:56 +01:00
|
|
|
++begin;
|
2020-12-26 00:46:42 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
++begin;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
// 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;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
int e = 0;
|
|
|
|
while (begin != end && *begin >= '0' && *begin <= '9') {
|
|
|
|
e = e * 10 + *begin - '0';
|
|
|
|
++begin;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
exp_part = pow10<T>(exp_sign * e);
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
if (begin != end) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sign * (int_part + frac_part) * exp_part;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::optional<short> from_char(char c) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(__clang__) || defined(__GNUC__) || defined(__GUNG__)
|
|
|
|
////////////////
|
|
|
|
// mul overflow detection
|
|
|
|
////////////////
|
|
|
|
template <typename T>
|
|
|
|
bool mul_overflow(T& result, T operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_mul_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(int& result, int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_smul_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(long& result, long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_smull_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(long long& result, long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_smulll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(unsigned int& result, unsigned int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_umul_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(unsigned long& result, unsigned long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_umull_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool mul_overflow(unsigned long long& result,
|
|
|
|
unsigned long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_umulll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// addition overflow detection
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline bool add_overflow(T& result, T operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_add_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(int& result, int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_sadd_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(long& result, long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_saddl_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(long long& result, long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_saddll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(unsigned int& result, unsigned int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_uadd_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(unsigned long& result, unsigned long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_uaddl_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool add_overflow(unsigned long long& result,
|
|
|
|
unsigned long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_uaddll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// substraction overflow detection
|
|
|
|
////////////////
|
|
|
|
template <typename T>
|
|
|
|
inline bool sub_overflow(T& result, T operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_sub_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(int& result, int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_ssub_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(long& result, long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_ssubl_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(long long& result, long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_ssubll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(unsigned int& result, unsigned int operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_usub_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(unsigned long& result, unsigned long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_usubl_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool sub_overflow(unsigned long long& result,
|
|
|
|
unsigned long long operand) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return __builtin_usubll_overflow(result, operand, &result);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename F>
|
|
|
|
bool shift_and_add_overflow(T& value, T digit, F add_last_digit_owerflow) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (mul_overflow<T>(value, 10) || add_last_digit_owerflow(value, digit)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#warning "use clang or gcc!!!"
|
|
|
|
template <typename T, typename U>
|
|
|
|
bool shift_and_add_overflow(T& value, T digit, U is_negative) {
|
2020-12-26 00:46:42 +01:00
|
|
|
digit = (is_negative) ? -digit : digit;
|
|
|
|
T old_value = value;
|
|
|
|
value = 10 * value + digit;
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
T expected_old_value = (value - digit) / 10;
|
|
|
|
if (old_value != expected_old_value) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_integral_v<T>, std::optional<T>> to_num(
|
|
|
|
const char* begin, const char* end) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (begin == end) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
bool is_negative = false;
|
|
|
|
if constexpr (std::is_signed_v<T>) {
|
|
|
|
is_negative = *begin == '-';
|
|
|
|
if (is_negative) {
|
|
|
|
++begin;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
|
|
|
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
|
2020-12-26 00:46:42 +01:00
|
|
|
auto add_last_digit_owerflow =
|
|
|
|
(is_negative) ? sub_overflow<T> : add_overflow<T>;
|
2020-12-10 19:26:56 +01:00
|
|
|
#else
|
2020-12-26 00:46:42 +01:00
|
|
|
auto add_last_digit_owerflow = is_negative;
|
2020-12-10 19:26:56 +01:00
|
|
|
#endif
|
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
T value = 0;
|
|
|
|
for (auto i = begin; i != end; ++i) {
|
|
|
|
if (auto digit = from_char(*i);
|
|
|
|
!digit || shift_and_add_overflow<T>(value, digit.value(),
|
|
|
|
add_last_digit_owerflow)) {
|
|
|
|
return std::nullopt;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
return value;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// extract
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
namespace error {
|
|
|
|
template <typename T>
|
|
|
|
struct unsupported_type {
|
2020-12-26 00:46:42 +01:00
|
|
|
constexpr static bool value = false;
|
2020-12-10 19:26:56 +01:00
|
|
|
};
|
|
|
|
} /* namespace */
|
|
|
|
|
|
|
|
template <typename T>
|
2020-12-11 21:17:07 +01:00
|
|
|
std::enable_if_t<!std::is_integral_v<T> && !std::is_floating_point_v<T> &&
|
2020-12-20 03:16:17 +01:00
|
|
|
!is_instance_of<T, std::optional>::value &&
|
|
|
|
!is_instance_of<T, std::variant>::value,
|
2020-12-11 21:17:07 +01:00
|
|
|
bool>
|
2020-12-10 19:26:56 +01:00
|
|
|
extract(const char*, const char*, T&) {
|
2020-12-26 00:46:42 +01:00
|
|
|
static_assert(error::unsupported_type<T>::value,
|
|
|
|
"Conversion for given type is not defined, an "
|
|
|
|
"\'extract\' function needs to be defined!");
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
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) {
|
2020-12-26 00:46:42 +01:00
|
|
|
auto optional_value = to_num<T>(begin, end);
|
|
|
|
if (!optional_value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = optional_value.value();
|
|
|
|
return true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 21:17:07 +01:00
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<is_instance_of<T, std::optional>::value, bool> extract(
|
|
|
|
const char* begin, const char* end, T& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
typename T::value_type raw_value;
|
|
|
|
if (extract(begin, end, raw_value)) {
|
|
|
|
value = raw_value;
|
|
|
|
} else {
|
|
|
|
value = std::nullopt;
|
|
|
|
}
|
|
|
|
return true;
|
2020-12-11 21:17:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-20 03:16:17 +01:00
|
|
|
template <typename T, size_t I>
|
|
|
|
bool extract_variant(const char* begin, const char* end, T& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
using IthType = std::variant_alternative_t<I, std::decay_t<T>>;
|
|
|
|
IthType ithValue;
|
|
|
|
if (extract<IthType>(begin, end, ithValue)) {
|
|
|
|
value = ithValue;
|
|
|
|
return true;
|
|
|
|
} else if constexpr (I + 1 < std::variant_size_v<T>) {
|
|
|
|
return extract_variant<T, I + 1>(begin, end, value);
|
|
|
|
}
|
|
|
|
return false;
|
2020-12-20 03:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<is_instance_of<T, std::variant>::value, bool> extract(
|
|
|
|
const char* begin, const char* end, T& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
return extract_variant<T, 0>(begin, end, value);
|
2020-12-20 03:16:17 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 21:17:07 +01:00
|
|
|
////////////////
|
|
|
|
// extract specialization
|
|
|
|
////////////////
|
|
|
|
|
2020-12-10 19:26:56 +01:00
|
|
|
template <>
|
|
|
|
inline bool extract(const char* begin, const char* end, bool& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
if (end == begin + 1) {
|
|
|
|
if (*begin == '1') {
|
|
|
|
value = true;
|
|
|
|
} else if (*begin == '0') {
|
|
|
|
value = false;
|
2020-12-10 19:26:56 +01:00
|
|
|
} else {
|
2020-12-26 00:46:42 +01:00
|
|
|
return false;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
2020-12-26 00:46:42 +01:00
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 19:26:56 +01:00
|
|
|
|
2020-12-26 00:46:42 +01:00
|
|
|
return true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool extract(const char* begin, const char* end, char& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
value = *begin;
|
|
|
|
return (end == begin + 1);
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline bool extract(const char* begin, const char* end, std::string& value) {
|
2020-12-26 00:46:42 +01:00
|
|
|
value = std::string(begin, end);
|
|
|
|
return true;
|
2020-12-10 19:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* ss */
|