Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFE: string extension - split, strip, join #43

Open
Xecutor opened this issue Dec 1, 2019 · 4 comments
Open

RFE: string extension - split, strip, join #43

Xecutor opened this issue Dec 1, 2019 · 4 comments
Labels
enhancement New feature or request version-2.1 Features that are destined for version 2.1

Comments

@Xecutor
Copy link

Xecutor commented Dec 1, 2019

Hello.

I have some preliminary implementation split range, strip transform and join sink. But I'm not sure if they should be part of main lib or should be placed in a separate file as an optional extension.

strip takes two const iterators of input, two const iterators of separator and produces basic_string_view as output.
strip takes range of const CharT*, basic_string_view or basic_string as input and produces basic_string_view as output.
join takes range of const CharT*, basic_string_view or basic_string and const CharT*, basic_string_view or basic_string as input and produces basic_string as output.

Need to discuss this and I'll make corresponding PR.

@simonask
Copy link
Owner

simonask commented Dec 2, 2019

It seems like join is already present as chain?

I would suggest expressing split (or split_by?) as taking an input range instead of iterator pairs. Since strings can be implicitly converted to ranges, this would work:

auto strings = "a,b"sv | split_by(",") | to_vector(); // {"a"sv, "b"sv}

@simonask simonask added enhancement New feature or request version-2.1 Features that are destined for version 2.1 labels Dec 2, 2019
@Xecutor
Copy link
Author

Xecutor commented Dec 2, 2019

I wasn't clear enough: join with separator, like inverse split : vector{{"aa","bb","cc"}} | join(":") yields std::string aa:bb:cc.

Arbitrary input range for split_by seems somewhat problematic. std::search takes two iterators of the same type as input.

@Hazurl
Copy link
Contributor

Hazurl commented Dec 2, 2019

To be consistent with the rest of the library, join should return a range outputting an element every other index, a sink can then be used to create a string from it. Same for split, it should take a range and return a range of range like in_groups_of.

strip, on the other hand, seems hard to implement (given its definition in the python std lib). The end trim is not possible without a lookahead, which means an allocation to store the elements.

@Xecutor
Copy link
Author

Xecutor commented Dec 5, 2019

If split_by should produce range of string_views, then these string_views should be constructed from pointer and size. I tried to do this with range as an input to split_by, but it looks quite cumbersome, and range based iterators being strictly forward iterators kind of incompatible with ability to takes distance between iterators and the memory not necessary will be continuous. The original idea was that split(" hello , world , foo , bar", ",') | string() | to_vector(); will produce vector of string_views to original string. Probably I'm missing something...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request version-2.1 Features that are destined for version 2.1
Projects
None yet
Development

No branches or pull requests

3 participants