Skip to content

Commit

Permalink
Update single header test, resolve additional clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
red0124 committed Mar 12, 2024
1 parent 2132d6a commit bcf6412
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
compile_commands.json
.clang-format
.clang-tidy
.ccls-cache/*
.ccls-cache/
.cache/
experiment/
build/
Expand Down
4 changes: 2 additions & 2 deletions include/ss/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ constexpr inline auto default_delimiter = ",";
constexpr inline auto get_line_initial_buffer_size = 128;

template <bool StringError>
inline void assert_string_error_defined() {
void assert_string_error_defined() {
static_assert(StringError,
"'string_error' needs to be enabled to use 'error_msg'");
}

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 "
"'throw_on_error' is enabled");
}
Expand Down
8 changes: 5 additions & 3 deletions include/ss/extract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#else
#include <algorithm>
#include <cstdlib>
#include <array>
#endif

namespace ss {
Expand Down Expand Up @@ -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");

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);
std::string long_buff;

char* buff;
char* buff = nullptr;
if (string_range > buff_max) {
long_buff = std::string{begin, end};
buff = long_buff.data();
} else {
buff = short_buff;
buff = short_buff.data();
buff[string_range] = '\0';
std::copy_n(begin, string_range, buff);
}
Expand Down
9 changes: 5 additions & 4 deletions include/ss/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class parser {
constexpr static bool ignore_empty = setup<Options...>::ignore_empty;

public:
parser(std::string file_name,
std::string delim = ss::default_delimiter)
parser(std::string file_name, std::string delim = ss::default_delimiter)
: file_name_{std::move(file_name)}, reader_{file_name_, delim} {
if (reader_.file_) {
read_line();
Expand Down Expand Up @@ -449,7 +448,8 @@ class parser {
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(std::forward<Arg>(arg), std::forward<Fun>(fun))) {
if (!try_invoke_impl(std::forward<Arg>(arg),
std::forward<Fun>(fun))) {
handle_error_failed_check();
}
} else {
Expand Down Expand Up @@ -681,7 +681,8 @@ class parser {

struct reader {
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,
Expand Down
20 changes: 11 additions & 9 deletions ssp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,13 @@ constexpr inline auto default_delimiter = ",";
constexpr inline auto get_line_initial_buffer_size = 128;

template <bool StringError>
inline void assert_string_error_defined() {
void assert_string_error_defined() {
static_assert(StringError,
"'string_error' needs to be enabled to use 'error_msg'");
}

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 "
"'throw_on_error' is enabled");
}
Expand Down Expand Up @@ -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");

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);
std::string long_buff;

char* buff;
char* buff = nullptr;
if (string_range > buff_max) {
long_buff = std::string{begin, end};
buff = long_buff.data();
} else {
buff = short_buff;
buff = short_buff.data();
buff[string_range] = '\0';
std::copy_n(begin, string_range, buff);
}
Expand Down Expand Up @@ -2280,8 +2281,7 @@ class parser {
constexpr static bool ignore_empty = setup<Options...>::ignore_empty;

public:
parser(std::string file_name,
std::string delim = ss::default_delimiter)
parser(std::string file_name, std::string delim = ss::default_delimiter)
: file_name_{std::move(file_name)}, reader_{file_name_, delim} {
if (reader_.file_) {
read_line();
Expand Down Expand Up @@ -2697,7 +2697,8 @@ class parser {
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(std::forward<Arg>(arg), std::forward<Fun>(fun))) {
if (!try_invoke_impl(std::forward<Arg>(arg),
std::forward<Fun>(fun))) {
handle_error_failed_check();
}
} else {
Expand Down Expand Up @@ -2929,7 +2930,8 @@ class parser {

struct reader {
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,
Expand Down
14 changes: 8 additions & 6 deletions test/test_single_header.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
set -x
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; }' \
>> ssp.cpp
python3 script/single_header_generator.py > ${TMP_HDR}
cat ${TMP_HDR} test/test_single_header_main.txt > ${TMP_SRC}

g++ -std=c++17 ssp.cpp -o ssp.bin -Wall -Wextra
./ssp.bin
g++ -std=c++17 ${TMP_SRC} -o ${TMP_BIN} -Wall -Wextra
./${TMP_BIN}

rm ssp.cpp ssp.bin
rm ${TMP_HDR} ${TMP_SRC} ${TMP_BIN}
12 changes: 12 additions & 0 deletions test/test_single_header_main.txt
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit bcf6412

Please sign in to comment.