You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.8 KiB

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