2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
21 #include <folly/portability/String.h>
22 #include <folly/Range.h>
24 #include "hphp/util/bstring.h"
25 #include "hphp/util/hash.h"
29 //////////////////////////////////////////////////////////////////////
32 * Small function objects for use with STL containers and things of
36 //////////////////////////////////////////////////////////////////////
39 size_t operator()(const char* s
) const {
40 return hash_string_cs(s
, strlen(s
));
45 bool operator()(const char *s1
, const char *s2
) const {
46 return strcmp(s1
, s2
) < 0;
51 bool operator()(const char* s1
, const char* s2
) const {
52 return strcmp(s1
, s2
) == 0;
57 bool operator()(const std::string
&s1
, const std::string
&s2
) const {
58 return strcmp(s1
.c_str(), s2
.c_str()) < 0;
63 using is_transparent
= void;
65 bool operator()(const std::string
&s1
, const std::string
&s2
) const {
66 return strcasecmp(s1
.c_str(), s2
.c_str()) < 0;
68 bool operator()(const std::string
&s1
, folly::StringPiece s2
) const {
69 return bstrcasecmp(s1
, s2
) < 0;
71 bool operator()(folly::StringPiece s1
, const std::string
&s2
) const {
72 return bstrcasecmp(s1
, s2
) < 0;
74 bool operator()(const std::string
&s1
, const char* s2
) const {
75 return strcasecmp(s1
.c_str(), s2
) < 0;
77 bool operator()(const char* s1
, const std::string
&s2
) const {
78 return strcasecmp(s1
, s2
.c_str()) < 0;
83 size_t operator()(const std::string
&s
) const {
84 return hash_string_cs_unsafe(s
.c_str(), s
.size());
86 size_t hash(const std::string
&s
) const {
92 struct integralHashCompare
{
93 bool equal(T s1
, T s2
) const {
94 static_assert(std::is_integral
<T
>::value
, "");
97 size_t hash(T s
) const {
98 static_assert(sizeof(T
) <= sizeof(int64_t), "");
99 return hash_int64(int64_t(s
));
103 struct stringHashCompare
{
104 bool equal(const std::string
& s1
, const std::string
& s2
) const {
107 size_t hash(const std::string
& s
) const {
108 return hash_string_cs_unsafe(s
.c_str(), s
.size());
112 template <typename T
, typename U
, typename THash
, typename UHash
>
113 struct pairHashCompare
{
117 using PairType
= std::pair
<T
, U
>;
119 size_t operator() (const PairType
& pair
) const {
123 size_t hash(const PairType
& pair
) const {
124 return hash_int64_pair(thash
.hash(pair
.first
), uhash
.hash(pair
.second
));
127 bool equal(const PairType
& a
, const PairType
& b
) const {
128 return thash
.equal(a
.first
, b
.first
) && uhash
.equal(a
.second
, b
.second
);
133 size_t operator() (const int64_t v
) const {
134 return hash_int64(v
);
136 size_t hash(const int64_t v
) const {
137 return operator()(v
);
139 bool equal(const int64_t lhs
, const int64_t rhs
) const {
145 struct pointer_hash
{
146 size_t operator() (const T
*const p
) const {
147 return hash_int64(intptr_t(p
));
149 size_t hash(const T
*const p
) const {
150 return operator()(p
);
152 bool equal(const T
*const lhs
,
153 const T
*const rhs
) const {
159 struct smart_pointer_hash
{
160 size_t operator() (const T
&p
) const {
161 return (size_t)hash_int64(intptr_t(p
.get()));
163 size_t hash (const T
&p
) const {
164 return operator()(p
);
166 bool equal(const T
&lhs
, const T
&rhs
) const {
167 return lhs
.get() == rhs
.get();
171 //////////////////////////////////////////////////////////////////////
172 // Case-insensitive ones.
175 size_t operator()(const char *s
) const {
176 return hash_string_i(s
, strlen(s
));
180 bool operator()(const char* s1
, const char* s2
) const {
181 return strcasecmp(s1
, s2
) == 0;
185 struct string_hashi
{
186 size_t operator()(const std::string
&s
) const {
187 return hash_string_i_unsafe(s
.data(), s
.size());
191 template<typename S
, typename S2
=S
>
192 struct stringlike_eqstri
{
193 bool operator()(const S
&s1
, const S2
&s2
) const {
194 return s1
.size() == s2
.size() &&
195 strncasecmp(s1
.data(), s2
.data(), s1
.size()) == 0;
198 typedef stringlike_eqstri
<std::string
> string_eqstri
;
200 struct string_lessi
{
201 bool operator()(const std::string
&s1
, const std::string
&s2
) const {
202 return strcasecmp(s1
.data(), s2
.data()) < 0;
206 //////////////////////////////////////////////////////////////////////