staticlib_pion  2.0.2
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
algorithm.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // ---------------------------------------------------------------------
18 // pion: a Boost C++ framework for building lightweight HTTP interfaces
19 // ---------------------------------------------------------------------
20 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
21 //
22 // Distributed under the Boost Software License, Version 1.0.
23 // See http://www.boost.org/LICENSE_1_0.txt
24 //
25 
26 #ifndef STATICLIB_PION_ALGORITHM_HPP
27 #define STATICLIB_PION_ALGORITHM_HPP
28 
29 #include <cctype>
30 #include <cstdint>
31 #include <functional>
32 #include <locale>
33 #include <string>
34 
35 #include "staticlib/config.hpp"
36 #include "staticlib/utils.hpp"
37 
38 namespace staticlib {
39 namespace pion {
40 
45 namespace algorithm {
46 
53  inline bool is_char(int c) {
54  return (c >= 0 && c <= 127);
55  }
56 
63  inline bool is_control(int c) {
64  return ( (c >= 0 && c <= 31) || c == 127);
65  }
66 
73  inline bool is_special(int c) {
74  switch (c) {
75  case '(': case ')': case '<': case '>': case '@':
76  case ',': case ';': case ':': case '\\': case '"':
77  case '/': case '[': case ']': case '?': case '=':
78  case '{': case '}': case ' ': case '\t':
79  return true;
80  default:
81  return false;
82  }
83  }
84 
92  // http://stackoverflow.com/a/2595226
93  template <class T>
94  static void hash_combine(std::size_t& seed, const T& v) {
95  std::hash<T> hasher;
96  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
97  }
98 
102  // http://www.boost.org/doc/libs/1_50_0/doc/html/unordered/hash_equality.html
103  struct iequal_to : std::binary_function<std::string, std::string, bool> {
111  bool operator()(std::string const& x, std::string const& y) const {
112  return sl::utils::iequals(x, y);
113  }
114  };
115 
119  // http://www.boost.org/doc/libs/1_50_0/doc/html/unordered/hash_equality.html
120  struct ihash : std::unary_function<std::string, std::size_t> {
127  std::size_t operator()(std::string const& x) const {
128  std::size_t seed = 0;
129  for (std::string::const_iterator it = x.begin(); it != x.end(); ++it) {
130  // locale is expensive here
131  algorithm::hash_combine(seed, std::toupper(*it/*, locale*/));
132  }
133  return seed;
134  }
135  };
136 
137 } // namespace
138 }
139 }
140 
141 #endif // STATICLIB_PION_ALGORITHM_HPP