We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / tools / balsa / string_piece_utils.h
blob12fc69be85fca9e2ccda824d50774bada11dc587
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.
5 #ifndef NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_
6 #define NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_
8 #include <ctype.h>
10 #include "base/port.h"
11 #include "base/strings/string_piece.h"
13 namespace net {
15 #if defined(COMPILER_MSVC)
16 struct StringPieceCaseCompare {
17 static const size_t bucket_size = 4;
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);
26 return hash_val;
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;
39 return rv < 0;
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);
51 return hash_val;
54 #endif // COMPILER_MSVC
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;
66 while (p1i != piece1.end() && p2i != piece2.end()) {
67 if (tolower(*p1i) != tolower(*p2i))
68 return false;
69 ++p1i;
70 ++p2i;
72 return true;
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;
81 if (c == e) {
82 *piece1 = base::StringPiece(c, e-c);
83 return;
85 --e;
86 while (c != e &&isspace(*e)) {
87 --e;
89 ++e;
90 *piece1 = base::StringPiece(c, e-c);
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);
100 struct StringPieceCaseEqual {
101 bool operator()(const base::StringPiece& piece1,
102 const base::StringPiece& piece2) const {
103 return StringPieceUtils::EqualIgnoreCase(piece1, piece2);
109 } // namespace net
111 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_