From 5d7b469e29affb283796b1a0415e856ee232b6d4 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sun, 10 Oct 2021 03:09:53 +0300 Subject: [PATCH] Improve .find --- include/boost/core/string_view.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/boost/core/string_view.hpp b/include/boost/core/string_view.hpp index 30186e9..05c7393 100644 --- a/include/boost/core/string_view.hpp +++ b/include/boost/core/string_view.hpp @@ -640,20 +640,21 @@ public: BOOST_CXX14_CONSTEXPR size_type find( Ch const* s, size_type pos, size_type n ) const BOOST_NOEXCEPT { + if( n == 1 ) return find( s[0], pos ); + if( pos + n > size() ) return npos; if( n == 0 ) return pos; Ch const* p = data() + pos; - Ch const* last = data() + size(); + Ch const* last = data() + size() - n + 1; for( ;; ) { p = traits_type::find( p, last - p, s[0] ); if( p == 0 ) break; - if( static_cast( last - p ) < n ) break; - if( traits_type::compare( p, s, n ) == 0 ) return p - data(); + if( traits_type::compare( p + 1, s + 1, n - 1 ) == 0 ) return p - data(); ++p; }