Fix crash in SpeechRecognizerImpl introduced in AudioParams refactor.
[chromium-blink-merge.git] / net / tools / balsa / string_piece_utils.h
blob6d8967a014219416c98ec37b75e1401d5de6fcd9
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"
11 namespace net {
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
19 size_t hash_val = 0;
20 for (base::StringPiece::const_iterator it = sp.begin();
21 it != sp.end(); ++it) {
22 hash_val = 5 * hash_val + base::ToLowerASCII(*it);
24 return hash_val;
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;
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;
42 if (sp1_lower > sp2_lower) {
43 rv = 1;
44 break;
48 if (rv == 0) {
49 return sp1_shorter;
51 return rv < 0;
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
58 size_t hash_val = 0;
59 for (base::StringPiece::const_iterator it = sp.begin();
60 it != sp.end(); ++it) {
61 hash_val = 5 * hash_val + base::ToLowerASCII(*it);
63 return hash_val;
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);
75 } // namespace net
77 #endif // NET_TOOLS_BALSA_STRING_PIECE_UTILS_H_