WebSocket++  0.8.3-dev
C++ websocket client/server library
utilities.hpp
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef WEBSOCKETPP_UTILITIES_HPP
29 #define WEBSOCKETPP_UTILITIES_HPP
30 
31 #include <websocketpp/common/stdint.hpp>
32 
33 #include <algorithm>
34 #include <string>
35 #include <locale>
36 
37 namespace websocketpp {
38 /// Generic non-websocket specific utility functions and data structures
39 namespace utility {
40 
41 /// Helper functor for case insensitive find
42 /**
43  * Based on code from
44  * http://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find
45  *
46  * templated version of my_equal so it could work with both char and wchar_t
47  */
48 template<typename charT>
49 struct my_equal {
50  /// Construct the functor with the given locale
51  /**
52  * @param [in] loc The locale to use for determining the case of values
53  */
54  my_equal(std::locale const & loc ) : m_loc(loc) {}
55 
56  /// Perform a case insensitive comparison
57  /**
58  * @param ch1 The first value to compare
59  * @param ch2 The second value to compare
60  * @return Whether or not the two values are equal when both are converted
61  * to uppercase using the given locale.
62  */
63  bool operator()(charT ch1, charT ch2) {
64  return std::toupper(ch1, m_loc) == std::toupper(ch2, m_loc);
65  }
66 private:
67  std::locale const & m_loc;
68 };
69 
70 /// Helper less than functor for case insensitive find
71 /**
72  * Based on code from
73  * http://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find
74  */
75 struct ci_less {
76  // case-independent (ci) compare_less binary function
77  struct nocase_compare {
78  bool operator() (unsigned char const & c1, unsigned char const & c2) const {
79  return tolower (c1) < tolower (c2);
80  }
81  };
82  bool operator() (std::string const & s1, std::string const & s2) const {
83  return std::lexicographical_compare
84  (s1.begin (), s1.end (), // source range
85  s2.begin (), s2.end (), // dest range
86  nocase_compare ()); // comparison
87  }
88 };
89 
90 /// Find substring (case insensitive)
91 /**
92  * @param [in] haystack The string to search in
93  * @param [in] needle The string to search for
94  * @param [in] loc The locale to use for determining the case of values.
95  * Defaults to the current locale.
96  * @return An iterator to the first element of the first occurrance of needle in
97  * haystack. If the sequence is not found, the function returns
98  * haystack.end()
99  */
100 template<typename T>
101 typename T::const_iterator ci_find_substr(T const & haystack, T const & needle,
102  std::locale const & loc = std::locale())
103 {
104  return std::search( haystack.begin(), haystack.end(),
105  needle.begin(), needle.end(), my_equal<typename T::value_type>(loc) );
106 }
107 
108 /// Find substring (case insensitive)
109 /**
110  * @todo Is this still used? This method may not make sense.. should use
111  * iterators or be less generic. As is it is too tightly coupled to std::string
112  *
113  * @param [in] haystack The string to search in
114  * @param [in] needle The string to search for as a char array of values
115  * @param [in] size Length of needle
116  * @param [in] loc The locale to use for determining the case of values.
117  * Defaults to the current locale.
118  * @return An iterator to the first element of the first occurrance of needle in
119  * haystack. If the sequence is not found, the function returns
120  * haystack.end()
121  */
122 template<typename T>
123 typename T::const_iterator ci_find_substr(T const & haystack,
124  typename T::value_type const * needle, typename T::size_type size,
125  std::locale const & loc = std::locale())
126 {
127  return std::search( haystack.begin(), haystack.end(),
128  needle, needle+size, my_equal<typename T::value_type>(loc) );
129 }
130 
131 /// Convert a string to lowercase
132 /**
133  * @param [in] in The string to convert
134  * @return The converted string
135  */
136 std::string to_lower(std::string const & in);
137 
138 /// Replace all occurrances of a substring with another
139 /**
140  * @param [in] subject The string to search in
141  * @param [in] search The string to search for
142  * @param [in] replace The string to replace with
143  * @return A copy of `subject` with all occurances of `search` replaced with
144  * `replace`
145  */
147  std::string const & replace);
148 
149 /// Convert std::string to ascii printed string of hex digits
150 /**
151  * @param [in] input The string to print
152  * @return A copy of `input` converted to the printable representation of the
153  * hex values of its data.
154  */
155 std::string to_hex(std::string const & input);
156 
157 /// Convert byte array (uint8_t) to ascii printed string of hex digits
158 /**
159  * @param [in] input The byte array to print
160  * @param [in] length The length of input
161  * @return A copy of `input` converted to the printable representation of the
162  * hex values of its data.
163  */
165 
166 /// Convert char array to ascii printed string of hex digits
167 /**
168  * @param [in] input The char array to print
169  * @param [in] length The length of input
170  * @return A copy of `input` converted to the printable representation of the
171  * hex values of its data.
172  */
173 std::string to_hex(char const * input, size_t length);
174 
175 } // namespace utility
176 } // namespace websocketpp
177 
178 #include <websocketpp/impl/utilities_impl.hpp>
179 
180 #endif // WEBSOCKETPP_UTILITIES_HPP
websocketpp::utility::to_hex
std::string to_hex(uint8_t const *input, size_t length)
Convert byte array (uint8_t) to ascii printed string of hex digits.
Definition: utilities_impl.hpp:56
websocketpp::utility
Generic non-websocket specific utility functions and data structures.
Definition: utilities_impl.hpp:35
websocketpp::utility::my_equal
Helper functor for case insensitive find.
Definition: utilities.hpp:49
websocketpp::utility::my_equal::my_equal
my_equal(std::locale const &loc)
Construct the functor with the given locale.
Definition: utilities.hpp:54
websocketpp::utility::to_lower
std::string to_lower(std::string const &in)
Convert a string to lowercase.
Definition: utilities_impl.hpp:37
websocketpp::utility::ci_find_substr
T::const_iterator ci_find_substr(T const &haystack, typename T::value_type const *needle, typename T::size_type size, std::locale const &loc=std::locale())
Find substring (case insensitive)
Definition: utilities.hpp:123
websocketpp::versions_supported
static std::vector< int > const versions_supported(helper, helper+4)
Container that stores the list of protocol versions supported.
websocketpp::utility::ci_less
Helper less than functor for case insensitive find.
Definition: utilities.hpp:75
websocketpp::utility::string_replace_all
std::string string_replace_all(std::string subject, std::string const &search, std::string const &replace)
Replace all occurrances of a substring with another.
Definition: utilities_impl.hpp:73
websocketpp::utility::ci_less::nocase_compare
Definition: utilities.hpp:77
websocketpp::utility::to_hex
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
Definition: utilities_impl.hpp:43
websocketpp::utility::my_equal::operator()
bool operator()(charT ch1, charT ch2)
Perform a case insensitive comparison.
Definition: utilities.hpp:63
websocketpp::utility::ci_find_substr
T::const_iterator ci_find_substr(T const &haystack, T const &needle, std::locale const &loc=std::locale())
Find substring (case insensitive)
Definition: utilities.hpp:101
websocketpp::utility::to_hex
std::string to_hex(const char *input, size_t length)
Convert char array to ascii printed string of hex digits.
Definition: utilities_impl.hpp:69