Update single header test, resolve additional clang-tidy warnings

This commit is contained in:
ado 2024-03-12 14:29:30 +01:00
parent 2132d6a234
commit bcf6412376
7 changed files with 44 additions and 25 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
compile_commands.json compile_commands.json
.clang-format .clang-format
.clang-tidy .clang-tidy
.ccls-cache/* .ccls-cache/
.cache/ .cache/
experiment/ experiment/
build/ build/

View File

@ -21,13 +21,13 @@ constexpr inline auto default_delimiter = ",";
constexpr inline auto get_line_initial_buffer_size = 128; constexpr inline auto get_line_initial_buffer_size = 128;
template <bool StringError> template <bool StringError>
inline void assert_string_error_defined() { void assert_string_error_defined() {
static_assert(StringError, static_assert(StringError,
"'string_error' needs to be enabled to use 'error_msg'"); "'string_error' needs to be enabled to use 'error_msg'");
} }
template <bool ThrowOnError> template <bool ThrowOnError>
inline void assert_throw_on_error_not_defined() { void assert_throw_on_error_not_defined() {
static_assert(!ThrowOnError, "cannot handle errors manually if " static_assert(!ThrowOnError, "cannot handle errors manually if "
"'throw_on_error' is enabled"); "'throw_on_error' is enabled");
} }

View File

@ -14,6 +14,7 @@
#else #else
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
#include <array>
#endif #endif
namespace ss { namespace ss {
@ -45,16 +46,17 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
"Conversion to long double is disabled"); "Conversion to long double is disabled");
constexpr static auto buff_max = 64; constexpr static auto buff_max = 64;
char short_buff[buff_max]; std::array<char, buff_max> short_buff;
size_t string_range = std::distance(begin, end); size_t string_range = std::distance(begin, end);
std::string long_buff; std::string long_buff;
char* buff; char* buff = nullptr;
if (string_range > buff_max) { if (string_range > buff_max) {
long_buff = std::string{begin, end}; long_buff = std::string{begin, end};
buff = long_buff.data(); buff = long_buff.data();
} else { } else {
buff = short_buff; buff = short_buff.data();
buff[string_range] = '\0'; buff[string_range] = '\0';
std::copy_n(begin, string_range, buff); std::copy_n(begin, string_range, buff);
} }

View File

@ -32,8 +32,7 @@ class parser {
constexpr static bool ignore_empty = setup<Options...>::ignore_empty; constexpr static bool ignore_empty = setup<Options...>::ignore_empty;
public: public:
parser(std::string file_name, parser(std::string file_name, std::string delim = ss::default_delimiter)
std::string delim = ss::default_delimiter)
: file_name_{std::move(file_name)}, reader_{file_name_, delim} { : file_name_{std::move(file_name)}, reader_{file_name_, delim} {
if (reader_.file_) { if (reader_.file_) {
read_line(); read_line();
@ -449,7 +448,8 @@ private:
using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun))); using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun)));
constexpr bool returns_void = std::is_same_v<Ret, void>; constexpr bool returns_void = std::is_same_v<Ret, void>;
if constexpr (!returns_void) { if constexpr (!returns_void) {
if (!try_invoke_impl(std::forward<Arg>(arg), std::forward<Fun>(fun))) { if (!try_invoke_impl(std::forward<Arg>(arg),
std::forward<Fun>(fun))) {
handle_error_failed_check(); handle_error_failed_check();
} }
} else { } else {
@ -681,7 +681,8 @@ private:
struct reader { struct reader {
reader(const std::string& file_name_, std::string delim) reader(const std::string& file_name_, std::string delim)
: delim_{std::move(delim)}, file_{std::fopen(file_name_.c_str(), "rb")} { : delim_{std::move(delim)},
file_{std::fopen(file_name_.c_str(), "rb")} {
} }
reader(const char* const buffer, size_t csv_data_size, reader(const char* const buffer, size_t csv_data_size,

20
ssp.hpp
View File

@ -637,13 +637,13 @@ constexpr inline auto default_delimiter = ",";
constexpr inline auto get_line_initial_buffer_size = 128; constexpr inline auto get_line_initial_buffer_size = 128;
template <bool StringError> template <bool StringError>
inline void assert_string_error_defined() { void assert_string_error_defined() {
static_assert(StringError, static_assert(StringError,
"'string_error' needs to be enabled to use 'error_msg'"); "'string_error' needs to be enabled to use 'error_msg'");
} }
template <bool ThrowOnError> template <bool ThrowOnError>
inline void assert_throw_on_error_not_defined() { void assert_throw_on_error_not_defined() {
static_assert(!ThrowOnError, "cannot handle errors manually if " static_assert(!ThrowOnError, "cannot handle errors manually if "
"'throw_on_error' is enabled"); "'throw_on_error' is enabled");
} }
@ -1562,16 +1562,17 @@ std::enable_if_t<std::is_floating_point_v<T>, std::optional<T>> to_num(
"Conversion to long double is disabled"); "Conversion to long double is disabled");
constexpr static auto buff_max = 64; constexpr static auto buff_max = 64;
char short_buff[buff_max]; std::array<char, buff_max> short_buff;
size_t string_range = std::distance(begin, end); size_t string_range = std::distance(begin, end);
std::string long_buff; std::string long_buff;
char* buff; char* buff = nullptr;
if (string_range > buff_max) { if (string_range > buff_max) {
long_buff = std::string{begin, end}; long_buff = std::string{begin, end};
buff = long_buff.data(); buff = long_buff.data();
} else { } else {
buff = short_buff; buff = short_buff.data();
buff[string_range] = '\0'; buff[string_range] = '\0';
std::copy_n(begin, string_range, buff); std::copy_n(begin, string_range, buff);
} }
@ -2280,8 +2281,7 @@ class parser {
constexpr static bool ignore_empty = setup<Options...>::ignore_empty; constexpr static bool ignore_empty = setup<Options...>::ignore_empty;
public: public:
parser(std::string file_name, parser(std::string file_name, std::string delim = ss::default_delimiter)
std::string delim = ss::default_delimiter)
: file_name_{std::move(file_name)}, reader_{file_name_, delim} { : file_name_{std::move(file_name)}, reader_{file_name_, delim} {
if (reader_.file_) { if (reader_.file_) {
read_line(); read_line();
@ -2697,7 +2697,8 @@ private:
using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun))); using Ret = decltype(try_invoke_impl(arg, std::forward<Fun>(fun)));
constexpr bool returns_void = std::is_same_v<Ret, void>; constexpr bool returns_void = std::is_same_v<Ret, void>;
if constexpr (!returns_void) { if constexpr (!returns_void) {
if (!try_invoke_impl(std::forward<Arg>(arg), std::forward<Fun>(fun))) { if (!try_invoke_impl(std::forward<Arg>(arg),
std::forward<Fun>(fun))) {
handle_error_failed_check(); handle_error_failed_check();
} }
} else { } else {
@ -2929,7 +2930,8 @@ private:
struct reader { struct reader {
reader(const std::string& file_name_, std::string delim) reader(const std::string& file_name_, std::string delim)
: delim_{std::move(delim)}, file_{std::fopen(file_name_.c_str(), "rb")} { : delim_{std::move(delim)},
file_{std::fopen(file_name_.c_str(), "rb")} {
} }
reader(const char* const buffer, size_t csv_data_size, reader(const char* const buffer, size_t csv_data_size,

View File

@ -3,12 +3,14 @@
set -x set -x
set -e set -e
python3 script/single_header_generator.py > ssp.cpp TMP_HDR=test_single_header.hpp
TMP_SRC=test_single_header.cpp
TMP_BIN=test_single_header
echo 'int main(){ ss::parser p{""}; p.get_next<int, float>(); return 0; }' \ python3 script/single_header_generator.py > ${TMP_HDR}
>> ssp.cpp cat ${TMP_HDR} test/test_single_header_main.txt > ${TMP_SRC}
g++ -std=c++17 ssp.cpp -o ssp.bin -Wall -Wextra g++ -std=c++17 ${TMP_SRC} -o ${TMP_BIN} -Wall -Wextra
./ssp.bin ./${TMP_BIN}
rm ssp.cpp ssp.bin rm ${TMP_HDR} ${TMP_SRC} ${TMP_BIN}

View File

@ -0,0 +1,12 @@
int main() {
using quote = ss::quote<'"'>;
using escape = ss::escape<'\\'>;
using trim = ss::trim<' '>;
std::string data = "1,string,2.34,c";
ss::parser<quote, escape, trim, ss::multiline> p{data.c_str(), data.size()};
auto tup = p.get_next<int, std::string, float, std::optional<char>>();
return 0;
}