From 0106ffda5f9d27ab0cb3685a1cc9c234fc78ecd8 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Tue, 3 May 2022 00:46:03 +0300 Subject: [PATCH] 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. --- include/boost/utility/string_ref.hpp | 4 ++++ include/boost/utility/string_view.hpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/include/boost/utility/string_ref.hpp b/include/boost/utility/string_ref.hpp index ffadb2e..6e9f7bb 100644 --- a/include/boost/utility/string_ref.hpp +++ b/include/boost/utility/string_ref.hpp @@ -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" ) ); diff --git a/include/boost/utility/string_view.hpp b/include/boost/utility/string_view.hpp index c3be4e3..6c682b2 100644 --- a/include/boost/utility/string_view.hpp +++ b/include/boost/utility/string_view.hpp @@ -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" ) );