Skip to content

Commit

Permalink
find() fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperPaul123 committed Aug 3, 2023
1 parent 1549638 commit 65fe895
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 15 additions & 7 deletions include/flux/op/find.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#ifndef FLUX_OP_FIND_HPP_INCLUDED
#define FLUX_OP_FIND_HPP_INCLUDED

#include <flux/core/utils.hpp>
#include <flux/op/for_each_while.hpp>
#include <type_traits>

#include <cstring>
#include <type_traits>

namespace flux {

Expand All @@ -29,17 +31,23 @@ struct find_fn {
constexpr auto operator()(Seq&& seq, Value const& value) const
-> cursor_t<Seq>
{
constexpr auto memchr_opt = std::is_integral_v<Value> &&
flux::detail::is_any_of<element_t<Seq>, char*, signed char*,
unsigned char*, const char*, const signed char*,
const unsigned char*>;
constexpr auto memchr_opt = flux::contiguous_sequence<Seq> &&
flux::sized_sequence<Seq> && std::is_integral_v<Value> &&
flux::detail::any_of<value_t<Seq>, char, signed char, unsigned char,
const char, const signed char, const unsigned char>;

if constexpr (memchr_opt) {
if (std::is_constant_evaluated()) {
return impl(seq, value);
} else {
std::memchr(flux::data(seq), value, flux::usize(seq));
}
auto location = std::memchr(flux::data(seq), value,
flux::usize(seq) * sizeof(value_t<Seq>));
if (location == nullptr)
return flux::usize(seq);
else
return static_cast<const value_t<Seq>*>(location) -
flux::data(seq);

} else {
return impl(seq, value);
}
Expand Down
5 changes: 5 additions & 0 deletions test/test_find.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ TEST_CASE("find")
REQUIRE(idx == std::ssize(vec));
}

{
std::string_view str = "abcdefg";
auto idx = flux::find(str, 'd');
REQUIRE(idx == 3);
}
}

0 comments on commit 65fe895

Please sign in to comment.