From 84b3bab4612f99d44bc17d97c9bd821b456f8d8d Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sat, 30 Sep 2023 00:25:00 -0400 Subject: Change CSV escape to replace at runtime Move Grammar.inc to CsvGrammar.hpp --- include/openvic-dataloader/csv/LineObject.hpp | 53 +++-- include/openvic-dataloader/csv/ValueNode.hpp | 81 +++++++ src/openvic-dataloader/csv/CsvGrammar.hpp | 317 ++++++++++++++++++++++++-- src/openvic-dataloader/csv/Grammar.inc | 221 ------------------ src/openvic-dataloader/csv/Parser.cpp | 19 +- src/openvic-dataloader/csv/ValueNode.cpp | 57 +++++ 6 files changed, 491 insertions(+), 257 deletions(-) create mode 100644 include/openvic-dataloader/csv/ValueNode.hpp delete mode 100644 src/openvic-dataloader/csv/Grammar.inc create mode 100644 src/openvic-dataloader/csv/ValueNode.cpp diff --git a/include/openvic-dataloader/csv/LineObject.hpp b/include/openvic-dataloader/csv/LineObject.hpp index c6d8e2a..b9ee9a5 100644 --- a/include/openvic-dataloader/csv/LineObject.hpp +++ b/include/openvic-dataloader/csv/LineObject.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -14,6 +13,7 @@ #include #include +#include #include namespace ovdl::csv { @@ -28,13 +28,13 @@ namespace ovdl::csv { /// ;a;b;c -> 0,4+ == "" /// /// If this is incorrect, please report an issue. - class LineObject final : public std::vector> { + class LineObject final : public std::vector { public: // Stored position of value - using position_type = std::uint32_t; + using position_type = ValueNode::position_type; // Value - using inner_value_type = std::string; - using container_type = std::vector>; + using inner_value_type = ValueNode; + using container_type = std::vector; OVDL_VECTOR_CONSTEXPR LineObject() = default; OVDL_VECTOR_CONSTEXPR LineObject(LineObject&) = default; @@ -57,18 +57,42 @@ namespace ovdl::csv { /// Special Functionality /// Retrieves value, produces "" for empty values - constexpr std::string_view get_value_for(std::size_t position) const { + constexpr std::string get_value_for(std::size_t position) const { if (position < _prefix_end || position >= _suffix_end) return ""; - for (const auto& [pos, val] : *this) { - if (pos == position) return val; + for (const auto& object : *this) { + if (object.get_position() == position) return object.make(); } return ""; } - /// Tries to retrieve reference, produces nullopt for empty values - constexpr std::optional> try_get_string_at(std::size_t position) const { + /// Tries to retrieve string, produces nullopt for empty values + constexpr std::optional try_get_string_at(std::size_t position) const { if (position < _prefix_end || position >= _suffix_end) return std::nullopt; - for (const auto& [pos, val] : *this) { - if (pos == position) return std::cref(val); + for (const auto& object : *this) { + if (object.get_position() == position) return object.make(); + } + return std::nullopt; + } + constexpr std::optional> try_get_object_at(std::size_t position) const { + if (position < _prefix_end || position >= _suffix_end) return std::nullopt; + for (const auto& object : *this) { + if (object.get_position() == position) return object; + } + return std::nullopt; + } + + /// Retrieves value, produces "" for empty values + constexpr std::string_view get_value_for(std::size_t position, const IsMap auto& map) const { + if (position < _prefix_end || position >= _suffix_end) return ""; + for (const auto& object : *this) { + if (object.get_position() == position) return object.make_from_map(map); + } + return ""; + } + /// Tries to retrieve string, produces nullopt for empty values + constexpr std::optional try_get_string_at(std::size_t position, const IsMap auto& map) const { + if (position < _prefix_end || position >= _suffix_end) return std::nullopt; + for (const auto& object : *this) { + if (object.get_position() == position) return object.make_from_map(map); } return std::nullopt; } @@ -91,8 +115,9 @@ namespace ovdl::csv { inline std::ostream& operator<<(std::ostream& stream, const LineObject& line) { static const char SEP = ';'; LineObject::position_type sep_index = 0; - for (const auto& [pos, val] : line) { - while (sep_index < pos) { + for (const auto& object : line) { + const std::string& val = object.make(); + while (sep_index < object.get_position()) { stream << SEP; sep_index++; } diff --git a/include/openvic-dataloader/csv/ValueNode.hpp b/include/openvic-dataloader/csv/ValueNode.hpp new file mode 100644 index 0000000..e66dac0 --- /dev/null +++ b/include/openvic-dataloader/csv/ValueNode.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace ovdl::csv { + template + concept IsMap = + requires(T t) { + { t.begin() } -> std::same_as; + { t.end() } -> std::same_as; + { t.empty() } -> std::same_as; + { t.size() } -> std::same_as; + { t.at("") } -> std::same_as; + { t.find("") } -> std::same_as; + { t[""] } -> std::same_as; + }; + + class ValueNode { + public: + struct Placeholder { + static constexpr char ESCAPE_CHAR = '$'; + static constexpr std::string_view ESCAPE_STR = std::string_view { &ESCAPE_CHAR, 1 }; + + std::string value; + inline std::string as_string(std::string_view prefix = ESCAPE_STR, std::optional suffix = std::nullopt) const { + return fmt::format(FMT_COMPILE("{}{}{}"), prefix, value, suffix.value_or(prefix)); + } + }; + + using internal_value_type = std::variant; + using position_type = std::uint32_t; + + ValueNode(); + ValueNode(std::string_view string, position_type position = 0); + ValueNode(std::vector value_list, position_type position = 0); + ValueNode(std::initializer_list value_list, position_type position = 0); + + void set_position(position_type position); + position_type get_position() const; + + void set_as_list(internal_value_type value); + void add_to_list(internal_value_type value); + bool list_is_empty() const; + + inline std::string make_from_map(const IsMap auto& map) const { + std::vector pre_joined(_value_list.size()); + + for (auto&& value : _value_list) { + pre_joined.push_back(std::visit([&](auto&& arg) -> std::string_view { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return arg; + } else if constexpr (std::is_same_v) { + return map[arg.value]; + } + }, + value)); + } + + return fmt::format(FMT_COMPILE("{}"), fmt::join(pre_joined, "")); + } + + std::string make(std::string_view prefix = Placeholder::ESCAPE_STR, std::optional suffix = std::nullopt) const; + + private: + position_type _position; + std::vector _value_list; + }; +} \ No newline at end of file diff --git a/src/openvic-dataloader/csv/CsvGrammar.hpp b/src/openvic-dataloader/csv/CsvGrammar.hpp index bfae8d0..69557da 100644 --- a/src/openvic-dataloader/csv/CsvGrammar.hpp +++ b/src/openvic-dataloader/csv/CsvGrammar.hpp @@ -1,41 +1,322 @@ #pragma once +#include +#include #include -#include #include #include -#include #include +#include #include #include -#include #include #include +#include -#include +#include "openvic-dataloader/csv/ValueNode.hpp" #include "detail/LexyLitRange.hpp" +namespace ovdl::csv::grammar { + template + concept ParseChars = requires() { + { T::character }; + { T::control }; + { T::xid_start_underscore }; + { T::xid_continue }; + }; + + template + struct ParseOptions { + /// @brief Seperator character + char SepChar; + /// @brief Determines whether StringValue is supported + bool SupportStrings; + /// @brief Paradox-style localization escape characters + /// @note Is ignored if SupportStrings is true + char EscapeChar; + + static constexpr auto parse_chars = T {}; + }; + + constexpr auto escaped_symbols = lexy::symbol_table // + .map<'"'>('"') + .map<'\''>('\'') + .map<'\\'>('\\') + .map<'/'>('/') + .map<'b'>('\b') + .map<'f'>('\f') + .map<'n'>('\n') + .map<'r'>('\r') + .map<'t'>('\t'); + + constexpr auto escaped_quote = lexy::symbol_table // + .map<'"'>('"'); + + template + struct StringValue { + static constexpr auto rule = [] { + // Arbitrary code points + auto c = Options.parse_chars.character - Options.parse_chars.control; + + auto back_escape = lexy::dsl::backslash_escape // + .symbol(); + + auto quote_escape = lexy::dsl::escape(lexy::dsl::lit_c<'"'>) // + .template symbol(); + + auto quotes = lexy::dsl::delimited(lexy::dsl::lit_c<'"'>, lexy::dsl::not_followed_by(lexy::dsl::lit_c<'"'>, lexy::dsl::lit_c<'"'>)); + + return quotes(c, back_escape, quote_escape); + }(); + + static constexpr auto value = + lexy::as_string >> + lexy::callback( + [](auto&& string) { + return ovdl::csv::ValueNode(LEXY_MOV(string)); + }); + ; + }; + + template + struct EscapeValue { + static constexpr auto rule = [] { + auto id = lexy::dsl::identifier(Options.parse_chars.xid_start_underscore, Options.parse_chars.xid_continue); + + return lexy::dsl::lit_b >> + (lexy::dsl::lit_b | + (id >> lexy::dsl::lit_b)); + }(); + static constexpr auto value = + lexy::callback( + [](auto&& lexeme) { + return ovdl::csv::ValueNode::Placeholder { std::string { lexeme.data(), lexeme.size() } }; + }, + [](lexy::nullopt = {}) { + return std::string(1, Options.EscapeChar); + }); + }; + + template + struct PlainValue { + static constexpr auto rule = [] { + auto min_skip = lexy::dsl::lit_b / lexy::dsl::ascii::newline; + if constexpr (Options.SupportStrings) { + return lexy::dsl::identifier(Options.parse_chars.character - min_skip); + } else { + auto escape_check_char = [=] { + if constexpr (Options.EscapeChar != 0) { + return Options.parse_chars.character - (min_skip / lexy::dsl::lit_b); + } else { + return Options.parse_chars.character - min_skip; + } + }(); + auto id_check_char = escape_check_char - lexy::dsl::lit_b<'\\'>; + auto id_segment = lexy::dsl::identifier(id_check_char); + auto escape_segement = lexy::dsl::token(escape_check_char); + auto escape_sym = lexy::dsl::symbol(escape_segement); + auto escape_rule = lexy::dsl::lit_b<'\\'> >> escape_sym; + if constexpr (Options.EscapeChar != 0) { + return lexy::dsl::list(lexy::dsl::p> | id_segment | escape_rule); + } else { + return lexy::dsl::list(id_segment | escape_rule); + } + } + }(); + static constexpr auto value = + lexy::fold_inplace>( + std::initializer_list {}, + [](auto& list, const char& c) { + if (list.empty()) { + list.push_back(std::string { c, 1 }); + return; + } + std::visit([&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + arg += c; + } else { + list.push_back(std::string { c, 1 }); + } + }, + list.back()); + }, + [](auto& list, lexy::lexeme lexeme) { + if (list.empty()) { + list.push_back(std::string { lexeme.data(), lexeme.size() }); + return; + } + std::visit([&](auto&& arg) { + using T = std::decay_t; + if constexpr (std::is_same_v) { + arg += std::string_view { lexeme.data(), lexeme.size() }; + } else { + list.push_back(std::string { lexeme.data(), lexeme.size() }); + } + }, + list.back()); + }, + [](auto& list, ovdl::csv::ValueNode::internal_value_type&& arg) { + if (list.empty()) { + list.push_back(LEXY_MOV(arg)); + return; + } + using T = std::decay_t; + std::visit([&](auto&& list_arg) { + using list_T = std::decay_t; + if constexpr (std::is_same_v && std::is_same_v) { + list_arg += LEXY_MOV(arg); + } else { + list.push_back(LEXY_MOV(arg)); + } + }, + list.back()); + }) >> + lexy::callback( + [](std::vector&& list) { + return ovdl::csv::ValueNode(LEXY_MOV(list)); + }, + [](auto&& lexeme) { + ovdl::csv::ValueNode result; + result.set_as_list(std::string { lexeme.data(), lexeme.size() }); + return result; + }); + }; + + template + struct Value { + static constexpr auto rule = [] { + if constexpr (Options.SupportStrings) { + return lexy::dsl::p> | lexy::dsl::p>; + } else { + return lexy::dsl::p>; + } + }(); + static constexpr auto value = lexy::forward; + }; + + template + struct SepConst { + static constexpr auto rule = lexy::dsl::lit_b; + static constexpr auto value = lexy::constant(1); + }; + + template + struct Seperator { + static constexpr auto rule = lexy::dsl::list(lexy::dsl::p>); + static constexpr auto value = lexy::count; + }; + + template + struct LineEnd { + static constexpr auto rule = lexy::dsl::list(lexy::dsl::p>, lexy::dsl::trailing_sep(lexy::dsl::p>)); + static constexpr auto value = lexy::fold_inplace( + std::initializer_list {}, + [](ovdl::csv::LineObject& result, auto&& arg) { + if constexpr (std::is_same_v, std::size_t>) { + // Count seperators, adds to previous value, making it a position + using position_type = ovdl::csv::LineObject::position_type; + result.emplace_back("", static_cast(arg + result.back().get_position())); + } else { + if (result.empty()) result.push_back(LEXY_MOV(arg)); + else { + auto& object = result.back(); + object = LEXY_MOV(arg); + } + } + }); + }; + + template + struct Line { + + static constexpr auto suffix_setter(ovdl::csv::LineObject& line) { + auto& object = line.back(); + if (object.list_is_empty()) { + line.set_suffix_end(object.get_position()); + line.pop_back(); + } else { + line.set_suffix_end(object.get_position() + 1); + } + }; + + static constexpr auto rule = lexy::dsl::p> | lexy::dsl::p> >> lexy::dsl::p>; + static constexpr auto value = + lexy::callback( + [](ovdl::csv::LineObject&& line) { + suffix_setter(line); + return LEXY_MOV(line); + }, + [](std::size_t prefix_count, ovdl::csv::LineObject&& line) { + line.set_prefix_end(prefix_count); + // position needs to be adjusted to prefix + for (auto& object : line) { + object.set_position(object.get_position() + prefix_count); + } + suffix_setter(line); + return LEXY_MOV(line); + }); + }; + + template + struct File { + static constexpr auto rule = + lexy::dsl::whitespace(lexy::dsl::newline) + + lexy::dsl::opt(lexy::dsl::list(lexy::dsl::p>, lexy::dsl::trailing_sep(lexy::dsl::eol))); + + static constexpr auto value = lexy::as_list>; + }; +} + // Grammar Definitions // namespace ovdl::csv::grammar::windows1252 { - constexpr auto character = detail::lexydsl::make_range<0x01, 0xFF>(); - constexpr auto control = - lexy::dsl::ascii::control / - lexy::dsl::lit_b<0x81> / lexy::dsl::lit_b<0x8D> / lexy::dsl::lit_b<0x8F> / - lexy::dsl::lit_b<0x90> / lexy::dsl::lit_b<0x9D>; - constexpr auto id_head = lexy::dsl::ascii::alpha_underscore; - constexpr auto id_tail = lexy::dsl::ascii::alpha_digit_underscore; - -#include "Grammar.inc" + struct windows1252_t { + static constexpr auto character = detail::lexydsl::make_range<0x01, 0xFF>(); + static constexpr auto control = + lexy::dsl::ascii::control / + lexy::dsl::lit_b<0x81> / lexy::dsl::lit_b<0x8D> / lexy::dsl::lit_b<0x8F> / + lexy::dsl::lit_b<0x90> / lexy::dsl::lit_b<0x9D>; + static constexpr auto xid_start_underscore = lexy::dsl::ascii::alpha_underscore; + static constexpr auto xid_continue = lexy::dsl::ascii::alpha_digit_underscore; + }; + + using CommaFile = File { ',', false, '$' }>; + using ColonFile = File { ':', false, '$' }>; + using SemiColonFile = File { ';', false, '$' }>; + using TabFile = File { '\t', false, '$' }>; + using BarFile = File { '|', false, '$' }>; + + namespace strings { + using CommaFile = File { ',', true, '$' }>; + using ColonFile = File { ':', true, '$' }>; + using SemiColonFile = File { ';', true, '$' }>; + using TabFile = File { '\t', true, '$' }>; + using BarFile = File { '|', true, '$' }>; + } } namespace ovdl::csv::grammar::utf8 { - constexpr auto character = lexy::dsl::unicode::character; - constexpr auto control = lexy::dsl::unicode::control; - constexpr auto id_head = lexy::dsl::unicode::xid_start_underscore; - constexpr auto id_tail = lexy::dsl::unicode::xid_continue; + struct unicode_t { + static constexpr auto character = lexy::dsl::unicode::character; + static constexpr auto control = lexy::dsl::unicode::control; + static constexpr auto xid_start_underscore = lexy::dsl::unicode::xid_start_underscore; + static constexpr auto xid_continue = lexy::dsl::unicode::xid_continue; + }; + + using CommaFile = File { ',', false, '$' }>; + using ColonFile = File { ':', false, '$' }>; + using SemiColonFile = File { ';', false, '$' }>; + using TabFile = File { '\t', false, '$' }>; + using BarFile = File { '|', false, '$' }>; -#include "Grammar.inc" + namespace strings { + using CommaFile = File { ',', true, '$' }>; + using ColonFile = File { ':', true, '$' }>; + using SemiColonFile = File { ';', true, '$' }>; + using TabFile = File { '\t', true, '$' }>; + using BarFile = File { '|', true, '$' }>; + } } \ No newline at end of file diff --git a/src/openvic-dataloader/csv/Grammar.inc b/src/openvic-dataloader/csv/Grammar.inc deleted file mode 100644 index 7de9e81..0000000 --- a/src/openvic-dataloader/csv/Grammar.inc +++ /dev/null @@ -1,221 +0,0 @@ -// This is designed to be reused in CsvGrammar.hpp multiple times -// Please ignore undeclared variable use for character and control, this is intended -// Sorry was the cleanest way I could think of to prevent code duplication - -// Includes to keep file errors small -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include - -#include "detail/LexyLitRange.hpp" - -struct ParseOptions { - /// @brief Seperator character - char SepChar; - /// @brief Determines whether StringValue is supported - bool SupportStrings; - /// @brief Paradox-style localization escape characters - /// @note Is ignored if SupportStrings is true - char EscapeChar; -}; - -constexpr auto escaped_symbols = lexy::symbol_table // - .map<'"'>('"') - .map<'\''>('\'') - .map<'\\'>('\\') - .map<'/'>('/') - .map<'b'>('\b') - .map<'f'>('\f') - .map<'n'>('\n') - .map<'r'>('\r') - .map<'t'>('\t'); - -constexpr auto escaped_quote = lexy::symbol_table // - .map<'"'>('"'); - -template -struct StringValue { - static constexpr auto rule = [] { - // Arbitrary code points - auto c = character - control; - - auto back_escape = lexy::dsl::backslash_escape // - .symbol(); - - auto quote_escape = lexy::dsl::escape(lexy::dsl::lit_c<'"'>) // - .symbol(); - - auto quotes = lexy::dsl::delimited(lexy::dsl::lit_c<'"'>, lexy::dsl::not_followed_by(lexy::dsl::lit_c<'"'>, lexy::dsl::lit_c<'"'>)); - - return quotes(c, back_escape, quote_escape); - }(); - - static constexpr auto value = lexy::as_string; -}; - -template -struct EscapeValue { - static constexpr auto rule = [] { - auto id = lexy::dsl::identifier(id_head, id_tail); - - return lexy::dsl::lit_b >> - (lexy::dsl::lit_b | - (id >> lexy::dsl::lit_b)); - }(); - static constexpr auto value = - lexy::callback_with_state( - [](const auto& state, auto&& lexeme) { - auto check = std::string_view { lexeme.data(), lexeme.size() }; - if (auto value = state.find_value(check); value != state.end()) - return std::string(value->second.data(), value->second.size()); - return fmt::format("${}$", check); - }, - [](auto&& lexeme) { - return fmt::format("${}$", std::string_view { lexeme.data(), lexeme.size() }); - }, - [](lexy::nullopt = {}) { - return std::string(1, Options.EscapeChar); - }, - [](const auto& state, lexy::nullopt = {}) { - return std::string(1, Options.EscapeChar); - }); -}; - -template -struct PlainValue { - static constexpr auto rule = [] { - auto min_skip = lexy::dsl::lit_b / lexy::dsl::ascii::newline; - if constexpr (Options.SupportStrings) { - return lexy::dsl::identifier(character - min_skip); - } else { - auto escape_check_char = [=] { - if constexpr (Options.EscapeChar != 0) { - return character - (min_skip / lexy::dsl::lit_b); - } else { - return character - min_skip; - } - }(); - auto id_check_char = escape_check_char - lexy::dsl::lit_b<'\\'>; - auto id_segment = lexy::dsl::identifier(id_check_char); - auto escape_segement = lexy::dsl::token(escape_check_char); - auto escape_sym = lexy::dsl::symbol(escape_segement); - auto escape_rule = lexy::dsl::lit_b<'\\'> >> escape_sym; - if constexpr (Options.EscapeChar != 0) { - return lexy::dsl::list(lexy::dsl::p> | id_segment | escape_rule); - } else { - return lexy::dsl::list(id_segment | escape_rule); - } - } - }(); - static constexpr auto value = lexy::as_string; -}; - -template -struct Value { - static constexpr auto rule = [] { - if constexpr (Options.SupportStrings) { - return lexy::dsl::p> | lexy::dsl::p>; - } else { - return lexy::dsl::p>; - } - }(); - static constexpr auto value = lexy::forward; -}; - -template -struct SepConst { - static constexpr auto rule = lexy::dsl::lit_b; - static constexpr auto value = lexy::constant(1); -}; - -template -struct Seperator { - static constexpr auto rule = lexy::dsl::list(lexy::dsl::p>); - static constexpr auto value = lexy::count; -}; - -template -struct LineEnd { - static constexpr auto rule = lexy::dsl::list(lexy::dsl::p>, lexy::dsl::trailing_sep(lexy::dsl::p>)); - static constexpr auto value = lexy::fold_inplace( - std::initializer_list {}, - [](ovdl::csv::LineObject& result, auto&& arg) { - if constexpr (std::is_same_v, std::size_t>) { - // Count seperators, adds to previous value, making it a position - using position_type = ovdl::csv::LineObject::position_type; - result.emplace_back(static_cast(arg + result.back().first), ""); - } else { - if (result.empty()) result.emplace_back(0u, LEXY_MOV(arg)); - else { - auto& [pos, value] = result.back(); - value = arg; - } - } - }); -}; - -template -struct Line { - - static constexpr auto suffix_setter(ovdl::csv::LineObject& line) { - auto& [position, value] = line.back(); - if (value.empty()) { - line.set_suffix_end(position); - line.pop_back(); - } else { - line.set_suffix_end(position + 1); - } - }; - - static constexpr auto rule = lexy::dsl::p> | lexy::dsl::p> >> lexy::dsl::p>; - static constexpr auto value = - lexy::callback( - [](ovdl::csv::LineObject&& line) { - suffix_setter(line); - return LEXY_MOV(line); - }, - [](std::size_t prefix_count, ovdl::csv::LineObject&& line) { - line.set_prefix_end(prefix_count); - // position needs to be adjusted to prefix - for (auto& [position, value] : line) { - position += prefix_count; - } - suffix_setter(line); - return LEXY_MOV(line); - }); -}; - -template -struct File { - static constexpr auto rule = - lexy::dsl::whitespace(lexy::dsl::newline) + - lexy::dsl::opt(lexy::dsl::list(lexy::dsl::p>, lexy::dsl::trailing_sep(lexy::dsl::eol))); - - static constexpr auto value = lexy::as_list>; -}; - -using CommaFile = File; -using ColonFile = File; -using SemiColonFile = File; -using TabFile = File; -using BarFile = File; - -namespace strings { - using CommaFile = File; - using ColonFile = File; - using SemiColonFile = File; - using TabFile = File; - using BarFile = File; -} diff --git a/src/openvic-dataloader/csv/Parser.cpp b/src/openvic-dataloader/csv/Parser.cpp index 40f0037..748b866 100644 --- a/src/openvic-dataloader/csv/Parser.cpp +++ b/src/openvic-dataloader/csv/Parser.cpp @@ -1,18 +1,29 @@ +#include +#include +#include +#include #include +#include +#include +#include +#include +#include #include +#include +#include +#include #include #include -#include +#include +#include #include #include -#include -#include +#include #include "csv/CsvGrammar.hpp" #include "detail/BasicBufferHandler.hpp" -#include "detail/Errors.hpp" #include "detail/LexyReportError.hpp" #include "detail/OStreamOutputIterator.hpp" diff --git a/src/openvic-dataloader/csv/ValueNode.cpp b/src/openvic-dataloader/csv/ValueNode.cpp new file mode 100644 index 0000000..84c375c --- /dev/null +++ b/src/openvic-dataloader/csv/ValueNode.cpp @@ -0,0 +1,57 @@ +#include + +using namespace ovdl; +using namespace ovdl::csv; + +ValueNode::ValueNode() = default; + +ValueNode::ValueNode(std::string_view string, position_type position) + : ValueNode(std::initializer_list { std::string(string) }, position) { +} +ValueNode::ValueNode(std::vector value_list, position_type position) + : _value_list(value_list), + _position(position) { +} +ValueNode::ValueNode(std::initializer_list value_list, position_type position) + : _value_list(value_list), + _position(position) { +} + +void ValueNode::set_position(position_type position) { + _position = position; +} + +ValueNode::position_type ValueNode::get_position() const { + return _position; +} + +void ValueNode::set_as_list(internal_value_type value) { + _value_list.assign({ value }); +} + +void ValueNode::add_to_list(internal_value_type value) { + _value_list.push_back(value); +} + +bool ValueNode::list_is_empty() const { + return _value_list.empty(); +} + +std::string ValueNode::make(std::string_view prefix, std::optional suffix) const { + std::vector pre_joined(_value_list.size()); + + for (auto&& value : _value_list) { + const auto& result = std::visit([&](auto&& arg) -> std::string { + using T = std::decay_t; + if constexpr (std::is_same_v) { + return arg; + } else if constexpr (std::is_same_v) { + return arg.as_string(prefix, suffix); + } + }, + value); + pre_joined.push_back(result); + } + + return fmt::format("{}", fmt::join(pre_joined, "")); +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1