diff options
author | Spartan322 <Megacake1234@gmail.com> | 2024-06-15 15:40:31 +0200 |
---|---|---|
committer | Spartan322 <Megacake1234@gmail.com> | 2024-06-22 13:57:49 +0200 |
commit | 1a694a8b26a441b12547057d6e0be61a111cced3 (patch) | |
tree | 51ca6d5948e92be37b9ee6674cb96801d2cd03f8 /include/openvic-dataloader/csv | |
parent | 8b623bf4087aa360842ad31145d4ab6946cee9aa (diff) |
Add unit testsadd/unit-testing
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<LineObject>`
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
Diffstat (limited to 'include/openvic-dataloader/csv')
-rw-r--r-- | include/openvic-dataloader/csv/LineObject.hpp | 50 | ||||
-rw-r--r-- | include/openvic-dataloader/csv/Parser.hpp | 6 |
2 files changed, 53 insertions, 3 deletions
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<LineObject>& vector; + std::string_view separator; + }; + + constexpr VectorSepTransformer use_sep(const std::vector<LineObject>& 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<detail::Encoding> encoding_fallback = std::nullopt); static Parser from_file(const std::filesystem::path& path, std::optional<detail::Encoding> encoding_fallback = std::nullopt); - constexpr Parser& load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback = std::nullopt); - constexpr Parser& load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback = std::nullopt); - constexpr Parser& load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback = std::nullopt); + Parser& load_from_buffer(const char* data, std::size_t size, std::optional<detail::Encoding> encoding_fallback = std::nullopt); + Parser& load_from_buffer(const char* start, const char* end, std::optional<detail::Encoding> encoding_fallback = std::nullopt); + Parser& load_from_string(const std::string_view string, std::optional<detail::Encoding> encoding_fallback = std::nullopt); Parser& load_from_file(const char* path, std::optional<detail::Encoding> encoding_fallback = std::nullopt); Parser& load_from_file(const std::filesystem::path& path, std::optional<detail::Encoding> encoding_fallback = std::nullopt); |