Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string_hash.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_STRING_HASH_H_
20 #define PAGESPEED_KERNEL_BASE_STRING_HASH_H_
21 
22 #include <cstddef>
23 
26 
27 namespace net_instaweb {
28 
31 template<class CharTransform, typename IntType>
32 inline IntType HashString(const char* s, size_t len) {
35  IntType result = 0;
36  for (const char* end = s + len; s != end; ++s) {
37  result = (result * 131) + CharTransform::Normalize(*s);
38  }
39  return result;
40 }
41 
44 inline size_t JoinHash(size_t a, size_t b) {
45  return (a + 56) * 137 + b * 151;
46 }
47 
49 struct CasePreserve {
57  static unsigned char Normalize(char c) {
58  return c;
59  }
60 
61  static bool Compare(const StringPiece& a, const StringPiece& b) {
62  return a < b;
63  }
64 };
65 
67 struct CaseFold {
68  static unsigned char Normalize(char c) {
69  return LowerChar(c);
70  }
71 
72  static bool Compare(const StringPiece& a, const StringPiece& b) {
73  return StringCaseCompare(a, b) < 0;
74  }
75 };
76 
79  size_t operator()(const GoogleString& str) const {
80  return HashString<CasePreserve, size_t>(str.data(), str.size());
81  }
82 };
83 
85  size_t operator()(const GoogleString& str) const {
86  return HashString<CaseFold, size_t>(str.data(), str.size());
87  }
88 };
89 
91  bool operator()(const GoogleString& a, const GoogleString& b) const {
92  return MemCaseEqual(a.data(), a.size(), b.data(), b.size());
93  }
94 };
95 
97  size_t operator()(StringPiece str) const {
98  return HashString<CasePreserve, size_t>(str.data(), str.size());
99  }
100 };
101 
103  size_t operator()(StringPiece str) const {
104  return HashString<CaseFold, size_t>(str.data(), str.size());
105  }
106 };
108  bool operator()(StringPiece a, StringPiece b) const {
109  return MemCaseEqual(a.data(), a.size(), b.data(), b.size());
110  }
111 };
112 
113 }
114 
115 #endif
size_t JoinHash(size_t a, size_t b)
Definition: string_hash.h:44
Definition: string_hash.h:84
char LowerChar(char c)
Definition: string_util.h:544
Functors for constructing case-insensitive and case-sensitive hash-tables.
Definition: string_hash.h:78
bool MemCaseEqual(const char *s1, size_t size1, const char *s2, size_t size2)
Return true iff the two strings are equal, ignoring case.
Definition: string_hash.h:96
A helper for case-sensitive hashing.
Definition: string_hash.h:49
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: string_hash.h:102
IntType HashString(const char *s, size_t len)
Definition: string_hash.h:32
int StringCaseCompare(StringPiece s1, StringPiece s2)
Case-insensitive string comparison that is locale-independent.
Definition: string_hash.h:107
Definition: string_hash.h:90
A helper for case-insensitive hashing, which folds to lowercase.
Definition: string_hash.h:67
static unsigned char Normalize(char c)
Definition: string_hash.h:57