classification.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Boost string_algo library classification.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2003.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/ for updates, documentation, and revision history.
  8. #ifndef BOOST_STRING_CLASSIFICATION_HPP
  9. #define BOOST_STRING_CLASSIFICATION_HPP
  10. #include <algorithm>
  11. #include <locale>
  12. #include <boost/range/value_type.hpp>
  13. #include <boost/range/as_literal.hpp>
  14. #include <boost/algorithm/string/detail/classification.hpp>
  15. #include <boost/algorithm/string/predicate_facade.hpp>
  16. /*! \file
  17. Classification predicates are included in the library to give
  18. some more convenience when using algorithms like \c trim() and \c all().
  19. They wrap functionality of STL classification functions ( e.g. \c std::isspace() )
  20. into generic functors.
  21. */
  22. namespace boost {
  23. namespace algorithm {
  24. // classification functor generator -------------------------------------//
  25. //! is_classified predicate
  26. /*!
  27. Construct the \c is_classified predicate. This predicate holds if the input is
  28. of specified \c std::ctype category.
  29. \param Type A \c std::ctype category
  30. \param Loc A locale used for classification
  31. \return An instance of the \c is_classified predicate
  32. */
  33. inline detail::is_classifiedF
  34. is_classified(std::ctype_base::mask Type, const std::locale& Loc=std::locale())
  35. {
  36. return detail::is_classifiedF(Type, Loc);
  37. }
  38. //! is_space predicate
  39. /*!
  40. Construct the \c is_classified predicate for the \c ctype_base::space category.
  41. \param Loc A locale used for classification
  42. \return An instance of the \c is_classified predicate
  43. */
  44. inline detail::is_classifiedF
  45. is_space(const std::locale& Loc=std::locale())
  46. {
  47. return detail::is_classifiedF(std::ctype_base::space, Loc);
  48. }
  49. //! is_alnum predicate
  50. /*!
  51. Construct the \c is_classified predicate for the \c ctype_base::alnum category.
  52. \param Loc A locale used for classification
  53. \return An instance of the \c is_classified predicate
  54. */
  55. inline detail::is_classifiedF
  56. is_alnum(const std::locale& Loc=std::locale())
  57. {
  58. return detail::is_classifiedF(std::ctype_base::alnum, Loc);
  59. }
  60. //! is_alpha predicate
  61. /*!
  62. Construct the \c is_classified predicate for the \c ctype_base::alpha category.
  63. \param Loc A locale used for classification
  64. \return An instance of the \c is_classified predicate
  65. */
  66. inline detail::is_classifiedF
  67. is_alpha(const std::locale& Loc=std::locale())
  68. {
  69. return detail::is_classifiedF(std::ctype_base::alpha, Loc);
  70. }
  71. //! is_cntrl predicate
  72. /*!
  73. Construct the \c is_classified predicate for the \c ctype_base::cntrl category.
  74. \param Loc A locale used for classification
  75. \return An instance of the \c is_classified predicate
  76. */
  77. inline detail::is_classifiedF
  78. is_cntrl(const std::locale& Loc=std::locale())
  79. {
  80. return detail::is_classifiedF(std::ctype_base::cntrl, Loc);
  81. }
  82. //! is_digit predicate
  83. /*!
  84. Construct the \c is_classified predicate for the \c ctype_base::digit category.
  85. \param Loc A locale used for classification
  86. \return An instance of the \c is_classified predicate
  87. */
  88. inline detail::is_classifiedF
  89. is_digit(const std::locale& Loc=std::locale())
  90. {
  91. return detail::is_classifiedF(std::ctype_base::digit, Loc);
  92. }
  93. //! is_graph predicate
  94. /*!
  95. Construct the \c is_classified predicate for the \c ctype_base::graph category.
  96. \param Loc A locale used for classification
  97. \return An instance of the \c is_classified predicate
  98. */
  99. inline detail::is_classifiedF
  100. is_graph(const std::locale& Loc=std::locale())
  101. {
  102. return detail::is_classifiedF(std::ctype_base::graph, Loc);
  103. }
  104. //! is_lower predicate
  105. /*!
  106. Construct the \c is_classified predicate for the \c ctype_base::lower category.
  107. \param Loc A locale used for classification
  108. \return An instance of \c is_classified predicate
  109. */
  110. inline detail::is_classifiedF
  111. is_lower(const std::locale& Loc=std::locale())
  112. {
  113. return detail::is_classifiedF(std::ctype_base::lower, Loc);
  114. }
  115. //! is_print predicate
  116. /*!
  117. Construct the \c is_classified predicate for the \c ctype_base::print category.
  118. \param Loc A locale used for classification
  119. \return An instance of the \c is_classified predicate
  120. */
  121. inline detail::is_classifiedF
  122. is_print(const std::locale& Loc=std::locale())
  123. {
  124. return detail::is_classifiedF(std::ctype_base::print, Loc);
  125. }
  126. //! is_punct predicate
  127. /*!
  128. Construct the \c is_classified predicate for the \c ctype_base::punct category.
  129. \param Loc A locale used for classification
  130. \return An instance of the \c is_classified predicate
  131. */
  132. inline detail::is_classifiedF
  133. is_punct(const std::locale& Loc=std::locale())
  134. {
  135. return detail::is_classifiedF(std::ctype_base::punct, Loc);
  136. }
  137. //! is_upper predicate
  138. /*!
  139. Construct the \c is_classified predicate for the \c ctype_base::upper category.
  140. \param Loc A locale used for classification
  141. \return An instance of the \c is_classified predicate
  142. */
  143. inline detail::is_classifiedF
  144. is_upper(const std::locale& Loc=std::locale())
  145. {
  146. return detail::is_classifiedF(std::ctype_base::upper, Loc);
  147. }
  148. //! is_xdigit predicate
  149. /*!
  150. Construct the \c is_classified predicate for the \c ctype_base::xdigit category.
  151. \param Loc A locale used for classification
  152. \return An instance of the \c is_classified predicate
  153. */
  154. inline detail::is_classifiedF
  155. is_xdigit(const std::locale& Loc=std::locale())
  156. {
  157. return detail::is_classifiedF(std::ctype_base::xdigit, Loc);
  158. }
  159. //! is_any_of predicate
  160. /*!
  161. Construct the \c is_any_of predicate. The predicate holds if the input
  162. is included in the specified set of characters.
  163. \param Set A set of characters to be recognized
  164. \return An instance of the \c is_any_of predicate
  165. */
  166. template<typename RangeT>
  167. inline detail::is_any_ofF<
  168. BOOST_STRING_TYPENAME range_value<RangeT>::type>
  169. is_any_of( const RangeT& Set )
  170. {
  171. iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_set(boost::as_literal(Set));
  172. return detail::is_any_ofF<BOOST_STRING_TYPENAME range_value<RangeT>::type>(lit_set);
  173. }
  174. //! is_from_range predicate
  175. /*!
  176. Construct the \c is_from_range predicate. The predicate holds if the input
  177. is included in the specified range. (i.e. From <= Ch <= To )
  178. \param From The start of the range
  179. \param To The end of the range
  180. \return An instance of the \c is_from_range predicate
  181. */
  182. template<typename CharT>
  183. inline detail::is_from_rangeF<CharT> is_from_range(CharT From, CharT To)
  184. {
  185. return detail::is_from_rangeF<CharT>(From,To);
  186. }
  187. // predicate combinators ---------------------------------------------------//
  188. //! predicate 'and' composition predicate
  189. /*!
  190. Construct the \c class_and predicate. This predicate can be used
  191. to logically combine two classification predicates. \c class_and holds,
  192. if both predicates return true.
  193. \param Pred1 The first predicate
  194. \param Pred2 The second predicate
  195. \return An instance of the \c class_and predicate
  196. */
  197. template<typename Pred1T, typename Pred2T>
  198. inline detail::pred_andF<Pred1T, Pred2T>
  199. operator&&(
  200. const predicate_facade<Pred1T>& Pred1,
  201. const predicate_facade<Pred2T>& Pred2 )
  202. {
  203. // Doing the static_cast with the pointer instead of the reference
  204. // is a workaround for some compilers which have problems with
  205. // static_cast's of template references, i.e. CW8. /grafik/
  206. return detail::pred_andF<Pred1T,Pred2T>(
  207. *static_cast<const Pred1T*>(&Pred1),
  208. *static_cast<const Pred2T*>(&Pred2) );
  209. }
  210. //! predicate 'or' composition predicate
  211. /*!
  212. Construct the \c class_or predicate. This predicate can be used
  213. to logically combine two classification predicates. \c class_or holds,
  214. if one of the predicates return true.
  215. \param Pred1 The first predicate
  216. \param Pred2 The second predicate
  217. \return An instance of the \c class_or predicate
  218. */
  219. template<typename Pred1T, typename Pred2T>
  220. inline detail::pred_orF<Pred1T, Pred2T>
  221. operator||(
  222. const predicate_facade<Pred1T>& Pred1,
  223. const predicate_facade<Pred2T>& Pred2 )
  224. {
  225. // Doing the static_cast with the pointer instead of the reference
  226. // is a workaround for some compilers which have problems with
  227. // static_cast's of template references, i.e. CW8. /grafik/
  228. return detail::pred_orF<Pred1T,Pred2T>(
  229. *static_cast<const Pred1T*>(&Pred1),
  230. *static_cast<const Pred2T*>(&Pred2));
  231. }
  232. //! predicate negation operator
  233. /*!
  234. Construct the \c class_not predicate. This predicate represents a negation.
  235. \c class_or holds if of the predicates return false.
  236. \param Pred The predicate to be negated
  237. \return An instance of the \c class_not predicate
  238. */
  239. template<typename PredT>
  240. inline detail::pred_notF<PredT>
  241. operator!( const predicate_facade<PredT>& Pred )
  242. {
  243. // Doing the static_cast with the pointer instead of the reference
  244. // is a workaround for some compilers which have problems with
  245. // static_cast's of template references, i.e. CW8. /grafik/
  246. return detail::pred_notF<PredT>(*static_cast<const PredT*>(&Pred));
  247. }
  248. } // namespace algorithm
  249. // pull names to the boost namespace
  250. using algorithm::is_classified;
  251. using algorithm::is_space;
  252. using algorithm::is_alnum;
  253. using algorithm::is_alpha;
  254. using algorithm::is_cntrl;
  255. using algorithm::is_digit;
  256. using algorithm::is_graph;
  257. using algorithm::is_lower;
  258. using algorithm::is_upper;
  259. using algorithm::is_print;
  260. using algorithm::is_punct;
  261. using algorithm::is_xdigit;
  262. using algorithm::is_any_of;
  263. using algorithm::is_from_range;
  264. } // namespace boost
  265. #endif // BOOST_STRING_PREDICATE_HPP