-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
case-insensitive equal example added (#1025)
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
1 parent
90ee1b1
commit bac1330
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
examples/algorithms/using_existing/case_insensitive_equals.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |