1// Copyright (c) 2010 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.
4
5#ifndef NET_TOOLS_FLIP_SERVER_STRING_PIECE_UTILS_H_
6#define NET_TOOLS_FLIP_SERVER_STRING_PIECE_UTILS_H_
7#pragma once
8
9#include <ctype.h>
10
11#include "base/port.h"
12#include "base/string_piece.h"
13
14namespace net {
15
16struct StringPieceCaseHash {
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 + tolower(*it);
23    }
24    return hash_val;
25  }
26};
27
28struct StringPieceUtils {
29  static bool EqualIgnoreCase(const base::StringPiece& piece1,
30                              const base::StringPiece& piece2) {
31    base::StringPiece::const_iterator p1i = piece1.begin();
32    base::StringPiece::const_iterator p2i = piece2.begin();
33    if (piece1.empty() && piece2.empty()) {
34      return true;
35    } else if (piece1.size() != piece2.size()) {
36      return false;
37    }
38    while (p1i != piece1.end() && p2i != piece2.begin()) {
39      if (tolower(*p1i) != tolower(*p2i))
40        return false;
41    }
42    return true;
43  }
44
45  static void RemoveWhitespaceContext(base::StringPiece* piece1) {
46    base::StringPiece::const_iterator c = piece1->begin();
47    base::StringPiece::const_iterator e = piece1->end();
48    while (c != e && isspace(*c)) {
49      ++c;
50    }
51    if (c == e) {
52      *piece1 = base::StringPiece(c, e-c);
53      return;
54    }
55    --e;
56    while (c != e &&isspace(*e)) {
57      --e;
58    }
59    ++e;
60    *piece1 = base::StringPiece(c, e-c);
61  }
62
63  static bool StartsWithIgnoreCase(const base::StringPiece& text,
64                                   const base::StringPiece& starts_with) {
65    if (text.size() < starts_with.size())
66      return false;
67    return EqualIgnoreCase(text.substr(0, starts_with.size()), starts_with);
68  }
69};
70struct StringPieceCaseEqual {
71  bool operator()(const base::StringPiece& piece1,
72                  const base::StringPiece& piece2) const {
73    return StringPieceUtils::EqualIgnoreCase(piece1, piece2);
74  }
75};
76
77
78
79}  // namespace net
80
81#endif  // NET_TOOLS_FLIP_SERVER_STRING_PIECE_UTILS_H_
82
83