Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
utilities.hpp
Go to the documentation of this file.
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
32
33#include <algorithm>
34#include <string>
35#include <locale>
36
37namespace websocketpp {
39namespace utility {
40
42
48template<typename charT>
49struct my_equal {
51
54 my_equal(std::locale const & loc ) : m_loc(loc) {}
55
57
63 bool operator()(charT ch1, charT ch2) {
64 return std::toupper(ch1, m_loc) == std::toupper(ch2, m_loc);
65 }
66private:
67 std::locale const & m_loc;
68};
69
71
75struct ci_less : std::binary_function<std::string, std::string, bool> {
76 // case-independent (ci) compare_less binary function
78 : public std::binary_function<unsigned char,unsigned char,bool>
79 {
80 bool operator() (unsigned char const & c1, unsigned char const & c2) const {
81 return tolower (c1) < tolower (c2);
82 }
83 };
84 bool operator() (std::string const & s1, std::string const & s2) const {
85 return std::lexicographical_compare
86 (s1.begin (), s1.end (), // source range
87 s2.begin (), s2.end (), // dest range
88 nocase_compare ()); // comparison
89 }
90};
91
93
102template<typename T>
103typename T::const_iterator ci_find_substr(T const & haystack, T const & needle,
104 std::locale const & loc = std::locale())
105{
106 return std::search( haystack.begin(), haystack.end(),
107 needle.begin(), needle.end(), my_equal<typename T::value_type>(loc) );
108}
109
111
124template<typename T>
125typename T::const_iterator ci_find_substr(T const & haystack,
126 typename T::value_type const * needle, typename T::size_type size,
127 std::locale const & loc = std::locale())
128{
129 return std::search( haystack.begin(), haystack.end(),
130 needle, needle+size, my_equal<typename T::value_type>(loc) );
131}
132
134
138std::string to_lower(std::string const & in);
139
141
148std::string string_replace_all(std::string subject, std::string const & search,
149 std::string const & replace);
150
152
157std::string to_hex(std::string const & input);
158
160
166std::string to_hex(uint8_t const * input, size_t length);
167
169
175std::string to_hex(char const * input, size_t length);
176
177} // namespace utility
178} // namespace websocketpp
179
181
182#endif // WEBSOCKETPP_UTILITIES_HPP
std::string to_hex(std::string const &input)
Convert std::string to ascii printed string of hex digits.
T::const_iterator ci_find_substr(T const &haystack, T const &needle, std::locale const &loc=std::locale())
Find substring (case insensitive)
std::string string_replace_all(std::string subject, std::string const &search, std::string const &replace)
Replace all occurrances of a substring with another.
std::string to_lower(std::string const &in)
Convert a string to lowercase.
Namespace for the WebSocket++ project.
Definition base64.hpp:41
#define T(meth, val, expected)
unsigned char uint8_t
Definition stdint.h:124
bool operator()(unsigned char const &c1, unsigned char const &c2) const
Definition utilities.hpp:80
Helper less than functor for case insensitive find.
Definition utilities.hpp:75
bool operator()(std::string const &s1, std::string const &s2) const
Definition utilities.hpp:84
Helper functor for case insensitive find.
Definition utilities.hpp:49
bool operator()(charT ch1, charT ch2)
Perform a case insensitive comparison.
Definition utilities.hpp:63
my_equal(std::locale const &loc)
Construct the functor with the given locale.
Definition utilities.hpp:54