1. Table of Contents
2. Changelog
2.1. R3
-
Remove
fromnoexcept
andstd :: basic_string :: first () std :: basic_string :: last () -
Remove template parameters from proposed wording
-
Added a
andconst &
overload for&&
andstd :: basic_string :: first () std :: basic_string :: last () -
Adjust Abstract
2.2. R2
-
Add similarities to
in Motivation and Scopestd :: span
2.3. R1
-
Add more to Motivation and Scope
-
Add Technical Specifications
-
Add Proposed Wording
2.4. R0
-
Initial revision
3. Motivation and Scope
Thestd :: string
(and std :: string_view
) api has a lot of nice features and utility functions, but can be improved even further.
Currently, to get the first N
characters of a string, we need to do std :: string :: substr ( 0 , N )
. This is fine, but people might be confused as to the usage of the 0
in the function call.
Similarly, to get the last N
characters of a string, we need to do std :: string :: substr ( size () - N , N )
. This is also fine, but again, the first parameter could lead to confusion, or even a possible error if the wrong number were passed.
To facilitate these simple operations, I suggest adding
and
(and similar counterparts to
).
Having these 2 utility functions brings a couple of benefits:
-
It is more obvious and clear what the programmer intent is,
conveys better meaning as opposed to. first ( N )
, same for. substr ( 0 , N )
and. last ( N ) . substr ( size () - N , N ) -
Since there are less arguments, and the argument is only a count, there is less potential for programmer error
-
It is generally simpler and less typing.
There is the argument of the
API already being far too large, and this being only a minor addition.
Although this is true, the
API has some weird missing features, and the richness of the API is important, currently, programmers are often forced to make small utility functions themselves to do trivial operations on a string that should be part of the STL.
There is also already existing similar functionality in
.
and
already exist and implement the feature proposed in this paper, even though there is also the
function, suggesting that
and
had enough interest to be added besides
.
I believe it should be the same treatment for
and
, which only have
.
4. Impact on the Standard
These are 2 minor utility functions to be added to thestd :: string
and std :: string_view
API, not impacting any existing code, so the impact on the standard is minimal.
5. Technical Specifications
-
takes 1 parameter:std :: basic_string :: first ()
, returns asize_t count
and has astd :: basic_string
guarantee.noexcept -
is the amount of characters to included, starting from the start of the original string.count -
Determines the effective length of
ascount std :: min ( count , size ())
-
-
-
takes 1 parameter:std :: basic_string :: last ()
, returns asize_t count
and has astd :: basic_string
guarantee.noexcept -
is the amount of characters to be included, starting from the end of the original string.count -
Determines the effective length of
ascount std :: min ( count , size ())
-
-
-
takes 1 parameter:std :: basic_string_view :: first ()
, returns asize_t count
and has astd :: basic_string_view
guarantee.noexcept -
is the amount of characters to included, starting from the start of the original string_view.count -
Determines the effective length of
ascount std :: min ( count , size ())
-
-
-
takes 1 parameter:std :: basic_string_view :: last ()
, returns asize_t count
and has astd :: basic_string_view
guarantee.noexcept -
is the amount of characters to be included, starting from the end of the original string_view.count -
Determines the effective length of
ascount std :: min ( count , size ())
-
-
6. Proposed Wording
6.1. Addition to < string >
Add the following to 23.4.3.1 basic.string.general:// [...] namespace std { // [...] // [string.ops], string operations // [...] constexpr basic_string first ( size_t count ) const & ; constexpr basic_string first ( size_t count ) && ; constexpr basic_string last ( size_t count ) const & ; constexpr basic_string last ( size_t count ) && ; }
6.2. std :: basic_string :: first
Add the following subclause to 23.4.3.8 string.ops:-
23.4.3.?:
basic_string :: first [ string . first ] -
constexpr basic_string first ( size_t count ) const ; -
Effects: Determines the effective length
of the string to be returned asxlen
.std :: min ( count , size ())
Returns the characters in the range[ data (), data () + xlen ); -
Returns:
basic_string ( data (), data () + xlen )
-
-
6.3. std :: basic_string :: last
Add the following subclause to 23.4.3.8 string.ops:-
23.4.3.?:
basic_string :: last [ string . last ] -
constexpr basic_string last ( size_t count ) const ; -
Effects: Determines the effective length
of the string to be returned asxlen
.std :: min ( count , size ())
Returns the characters in the range[ data () + size () - xlen , xlen ); -
Returns:
basic_string ( data () + size () - xlen , data () + size ())
-
-
6.4. Addition to < string_view >
Add the following to 23.3.3.1 string.view.template.general:// [...] namespace std { // [...] // [string.ops], string operations // [...] constexpr basic_string_view first ( size_t count ) const noexcept ; constexpr basic_string_view last ( size_t count ) const noexcept ; }
6.5. std :: basic_string_view :: first
Add the following subclause to 23.3.3.8 string.view.ops:-
23.3.3.?:
basic_string_view :: first [ string . first ] -
constexpr basic_string_view first ( size_t count ) const noexcept ; -
Effects: Determines the effective length
of the string to be returned asxlen
.std :: min ( count , size ())
Returns the characters in the range[ data (), data () + xlen ); -
Returns:
basic_string_view ( data (), data () + xlen )
-
-
6.6. std :: basic_string_view :: last
Add the following subclause to 23.3.3.8 string.view.ops:-
23.3.3.?:
basic_string_view :: last [ string . last ] -
constexpr basic_string_view last ( size_t count ) const noexcept ; -
Effects: Determines the effective length
of the string to be returned asxlen
.std :: min ( count , size ())
Returns the characters in the range[ data () + size () - xlen , xlen ); -
Returns:
basic_string_view ( data () + size () - xlen , data () + size ())
-
-