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_QUIC_STRING_PIECE_UTILS_H_ |
6 #define NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 6 #define NET_QUIC_STRING_PIECE_UTILS_H_ |
7 | 7 |
8 #include <ctype.h> | 8 #include <ctype.h> |
9 | 9 |
10 #include "base/port.h" | |
11 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
12 | 11 |
13 namespace net { | 12 namespace net { |
14 | 13 |
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 + tolower(*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 int rv = _memicmp(sp1.data(), sp2.data(), len); | |
36 if (rv == 0) { | |
37 return sp1_shorter; | |
38 } | |
39 return rv < 0; | |
40 } | |
41 }; | |
42 #else // COMPILER_MSVC | |
43 struct StringPieceCaseHash { | |
44 size_t operator()(const base::StringPiece& sp) const { | |
45 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string | |
46 size_t hash_val = 0; | |
47 for (base::StringPiece::const_iterator it = sp.begin(); | |
48 it != sp.end(); ++it) { | |
49 hash_val = 5 * hash_val + tolower(*it); | |
50 } | |
51 return hash_val; | |
52 } | |
53 }; | |
54 #endif // COMPILER_MSVC | |
55 | |
56 struct StringPieceUtils { | 14 struct StringPieceUtils { |
57 static bool EqualIgnoreCase(const base::StringPiece& piece1, | 15 static bool EqualIgnoreCase(const base::StringPiece& piece1, |
58 const base::StringPiece& piece2) { | 16 const base::StringPiece& piece2) { |
59 base::StringPiece::const_iterator p1i = piece1.begin(); | 17 base::StringPiece::const_iterator p1i = piece1.begin(); |
60 base::StringPiece::const_iterator p2i = piece2.begin(); | 18 base::StringPiece::const_iterator p2i = piece2.begin(); |
61 if (piece1.empty() && piece2.empty()) { | 19 if (piece1.empty() && piece2.empty()) { |
62 return true; | 20 return true; |
63 } else if (piece1.size() != piece2.size()) { | 21 } else if (piece1.size() != piece2.size()) { |
64 return false; | 22 return false; |
65 } | 23 } |
66 while (p1i != piece1.end() && p2i != piece2.end()) { | 24 while (p1i != piece1.end() && p2i != piece2.end()) { |
67 if (tolower(*p1i) != tolower(*p2i)) | 25 if (tolower(*p1i) != tolower(*p2i)) |
68 return false; | 26 return false; |
69 ++p1i; | 27 ++p1i; |
70 ++p2i; | 28 ++p2i; |
71 } | 29 } |
72 return true; | 30 return true; |
73 } | 31 } |
74 | 32 |
75 static void RemoveWhitespaceContext(base::StringPiece* piece1) { | |
76 base::StringPiece::const_iterator c = piece1->begin(); | |
77 base::StringPiece::const_iterator e = piece1->end(); | |
78 while (c != e && isspace(*c)) { | |
79 ++c; | |
80 } | |
81 if (c == e) { | |
82 *piece1 = base::StringPiece(c, e-c); | |
83 return; | |
84 } | |
85 --e; | |
86 while (c != e &&isspace(*e)) { | |
87 --e; | |
88 } | |
89 ++e; | |
90 *piece1 = base::StringPiece(c, e-c); | |
91 } | |
92 | |
93 static bool StartsWithIgnoreCase(const base::StringPiece& text, | 33 static bool StartsWithIgnoreCase(const base::StringPiece& text, |
wtc
2014/06/25 22:22:25
IMPORTANT: It seems that the only thing we need fr
dmziegler
2014/06/26 01:23:29
Done.
| |
94 const base::StringPiece& starts_with) { | 34 const base::StringPiece& starts_with) { |
95 if (text.size() < starts_with.size()) | 35 if (text.size() < starts_with.size()) |
96 return false; | 36 return false; |
97 return EqualIgnoreCase(text.substr(0, starts_with.size()), starts_with); | 37 return EqualIgnoreCase(text.substr(0, starts_with.size()), starts_with); |
98 } | 38 } |
99 }; | 39 }; |
100 struct StringPieceCaseEqual { | |
101 bool operator()(const base::StringPiece& piece1, | |
102 const base::StringPiece& piece2) const { | |
103 return StringPieceUtils::EqualIgnoreCase(piece1, piece2); | |
104 } | |
105 }; | |
106 | |
107 | |
108 | |
109 } // namespace net | 40 } // namespace net |
wtc
2014/06/25 22:22:24
Add a blank line before this line.
| |
110 | 41 |
111 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | 42 #endif // NET_QUIC_STRING_PIECE_UTILS_H_ |
112 | 43 |
OLD | NEW |