Skip to content

Commit bac9b55

Browse files
committed
Add configuration macro nssv_CONFIG_CONSTEXPR11_STD_SEARCH (#50, thanks @oliverlee)
Allow to avoid constexpr with std::search() and select a local implementation for it for C++14 constexpr.
1 parent 5e8d492 commit bac9b55

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,16 @@ Define this to 1 to provide `std::string`– `nonstd::string_view` interoper
201201
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:
202202

203203
-D<b>nssv_CONFIG_NO_STREAM_INSERTION</b>=1
204-
Define this to 1 to omit the use of standard streams. Default is undefined
204+
Define this to 1 to omit the use of standard streams. Default is undefined.
205+
206+
### Avoid `constexpr` with `std::search()`
207+
208+
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.
209+
210+
If you encounter an error related to this, you can control this behaviour with the following macro:
211+
212+
-D<b>nssv_CONFIG_CONSTEXPR11_STD_SEARCH</b>=0
213+
Define this to 0 to omit the use constexpr with `std::search()` and substitute a local implementation using `nssv_constexpr14`. Default is 1.
205214

206215
### Enable compilation errors
207216

include/nonstd/string_view.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
# define nssv_CONFIG_NO_STREAM_INSERTION 0
7070
#endif
7171

72+
#ifndef nssv_CONFIG_CONSTEXPR11_STD_SEARCH
73+
# define nssv_CONFIG_CONSTEXPR11_STD_SEARCH 1
74+
#endif
75+
7276
// Control presence of exception handling (try and auto discover):
7377

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

568572
// non-recursive:
569573

574+
#if nssv_CONFIG_CONSTEXPR11_STD_SEARCH
575+
570576
template< class CharT, class Traits = std::char_traits<CharT> >
571577
constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
572578
{
573579
return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
574580
}
575581

582+
#else // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
583+
584+
template< class CharT, class Traits = std::char_traits<CharT> >
585+
nssv_constexpr14 const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
586+
{
587+
while ( needle.size() <= haystack.size() )
588+
{
589+
if ( haystack.starts_with(needle) )
590+
{
591+
return haystack.cbegin();
592+
}
593+
haystack = basic_string_view<CharT, Traits>{ haystack.begin() + 1, haystack.size() - 1U };
594+
}
595+
return haystack.cend();
596+
}
597+
#endif // nssv_CONFIG_CONSTEXPR11_STD_SEARCH
598+
576599
#endif // OPTIMIZE
577600
#endif // nssv_CPP11_OR_GREATER && ! nssv_CPP17_OR_GREATER
578601

0 commit comments

Comments
 (0)