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 |
15 struct StringPieceCaseHash { | 57 struct StringPieceCaseHash { |
16 size_t operator()(const base::StringPiece& sp) const { | 58 size_t operator()(const base::StringPiece& sp) const { |
17 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string | 59 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string |
18 size_t hash_val = 0; | 60 size_t hash_val = 0; |
19 for (base::StringPiece::const_iterator it = sp.begin(); | 61 for (base::StringPiece::const_iterator it = sp.begin(); |
20 it != sp.end(); ++it) { | 62 it != sp.end(); ++it) { |
21 hash_val = 5 * hash_val + base::ToLowerASCII(*it); | 63 hash_val = 5 * hash_val + base::ToLowerASCII(*it); |
22 } | 64 } |
23 return hash_val; | 65 return hash_val; |
24 } | 66 } |
25 }; | 67 }; |
| 68 #endif // COMPILER_MSVC |
26 | 69 |
27 struct StringPieceCaseEqual { | 70 struct StringPieceCaseEqual { |
28 bool operator()(const base::StringPiece& piece1, | 71 bool operator()(const base::StringPiece& piece1, |
29 const base::StringPiece& piece2) const { | 72 const base::StringPiece& piece2) const { |
30 return base::EqualsCaseInsensitiveASCII(piece1, piece2); | 73 return base::EqualsCaseInsensitiveASCII(piece1, piece2); |
31 } | 74 } |
32 }; | 75 }; |
33 | 76 |
34 } // namespace net | 77 } // namespace net |
35 | 78 |
36 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 79 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ |
37 | 80 |
OLD | NEW |