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