1 // Boost string_algo library find.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003.
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_FIND_HPP
12 #define BOOST_STRING_FIND_HPP
14 #include <boost/algorithm/string/config.hpp>
16 #include <boost/range/iterator_range_core.hpp>
17 #include <boost/range/begin.hpp>
18 #include <boost/range/end.hpp>
19 #include <boost/range/iterator.hpp>
20 #include <boost/range/as_literal.hpp>
22 #include <boost/algorithm/string/finder.hpp>
23 #include <boost/algorithm/string/compare.hpp>
24 #include <boost/algorithm/string/constants.hpp>
27 Defines a set of find algorithms. The algorithms are searching
28 for a substring of the input. The result is given as an \c iterator_range
29 delimiting the substring.
35 // Generic find -----------------------------------------------//
37 //! Generic find algorithm
39 Search the input using the given finder.
41 \param Input A string which will be searched.
42 \param Finder Finder object used for searching.
44 An \c iterator_range delimiting the match.
45 Returned iterator is either \c RangeT::iterator or
46 \c RangeT::const_iterator, depending on the constness of
49 template<typename RangeT, typename FinderT>
50 inline iterator_range<
51 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
54 const FinderT& Finder)
56 iterator_range<BOOST_STRING_TYPENAME range_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
58 return Finder(::boost::begin(lit_input),::boost::end(lit_input));
61 // find_first -----------------------------------------------//
63 //! Find first algorithm
65 Search for the first occurrence of the substring in the input.
67 \param Input A string which will be searched.
68 \param Search A substring to be searched for.
70 An \c iterator_range delimiting the match.
71 Returned iterator is either \c RangeT::iterator or
72 \c RangeT::const_iterator, depending on the constness of
75 \note This function provides the strong exception-safety guarantee
77 template<typename Range1T, typename Range2T>
78 inline iterator_range<
79 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
82 const Range2T& Search)
84 return ::boost::algorithm::find(Input, ::boost::algorithm::first_finder(Search));
87 //! Find first algorithm ( case insensitive )
89 Search for the first occurrence of the substring in the input.
90 Searching is case insensitive.
92 \param Input A string which will be searched.
93 \param Search A substring to be searched for.
94 \param Loc A locale used for case insensitive comparison
96 An \c iterator_range delimiting the match.
97 Returned iterator is either \c Range1T::iterator or
98 \c Range1T::const_iterator, depending on the constness of
101 \note This function provides the strong exception-safety guarantee
103 template<typename Range1T, typename Range2T>
104 inline iterator_range<
105 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
108 const Range2T& Search,
109 const std::locale& Loc=std::locale())
111 return ::boost::algorithm::find(Input, ::boost::algorithm::first_finder(Search,is_iequal(Loc)));
114 // find_last -----------------------------------------------//
116 //! Find last algorithm
118 Search for the last occurrence of the substring in the input.
120 \param Input A string which will be searched.
121 \param Search A substring to be searched for.
123 An \c iterator_range delimiting the match.
124 Returned iterator is either \c Range1T::iterator or
125 \c Range1T::const_iterator, depending on the constness of
128 \note This function provides the strong exception-safety guarantee
130 template<typename Range1T, typename Range2T>
131 inline iterator_range<
132 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
135 const Range2T& Search)
137 return ::boost::algorithm::find(Input, ::boost::algorithm::last_finder(Search));
140 //! Find last algorithm ( case insensitive )
142 Search for the last match a string in the input.
143 Searching is case insensitive.
145 \param Input A string which will be searched.
146 \param Search A substring to be searched for.
147 \param Loc A locale used for case insensitive comparison
149 An \c iterator_range delimiting the match.
150 Returned iterator is either \c Range1T::iterator or
151 \c Range1T::const_iterator, depending on the constness of
154 \note This function provides the strong exception-safety guarantee
156 template<typename Range1T, typename Range2T>
157 inline iterator_range<
158 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
161 const Range2T& Search,
162 const std::locale& Loc=std::locale())
164 return ::boost::algorithm::find(Input, ::boost::algorithm::last_finder(Search, is_iequal(Loc)));
167 // find_nth ----------------------------------------------------------------------//
169 //! Find n-th algorithm
171 Search for the n-th (zero-indexed) occurrence of the substring in the
174 \param Input A string which will be searched.
175 \param Search A substring to be searched for.
176 \param Nth An index (zero-indexed) of the match to be found.
177 For negative N, the matches are counted from the end of string.
179 An \c iterator_range delimiting the match.
180 Returned iterator is either \c Range1T::iterator or
181 \c Range1T::const_iterator, depending on the constness of
184 template<typename Range1T, typename Range2T>
185 inline iterator_range<
186 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
189 const Range2T& Search,
192 return ::boost::algorithm::find(Input, ::boost::algorithm::nth_finder(Search,Nth));
195 //! Find n-th algorithm ( case insensitive ).
197 Search for the n-th (zero-indexed) occurrence of the substring in the
198 input. Searching is case insensitive.
200 \param Input A string which will be searched.
201 \param Search A substring to be searched for.
202 \param Nth An index (zero-indexed) of the match to be found.
203 For negative N, the matches are counted from the end of string.
204 \param Loc A locale used for case insensitive comparison
206 An \c iterator_range delimiting the match.
207 Returned iterator is either \c Range1T::iterator or
208 \c Range1T::const_iterator, depending on the constness of
212 \note This function provides the strong exception-safety guarantee
214 template<typename Range1T, typename Range2T>
215 inline iterator_range<
216 BOOST_STRING_TYPENAME range_iterator<Range1T>::type>
219 const Range2T& Search,
221 const std::locale& Loc=std::locale())
223 return ::boost::algorithm::find(Input, ::boost::algorithm::nth_finder(Search,Nth,is_iequal(Loc)));
226 // find_head ----------------------------------------------------------------------//
228 //! Find head algorithm
230 Get the head of the input. Head is a prefix of the string of the
231 given size. If the input is shorter then required, whole input is considered
234 \param Input An input string
235 \param N Length of the head
236 For N>=0, at most N characters are extracted.
237 For N<0, at most size(Input)-|N| characters are extracted.
239 An \c iterator_range delimiting the match.
240 Returned iterator is either \c Range1T::iterator or
241 \c Range1T::const_iterator, depending on the constness of
244 \note This function provides the strong exception-safety guarantee
246 template<typename RangeT>
247 inline iterator_range<
248 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
253 return ::boost::algorithm::find(Input, ::boost::algorithm::head_finder(N));
256 // find_tail ----------------------------------------------------------------------//
258 //! Find tail algorithm
260 Get the tail of the input. Tail is a suffix of the string of the
261 given size. If the input is shorter then required, whole input is considered
264 \param Input An input string
265 \param N Length of the tail.
266 For N>=0, at most N characters are extracted.
267 For N<0, at most size(Input)-|N| characters are extracted.
269 An \c iterator_range delimiting the match.
270 Returned iterator is either \c RangeT::iterator or
271 \c RangeT::const_iterator, depending on the constness of
275 \note This function provides the strong exception-safety guarantee
277 template<typename RangeT>
278 inline iterator_range<
279 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
284 return ::boost::algorithm::find(Input, ::boost::algorithm::tail_finder(N));
287 // find_token --------------------------------------------------------------------//
289 //! Find token algorithm
291 Look for a given token in the string. Token is a character that matches the
293 If the "token compress mode" is enabled, adjacent tokens are considered to be one match.
295 \param Input A input string.
296 \param Pred A unary predicate to identify a token
297 \param eCompress Enable/Disable compressing of adjacent tokens
299 An \c iterator_range delimiting the match.
300 Returned iterator is either \c RangeT::iterator or
301 \c RangeT::const_iterator, depending on the constness of
304 \note This function provides the strong exception-safety guarantee
306 template<typename RangeT, typename PredicateT>
307 inline iterator_range<
308 BOOST_STRING_TYPENAME range_iterator<RangeT>::type>
312 token_compress_mode_type eCompress=token_compress_off)
314 return ::boost::algorithm::find(Input, ::boost::algorithm::token_finder(Pred, eCompress));
317 } // namespace algorithm
319 // pull names to the boost namespace
320 using algorithm::find;
321 using algorithm::find_first;
322 using algorithm::ifind_first;
323 using algorithm::find_last;
324 using algorithm::ifind_last;
325 using algorithm::find_nth;
326 using algorithm::ifind_nth;
327 using algorithm::find_head;
328 using algorithm::find_tail;
329 using algorithm::find_token;
334 #endif // BOOST_STRING_FIND_HPP