1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:27:35 +00:00

AK+Kernel: Unify Traits<T>::equals()'s argument order on different types

There was a small mishmash of argument order, as seen on the table:

                 | Traits<T>::equals(U, T) | Traits<T>::equals(T, U)
   ============= | ======================= | =======================
   uses equals() | HashMap                 | Vector, HashTable
defines equals() | *String[^1]             | ByteBuffer

[^1]: String, DeprecatedString, their Fly-type equivalents and KString.

This mostly meant that you couldn't use a StringView for finding a value
in Vector<String>.

I'm changing the order of arguments to make the trait type itself first
(`Traits<T>::equals(T, U)`), as I think it's more expected and makes us
more consistent with the rest of the functions that put the stored type
first (like StringUtils functions and binary_serach). I've also renamed
the variable name "other" in find functions to "entry" to give more
importance to the value.

With this change, each of the following lines will now compile
successfully:

    Vector<String>().contains_slow("WHF!"sv);
    HashTable<String>().contains("WHF!"sv);
    HashMap<ByteBuffer, int>().contains("WHF!"sv.bytes());
This commit is contained in:
Karol Kosek 2023-08-21 16:38:11 +02:00 committed by Andreas Kling
parent 5ff7448fee
commit e575ee4462
7 changed files with 26 additions and 16 deletions

View file

@ -9,6 +9,7 @@
#include <AK/DeprecatedString.h>
#include <AK/OwnPtr.h>
#include <AK/ReverseIterator.h>
#include <AK/String.h>
#include <AK/Vector.h>
TEST_CASE(construct)
@ -431,6 +432,13 @@ TEST_CASE(should_find_predicate_index)
EXPECT(!v.find_first_index_if([](auto const v) { return v == 123; }).has_value());
}
TEST_CASE(should_find_using_a_hashcompatible_value)
{
// Tests whether a hash-compatible value can be used to compare (Strings cannot be impliticly constructed from a StringView.)
Vector v { "hello!"_string };
EXPECT(v.contains_slow("hello!"sv));
}
TEST_CASE(should_contain_start)
{
// Tests whether value is found if at the start of the range.