Skip to content

Commit

Permalink
Add configuration macro nssv_CONFIG_CONSTEXPR11_STD_SEARCH (#50, thanks
Browse files Browse the repository at this point in the history
@oliverlee)

Allow to avoid constexpr with std::search() and select a local implementation for it for C++14 constexpr.
  • Loading branch information
martinmoene committed Dec 29, 2023
1 parent 5e8d492 commit bac9b55
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,16 @@ Define this to 1 to provide `std::string`– `nonstd::string_view` interoper
At default, *string-view lite* provides operations to overload the `operator<<`. If you want to use the library without the use of standard streams, you can control this with the following macro:

-D<b>nssv_CONFIG_NO_STREAM_INSERTION</b>=1
Define this to 1 to omit the use of standard streams. Default is undefined
Define this to 1 to omit the use of standard streams. Default is undefined.

### Avoid `constexpr` with `std::search()`

At default, *string-view lite* may use constexpr with `std::search()` for `string_view::find()` when compiling for C++11 or C++14, whereas `std::search()` is only constexpr since C++20. This may occur when macro `__OPTIMIZE__` is not defined and a non-recursive implementation for search is selected. Macro `__OPTIMIZE__` is used with GCC and clang.

If you encounter an error related to this, you can control this behaviour with the following macro:

-D<b>nssv_CONFIG_CONSTEXPR11_STD_SEARCH</b>=0
Define this to 0 to omit the use constexpr with `std::search()` and substitute a local implementation using `nssv_constexpr14`. Default is 1.

### Enable compilation errors

Expand Down
23 changes: 23 additions & 0 deletions include/nonstd/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
# define nssv_CONFIG_NO_STREAM_INSERTION 0
#endif

#ifndef nssv_CONFIG_CONSTEXPR11_STD_SEARCH
# define nssv_CONFIG_CONSTEXPR11_STD_SEARCH 1
#endif

// Control presence of exception handling (try and auto discover):

#ifndef nssv_CONFIG_NO_EXCEPTIONS
Expand Down Expand Up @@ -567,12 +571,31 @@ constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_

// non-recursive:

#if nssv_CONFIG_CONSTEXPR11_STD_SEARCH

template< class CharT, class Traits = std::char_traits<CharT> >
constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
}

#else // nssv_CONFIG_CONSTEXPR11_STD_SEARCH

template< class CharT, class Traits = std::char_traits<CharT> >
nssv_constexpr14 const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
while ( needle.size() <= haystack.size() )
{
if ( haystack.starts_with(needle) )
{
return haystack.cbegin();
}
haystack = basic_string_view<CharT, Traits>{ haystack.begin() + 1, haystack.size() - 1U };
}
return haystack.cend();
}
#endif // nssv_CONFIG_CONSTEXPR11_STD_SEARCH

#endif // OPTIMIZE
#endif // nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER

Expand Down

0 comments on commit bac9b55

Please sign in to comment.