utilities.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifndef WEBSOCKETPP_UTILITIES_HPP
  28. #define WEBSOCKETPP_UTILITIES_HPP
  29. #include <websocketpp/common/stdint.hpp>
  30. #include <algorithm>
  31. #include <string>
  32. #include <locale>
  33. namespace websocketpp {
  34. /// Generic non-websocket specific utility functions and data structures
  35. namespace utility {
  36. /// Helper functor for case insensitive find
  37. /**
  38. * Based on code from
  39. * http://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find
  40. *
  41. * templated version of my_equal so it could work with both char and wchar_t
  42. */
  43. template<typename charT>
  44. struct my_equal {
  45. /// Construct the functor with the given locale
  46. /**
  47. * @param [in] loc The locale to use for determining the case of values
  48. */
  49. my_equal(std::locale const & loc ) : m_loc(loc) {}
  50. /// Perform a case insensitive comparison
  51. /**
  52. * @param ch1 The first value to compare
  53. * @param ch2 The second value to compare
  54. * @return Whether or not the two values are equal when both are converted
  55. * to uppercase using the given locale.
  56. */
  57. bool operator()(charT ch1, charT ch2) {
  58. return std::toupper(ch1, m_loc) == std::toupper(ch2, m_loc);
  59. }
  60. private:
  61. std::locale const & m_loc;
  62. };
  63. /// Helper less than functor for case insensitive find
  64. /**
  65. * Based on code from
  66. * http://stackoverflow.com/questions/3152241/case-insensitive-stdstring-find
  67. */
  68. struct ci_less {
  69. // case-independent (ci) compare_less binary function
  70. struct nocase_compare {
  71. bool operator() (unsigned char const & c1, unsigned char const & c2) const {
  72. return tolower (c1) < tolower (c2);
  73. }
  74. };
  75. bool operator() (std::string const & s1, std::string const & s2) const {
  76. return std::lexicographical_compare
  77. (s1.begin (), s1.end (), // source range
  78. s2.begin (), s2.end (), // dest range
  79. nocase_compare ()); // comparison
  80. }
  81. };
  82. /// Find substring (case insensitive)
  83. /**
  84. * @param [in] haystack The string to search in
  85. * @param [in] needle The string to search for
  86. * @param [in] loc The locale to use for determining the case of values.
  87. * Defaults to the current locale.
  88. * @return An iterator to the first element of the first occurrance of needle in
  89. * haystack. If the sequence is not found, the function returns
  90. * haystack.end()
  91. */
  92. template<typename T>
  93. typename T::const_iterator ci_find_substr(T const & haystack, T const & needle,
  94. std::locale const & loc = std::locale())
  95. {
  96. return std::search( haystack.begin(), haystack.end(),
  97. needle.begin(), needle.end(), my_equal<typename T::value_type>(loc) );
  98. }
  99. /// Find substring (case insensitive)
  100. /**
  101. * @todo Is this still used? This method may not make sense.. should use
  102. * iterators or be less generic. As is it is too tightly coupled to std::string
  103. *
  104. * @param [in] haystack The string to search in
  105. * @param [in] needle The string to search for as a char array of values
  106. * @param [in] size Length of needle
  107. * @param [in] loc The locale to use for determining the case of values.
  108. * Defaults to the current locale.
  109. * @return An iterator to the first element of the first occurrance of needle in
  110. * haystack. If the sequence is not found, the function returns
  111. * haystack.end()
  112. */
  113. template<typename T>
  114. typename T::const_iterator ci_find_substr(T const & haystack,
  115. typename T::value_type const * needle, typename T::size_type size,
  116. std::locale const & loc = std::locale())
  117. {
  118. return std::search( haystack.begin(), haystack.end(),
  119. needle, needle+size, my_equal<typename T::value_type>(loc) );
  120. }
  121. /// Convert a string to lowercase
  122. /**
  123. * @param [in] in The string to convert
  124. * @return The converted string
  125. */
  126. std::string to_lower(std::string const & in);
  127. /// Replace all occurrances of a substring with another
  128. /**
  129. * @param [in] subject The string to search in
  130. * @param [in] search The string to search for
  131. * @param [in] replace The string to replace with
  132. * @return A copy of `subject` with all occurances of `search` replaced with
  133. * `replace`
  134. */
  135. std::string string_replace_all(std::string subject, std::string const & search,
  136. std::string const & replace);
  137. /// Convert std::string to ascii printed string of hex digits
  138. /**
  139. * @param [in] input The string to print
  140. * @return A copy of `input` converted to the printable representation of the
  141. * hex values of its data.
  142. */
  143. std::string to_hex(std::string const & input);
  144. /// Convert byte array (uint8_t) to ascii printed string of hex digits
  145. /**
  146. * @param [in] input The byte array to print
  147. * @param [in] length The length of input
  148. * @return A copy of `input` converted to the printable representation of the
  149. * hex values of its data.
  150. */
  151. std::string to_hex(uint8_t const * input, size_t length);
  152. /// Convert char array to ascii printed string of hex digits
  153. /**
  154. * @param [in] input The char array to print
  155. * @param [in] length The length of input
  156. * @return A copy of `input` converted to the printable representation of the
  157. * hex values of its data.
  158. */
  159. std::string to_hex(char const * input, size_t length);
  160. } // namespace utility
  161. } // namespace websocketpp
  162. #include <websocketpp/impl/utilities_impl.hpp>
  163. #endif // WEBSOCKETPP_UTILITIES_HPP