Version (1) won't do implicit types conversions for you, you can either handle them in the predicate or use views::convert
.
unaligned iterator pointing to where in haystack the sequence is. If the needle is empty == haystack.begin() is returned. If the needle isn't found == haystack.end().
#include <eve/module/algo.hpp>
#include <eve/module/core.hpp>
#include <span>
#include <vector>
#include <string_view>
#include <iostream>
std::ptrdiff_t
substring_in_string(std::string_view haystack, std::string_view needle)
{
std::span h(reinterpret_cast<const std::uint8_t *>(haystack.data()), haystack.size());
std::span n(reinterpret_cast<const std::uint8_t *>(needle.data()), needle.size());
return res - h.begin();
}
void
basic_example()
{
std::cout << __func__ << std::endl;
std::string_view haystack("one two three");
std::cout << "empty: " << substring_in_string(haystack, "") << std::endl;
std::cout << "one: " << substring_in_string(haystack, "one") << std::endl;
std::cout << "two: " << substring_in_string(haystack, "two") << std::endl;
std::cout << "three: " << substring_in_string(haystack, "three") << std::endl;
std::cout << "not there: " << substring_in_string(haystack, "not there") << std::endl;
std::cout << "<<<<<<<\n" << std::endl;
}
void
implicit_conversions()
{
std::cout << "<<< " << __func__ << " <<<" << std::endl;
std::vector<double> haystack {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
std::vector<std::int8_t> needle {2, 3};
std::cout <<
"chars in doubles: " << (
eve::algo::search(haystack, needle) - haystack.begin())
<< std::endl;
std::cout << "<<<<<<<\n" << std::endl;
}
std::ptrdiff_t
substring_in_string_case_insensitive(std::string_view haystack, std::string_view needle)
{
std::span h(reinterpret_cast<const std::uint8_t *>(haystack.data()), haystack.size());
std::span n(reinterpret_cast<const std::uint8_t *>(needle.data()), needle.size());
{
constexpr std::uint8_t alphabet_length = 'z' - 'a';
constexpr std::uint8_t a_A_offset = 'a' - 'A';
return eve::sub[(c - std::uint8_t(
'a')) <= alphabet_length](c, a_A_offset);
};
return eve::algo::search(h, n, [&](
auto x,
auto y) {
return to_upper(x) == to_upper(y); })
- h.begin();
}
void
custom_predicates()
{
std::cout << "<<< " << __func__ << " <<<" << std::endl;
std::cout << "substring_in_string_case_insensitive: "
<< substring_in_string_case_insensitive("Uabc Ab", "C A") << std::endl;
std::cout << "substring_in_string_case_insensitive: "
<< substring_in_string_case_insensitive("Uabc Ab", "Ab") << std::endl;
std::cout << "<<<<<<<\n" << std::endl;
}
int
main()
{
basic_example();
implicit_conversions();
custom_predicates();
}
Specifies semantic compatibility between wrapper/wrapped types.
Definition product_type.hpp:107
constexpr auto sub
tuple_callable computing the difference of its first argument with the sum of the others.
Definition sub.hpp:122