From 1a694a8b26a441b12547057d6e0be61a111cced3 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sat, 15 Jun 2024 09:40:31 -0400 Subject: Add unit tests Make github action tests run explicit Fix dropping annotation list for Errors Fix potential empty get_errors crashes Fix incorrect csv error behavior Add use_sep for `LineObject` and `std::vector` Remove constexpr of load_from_buffer and load_from_string for parsers Add snitch-org/snitch@d6632123cc8d13bdbc5cd60fd6741b9e0f635e82 Make versioned submodules ignore dirty Add tests/bin/* to gitignore --- include/openvic-dataloader/csv/LineObject.hpp | 50 +++++++++++++++++++++++++++ include/openvic-dataloader/csv/Parser.hpp | 6 ++-- 2 files changed, 53 insertions(+), 3 deletions(-) (limited to 'include/openvic-dataloader/csv') diff --git a/include/openvic-dataloader/csv/LineObject.hpp b/include/openvic-dataloader/csv/LineObject.hpp index c839be2..5de55ba 100644 --- a/include/openvic-dataloader/csv/LineObject.hpp +++ b/include/openvic-dataloader/csv/LineObject.hpp @@ -80,6 +80,15 @@ namespace ovdl::csv { constexpr std::size_t value_count() const { return _suffix_end; } + struct SepTransformer { + const LineObject& line_object; + std::string_view separator; + }; + + constexpr SepTransformer use_sep(std::string_view seperator) const { + return { *this, seperator }; + } + private: // Should be position of first valid value on line position_type _prefix_end = 0; @@ -87,6 +96,15 @@ namespace ovdl::csv { position_type _suffix_end = 0; }; + struct VectorSepTransformer { + const std::vector& vector; + std::string_view separator; + }; + + constexpr VectorSepTransformer use_sep(const std::vector& vector, std::string_view separator) { + return { vector, separator }; + } + inline std::ostream& operator<<(std::ostream& stream, const LineObject& line) { static constexpr char SEP = ';'; LineObject::position_type sep_index = 0; @@ -110,4 +128,36 @@ namespace ovdl::csv { } return stream; } + + inline std::ostream& operator<<(std::ostream& stream, const LineObject::SepTransformer& transformer) { + auto quote_check = [&transformer, is_one = transformer.separator.size() == 1](const std::string_view str) { + if (is_one) { + char SEP = transformer.separator[0]; + return std::any_of(str.begin(), str.end(), [SEP](char c) { return c == SEP || std::isspace(c); }); + } + return std::any_of(str.begin(), str.end(), [](char c) { return std::isspace(c); }) || + str.find(transformer.separator) != std::string::npos; + }; + + LineObject::position_type sep_index = 0; + for (const auto& [pos, val] : transformer.line_object) { + while (sep_index < pos) { + stream << transformer.separator; + sep_index++; + } + if (quote_check(val)) { + stream << '"' << val << '"'; + } else { + stream << val; + } + } + return stream; + } + + inline std::ostream& operator<<(std::ostream& stream, const VectorSepTransformer& transformer) { + for (const LineObject& line : transformer.vector) { + stream << line.use_sep(transformer.separator) << '\n'; + } + return stream; + } } \ No newline at end of file diff --git a/include/openvic-dataloader/csv/Parser.hpp b/include/openvic-dataloader/csv/Parser.hpp index 35421c8..1c363c8 100644 --- a/include/openvic-dataloader/csv/Parser.hpp +++ b/include/openvic-dataloader/csv/Parser.hpp @@ -25,9 +25,9 @@ namespace ovdl::csv { static Parser from_file(const char* path, std::optional encoding_fallback = std::nullopt); static Parser from_file(const std::filesystem::path& path, std::optional encoding_fallback = std::nullopt); - constexpr Parser& load_from_buffer(const char* data, std::size_t size, std::optional encoding_fallback = std::nullopt); - constexpr Parser& load_from_buffer(const char* start, const char* end, std::optional encoding_fallback = std::nullopt); - constexpr Parser& load_from_string(const std::string_view string, std::optional encoding_fallback = std::nullopt); + Parser& load_from_buffer(const char* data, std::size_t size, std::optional encoding_fallback = std::nullopt); + Parser& load_from_buffer(const char* start, const char* end, std::optional encoding_fallback = std::nullopt); + Parser& load_from_string(const std::string_view string, std::optional encoding_fallback = std::nullopt); Parser& load_from_file(const char* path, std::optional encoding_fallback = std::nullopt); Parser& load_from_file(const std::filesystem::path& path, std::optional encoding_fallback = std::nullopt); -- cgit v1.2.3-56-ga3b1