AK: Ensure string types are actually considered hash-compatible

The AnyString concept is currently broken because it checks whether a
StringView is constructible from a type T. The StringView constructors,
however, only accept constant rvalue references - i.e. `T const&`.

This also adds a test to ensure this continues to work.
This commit is contained in:
Timothy Flynn 2023-02-02 10:48:11 -05:00 committed by Linus Groh
parent 2f67f2ba3d
commit f31bd190b0
Notes: sideshowbarker 2024-07-17 00:52:09 +09:00
2 changed files with 25 additions and 1 deletions

View file

@ -49,7 +49,7 @@ template<typename T, typename S>
concept DerivedFrom = IsBaseOf<S, T>;
template<typename T>
concept AnyString = IsConstructible<StringView, T>;
concept AnyString = IsConstructible<StringView, RemoveCVReference<T> const&>;
template<typename T, typename U>
concept HashCompatible = IsHashCompatible<Detail::Decay<T>, Detail::Decay<U>>;

View file

@ -6,9 +6,33 @@
#include <LibTest/TestCase.h>
#include <AK/ByteBuffer.h>
#include <AK/Concepts.h>
#include <AK/FlyString.h>
#include <AK/String.h>
#include <AK/StringUtils.h>
#include <AK/Vector.h>
TEST_CASE(hash_compatible)
{
static_assert(AK::Concepts::HashCompatible<String, StringView>);
static_assert(AK::Concepts::HashCompatible<String, FlyString>);
static_assert(AK::Concepts::HashCompatible<StringView, String>);
static_assert(AK::Concepts::HashCompatible<StringView, FlyString>);
static_assert(AK::Concepts::HashCompatible<FlyString, String>);
static_assert(AK::Concepts::HashCompatible<FlyString, StringView>);
static_assert(AK::Concepts::HashCompatible<DeprecatedString, StringView>);
static_assert(AK::Concepts::HashCompatible<DeprecatedString, DeprecatedFlyString>);
static_assert(AK::Concepts::HashCompatible<StringView, DeprecatedString>);
static_assert(AK::Concepts::HashCompatible<StringView, DeprecatedFlyString>);
static_assert(AK::Concepts::HashCompatible<DeprecatedFlyString, DeprecatedString>);
static_assert(AK::Concepts::HashCompatible<DeprecatedFlyString, StringView>);
static_assert(AK::Concepts::HashCompatible<StringView, ByteBuffer>);
static_assert(AK::Concepts::HashCompatible<ByteBuffer, StringView>);
}
TEST_CASE(matches_null)
{
EXPECT(AK::StringUtils::matches(StringView(), StringView()));