From 65188a565f68e96433db904c0087278982ddcbba Mon Sep 17 00:00:00 2001 From: Arthur Passos Date: Tue, 10 Jan 2023 16:36:32 -0300 Subject: [PATCH] Remove state from Extractor. It's usually a bad idea that bites us in the future, it did. --- .../impl/LazyEscapingKeyValuePairExtractor.h | 30 +++++++++++-------- .../src/impl/state/KeyStateHandler.cpp | 9 ++---- .../src/impl/state/KeyStateHandler.h | 7 ++--- .../src/impl/state/StateHandler.h | 2 -- .../src/impl/state/ValueStateHandler.cpp | 11 ++----- .../src/impl/state/ValueStateHandler.h | 9 ++---- 6 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/Functions/keyvaluepair/src/impl/LazyEscapingKeyValuePairExtractor.h b/src/Functions/keyvaluepair/src/impl/LazyEscapingKeyValuePairExtractor.h index 97b3bbd6b75..7536a824ab4 100644 --- a/src/Functions/keyvaluepair/src/impl/LazyEscapingKeyValuePairExtractor.h +++ b/src/Functions/keyvaluepair/src/impl/LazyEscapingKeyValuePairExtractor.h @@ -34,13 +34,18 @@ public: [[nodiscard]] Response extract(const std::string & file) override { + std::unordered_map response_views; + auto state = State::WAITING_KEY; std::size_t pos = 0; + std::string_view key; + std::string_view value; + while (state != State::END) { - auto next_state = processState(file, pos, state); + auto next_state = processState(file, pos, state, key, value, response_views); pos = next_state.position_in_string; state = next_state.state; @@ -50,36 +55,39 @@ public: } private: - NextState processState(const std::string & file, std::size_t pos, State state) + NextState processState(const std::string & file, std::size_t pos, State state, + std::string_view & key, std::string_view & value, + std::unordered_map & response_views) { switch (state) { case State::WAITING_KEY: return key_state_handler.wait(file, pos); case State::READING_KEY: - return key_state_handler.read(file, pos); + return key_state_handler.read(file, pos, key); case State::READING_ENCLOSED_KEY: - return key_state_handler.readEnclosed(file, pos); + return key_state_handler.readEnclosed(file, pos, key); case State::READING_KV_DELIMITER: return key_state_handler.readKeyValueDelimiter(file, pos); case State::WAITING_VALUE: return value_state_handler.wait(file, pos); case State::READING_VALUE: - return value_state_handler.read(file, pos); + return value_state_handler.read(file, pos, value); case State::READING_ENCLOSED_VALUE: - return value_state_handler.readEnclosed(file, pos); + return value_state_handler.readEnclosed(file, pos, value); case State::READING_EMPTY_VALUE: - return value_state_handler.readEmpty(file, pos); + return value_state_handler.readEmpty(file, pos, value); case State::FLUSH_PAIR: - return flushPair(file, pos); + return flushPair(file, pos, key, value, response_views); case END: return {pos, state}; } } - NextState flushPair(const std::string & file, std::size_t pos) + NextState flushPair(const std::string & file, std::size_t pos, std::string_view key, + std::string_view value, std::unordered_map & response_views) { - response_views[key_state_handler.getElement()] = value_state_handler.getElement(); + response_views[key] = value; return {pos, pos == file.size() ? State::END : State::WAITING_KEY}; } @@ -87,8 +95,6 @@ private: KeyStateHandler key_state_handler; ValueStateHandler value_state_handler; std::shared_ptr> escaping_processor; - - std::unordered_map response_views; }; } diff --git a/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.cpp b/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.cpp index 625f62e31bd..d4b162236af 100644 --- a/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.cpp +++ b/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.cpp @@ -30,7 +30,7 @@ NextState KeyStateHandler::wait(const std::string & file, size_t pos) const return {pos, State::END}; } -NextState KeyStateHandler::read(const std::string & file, size_t pos) +NextState KeyStateHandler::read(const std::string & file, size_t pos, std::string_view & key) { bool escape = false; @@ -65,7 +65,7 @@ NextState KeyStateHandler::read(const std::string & file, size_t pos) return {pos, State::END}; } -NextState KeyStateHandler::readEnclosed(const std::string & file, size_t pos) +NextState KeyStateHandler::readEnclosed(const std::string & file, size_t pos, std::string_view & key) { auto start_index = pos; key = {}; @@ -104,9 +104,4 @@ NextState KeyStateHandler::readKeyValueDelimiter(const std::string & file, size_ } } -std::string_view KeyStateHandler::getElement() const -{ - return key; -} - } diff --git a/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.h b/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.h index 4f8189e8cbe..215700c8f09 100644 --- a/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.h +++ b/src/Functions/keyvaluepair/src/impl/state/KeyStateHandler.h @@ -15,15 +15,12 @@ public: KeyStateHandler(char key_value_delimiter, char escape_character, std::optional enclosing_character); [[nodiscard]] NextState wait(const std::string & file, size_t pos) const; - [[nodiscard]] NextState read(const std::string & file, size_t pos); - [[nodiscard]] NextState readEnclosed(const std::string & file, size_t pos); + [[nodiscard]] NextState read(const std::string & file, size_t pos, std::string_view & key); + [[nodiscard]] NextState readEnclosed(const std::string & file, size_t pos, std::string_view & key); [[nodiscard]] NextState readKeyValueDelimiter(const std::string & file, size_t pos) const; - [[nodiscard]] std::string_view getElement() const override; - private: const char key_value_delimiter; - std::string_view key; }; } diff --git a/src/Functions/keyvaluepair/src/impl/state/StateHandler.h b/src/Functions/keyvaluepair/src/impl/state/StateHandler.h index 8e3cb4a615f..0a7f503ecf5 100644 --- a/src/Functions/keyvaluepair/src/impl/state/StateHandler.h +++ b/src/Functions/keyvaluepair/src/impl/state/StateHandler.h @@ -16,8 +16,6 @@ struct StateHandler const char escape_character = '\\'; const std::optional enclosing_character; - [[nodiscard]] virtual std::string_view getElement() const = 0; - protected: [[nodiscard]] static std::string_view createElement(const std::string & file, std::size_t begin, std::size_t end); }; diff --git a/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.cpp b/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.cpp index c6a8fd76920..896ee2a6a42 100644 --- a/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.cpp +++ b/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.cpp @@ -41,7 +41,7 @@ NextState ValueStateHandler::wait(const std::string & file, size_t pos) const return {pos, State::READING_EMPTY_VALUE}; } -NextState ValueStateHandler::read(const std::string & file, size_t pos) +NextState ValueStateHandler::read(const std::string & file, size_t pos, std::string_view & value) { bool escape = false; @@ -73,7 +73,7 @@ NextState ValueStateHandler::read(const std::string & file, size_t pos) return {pos, State::FLUSH_PAIR}; } -NextState ValueStateHandler::readEnclosed(const std::string & file, size_t pos) +NextState ValueStateHandler::readEnclosed(const std::string & file, size_t pos, std::string_view & value) { auto start_index = pos; @@ -92,7 +92,7 @@ NextState ValueStateHandler::readEnclosed(const std::string & file, size_t pos) return {pos, State::END}; } -NextState ValueStateHandler::readEmpty(const std::string &, size_t pos) +NextState ValueStateHandler::readEmpty(const std::string &, size_t pos, std::string_view & value) { value = {}; return {pos + 1, State::FLUSH_PAIR}; @@ -103,9 +103,4 @@ bool ValueStateHandler::isValidCharacter(char character) const return special_character_allowlist.contains(character) || std::isalnum(character) || character == '_'; } -std::string_view ValueStateHandler::getElement() const -{ - return value; -} - } diff --git a/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.h b/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.h index d7f784e1661..346cabc0f64 100644 --- a/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.h +++ b/src/Functions/keyvaluepair/src/impl/state/ValueStateHandler.h @@ -19,16 +19,13 @@ public: std::unordered_set special_character_allowlist_); [[nodiscard]] NextState wait(const std::string & file, size_t pos) const; - [[nodiscard]] NextState read(const std::string & file, size_t pos); - [[nodiscard]] NextState readEnclosed(const std::string & file, size_t pos); - [[nodiscard]] NextState readEmpty(const std::string & file, size_t pos); - - [[nodiscard]] std::string_view getElement() const override; + [[nodiscard]] NextState read(const std::string & file, size_t pos, std::string_view & value); + [[nodiscard]] NextState readEnclosed(const std::string & file, size_t pos, std::string_view & value); + [[nodiscard]] static NextState readEmpty(const std::string & file, size_t pos, std::string_view & value); private: const char item_delimiter; std::unordered_set special_character_allowlist; - std::string_view value; bool isValidCharacter(char character) const; };