Skip to content

Split range adaptors#10

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

Split range adaptors#10
bartholomaios wants to merge 4 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>>();

}

TEST(Split, SplitsOnceOnSingleValue) {
auto name = "Alice's"sv;

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 name = "Alice's"sv;
auto name = "Alice's"sv;

Comment on lines +34 to +36
auto parts = "A"sv
| bss::views::split(""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 = "A"sv
| bss::views::split(""sv)
| std::ranges::to<std::vector<std::string>>();
auto parts = "A"sv | bss::views::split(""sv) | std::ranges::to<std::vector<std::string>>();

Comment on lines +42 to +44
auto parts = "Alice"sv
| bss::views::split(""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 = "Alice"sv
| bss::views::split(""sv)
| std::ranges::to<std::vector<std::string>>();
auto parts = "Alice"sv | bss::views::split(""sv) | std::ranges::to<std::vector<std::string>>();

Comment on lines +55 to +57
auto parts = "Down the Rabbit-Hole."sv
| bss::views::split("the"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("the"sv)
| std::ranges::to<std::vector<std::string>>();
auto parts = "Down the Rabbit-Hole."sv | bss::views::split("the"sv) | std::ranges::to<std::vector<std::string>>();

Comment on lines +63 to +65
auto parts = "Alice was beginning to get very tired"sv
| bss::views::split(" 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 parts = "Alice was beginning to get very tired"sv
| bss::views::split(" t"sv)
| std::ranges::to<std::vector<std::string>>();
auto parts = "Alice was beginning to get very tired"sv | bss::views::split(" t"sv) |
std::ranges::to<std::vector<std::string>>();

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

coveralls commented Jun 8, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 27506021983

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.226%

Details

  • Patch coverage: 2 uncovered changes across 1 file (51 of 53 lines covered, 96.23%).

Uncovered Changes

File Changed Covered %
include/beman/str_split/detail/split_iterator.hpp 15 13 86.67%
Total (4 files) 53 51 96.23%

Coverage Regressions

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


Coverage Stats

Coverage Status
Relevant Lines: 53
Covered Lines: 51
Line Coverage: 96.23%
Coverage Strength: 22.21 hits per line

💛 - Coveralls

Comment on lines +58 to +60
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>>();

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 = "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>>();
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>>();

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