From a71d028a874e6ce2942bf8980c175dd151e3d8ee Mon Sep 17 00:00:00 2001 From: m-peko Date: Thu, 28 May 2020 08:06:39 +0200 Subject: [PATCH] Use std::string_view instead of std::string in split_range --- include/booleval/utils/split_range.hpp | 47 ++++++++++++-------------- src/token/tokenizer.cpp | 9 ++--- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/include/booleval/utils/split_range.hpp b/include/booleval/utils/split_range.hpp index 29522bf..6f67d6b 100644 --- a/include/booleval/utils/split_range.hpp +++ b/include/booleval/utils/split_range.hpp @@ -30,7 +30,6 @@ #ifndef BOOLEVAL_SPLIT_RANGE_H #define BOOLEVAL_SPLIT_RANGE_H -#include #include #include #include @@ -73,11 +72,10 @@ class split_range { using reference = value_type&; public: - explicit constexpr iterator(std::string_view strv, std::string const& delims) noexcept + explicit constexpr iterator(std::string_view strv, std::string_view delims) noexcept : strv_(strv), delims_(delims), prev_(std::begin(strv_)) { - apply_split_options(); next(); } @@ -88,17 +86,17 @@ class split_range { iterator& operator=(iterator const& rhs) = default; [[nodiscard]] constexpr auto operator*() const noexcept { - return curr_elem_; + return curr_value_; } [[nodiscard]] constexpr auto operator->() const noexcept { - return &curr_elem_; + return &curr_value_; } constexpr iterator& operator++() noexcept { - ++curr_elem_.index; + ++curr_value_.index; - if (curr_elem_.quoted) { + if (curr_value_.quoted) { curr_ = skip_char(curr_, whitespace_char); if (std::end(strv_) != curr_ && curr_ != std::prev(std::end(strv_))) { curr_ = std::next(curr_); @@ -119,7 +117,7 @@ class split_range { } } - curr_elem_.quoted = false; + curr_value_.quoted = false; next(); return *this; @@ -146,15 +144,6 @@ class split_range { : prev_(end) {} - /** - * Applies splitting options to the string containing delimiters. - */ - constexpr void apply_split_options() noexcept { - if constexpr (is_set(iter_options, split_options::split_by_whitespace)) { - delims_.append(1, ' '); - } - } - /** * Finds the iterator pointing to the beginning of the next token in the specified range. * @@ -166,7 +155,7 @@ class split_range { [[nodiscard]] constexpr std::string_view::iterator find_next(std::string_view::iterator first, std::string_view::iterator last) const noexcept { if constexpr (is_set(iter_options, split_options::allow_quoted_strings)) { - if (curr_elem_.quoted) { + if (curr_value_.quoted) { return find_next_quote(first, last); } else { return std::min( @@ -189,7 +178,13 @@ class split_range { */ [[nodiscard]] constexpr std::string_view::iterator find_next_delim(std::string_view::iterator first, std::string_view::iterator last) const noexcept { - return std::find_first_of(first, last, std::begin(delims_), std::end(delims_)); + if constexpr (is_set(iter_options, split_options::split_by_whitespace)) { + auto whitespace = std::find(first, last, whitespace_char); + auto other_delim = std::find_first_of(first, last, std::begin(delims_), std::end(delims_)); + return std::min(whitespace, other_delim); + } else { + return std::find_first_of(first, last, std::begin(delims_), std::end(delims_)); + } } /** @@ -227,12 +222,12 @@ class split_range { return; } - curr_elem_.quoted = false; + curr_value_.quoted = false; curr_ = find_next(prev_, std::end(strv_)); if (std::end(strv_) != curr_) { if (iter_quote_char == *curr_) { - curr_elem_.quoted = true; + curr_value_.quoted = true; prev_ = skip_char(curr_, iter_quote_char); curr_ = find_next(prev_, std::end(strv_)); } else { @@ -248,7 +243,7 @@ class split_range { return; } - curr_elem_.value = strv_.substr( + curr_value_.value = strv_.substr( std::distance(std::begin(strv_), prev_), std::distance(prev_, curr_) ); @@ -256,18 +251,18 @@ class split_range { private: std::string_view strv_; - std::string delims_; + std::string_view delims_; std::string_view::iterator prev_; std::string_view::iterator curr_; - element curr_elem_; + value_type curr_value_; }; public: constexpr split_range() = default; - constexpr split_range(std::string_view strv, std::string const& delims = " ") + constexpr split_range(std::string_view strv, std::string_view delims = " ") : strv_(strv), delims_(delims) {} @@ -300,7 +295,7 @@ class split_range { private: std::string_view strv_; - std::string delims_; + std::string_view delims_; }; } // utils diff --git a/src/token/tokenizer.cpp b/src/token/tokenizer.cpp index 968712a..fe38ae8 100644 --- a/src/token/tokenizer.cpp +++ b/src/token/tokenizer.cpp @@ -74,13 +74,8 @@ void tokenizer::tokenize() { utils::split_options::split_by_whitespace | utils::split_options::allow_quoted_strings; - auto tokens_range = utils::split_range( - expression_, - utils::join( - std::begin(single_char_symbols), - std::end(single_char_symbols) - ) - ); + auto delims = utils::join(std::begin(single_char_symbols), std::end(single_char_symbols)); + auto tokens_range = utils::split_range(expression_, delims); for (auto const& [quoted, index, value] : tokens_range) { auto type = quoted ? token_type::field : map_to_token_type(value);