Skip to content

Commit

Permalink
case-insensitive equal example added (#1025)
Browse files Browse the repository at this point in the history
Merging!
Thanks

* case-insensitive example added (#1023)

* resolving issues

* moving the file to the right place

* making iequals to match other examples (#1025)

* comment suggestion update (#1025)

* added case_insensitive_equals.cpp to CMakeLists.txt file (#1025)
  • Loading branch information
the-moisrex authored Oct 16, 2021
1 parent 90ee1b1 commit bac1330
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
64 changes: 64 additions & 0 deletions examples/algorithms/using_existing/case_insensitive_equals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//==================================================================================================
/*
EVE - Expressive Vector Engine
Copyright : EVE Contributors & Maintainers
SPDX-License-Identifier: MIT
*/
//==================================================================================================


#include <eve/algo/equal.hpp>
#include <eve/function/sub.hpp>

#include <cstdint>
#include <string_view>

namespace ascii
{
struct
{
// This accepts both std::uint8_t and eve::wide<std::uint8_t>
EVE_FORCEINLINE auto operator()(eve::like<std::uint8_t> 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<std::uint8_t const *>(a.begin());
auto *l1 = reinterpret_cast<std::uint8_t const *>(a.end());
auto *f2 = reinterpret_cast<std::uint8_t const *>(b.begin());

return eve::algo::equal(eve::algo::as_range(f1, l1),
f2,
[](eve::wide<std::uint8_t> a, eve::wide<std::uint8_t> 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);
};

0 comments on commit bac1330

Please sign in to comment.