aboutsummaryrefslogtreecommitdiff
path: root/include/openvic-dataloader/csv
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2024-06-22 21:58:35 +0200
committer GitHub <noreply@github.com>2024-06-22 21:58:35 +0200
commitdeed8ec0ae23651529a58125012c1b4aab015d02 (patch)
tree51ca6d5948e92be37b9ee6674cb96801d2cd03f8 /include/openvic-dataloader/csv
parent8b623bf4087aa360842ad31145d4ab6946cee9aa (diff)
parent1a694a8b26a441b12547057d6e0be61a111cced3 (diff)
Merge pull request #49 from OpenVicProject/add/unit-testing
Add unit testing
Diffstat (limited to 'include/openvic-dataloader/csv')
-rw-r--r--include/openvic-dataloader/csv/LineObject.hpp50
-rw-r--r--include/openvic-dataloader/csv/Parser.hpp6
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);