source: NonGTP/Boost/boost/algorithm/string/regex.hpp @ 857

Revision 857, 18.6 KB checked in by igarcia, 18 years ago (diff)
Line 
1//  Boost string_algo library regex.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_REGEX_HPP
11#define BOOST_STRING_REGEX_HPP
12
13#include <boost/algorithm/string/config.hpp>
14#include <boost/regex.hpp>
15
16#include <boost/range/iterator_range.hpp>
17#include <boost/range/begin.hpp>
18#include <boost/range/end.hpp>
19#include <boost/range/result_iterator.hpp>
20
21#include <boost/algorithm/string/find_format.hpp>
22#include <boost/algorithm/string/regex_find_format.hpp>
23#include <boost/algorithm/string/formatter.hpp>
24#include <boost/algorithm/string/iter_find.hpp>
25
26/*! \file
27    Defines regex variants of the algorithms.
28*/
29
30namespace boost {
31    namespace algorithm {
32
33//  find_regex  -----------------------------------------------//
34
35        //! Find regex algorithm
36        /*!
37            Search for a substring matching the given regex in the input.
38           
39            \param Input A container which will be searched.
40            \param Rx A regular expression
41            \param Flags Regex options
42            \return
43                An \c iterator_range delimiting the match.
44                Returned iterator is either \c RangeT::iterator or
45                \c RangeT::const_iterator, depending on the constness of
46                the input parameter.
47
48              \note This function provides the strong exception-safety guarantee
49        */
50        template<
51            typename RangeT,
52            typename CharT,
53            typename RegexTraitsT>
54        inline iterator_range<
55            BOOST_STRING_TYPENAME range_result_iterator<RangeT>::type >
56        find_regex(
57            RangeT& Input,
58            const basic_regex<CharT, RegexTraitsT>& Rx,
59            match_flag_type Flags=match_default )
60        {
61            return regex_finder(Rx,Flags)(
62                begin(Input), end(Input) );
63        }
64
65//  replace_regex --------------------------------------------------------------------//
66
67        //! Replace regex algorithm
68        /*!
69            Search for a substring matching given regex and format it with
70            the specified format.             
71            The result is a modified copy of the input. It is returned as a sequence
72            or copied to the output iterator.
73
74            \param Output An output iterator to which the result will be copied
75            \param Input An input string
76            \param Rx A regular expression
77            \param Format Regex format definition
78            \param Flags Regex options
79            \return An output iterator pointing just after the last inserted character or
80                    a modified copy of the input   
81
82             \note The second variant of this function provides the strong exception-safety guarantee
83        */
84        template<
85            typename OutputIteratorT,
86            typename RangeT,
87            typename CharT,
88            typename RegexTraitsT,
89            typename FormatStringTraitsT, typename FormatStringAllocatorT >
90        inline OutputIteratorT replace_regex_copy(
91            OutputIteratorT Output,
92            const RangeT& Input,
93            const basic_regex<CharT, RegexTraitsT>& Rx,
94            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
95            match_flag_type Flags=match_default | format_default )
96        {
97            return find_format_copy(
98                Output,
99                Input,
100                regex_finder( Rx, Flags ),
101                regex_formatter( Format, Flags ) );
102        }
103
104        //! Replace regex algorithm
105        /*!
106            \overload
107        */
108        template<
109            typename SequenceT,
110            typename CharT,
111            typename RegexTraitsT,
112            typename FormatStringTraitsT, typename FormatStringAllocatorT >
113        inline SequenceT replace_regex_copy(
114            const SequenceT& Input,
115            const basic_regex<CharT, RegexTraitsT>& Rx,
116            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
117            match_flag_type Flags=match_default | format_default )
118        {
119            return find_format_copy(
120                Input,
121                regex_finder( Rx, Flags ),
122                regex_formatter( Format, Flags ) );
123        }
124
125        //! Replace regex algorithm
126        /*!
127            Search for a substring matching given regex and format it with
128            the specified format. The input string is modified in-place.
129
130            \param Input An input string
131            \param Rx A regular expression
132            \param Format Regex format definition
133            \param Flags Regex options
134        */
135        template<
136            typename SequenceT,
137            typename CharT,
138            typename RegexTraitsT,
139            typename FormatStringTraitsT, typename FormatStringAllocatorT >
140        inline void replace_regex(
141            SequenceT& Input,
142            const basic_regex<CharT, RegexTraitsT>& Rx,
143            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
144            match_flag_type Flags=match_default | format_default )
145        {
146            find_format(
147                Input,
148                regex_finder( Rx, Flags ),
149                regex_formatter( Format, Flags ) );
150        }
151
152//  replace_all_regex --------------------------------------------------------------------//
153
154        //! Replace all regex algorithm
155        /*!
156            Format all substrings, matching given regex, with the specified format.
157            The result is a modified copy of the input. It is returned as a sequence
158            or copied to the output iterator.
159
160            \param Output An output iterator to which the result will be copied
161            \param Input An input string
162            \param Rx A regular expression
163            \param Format Regex format definition
164            \param Flags Regex options
165            \return An output iterator pointing just after the last inserted character or
166                    a modified copy of the input     
167
168              \note The second variant of this function provides the strong exception-safety guarantee
169        */
170        template<
171            typename OutputIteratorT,
172            typename RangeT,
173            typename CharT,
174            typename RegexTraitsT,
175            typename FormatStringTraitsT, typename FormatStringAllocatorT >
176        inline OutputIteratorT replace_all_regex_copy(
177            OutputIteratorT Output,
178            const RangeT& Input,
179            const basic_regex<CharT, RegexTraitsT>& Rx,
180            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
181            match_flag_type Flags=match_default | format_default )
182        {
183            return find_format_all_copy(
184                Output,
185                Input,
186                regex_finder( Rx, Flags ),
187                regex_formatter( Format, Flags ) );
188        }
189
190        //! Replace all regex algorithm
191        /*!
192            \overload
193        */
194        template<
195            typename SequenceT,
196            typename CharT,
197            typename RegexTraitsT,
198            typename FormatStringTraitsT, typename FormatStringAllocatorT >
199        inline SequenceT replace_all_regex_copy(
200            const SequenceT& Input,
201            const basic_regex<CharT, RegexTraitsT>& Rx,
202            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
203            match_flag_type Flags=match_default | format_default )
204        {
205            return find_format_all_copy(
206                Input,
207                regex_finder( Rx, Flags ),
208                regex_formatter( Format, Flags ) );
209        }
210
211        //! Replace all regex algorithm
212        /*!
213            Format all substrings, matching given regex, with the specified format.
214            The input string is modified in-place.
215
216            \param Input An input string
217            \param Rx A regular expression
218            \param Format Regex format definition
219            \param Flags Regex options           
220        */
221        template<
222            typename SequenceT,
223            typename CharT,
224            typename RegexTraitsT,
225            typename FormatStringTraitsT, typename FormatStringAllocatorT >
226        inline void replace_all_regex(
227            SequenceT& Input,
228            const basic_regex<CharT, RegexTraitsT>& Rx,
229            const std::basic_string<CharT, FormatStringTraitsT, FormatStringAllocatorT>& Format,
230            match_flag_type Flags=match_default | format_default )
231        {
232            find_format_all(
233                Input,
234                regex_finder( Rx, Flags ),
235                regex_formatter( Format, Flags ) );
236        }
237
238//  erase_regex --------------------------------------------------------------------//
239
240        //! Erase regex algorithm
241        /*!
242            Remove a substring matching given regex from the input.
243            The result is a modified copy of the input. It is returned as a sequence
244            or copied to the output iterator.                       
245
246            \param Output An output iterator to which the result will be copied
247            \param Input An input string
248            \param Rx A regular expression
249            \param Flags Regex options
250            \return An output iterator pointing just after the last inserted character or
251                       a modified copy of the input   
252
253             \note The second variant of this function provides the strong exception-safety guarantee
254       */
255        template<
256            typename OutputIteratorT,
257            typename RangeT,
258            typename CharT,
259            typename RegexTraitsT >
260        inline OutputIteratorT erase_regex_copy(
261            OutputIteratorT Output,
262            const RangeT& Input,
263            const basic_regex<CharT, RegexTraitsT>& Rx,
264            match_flag_type Flags=match_default )
265        {
266            return find_format_copy(
267                Output,
268                Input,
269                regex_finder( Rx, Flags ),
270                empty_formatter( Input ) );
271        }
272
273        //! Erase regex algorithm
274        /*!
275            \overload
276        */
277        template<
278            typename SequenceT,
279            typename CharT,
280            typename RegexTraitsT >
281        inline SequenceT erase_regex_copy(
282            const SequenceT& Input,
283            const basic_regex<CharT, RegexTraitsT>& Rx,
284            match_flag_type Flags=match_default )
285        {
286            return find_format_copy(
287                Input,
288                regex_finder( Rx, Flags ),
289                empty_formatter( Input ) );
290        }
291
292        //! Erase regex algorithm
293        /*!
294            Remove a substring matching given regex from the input.
295            The input string is modified in-place.
296
297            \param Input An input string
298            \param Rx A regular expression
299            \param Flags Regex options
300        */
301        template<
302            typename SequenceT,
303            typename CharT,
304            typename RegexTraitsT >
305        inline void erase_regex(
306            SequenceT& Input,
307            const basic_regex<CharT, RegexTraitsT>& Rx,
308            match_flag_type Flags=match_default )
309        {
310            find_format(
311                Input,
312                regex_finder( Rx, Flags ),
313                empty_formatter( Input ) );
314        }
315
316//  erase_all_regex --------------------------------------------------------------------//
317
318        //! Erase all regex algorithm
319        /*!
320            Erase all substrings, matching given regex, from the input.
321            The result is a modified copy of the input. It is returned as a sequence
322            or copied to the output iterator.
323           
324
325            \param Output An output iterator to which the result will be copied
326            \param Input An input string
327            \param Rx A regular expression
328            \param Flags Regex options
329            \return An output iterator pointing just after the last inserted character or
330                    a modified copy of the input                       
331
332             \note The second variant of this function provides the strong exception-safety guarantee
333        */
334        template<
335            typename OutputIteratorT,
336            typename RangeT,
337            typename CharT,
338            typename RegexTraitsT >
339        inline OutputIteratorT erase_all_regex_copy(
340            OutputIteratorT Output,
341            const RangeT& Input,
342            const basic_regex<CharT, RegexTraitsT>& Rx,
343            match_flag_type Flags=match_default )
344        {
345            return find_format_all_copy(
346                Output,
347                Input,
348                regex_finder( Rx, Flags ),
349                empty_formatter( Input ) );
350        }
351
352        //! Erase all regex algorithm
353        /*!
354            \overload
355        */
356        template<
357            typename SequenceT,
358            typename CharT,
359            typename RegexTraitsT >
360        inline SequenceT erase_all_regex_copy(
361            const SequenceT& Input,
362            const basic_regex<CharT, RegexTraitsT>& Rx,
363            match_flag_type Flags=match_default )
364        {
365            return find_format_all_copy(
366                Input,
367                regex_finder( Rx, Flags ),
368                empty_formatter( Input ) );
369        }
370
371        //! Erase all regex algorithm
372        /*!
373            Erase all substrings, matching given regex, from the input.
374            The input string is modified in-place.
375
376            \param Input An input string
377            \param Rx A regular expression
378            \param Flags Regex options
379        */
380        template<
381            typename SequenceT,
382            typename CharT,
383            typename RegexTraitsT>
384        inline void erase_all_regex(
385            SequenceT& Input,
386            const basic_regex<CharT, RegexTraitsT>& Rx,
387            match_flag_type Flags=match_default )
388        {
389            find_format_all(
390                Input,
391                regex_finder( Rx, Flags ),
392                empty_formatter( Input ) );
393        }
394
395//  find_all_regex ------------------------------------------------------------------//
396
397        //! Find all regex algorithm
398        /*!
399            This algorithm finds all substrings matching the give regex
400            in the input.             
401           
402            Each part is copied and added as a new element to the output container.
403            Thus the result container must be able to hold copies
404            of the matches (in a compatible structure like std::string) or
405            a reference to it (e.g. using the iterator range class).
406            Examples of such a container are \c std::vector<std::string>
407            or \c std::list<boost::iterator_range<std::string::iterator>>
408
409            \param Result A container that can hold copies of references to the substrings.
410            \param Input A container which will be searched.
411            \param Rx A regular expression
412            \param Flags Regex options
413            \return A reference to the result
414
415            \note Prior content of the result will be overwritten.
416
417             \note This function provides the strong exception-safety guarantee
418        */
419        template<
420            typename SequenceSequenceT,
421            typename RangeT,         
422            typename CharT,
423            typename RegexTraitsT >
424        inline SequenceSequenceT& find_all_regex(
425            SequenceSequenceT& Result,
426            const RangeT& Input,
427            const basic_regex<CharT, RegexTraitsT>& Rx,
428            match_flag_type Flags=match_default )
429        {
430            return iter_find(
431                Result,
432                Input,
433                regex_finder(Rx,Flags) );         
434        }
435
436//  split_regex ------------------------------------------------------------------//
437
438        //! Split regex algorithm
439        /*!
440            Tokenize expression. This function is equivalent to C strtok. Input
441            sequence is split into tokens, separated  by separators. Separator
442            is an every match of the given regex.
443            Each part is copied and added as a new element to the output container.
444            Thus the result container must be able to hold copies
445            of the matches (in a compatible structure like std::string) or
446            a reference to it (e.g. using the iterator range class).
447            Examples of such a container are \c std::vector<std::string>
448            or \c std::list<boost::iterator_range<std::string::iterator>>
449   
450            \param Result A container that can hold copies of references to the substrings.         
451            \param Input A container which will be searched.
452            \param Rx A regular expression
453            \param Flags Regex options
454            \return A reference to the result
455
456            \note Prior content of the result will be overwritten.
457
458               \note This function provides the strong exception-safety guarantee
459        */
460        template<
461            typename SequenceSequenceT,
462            typename RangeT,         
463            typename CharT,
464            typename RegexTraitsT >
465        inline SequenceSequenceT& split_regex(
466            SequenceSequenceT& Result,
467            const RangeT& Input,
468            const basic_regex<CharT, RegexTraitsT>& Rx,
469            match_flag_type Flags=match_default )
470        {
471            return iter_split(
472                Result,
473                Input,
474                regex_finder(Rx,Flags) );         
475        }
476
477    } // namespace algorithm
478
479    // pull names into the boost namespace
480    using algorithm::find_regex;
481    using algorithm::replace_regex;
482    using algorithm::replace_regex_copy;
483    using algorithm::replace_all_regex;
484    using algorithm::replace_all_regex_copy;
485    using algorithm::erase_regex;
486    using algorithm::erase_regex_copy;
487    using algorithm::erase_all_regex;
488    using algorithm::erase_all_regex_copy;
489    using algorithm::find_all_regex;
490    using algorithm::split_regex;
491
492} // namespace boost
493
494
495#endif  // BOOST_STRING_REGEX_HPP
Note: See TracBrowser for help on using the repository browser.