Added string_view/ref::substr overloads taking no arguments.

This effectively adds support for pos=0 default argument value.
The separate overload is better as it avoids instantiating std::min,
boost::throw_exception and removes std::out_of_range construction, which
potentially reduces code size, while maintaining the same behavior.

Fixes https://github.com/boostorg/utility/issues/96.
This commit is contained in:
Andrey Semashev 2022-05-03 00:46:03 +03:00
parent 0c1d01d30a
commit 0106ffda5f
2 changed files with 8 additions and 0 deletions

View File

@ -161,6 +161,10 @@ namespace boost {
// basic_string_ref string operations
basic_string_ref substr() const {
return basic_string_ref(data(), size());
}
basic_string_ref substr(size_type pos, size_type n=npos) const {
if ( pos > size())
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_ref::substr" ) );

View File

@ -189,6 +189,10 @@ namespace boost {
return rlen;
}
BOOST_CXX14_CONSTEXPR basic_string_view substr() const {
return basic_string_view(data(), size());
}
BOOST_CXX14_CONSTEXPR basic_string_view substr(size_type pos, size_type n=npos) const {
if ( pos > size())
BOOST_THROW_EXCEPTION( std::out_of_range ( "string_view::substr" ) );