diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 45ad962d0e..8a96479ca9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,6 +7,7 @@ make_unit( "examples" start_here.cpp ) make_unit( "examples" algorithms/using_existing/memcmp__two_range_algorithms_interface_specifics.cpp ) make_unit( "examples" algorithms/using_existing/inclusive_scan_zip__using_zip_with_algorithms.cpp ) +make_unit( "examples" algorithms/using_existing/case_insensitive_equals.cpp ) find_package(Threads REQUIRED) make_unit( "examples" algorithms/using_existing/inclusive_scan_par_unseq__using_eve_to_build_both_vectorized_and_parallel_algos.cpp ) diff --git a/examples/algorithms/using_existing/case_insensitive_equals.cpp b/examples/algorithms/using_existing/case_insensitive_equals.cpp new file mode 100644 index 0000000000..d0efa9e542 --- /dev/null +++ b/examples/algorithms/using_existing/case_insensitive_equals.cpp @@ -0,0 +1,64 @@ +//================================================================================================== +/* + EVE - Expressive Vector Engine + Copyright : EVE Contributors & Maintainers + SPDX-License-Identifier: MIT +*/ +//================================================================================================== + + +#include +#include + +#include +#include + +namespace ascii +{ + struct + { + // This accepts both std::uint8_t and eve::wide + EVE_FORCEINLINE auto operator()(eve::like auto c) const + { + // if C - 'a' is less than 26, then C is uppercased, otherwide it's lowercased + // 'a' < c < 'z' is equivalent to (c - 'a') < 26 because of the underflow + return eve::sub[c - 'a' <= 26](c, ('a' - 'A')); + } + + } inline constexpr our_to_upper; + + bool iequals(std::string_view a, std::string_view b) + { + // If they're not the same size, why bother converting them both to uppercase and then check? + // btw, it will just crash if the sizes are not equal. You cannot run algo::equal on different sizes. + if( a.size() != b.size() ) + return false; + + // converting them to uint8_t; because our to upper algorithm relies on unsigned integers. + auto *f1 = reinterpret_cast(a.begin()); + auto *l1 = reinterpret_cast(a.end()); + auto *f2 = reinterpret_cast(b.begin()); + + return eve::algo::equal(eve::algo::as_range(f1, l1), + f2, + [](eve::wide a, eve::wide b) + { + // convert both to uppercase and then check if they're equal + return our_to_upper(a) == our_to_upper(b); + }); + } + +} + + +// ----------------------- + +#include "test.hpp" + +TTS_CASE("IEquals, basics") +{ + TTS_EQUAL(ascii::iequals("123 One Two aZ", "123 oNe TWo Az"), true); + TTS_EQUAL(ascii::iequals("103 One Two aZ", "123 oNe TWo Az"), false); + TTS_EQUAL(ascii::iequals("not the same size as", "123 oNe TWo Az"), false); + TTS_EQUAL(ascii::iequals("Short", "SHorT"), true); +};