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 "base/strings/string_piece.h"
9 #include "base/strings/string_util.h"
13 #if defined(COMPILER_MSVC)
14 struct StringPieceCaseCompare
{
15 static const size_t bucket_size
= 4;
17 size_t operator()(const base::StringPiece
& sp
) const {
18 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string
20 for (base::StringPiece::const_iterator it
= sp
.begin();
21 it
!= sp
.end(); ++it
) {
22 hash_val
= 5 * hash_val
+ base::ToLowerASCII(*it
);
27 bool operator()(const base::StringPiece
& sp1
,
28 const base::StringPiece
& sp2
) const {
29 size_t len1
= sp1
.length();
30 size_t len2
= sp2
.length();
31 bool sp1_shorter
= len1
< len2
;
32 size_t len
= sp1_shorter
? len1
: len2
;
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
) {
42 if (sp1_lower
> sp2_lower
) {
54 #else // COMPILER_MSVC
55 struct StringPieceCaseHash
{
56 size_t operator()(const base::StringPiece
& sp
) const {
57 // based on __stl_string_hash in http://www.sgi.com/tech/stl/string
59 for (base::StringPiece::const_iterator it
= sp
.begin();
60 it
!= sp
.end(); ++it
) {
61 hash_val
= 5 * hash_val
+ base::ToLowerASCII(*it
);
66 #endif // COMPILER_MSVC
68 struct StringPieceCaseEqual
{
69 bool operator()(const base::StringPiece
& piece1
,
70 const base::StringPiece
& piece2
) const {
71 return base::EqualsCaseInsensitiveASCII(piece1
, piece2
);
77 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_