| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | |
| 6 #define NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | |
| 7 | |
| 8 #include <ctype.h> | |
| 9 | |
| 10 #include "base/port.h" | |
| 11 #include "base/strings/string_piece.h" | |
| 12 | |
| 13 namespace net { | |
| 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 + 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 { | |
| 57 static bool EqualIgnoreCase(const base::StringPiece& piece1, | |
| 58 const base::StringPiece& piece2) { | |
| 59 base::StringPiece::const_iterator p1i = piece1.begin(); | |
| 60 base::StringPiece::const_iterator p2i = piece2.begin(); | |
| 61 if (piece1.empty() && piece2.empty()) { | |
| 62 return true; | |
| 63 } else if (piece1.size() != piece2.size()) { | |
| 64 return false; | |
| 65 } | |
| 66 while (p1i != piece1.end() && p2i != piece2.end()) { | |
| 67 if (tolower(*p1i) != tolower(*p2i)) | |
| 68 return false; | |
| 69 ++p1i; | |
| 70 ++p2i; | |
| 71 } | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 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, | |
| 94 const base::StringPiece& starts_with) { | |
| 95 if (text.size() < starts_with.size()) | |
| 96 return false; | |
| 97 return EqualIgnoreCase(text.substr(0, starts_with.size()), starts_with); | |
| 98 } | |
| 99 }; | |
| 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 | |
| 110 | |
| 111 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ | |
| 112 | |
| OLD | NEW |