Skip to content

Commit

Permalink
Use std::string_view instead of std::string in split_range
Browse files Browse the repository at this point in the history
  • Loading branch information
m-peko committed Jun 1, 2020
1 parent 8eecca3 commit a71d028
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
47 changes: 21 additions & 26 deletions include/booleval/utils/split_range.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#ifndef BOOLEVAL_SPLIT_RANGE_H
#define BOOLEVAL_SPLIT_RANGE_H

#include <string>
#include <iterator>
#include <algorithm>
#include <string_view>
Expand Down Expand Up @@ -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();
}

Expand All @@ -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_);
Expand All @@ -119,7 +117,7 @@ class split_range {
}
}

curr_elem_.quoted = false;
curr_value_.quoted = false;

next();
return *this;
Expand All @@ -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.
*
Expand All @@ -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(
Expand All @@ -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_));
}
}

/**
Expand Down Expand Up @@ -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 {
Expand All @@ -248,26 +243,26 @@ class split_range {
return;
}

curr_elem_.value = strv_.substr(
curr_value_.value = strv_.substr(
std::distance(std::begin(strv_), prev_),
std::distance(prev_, curr_)
);
}

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)
{}
Expand Down Expand Up @@ -300,7 +295,7 @@ class split_range {

private:
std::string_view strv_;
std::string delims_;
std::string_view delims_;
};

} // utils
Expand Down
9 changes: 2 additions & 7 deletions src/token/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<options>(
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<options>(expression_, delims);

for (auto const& [quoted, index, value] : tokens_range) {
auto type = quoted ? token_type::field : map_to_token_type(value);
Expand Down

0 comments on commit a71d028

Please sign in to comment.