finder.hpp 9.4 KB

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