find.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Boost string_algo library find.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_FIND_HPP
  9. #define BOOST_STRING_FIND_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/as_literal.hpp>
  16. #include <boost/algorithm/string/finder.hpp>
  17. #include <boost/algorithm/string/compare.hpp>
  18. #include <boost/algorithm/string/constants.hpp>
  19. /*! \file
  20. Defines a set of find algorithms. The algorithms are searching
  21. for a substring of the input. The result is given as an \c iterator_range
  22. delimiting the substring.
  23. */
  24. namespace boost {
  25. namespace algorithm {
  26. // Generic find -----------------------------------------------//
  27. //! Generic find algorithm
  28. /*!
  29. Search the input using the given finder.
  30. \param Input A string which will be searched.
  31. \param Finder Finder object used for searching.
  32. \return
  33. An \c iterator_range delimiting the match.
  34. Returned iterator is either \c RangeT::iterator or
  35. \c RangeT::const_iterator, depending on the constness of
  36. the input parameter.
  37. */
  38. template<typename RangeT, typename FinderT>
  39. inline iterator_range<
  40. BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
  41. find(
  42. RangeT& Input,
  43. const FinderT& Finder)
  44. {
  45. iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
  46. return Finder(::boost::begin(lit_input),::boost::end(lit_input));
  47. }
  48. // find_first -----------------------------------------------//
  49. //! Find first algorithm
  50. /*!
  51. Search for the first occurrence of the substring in the input.
  52. \param Input A string which will be searched.
  53. \param Search A substring to be searched for.
  54. \return
  55. An \c iterator_range delimiting the match.
  56. Returned iterator is either \c RangeT::iterator or
  57. \c RangeT::const_iterator, depending on the constness of
  58. the input parameter.
  59. \note This function provides the strong exception-safety guarantee
  60. */
  61. template<typename Range1T, typename Range2T>
  62. inline iterator_range<
  63. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  64. find_first(
  65. Range1T& Input,
  66. const Range2T& Search)
  67. {
  68. return ::boost::algorithm::find(Input, ::boost::algorithm::first_finder(Search));
  69. }
  70. //! Find first algorithm ( case insensitive )
  71. /*!
  72. Search for the first occurrence of the substring in the input.
  73. Searching is case insensitive.
  74. \param Input A string which will be searched.
  75. \param Search A substring to be searched for.
  76. \param Loc A locale used for case insensitive comparison
  77. \return
  78. An \c iterator_range delimiting the match.
  79. Returned iterator is either \c Range1T::iterator or
  80. \c Range1T::const_iterator, depending on the constness of
  81. the input parameter.
  82. \note This function provides the strong exception-safety guarantee
  83. */
  84. template<typename Range1T, typename Range2T>
  85. inline iterator_range<
  86. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  87. ifind_first(
  88. Range1T& Input,
  89. const Range2T& Search,
  90. const std::locale& Loc=std::locale())
  91. {
  92. return ::boost::algorithm::find(Input, ::boost::algorithm::first_finder(Search,is_iequal(Loc)));
  93. }
  94. // find_last -----------------------------------------------//
  95. //! Find last algorithm
  96. /*!
  97. Search for the last occurrence of the substring in the input.
  98. \param Input A string which will be searched.
  99. \param Search A substring to be searched for.
  100. \return
  101. An \c iterator_range delimiting the match.
  102. Returned iterator is either \c Range1T::iterator or
  103. \c Range1T::const_iterator, depending on the constness of
  104. the input parameter.
  105. \note This function provides the strong exception-safety guarantee
  106. */
  107. template<typename Range1T, typename Range2T>
  108. inline iterator_range<
  109. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  110. find_last(
  111. Range1T& Input,
  112. const Range2T& Search)
  113. {
  114. return ::boost::algorithm::find(Input, ::boost::algorithm::last_finder(Search));
  115. }
  116. //! Find last algorithm ( case insensitive )
  117. /*!
  118. Search for the last match a string in the input.
  119. Searching is case insensitive.
  120. \param Input A string which will be searched.
  121. \param Search A substring to be searched for.
  122. \param Loc A locale used for case insensitive comparison
  123. \return
  124. An \c iterator_range delimiting the match.
  125. Returned iterator is either \c Range1T::iterator or
  126. \c Range1T::const_iterator, depending on the constness of
  127. the input parameter.
  128. \note This function provides the strong exception-safety guarantee
  129. */
  130. template<typename Range1T, typename Range2T>
  131. inline iterator_range<
  132. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  133. ifind_last(
  134. Range1T& Input,
  135. const Range2T& Search,
  136. const std::locale& Loc=std::locale())
  137. {
  138. return ::boost::algorithm::find(Input, ::boost::algorithm::last_finder(Search, is_iequal(Loc)));
  139. }
  140. // find_nth ----------------------------------------------------------------------//
  141. //! Find n-th algorithm
  142. /*!
  143. Search for the n-th (zero-indexed) occurrence of the substring in the
  144. input.
  145. \param Input A string which will be searched.
  146. \param Search A substring to be searched for.
  147. \param Nth An index (zero-indexed) of the match to be found.
  148. For negative N, the matches are counted from the end of string.
  149. \return
  150. An \c iterator_range delimiting the match.
  151. Returned iterator is either \c Range1T::iterator or
  152. \c Range1T::const_iterator, depending on the constness of
  153. the input parameter.
  154. */
  155. template<typename Range1T, typename Range2T>
  156. inline iterator_range<
  157. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  158. find_nth(
  159. Range1T& Input,
  160. const Range2T& Search,
  161. int Nth)
  162. {
  163. return ::boost::algorithm::find(Input, ::boost::algorithm::nth_finder(Search,Nth));
  164. }
  165. //! Find n-th algorithm ( case insensitive ).
  166. /*!
  167. Search for the n-th (zero-indexed) occurrence of the substring in the
  168. input. Searching is case insensitive.
  169. \param Input A string which will be searched.
  170. \param Search A substring to be searched for.
  171. \param Nth An index (zero-indexed) of the match to be found.
  172. For negative N, the matches are counted from the end of string.
  173. \param Loc A locale used for case insensitive comparison
  174. \return
  175. An \c iterator_range delimiting the match.
  176. Returned iterator is either \c Range1T::iterator or
  177. \c Range1T::const_iterator, depending on the constness of
  178. the input parameter.
  179. \note This function provides the strong exception-safety guarantee
  180. */
  181. template<typename Range1T, typename Range2T>
  182. inline iterator_range<
  183. BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
  184. ifind_nth(
  185. Range1T& Input,
  186. const Range2T& Search,
  187. int Nth,
  188. const std::locale& Loc=std::locale())
  189. {
  190. return ::boost::algorithm::find(Input, ::boost::algorithm::nth_finder(Search,Nth,is_iequal(Loc)));
  191. }
  192. // find_head ----------------------------------------------------------------------//
  193. //! Find head algorithm
  194. /*!
  195. Get the head of the input. Head is a prefix of the string of the
  196. given size. If the input is shorter then required, whole input is considered
  197. to be the head.
  198. \param Input An input string
  199. \param N Length of the head
  200. For N>=0, at most N characters are extracted.
  201. For N<0, at most size(Input)-|N| characters are extracted.
  202. \return
  203. An \c iterator_range delimiting the match.
  204. Returned iterator is either \c Range1T::iterator or
  205. \c Range1T::const_iterator, depending on the constness of
  206. the input parameter.
  207. \note This function provides the strong exception-safety guarantee
  208. */
  209. template<typename RangeT>
  210. inline iterator_range<
  211. BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
  212. find_head(
  213. RangeT& Input,
  214. int N)
  215. {
  216. return ::boost::algorithm::find(Input, ::boost::algorithm::head_finder(N));
  217. }
  218. // find_tail ----------------------------------------------------------------------//
  219. //! Find tail algorithm
  220. /*!
  221. Get the tail of the input. Tail is a suffix of the string of the
  222. given size. If the input is shorter then required, whole input is considered
  223. to be the tail.
  224. \param Input An input string
  225. \param N Length of the tail.
  226. For N>=0, at most N characters are extracted.
  227. For N<0, at most size(Input)-|N| characters are extracted.
  228. \return
  229. An \c iterator_range delimiting the match.
  230. Returned iterator is either \c RangeT::iterator or
  231. \c RangeT::const_iterator, depending on the constness of
  232. the input parameter.
  233. \note This function provides the strong exception-safety guarantee
  234. */
  235. template<typename RangeT>
  236. inline iterator_range<
  237. BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
  238. find_tail(
  239. RangeT& Input,
  240. int N)
  241. {
  242. return ::boost::algorithm::find(Input, ::boost::algorithm::tail_finder(N));
  243. }
  244. // find_token --------------------------------------------------------------------//
  245. //! Find token algorithm
  246. /*!
  247. Look for a given token in the string. Token is a character that matches the
  248. given predicate.
  249. If the "token compress mode" is enabled, adjacent tokens are considered to be one match.
  250. \param Input A input string.
  251. \param Pred A unary predicate to identify a token
  252. \param eCompress Enable/Disable compressing of adjacent tokens
  253. \return
  254. An \c iterator_range delimiting the match.
  255. Returned iterator is either \c RangeT::iterator or
  256. \c RangeT::const_iterator, depending on the constness of
  257. the input parameter.
  258. \note This function provides the strong exception-safety guarantee
  259. */
  260. template<typename RangeT, typename PredicateT>
  261. inline iterator_range<
  262. BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
  263. find_token(
  264. RangeT& Input,
  265. PredicateT Pred,
  266. token_compress_mode_type eCompress=token_compress_off)
  267. {
  268. return ::boost::algorithm::find(Input, ::boost::algorithm::token_finder(Pred, eCompress));
  269. }
  270. } // namespace algorithm
  271. // pull names to the boost namespace
  272. using algorithm::find;
  273. using algorithm::find_first;
  274. using algorithm::ifind_first;
  275. using algorithm::find_last;
  276. using algorithm::ifind_last;
  277. using algorithm::find_nth;
  278. using algorithm::ifind_nth;
  279. using algorithm::find_head;
  280. using algorithm::find_tail;
  281. using algorithm::find_token;
  282. } // namespace boost
  283. #endif // BOOST_STRING_FIND_HPP