null_regex_traits.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file null_regex_traits.hpp
  3. /// Contains the definition of the null_regex_traits\<\> template, which is a
  4. /// stub regex traits implementation that can be used by static and dynamic
  5. /// regexes for searching non-character data.
  6. //
  7. // Copyright 2008 Eric Niebler. Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_XPRESSIVE_TRAITS_NULL_REGEX_TRAITS_HPP_EAN_10_04_2005
  11. #define BOOST_XPRESSIVE_TRAITS_NULL_REGEX_TRAITS_HPP_EAN_10_04_2005
  12. // MS compatible compilers support #pragma once
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <vector>
  17. #include <boost/assert.hpp>
  18. #include <boost/mpl/assert.hpp>
  19. #include <boost/xpressive/detail/detail_fwd.hpp>
  20. #include <boost/xpressive/detail/utility/never_true.hpp>
  21. #include <boost/xpressive/detail/utility/ignore_unused.hpp>
  22. namespace boost { namespace xpressive
  23. {
  24. namespace detail
  25. {
  26. struct not_a_locale {};
  27. }
  28. struct regex_traits_version_1_tag;
  29. ///////////////////////////////////////////////////////////////////////////////
  30. // null_regex_traits
  31. //
  32. /// \brief stub regex_traits for non-char data
  33. ///
  34. template<typename Elem>
  35. struct null_regex_traits
  36. {
  37. typedef Elem char_type;
  38. typedef std::vector<char_type> string_type;
  39. typedef detail::not_a_locale locale_type;
  40. typedef int char_class_type;
  41. typedef regex_traits_version_1_tag version_tag;
  42. /// Initialize a null_regex_traits object.
  43. ///
  44. null_regex_traits(locale_type = locale_type())
  45. {
  46. }
  47. /// Checks two null_regex_traits objects for equality
  48. ///
  49. /// \return true.
  50. bool operator ==(null_regex_traits<char_type> const &that) const
  51. {
  52. detail::ignore_unused(that);
  53. return true;
  54. }
  55. /// Checks two null_regex_traits objects for inequality
  56. ///
  57. /// \return false.
  58. bool operator !=(null_regex_traits<char_type> const &that) const
  59. {
  60. detail::ignore_unused(that);
  61. return false;
  62. }
  63. /// Convert a char to a Elem
  64. ///
  65. /// \param ch The source character.
  66. /// \return Elem(ch).
  67. char_type widen(char ch) const
  68. {
  69. return char_type(ch);
  70. }
  71. /// Returns a hash value for a Elem in the range [0, UCHAR_MAX]
  72. ///
  73. /// \param ch The source character.
  74. /// \return a value between 0 and UCHAR_MAX, inclusive.
  75. static unsigned char hash(char_type ch)
  76. {
  77. return static_cast<unsigned char>(ch);
  78. }
  79. /// No-op
  80. ///
  81. /// \param ch The source character.
  82. /// \return ch
  83. static char_type translate(char_type ch)
  84. {
  85. return ch;
  86. }
  87. /// No-op
  88. ///
  89. /// \param ch The source character.
  90. /// \return ch
  91. static char_type translate_nocase(char_type ch)
  92. {
  93. return ch;
  94. }
  95. /// Checks to see if a character is within a character range.
  96. ///
  97. /// \param first The bottom of the range, inclusive.
  98. /// \param last The top of the range, inclusive.
  99. /// \param ch The source character.
  100. /// \return first <= ch && ch <= last.
  101. static bool in_range(char_type first, char_type last, char_type ch)
  102. {
  103. return first <= ch && ch <= last;
  104. }
  105. /// Checks to see if a character is within a character range.
  106. ///
  107. /// \param first The bottom of the range, inclusive.
  108. /// \param last The top of the range, inclusive.
  109. /// \param ch The source character.
  110. /// \return first <= ch && ch <= last.
  111. /// \attention Since the null_regex_traits does not do case-folding,
  112. /// this function is equivalent to in_range().
  113. static bool in_range_nocase(char_type first, char_type last, char_type ch)
  114. {
  115. return first <= ch && ch <= last;
  116. }
  117. /// Returns a sort key for the character sequence designated by the iterator range [F1, F2)
  118. /// such that if the character sequence [G1, G2) sorts before the character sequence [H1, H2)
  119. /// then v.transform(G1, G2) < v.transform(H1, H2).
  120. ///
  121. /// \attention Not currently used
  122. template<typename FwdIter>
  123. static string_type transform(FwdIter begin, FwdIter end)
  124. {
  125. return string_type(begin, end);
  126. }
  127. /// Returns a sort key for the character sequence designated by the iterator range [F1, F2)
  128. /// such that if the character sequence [G1, G2) sorts before the character sequence [H1, H2)
  129. /// when character case is not considered then
  130. /// v.transform_primary(G1, G2) < v.transform_primary(H1, H2).
  131. ///
  132. /// \attention Not currently used
  133. template<typename FwdIter>
  134. static string_type transform_primary(FwdIter begin, FwdIter end)
  135. {
  136. return string_type(begin, end);
  137. }
  138. /// Returns a sequence of characters that represents the collating element
  139. /// consisting of the character sequence designated by the iterator range [F1, F2).
  140. /// Returns an empty string if the character sequence is not a valid collating element.
  141. ///
  142. /// \attention Not currently used
  143. template<typename FwdIter>
  144. static string_type lookup_collatename(FwdIter begin, FwdIter end)
  145. {
  146. detail::ignore_unused(begin);
  147. detail::ignore_unused(end);
  148. return string_type();
  149. }
  150. /// The null_regex_traits does not have character classifications, so lookup_classname()
  151. /// is unused.
  152. ///
  153. /// \param begin not used
  154. /// \param end not used
  155. /// \param icase not used
  156. /// \return static_cast\<char_class_type\>(0)
  157. template<typename FwdIter>
  158. static char_class_type lookup_classname(FwdIter begin, FwdIter end, bool icase)
  159. {
  160. detail::ignore_unused(begin);
  161. detail::ignore_unused(end);
  162. detail::ignore_unused(icase);
  163. return 0;
  164. }
  165. /// The null_regex_traits does not have character classifications, so isctype()
  166. /// is unused.
  167. ///
  168. /// \param ch not used
  169. /// \param mask not used
  170. /// \return false
  171. static bool isctype(char_type ch, char_class_type mask)
  172. {
  173. detail::ignore_unused(ch);
  174. detail::ignore_unused(mask);
  175. return false;
  176. }
  177. /// The null_regex_traits recognizes no elements as digits, so value() is unused.
  178. ///
  179. /// \param ch not used
  180. /// \param radix not used
  181. /// \return -1
  182. static int value(char_type ch, int radix)
  183. {
  184. detail::ignore_unused(ch);
  185. detail::ignore_unused(radix);
  186. return -1;
  187. }
  188. /// Not used
  189. ///
  190. /// \param loc not used
  191. /// \return loc
  192. static locale_type imbue(locale_type loc)
  193. {
  194. return loc;
  195. }
  196. /// Returns locale_type().
  197. ///
  198. /// \return locale_type()
  199. static locale_type getloc()
  200. {
  201. return locale_type();
  202. }
  203. };
  204. }}
  205. #endif