Skip to content

Split range adaptors#10

Open
bartholomaios wants to merge 3 commits into
mainfrom
split-range-adaptors
Open

Split range adaptors#10
bartholomaios wants to merge 3 commits into
mainfrom
split-range-adaptors

Conversation

@bartholomaios

Copy link
Copy Markdown
Contributor

beman::str_split::split_when_view is a range adaptor based on std::ranges::split_view that splits a view on delimiters found by the provided search function. The search function takes the remaining sub-range to search as input, and returns the nested sub-range around the next delimiter.

// Custom delimiter search operation.
class any_seq_of {
public:
    constexpr explicit any_seq_of(std::string_view delimiters)
        : delimiters_(delimiters)
    {
    }

    template<class V>
        requires std::constructible_from<std::string_view, V>
    V operator()(V view)
    {
        auto str = std::string_view(view);
        auto begin_pos = str.find_first_of(delimiters_);
        if (begin_pos == str.npos) {
            return {std::ranges::end(view), std::ranges::end(view)};
        }
        auto end_pos = str.find_first_not_of(delimiters_, begin_pos + 1);
        return {std::ranges::begin(view) + begin_pos, std::ranges::begin(view) + end_pos};
    }

private:
    std::string_view delimiters_;
};

std::vector<std::string> make_words() {
    return "Alice \t was beginning to\tget \tvery    tired"sv
            | bss::views::split_when(any_seq_of(" \t"sv))
            | std::ranges::to<std::vector<std::string>>();
}

I've also implemented std::range::split_view as beman::str_split::split_view. I haven't written range adaptors before, so this was a good learning exercise.

beman::str_split::split_when_view still has all the same ergonomic issues that std::ranges::split_view has W.R.T working with strings. Nonetheless, I still think this is a useful starting point for range-based splitting.

I've not made an attempt to use modules yet. Everything is header based. I do want to support modules, I just haven't take the time to learn how to use them.

@bartholomaios bartholomaios requested a review from aryann as a code owner June 8, 2026 02:34

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

pre-commit

[pre-commit] reported by reviewdog 🐶

auto words = "Alice \t was beginning to\tget \tvery tired"sv
| bss::views::split_when(any_seq_of(" \t"sv))
| std::ranges::to<std::vector<std::string>>();


namespace beman::str_split::detail {

template<class Parent>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
template<class Parent>
template <class Parent>

Comment on lines +14 to +16
template<class Parent>
class split_iterator
{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
template<class Parent>
class split_iterator
{
template <class Parent>
class split_iterator {

Comment on lines +21 to +22
public:
using iterator_concept = std::forward_iterator_tag;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
public:
using iterator_concept = std::forward_iterator_tag;
public:
using iterator_concept = std::forward_iterator_tag;

Comment on lines +24 to +25
using value_type = Parent::base_subrange;
using difference_type = std::iter_difference_t<base_iterator>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
using value_type = Parent::base_subrange;
using difference_type = std::iter_difference_t<base_iterator>;
using value_type = Parent::base_subrange;
using difference_type = std::iter_difference_t<base_iterator>;

Comment on lines +30 to +34
: parent_(std::addressof(parent))
, current_(std::move(current))
, next_(std::move(next))
{
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
: parent_(std::addressof(parent))
, current_(std::move(current))
, next_(std::move(next))
{
}
: parent_(std::addressof(parent)), current_(std::move(current)), next_(std::move(next)) {}

Comment on lines +24 to +26
V operator()(V view)
{
auto str = std::string_view(view);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
V operator()(V view)
{
auto str = std::string_view(view);
V operator()(V view) {
auto str = std::string_view(view);

return {std::ranges::begin(view) + begin_pos, std::ranges::begin(view) + end_pos};
}

private:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
private:
private:

std::string_view delimiters_;
};

}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
}
} // namespace

Comment on lines +42 to +44
auto words = ""sv
| bss::views::split_when(any_seq_of(" \t"sv))
| std::ranges::to<std::vector<std::string>>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
auto words = ""sv
| bss::views::split_when(any_seq_of(" \t"sv))
| std::ranges::to<std::vector<std::string>>();
auto words = ""sv | bss::views::split_when(any_seq_of(" \t"sv)) | std::ranges::to<std::vector<std::string>>();

Comment on lines +50 to +52
auto parts = "Down the Rabbit-Hole"sv
| bss::views::split_when(any_seq_of("-."sv))
| std::ranges::to<std::vector<std::string>>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[pre-commit] reported by reviewdog 🐶

Suggested change
auto parts = "Down the Rabbit-Hole"sv
| bss::views::split_when(any_seq_of("-."sv))
| std::ranges::to<std::vector<std::string>>();
auto parts = "Down the Rabbit-Hole"sv | bss::views::split_when(any_seq_of("-."sv)) |
std::ranges::to<std::vector<std::string>>();

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 27112801698

Warning

No base build found for commit f097c5d on main.
Coverage changes can't be calculated without a base build.
If a base build is processing, this comment will update automatically when it completes.

Coverage: 96.491%

Details

  • Patch coverage: 2 uncovered changes across 1 file (55 of 57 lines covered, 96.49%).

Uncovered Changes

File Changed Covered %
include/beman/str_split/detail/split_iterator.hpp 19 17 89.47%
Total (4 files) 57 55 96.49%

Coverage Regressions

Requires a base build to compare against. How to fix this →


Coverage Stats

Coverage Status
Relevant Lines: 57
Covered Lines: 55
Line Coverage: 96.49%
Coverage Strength: 22.35 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants