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