Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(964)

Side by Side Diff: net/tools/balsa/string_piece_utils.h

Issue 1215933004: New new versions of Starts/EndsWith and SplitString in net (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@starts_with
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <ctype.h>
9
10 #include "base/strings/string_piece.h" 8 #include "base/strings/string_piece.h"
9 #include "base/strings/string_util.h"
11 10
12 namespace net { 11 namespace net {
13 12
14 #if defined(COMPILER_MSVC) 13 #if defined(COMPILER_MSVC)
15 struct StringPieceCaseCompare { 14 struct StringPieceCaseCompare {
16 static const size_t bucket_size = 4; 15 static const size_t bucket_size = 4;
17 16
18 size_t operator()(const base::StringPiece& sp) const { 17 size_t operator()(const base::StringPiece& sp) const {
19 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string 18 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string
20 size_t hash_val = 0; 19 size_t hash_val = 0;
21 for (base::StringPiece::const_iterator it = sp.begin(); 20 for (base::StringPiece::const_iterator it = sp.begin();
22 it != sp.end(); ++it) { 21 it != sp.end(); ++it) {
23 hash_val = 5 * hash_val + tolower(*it); 22 hash_val = 5 * hash_val + base::ToLowerASCII(*it);
24 } 23 }
25 return hash_val; 24 return hash_val;
26 } 25 }
27 26
28 bool operator()(const base::StringPiece& sp1, 27 bool operator()(const base::StringPiece& sp1,
29 const base::StringPiece& sp2) const { 28 const base::StringPiece& sp2) const {
30 size_t len1 = sp1.length(); 29 size_t len1 = sp1.length();
31 size_t len2 = sp2.length(); 30 size_t len2 = sp2.length();
32 bool sp1_shorter = len1 < len2; 31 bool sp1_shorter = len1 < len2;
33 size_t len = sp1_shorter ? len1 : len2; 32 size_t len = sp1_shorter ? len1 : len2;
34 int rv = _memicmp(sp1.data(), sp2.data(), len); 33
34 int rv = 0;
35 for (size_t i = 0; i < len; i++) {
36 char sp1_lower = base::ToLowerASCII(sp1[i]);
37 char sp2_lower = base::ToLowerASCII(sp2[i]);
38 if (sp1_lower < sp2_lower) {
39 rv = -1;
40 break;
41 }
42 if (sp1_lower > sp2_lower) {
43 rv = 1;
44 break;
45 }
46 }
47
35 if (rv == 0) { 48 if (rv == 0) {
36 return sp1_shorter; 49 return sp1_shorter;
37 } 50 }
38 return rv < 0; 51 return rv < 0;
39 } 52 }
40 }; 53 };
41 #else // COMPILER_MSVC 54 #else // COMPILER_MSVC
42 struct StringPieceCaseHash { 55 struct StringPieceCaseHash {
43 size_t operator()(const base::StringPiece& sp) const { 56 size_t operator()(const base::StringPiece& sp) const {
44 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string 57 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string
45 size_t hash_val = 0; 58 size_t hash_val = 0;
46 for (base::StringPiece::const_iterator it = sp.begin(); 59 for (base::StringPiece::const_iterator it = sp.begin();
47 it != sp.end(); ++it) { 60 it != sp.end(); ++it) {
48 hash_val = 5 * hash_val + tolower(*it); 61 hash_val = 5 * hash_val + base::ToLowerASCII(*it);
49 } 62 }
50 return hash_val; 63 return hash_val;
51 } 64 }
52 }; 65 };
53 #endif // COMPILER_MSVC 66 #endif // COMPILER_MSVC
54 67
55 struct StringPieceUtils { 68 struct StringPieceUtils {
69 // ASCII case-insensitive equality.
56 static bool EqualIgnoreCase(const base::StringPiece& piece1, 70 static bool EqualIgnoreCase(const base::StringPiece& piece1,
57 const base::StringPiece& piece2) { 71 const base::StringPiece& piece2) {
58 base::StringPiece::const_iterator p1i = piece1.begin(); 72 base::StringPiece::const_iterator p1i = piece1.begin();
59 base::StringPiece::const_iterator p2i = piece2.begin(); 73 base::StringPiece::const_iterator p2i = piece2.begin();
60 if (piece1.empty() && piece2.empty()) { 74 if (piece1.empty() && piece2.empty()) {
61 return true; 75 return true;
62 } else if (piece1.size() != piece2.size()) { 76 } else if (piece1.size() != piece2.size()) {
63 return false; 77 return false;
64 } 78 }
65 while (p1i != piece1.end() && p2i != piece2.end()) { 79 while (p1i != piece1.end() && p2i != piece2.end()) {
66 if (tolower(*p1i) != tolower(*p2i)) 80 if (base::ToLowerASCII(*p1i) != base::ToLowerASCII(*p2i))
67 return false; 81 return false;
68 ++p1i; 82 ++p1i;
69 ++p2i; 83 ++p2i;
70 } 84 }
71 return true; 85 return true;
72 } 86 }
87 };
73 88
74 static void RemoveWhitespaceContext(base::StringPiece* piece1) {
75 base::StringPiece::const_iterator c = piece1->begin();
76 base::StringPiece::const_iterator e = piece1->end();
77 while (c != e && isspace(*c)) {
78 ++c;
79 }
80 if (c == e) {
81 *piece1 = base::StringPiece(c, e-c);
82 return;
83 }
84 --e;
85 while (c != e &&isspace(*e)) {
86 --e;
87 }
88 ++e;
89 *piece1 = base::StringPiece(c, e-c);
90 }
91
92 static bool StartsWithIgnoreCase(const base::StringPiece& text,
93 const base::StringPiece& starts_with) {
94 if (text.size() < starts_with.size())
95 return false;
96 return EqualIgnoreCase(text.substr(0, starts_with.size()), starts_with);
97 }
98 };
99 struct StringPieceCaseEqual { 89 struct StringPieceCaseEqual {
100 bool operator()(const base::StringPiece& piece1, 90 bool operator()(const base::StringPiece& piece1,
101 const base::StringPiece& piece2) const { 91 const base::StringPiece& piece2) const {
102 return StringPieceUtils::EqualIgnoreCase(piece1, piece2); 92 return StringPieceUtils::EqualIgnoreCase(piece1, piece2);
103 } 93 }
104 }; 94 };
105 95
106
107
108 } // namespace net 96 } // namespace net
109 97
110 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_ 98 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_
111 99
OLDNEW
« no previous file with comments | « net/tools/balsa/balsa_headers_token_utils.cc ('k') | net/tools/disk_cache_memory_test/disk_cache_memory_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698