From b01f2fe4d28414964c7c7d27c51308846f72ed7b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 9 Oct 2021 07:08:48 +0300 Subject: [PATCH] Add optimized implementation of find_last_not_of --- include/boost/core/string_view.hpp | 130 +++++++++++++++++++++++------ 1 file changed, 105 insertions(+), 25 deletions(-) diff --git a/include/boost/core/string_view.hpp b/include/boost/core/string_view.hpp index 5ea29c2..8540f0c 100644 --- a/include/boost/core/string_view.hpp +++ b/include/boost/core/string_view.hpp @@ -235,6 +235,84 @@ template BOOST_CXX14_CONSTEXPR std::size_t find_first_not_of( Ch const return static_cast( -1 ); } +template BOOST_CXX14_CONSTEXPR std::size_t find_last_not_of( Ch const* p_, Ch const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT +{ + typedef typename sv_to_uchar::type UCh; + + unsigned char table[ 256 ] = {}; + + bool use_table = true; + + for( std::size_t j = 0; j < n; ++j ) + { + UCh ch = s[ j ]; + + if( ch >= 0 && ch < 256 ) + { + table[ ch ] = 1; + } + else + { + use_table = false; + break; + } + } + + std::size_t const npos = static_cast< std::size_t >( -1 ); + + std::size_t i = pos; + + if( use_table ) + { + do + { + UCh ch = p_[ i ]; + + if( !( ch >= 0 && ch < 256 && table[ ch ] ) ) return i; + + --i; + } + while( i != npos ); + } + else if( n >= 16 ) + { + do + { + Ch ch = p_[ i ]; + + if( std::char_traits::find( s, n, ch ) == 0 ) return i; + + --i; + } + while( i != npos ); + } + else + { + do + { + Ch ch = p_[ i ]; + + bool r = false; + + for( std::size_t j = 0; j < n; ++j ) + { + if( s[ j ] == ch ) + { + r = true; + break; + } + } + + if( !r ) return i; + + --i; + } + while( i != npos ); + } + + return npos; +} + } // namespace detail template class basic_string_view @@ -728,36 +806,21 @@ public: BOOST_CXX14_CONSTEXPR size_type find_last_not_of( basic_string_view str, size_type pos = npos ) const BOOST_NOEXCEPT { - if( size() == 0 ) - { - return npos; - } - - if( pos > size() - 1 ) - { - pos = size() - 1; - } - - do - { - if( !str.contains( p_[ pos ] ) ) return pos; - --pos; - } - while( pos != npos ); - - return npos; + return find_last_not_of( str.data(), pos, str.size() ); } BOOST_CXX14_CONSTEXPR size_type find_last_not_of( Ch c, size_type pos = npos ) const BOOST_NOEXCEPT { - if( size() == 0 ) + size_type m = size(); + + if( m == 0 ) { return npos; } - if( pos > size() - 1 ) + if( pos > m - 1 ) { - pos = size() - 1; + pos = m - 1; } do @@ -770,14 +833,31 @@ public: return npos; } - BOOST_CONSTEXPR size_type find_last_not_of( Ch const* s, size_type pos, size_type n ) const BOOST_NOEXCEPT + BOOST_CXX14_CONSTEXPR size_type find_last_not_of( Ch const* s, size_type pos, size_type n ) const BOOST_NOEXCEPT { - return find_last_not_of( basic_string_view( s, n ), pos ); + if( n == 1 ) + { + return find_last_not_of( s[0], pos ); + } + + size_type m = size(); + + if( m == 0 ) + { + return npos; + } + + if( pos > m - 1 ) + { + pos = m - 1; + } + + return detail::find_last_not_of( data(), s, pos, n ); } - BOOST_CONSTEXPR size_type find_last_not_of( Ch const* s, size_type pos = npos ) const BOOST_NOEXCEPT + BOOST_CXX14_CONSTEXPR size_type find_last_not_of( Ch const* s, size_type pos = npos ) const BOOST_NOEXCEPT { - return find_last_not_of( basic_string_view( s ), pos ); + return find_last_not_of( s, pos, traits_type::length( s ) ); } // contains