AK: Add dedicated Traits for c-strings

This commit is contained in:
Hendiadyoin1 2021-11-07 14:37:05 +01:00 committed by Ali Mohammad Pur
parent dea86f511c
commit 6c6e917cf0
Notes: sideshowbarker 2024-07-17 22:46:53 +09:00

View file

@ -9,6 +9,8 @@
#include <AK/Concepts.h>
#include <AK/Forward.h>
#include <AK/HashFunctions.h>
#include <AK/StringHash.h>
#include <string.h>
namespace AK {
@ -37,7 +39,7 @@ requires(IsIntegral<T>) struct Traits<T> : public GenericTraits<T> {
};
template<typename T>
requires(IsPointer<T>) struct Traits<T> : public GenericTraits<T> {
requires(IsPointer<T> && !Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> {
static unsigned hash(T p) { return ptr_hash((FlatPtr)p); }
static constexpr bool is_trivial() { return true; }
};
@ -48,6 +50,13 @@ struct Traits<T> : public GenericTraits<T> {
static constexpr bool is_trivial() { return Traits<UnderlyingType<T>>::is_trivial(); }
};
template<typename T>
requires(Detail::IsPointerOfType<char, T>) struct Traits<T> : public GenericTraits<T> {
static unsigned hash(T const value) { return string_hash(value, strlen(value)); }
static constexpr bool equals(T const a, T const b) { return strcmp(a, b); }
static constexpr bool is_trivial() { return true; }
};
}
using AK::GenericTraits;