@@ -471,6 +471,17 @@ nssv_DISABLE_MSVC_WARNINGS( 4455 26481 26472 )
471471
472472namespace nonstd { namespace sv_lite {
473473
474+ //
475+ // basic_string_view declaration:
476+ //
477+
478+ template
479+ <
480+ class CharT ,
481+ class Traits = std::char_traits<CharT>
482+ >
483+ class basic_string_view ;
484+
474485namespace detail {
475486
476487// support constexpr comparison in C++14;
@@ -538,28 +549,33 @@ inline nssv_constexpr14 std::size_t length( CharT * s )
538549
539550#endif // OPTIMIZE
540551
541- } // namespace detail
552+ #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
553+ #if defined(__OPTIMIZE__)
542554
543- template
544- <
545- class CharT ,
546- class Traits = std::char_traits<CharT>
547- >
548- class basic_string_view ;
555+ // gcc, clang provide __OPTIMIZE__
556+ // Expect tail call optimization to make search() non-recursive:
549557
550- #if nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
551- namespace detail {
558+ template < class CharT , class Traits = std::char_traits<CharT> >
559+ constexpr const CharT* search ( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
560+ {
561+ return haystack.starts_with ( needle ) ? haystack.begin () :
562+ haystack.empty () ? haystack.end () : search ( haystack.substr (1 ), needle );
563+ }
552564
553- template <class CharT , class Traits = std::char_traits<CharT> >
554- constexpr const CharT* search (basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle)
565+ #else // OPTIMIZE
566+
567+ // non-recursive:
568+
569+ template < class CharT , class Traits = std::char_traits<CharT> >
570+ constexpr const CharT* search ( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
555571{
556- return haystack.starts_with (needle) ? haystack.begin () :
557- haystack.empty () ? haystack.end () :
558- search (haystack.substr (1 ), needle);
572+ return std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end () );
559573}
560574
575+ #endif // OPTIMIZE
576+ #endif // nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
577+
561578} // namespace detail
562- #endif
563579
564580//
565581// basic_string_view:
0 commit comments