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