finder.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Boost string_algo library finder.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2006.
  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_FINDER_HPP
  9. #define BOOST_STRING_FINDER_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/range/iterator_range_core.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <boost/range/const_iterator.hpp>
  16. #include <boost/algorithm/string/constants.hpp>
  17. #include <boost/algorithm/string/detail/finder.hpp>
  18. #include <boost/algorithm/string/compare.hpp>
  19. /*! \file
  20. Defines Finder generators. Finder object is a functor which is able to
  21. find a substring matching a specific criteria in the input.
  22. Finders are used as a pluggable components for replace, find
  23. and split facilities. This header contains generator functions
  24. for finders provided in this library.
  25. */
  26. namespace boost {
  27. namespace algorithm {
  28. // Finder generators ------------------------------------------//
  29. //! "First" finder
  30. /*!
  31. Construct the \c first_finder. The finder searches for the first
  32. occurrence of the string in a given input.
  33. The result is given as an \c iterator_range delimiting the match.
  34. \param Search A substring to be searched for.
  35. \return An instance of the \c first_finder object
  36. */
  37. template<typename RangeT>
  38. inline detail::first_finderF<
  39. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  40. is_equal>
  41. first_finder( const RangeT& Search )
  42. {
  43. return
  44. detail::first_finderF<
  45. BOOST_STRING_TYPENAME
  46. range_const_iterator<RangeT>::type,
  47. is_equal>( ::boost::as_literal(Search), is_equal() ) ;
  48. }
  49. //! "First" finder
  50. /*!
  51. \overload
  52. */
  53. template<typename RangeT,typename PredicateT>
  54. inline detail::first_finderF<
  55. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  56. PredicateT>
  57. first_finder(
  58. const RangeT& Search, PredicateT Comp )
  59. {
  60. return
  61. detail::first_finderF<
  62. BOOST_STRING_TYPENAME
  63. range_const_iterator<RangeT>::type,
  64. PredicateT>( ::boost::as_literal(Search), Comp );
  65. }
  66. //! "Last" finder
  67. /*!
  68. Construct the \c last_finder. The finder searches for the last
  69. occurrence of the string in a given input.
  70. The result is given as an \c iterator_range delimiting the match.
  71. \param Search A substring to be searched for.
  72. \return An instance of the \c last_finder object
  73. */
  74. template<typename RangeT>
  75. inline detail::last_finderF<
  76. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  77. is_equal>
  78. last_finder( const RangeT& Search )
  79. {
  80. return
  81. detail::last_finderF<
  82. BOOST_STRING_TYPENAME
  83. range_const_iterator<RangeT>::type,
  84. is_equal>( ::boost::as_literal(Search), is_equal() );
  85. }
  86. //! "Last" finder
  87. /*!
  88. \overload
  89. */
  90. template<typename RangeT, typename PredicateT>
  91. inline detail::last_finderF<
  92. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  93. PredicateT>
  94. last_finder( const RangeT& Search, PredicateT Comp )
  95. {
  96. return
  97. detail::last_finderF<
  98. BOOST_STRING_TYPENAME
  99. range_const_iterator<RangeT>::type,
  100. PredicateT>( ::boost::as_literal(Search), Comp ) ;
  101. }
  102. //! "Nth" finder
  103. /*!
  104. Construct the \c nth_finder. The finder searches for the n-th (zero-indexed)
  105. occurrence of the string in a given input.
  106. The result is given as an \c iterator_range delimiting the match.
  107. \param Search A substring to be searched for.
  108. \param Nth An index of the match to be find
  109. \return An instance of the \c nth_finder object
  110. */
  111. template<typename RangeT>
  112. inline detail::nth_finderF<
  113. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  114. is_equal>
  115. nth_finder(
  116. const RangeT& Search,
  117. int Nth)
  118. {
  119. return
  120. detail::nth_finderF<
  121. BOOST_STRING_TYPENAME
  122. range_const_iterator<RangeT>::type,
  123. is_equal>( ::boost::as_literal(Search), Nth, is_equal() ) ;
  124. }
  125. //! "Nth" finder
  126. /*!
  127. \overload
  128. */
  129. template<typename RangeT, typename PredicateT>
  130. inline detail::nth_finderF<
  131. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type,
  132. PredicateT>
  133. nth_finder(
  134. const RangeT& Search,
  135. int Nth,
  136. PredicateT Comp )
  137. {
  138. return
  139. detail::nth_finderF<
  140. BOOST_STRING_TYPENAME
  141. range_const_iterator<RangeT>::type,
  142. PredicateT>( ::boost::as_literal(Search), Nth, Comp );
  143. }
  144. //! "Head" finder
  145. /*!
  146. Construct the \c head_finder. The finder returns a head of a given
  147. input. The head is a prefix of a string up to n elements in
  148. size. If an input has less then n elements, whole input is
  149. considered a head.
  150. The result is given as an \c iterator_range delimiting the match.
  151. \param N The size of the head
  152. \return An instance of the \c head_finder object
  153. */
  154. inline detail::head_finderF
  155. head_finder( int N )
  156. {
  157. return detail::head_finderF(N);
  158. }
  159. //! "Tail" finder
  160. /*!
  161. Construct the \c tail_finder. The finder returns a tail of a given
  162. input. The tail is a suffix of a string up to n elements in
  163. size. If an input has less then n elements, whole input is
  164. considered a head.
  165. The result is given as an \c iterator_range delimiting the match.
  166. \param N The size of the head
  167. \return An instance of the \c tail_finder object
  168. */
  169. inline detail::tail_finderF
  170. tail_finder( int N )
  171. {
  172. return detail::tail_finderF(N);
  173. }
  174. //! "Token" finder
  175. /*!
  176. Construct the \c token_finder. The finder searches for a token
  177. specified by a predicate. It is similar to std::find_if
  178. algorithm, with an exception that it return a range of
  179. instead of a single iterator.
  180. If "compress token mode" is enabled, adjacent matching tokens are
  181. concatenated into one match. Thus the finder can be used to
  182. search for continuous segments of characters satisfying the
  183. given predicate.
  184. The result is given as an \c iterator_range delimiting the match.
  185. \param Pred An element selection predicate
  186. \param eCompress Compress flag
  187. \return An instance of the \c token_finder object
  188. */
  189. template< typename PredicateT >
  190. inline detail::token_finderF<PredicateT>
  191. token_finder(
  192. PredicateT Pred,
  193. token_compress_mode_type eCompress=token_compress_off )
  194. {
  195. return detail::token_finderF<PredicateT>( Pred, eCompress );
  196. }
  197. //! "Range" finder
  198. /*!
  199. Construct the \c range_finder. The finder does not perform
  200. any operation. It simply returns the given range for
  201. any input.
  202. \param Begin Beginning of the range
  203. \param End End of the range
  204. \return An instance of the \c range_finger object
  205. */
  206. template< typename ForwardIteratorT >
  207. inline detail::range_finderF<ForwardIteratorT>
  208. range_finder(
  209. ForwardIteratorT Begin,
  210. ForwardIteratorT End )
  211. {
  212. return detail::range_finderF<ForwardIteratorT>( Begin, End );
  213. }
  214. //! "Range" finder
  215. /*!
  216. \overload
  217. */
  218. template< typename ForwardIteratorT >
  219. inline detail::range_finderF<ForwardIteratorT>
  220. range_finder( iterator_range<ForwardIteratorT> Range )
  221. {
  222. return detail::range_finderF<ForwardIteratorT>( Range );
  223. }
  224. } // namespace algorithm
  225. // pull the names to the boost namespace
  226. using algorithm::first_finder;
  227. using algorithm::last_finder;
  228. using algorithm::nth_finder;
  229. using algorithm::head_finder;
  230. using algorithm::tail_finder;
  231. using algorithm::token_finder;
  232. using algorithm::range_finder;
  233. } // namespace boost
  234. #endif // BOOST_STRING_FINDER_HPP