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_
10 #include "base/port.h"
11 #include "base/strings/string_piece.h"
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
22 for (base::StringPiece::const_iterator it
= sp
.begin();
23 it
!= sp
.end(); ++it
) {
24 hash_val
= 5 * hash_val
+ tolower(*it
);
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
);
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
47 for (base::StringPiece::const_iterator it
= sp
.begin();
48 it
!= sp
.end(); ++it
) {
49 hash_val
= 5 * hash_val
+ tolower(*it
);
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()) {
63 } else if (piece1
.size() != piece2
.size()) {
66 while (p1i
!= piece1
.end() && p2i
!= piece2
.end()) {
67 if (tolower(*p1i
) != tolower(*p2i
))
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
)) {
82 *piece1
= base::StringPiece(c
, e
-c
);
86 while (c
!= e
&&isspace(*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())
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
);
111 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_