From 969a201c7596e6197b04f48be1ed8886168e2261 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 9 Oct 2021 06:43:48 +0300 Subject: [PATCH] Update implementation of find_first_of --- include/boost/core/string_view.hpp | 83 ++++++++++++++---------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/include/boost/core/string_view.hpp b/include/boost/core/string_view.hpp index bf29541..9be8dc2 100644 --- a/include/boost/core/string_view.hpp +++ b/include/boost/core/string_view.hpp @@ -37,9 +37,48 @@ namespace core namespace detail { +template struct sv_to_uchar +{ + typedef Ch type; +}; + +template<> struct sv_to_uchar +{ + typedef unsigned char type; +}; + template BOOST_CXX14_CONSTEXPR std::size_t find_first_of( Ch const* p_, std::size_t n_, Ch const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT { - if( n >= 16 ) + 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; + } + } + + if( use_table ) + { + for( std::size_t i = pos; i < n_; ++i ) + { + UCh ch = p_[ i ]; + if( ch >= 0 && ch < 256 && table[ ch ] ) return i; + } + } + else if( n >= 16 ) { for( std::size_t i = pos; i < n_; ++i ) { @@ -63,38 +102,6 @@ template BOOST_CXX14_CONSTEXPR std::size_t find_first_of( Ch const* p_ return static_cast( -1 ); } -#if CHAR_BIT == 8 - -BOOST_CXX14_CONSTEXPR std::size_t find_first_of( char const* p_, std::size_t n_, char const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT -{ - unsigned char table[ 256 ] = {}; - - for( std::size_t j = 0; j < n; ++j ) - { - unsigned char ch = s[ j ]; - table[ ch ] = 1; - } - - for( std::size_t i = pos; i < n_; ++i ) - { - unsigned char ch = p_[ i ]; - if( table[ ch ] ) return i; - } - - return static_cast( -1 ); -} - -#endif - -#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L - -std::size_t find_first_of( char8_t const* p_, std::size_t n_, char8_t const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT -{ - return detail::find_first_of( reinterpret_cast< char const* >( p_ ), n_, reinterpret_cast< char const* >( s ), pos, n ); -} - -#endif - template BOOST_CXX14_CONSTEXPR std::size_t find_last_of( Ch const* p_, Ch const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT { std::size_t const npos = static_cast< std::size_t >( -1 ); @@ -172,16 +179,6 @@ std::size_t find_last_of( char8_t const* p_, char8_t const* s, std::size_t pos, #endif -template struct sv_to_uchar -{ - typedef Ch type; -}; - -template<> struct sv_to_uchar -{ - typedef unsigned char type; -}; - template BOOST_CXX14_CONSTEXPR std::size_t find_first_not_of( Ch const* p_, std::size_t n_, Ch const* s, std::size_t pos, std::size_t n ) BOOST_NOEXCEPT { typedef typename sv_to_uchar::type UCh;