-
Notifications
You must be signed in to change notification settings - Fork 15
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
Implement a trim feature for string and string_view #51
Comments
Do you still need help? This needs a new repo in |
Hi @sandordargo -- yes we're still looking for help here. @bretbrownjr I assume there's no proposal yet? Right off one might ask if this should be implemented as free functions (aka like boost string algo) instead of as part string_view |
I don't have specific design notes or plans. I do have observations that this feature is conspicuously missing from the standard. If I were picking up this task, I would consider what existing C++ libraries like boost already provide for this feature. |
Boost provides many different trims. Apart from the left/rigth/all versions, it provides copy and in-place versions, and even versions suffixed with But maybe it would fit better with the design decisions of What do you think? |
Hey I am also interested in this and putting out more libraries in general. I think I will add this to the agenda of our next weekly sync. I don't think we have a standard approach to adding member functions to std objects for demonstration. We probably will have to do this as helper functions, e. g. beman::string_utils::trim. I would suggest adding a general purpose string utils library. As I think there's tons of improvements we could propose, e. g. Split. |
you can add member functions to a class, call it
|
Could this be implemented with range adaptors instead of member functions? That way trims could be used on any type that implements the interface for a range of characters, not just string and string_view. Sure, you could convert a range into a string_view, but only with contiguous ranges, right? I'm not sure if there's any need for the trim algorithm to require contiguous ranges. |
@LapNik same logic applies to |
@alexcohn I'm sorry, I can't figure out what you mean by If the member function didn't modify the string and instead returned a return std::basic_string_view{ std::views::drop_while( s, is_white_space ) }; Returning a std::string s = " a"s;
auto trimmed = std::ranges::ltrim( s );
std::string copy{ trimmed };
s.erase( s.begin(), trimmed.begin() ); Would something like this work? https://gist.github.com/LapNik/076909e6b704e99221b6e86e243b4c94 Closure objects not included. |
Could we use something like |
@LapNik sure you are right, returning a trimmed |
Just going to drop in (haha) and ask this -- what about drop_while? The cppreference example has this for trim_left
https://en.cppreference.com/w/cpp/ranges/drop_while_view The original plan for c++26 ranges also had a views::drop_last_while which would make the backwards trim just as simple. Of course these aren't dedicated to string processing and hence would likely be less efficient than a dedicated algorithm. |
I'm personally flexible on the details of the implementation, but it's an extremely common operation, spaces are modeled with APIs like
std::isspace
, and it's surprising it's not available already.Probably the proposal should provide
ltrim
andrtrim
as well.StackOverflow with advice on implementing trim from scratch for reference:
https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring
The text was updated successfully, but these errors were encountered: