OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 5 #ifndef NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ |
6 #define NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 6 #define NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 #if defined(COMPILER_MSVC) | |
16 struct StringPieceCaseCompare { | |
17 static const size_t bucket_size = 4; | |
18 | |
19 size_t operator()(const base::StringPiece& sp) const { | |
20 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string | |
21 size_t hash_val = 0; | |
22 for (base::StringPiece::const_iterator it = sp.begin(); | |
23 it != sp.end(); ++it) { | |
24 hash_val = 5 * hash_val + base::ToLowerASCII(*it); | |
25 } | |
26 return hash_val; | |
27 } | |
28 | |
29 bool operator()(const base::StringPiece& sp1, | |
30 const base::StringPiece& sp2) const { | |
31 size_t len1 = sp1.length(); | |
32 size_t len2 = sp2.length(); | |
33 bool sp1_shorter = len1 < len2; | |
34 size_t len = sp1_shorter ? len1 : len2; | |
35 | |
36 int rv = 0; | |
37 for (size_t i = 0; i < len; i++) { | |
38 char sp1_lower = base::ToLowerASCII(sp1[i]); | |
39 char sp2_lower = base::ToLowerASCII(sp2[i]); | |
40 if (sp1_lower < sp2_lower) { | |
41 rv = -1; | |
42 break; | |
43 } | |
44 if (sp1_lower > sp2_lower) { | |
45 rv = 1; | |
46 break; | |
47 } | |
48 } | |
49 | |
50 if (rv == 0) { | |
51 return sp1_shorter; | |
52 } | |
53 return rv < 0; | |
54 } | |
55 }; | |
56 #else // COMPILER_MSVC | |
57 struct StringPieceCaseHash { | 15 struct StringPieceCaseHash { |
58 size_t operator()(const base::StringPiece& sp) const { | 16 size_t operator()(const base::StringPiece& sp) const { |
59 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string | 17 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string |
60 size_t hash_val = 0; | 18 size_t hash_val = 0; |
61 for (base::StringPiece::const_iterator it = sp.begin(); | 19 for (base::StringPiece::const_iterator it = sp.begin(); |
62 it != sp.end(); ++it) { | 20 it != sp.end(); ++it) { |
63 hash_val = 5 * hash_val + base::ToLowerASCII(*it); | 21 hash_val = 5 * hash_val + base::ToLowerASCII(*it); |
64 } | 22 } |
65 return hash_val; | 23 return hash_val; |
66 } | 24 } |
67 }; | 25 }; |
68 #endif // COMPILER_MSVC | |
69 | 26 |
70 struct StringPieceCaseEqual { | 27 struct StringPieceCaseEqual { |
71 bool operator()(const base::StringPiece& piece1, | 28 bool operator()(const base::StringPiece& piece1, |
72 const base::StringPiece& piece2) const { | 29 const base::StringPiece& piece2) const { |
73 return base::EqualsCaseInsensitiveASCII(piece1, piece2); | 30 return base::EqualsCaseInsensitiveASCII(piece1, piece2); |
74 } | 31 } |
75 }; | 32 }; |
76 | 33 |
77 } // namespace net | 34 } // namespace net |
78 | 35 |
79 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 36 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ |
80 | 37 |
OLD | NEW |