1// Boost string_algo library find_format.hpp header file ---------------------------//
2
3// Copyright Pavol Droba 2002-2003.
4//
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)
8
9// See http://www.boost.org/ for updates, documentation, and revision history.
10
11#ifndef BOOST_STRING_FIND_FORMAT_HPP
12#define BOOST_STRING_FIND_FORMAT_HPP
13
14#include <deque>
15#include <boost/detail/iterator.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/const_iterator.hpp>
20#include <boost/range/as_literal.hpp>
21
22#include <boost/algorithm/string/concept.hpp>
23#include <boost/algorithm/string/detail/find_format.hpp>
24#include <boost/algorithm/string/detail/find_format_all.hpp>
25
26/*! \file
27 Defines generic replace algorithms. Each algorithm replaces
28 part(s) of the input. The part to be replaced is looked up using a Finder object.
29 Result of finding is then used by a Formatter object to generate the replacement.
30*/
31
32namespace boost {
33 namespace algorithm {
34
35// generic replace -----------------------------------------------------------------//
36
37 //! Generic replace algorithm
38 /*!
39 Use the Finder to search for a substring. Use the Formatter to format
40 this substring and replace it in the input.
41 The result is a modified copy of the input. It is returned as a sequence
42 or copied to the output iterator.
43
44 \param Output An output iterator to which the result will be copied
45 \param Input An input sequence
46 \param Finder A Finder object used to search for a match to be replaced
47 \param Formatter A Formatter object used to format a match
48 \return An output iterator pointing just after the last inserted character or
49 a modified copy of the input
50
51 \note The second variant of this function provides the strong exception-safety guarantee
52 */
53 template<
54 typename OutputIteratorT,
55 typename RangeT,
56 typename FinderT,
57 typename FormatterT>
58 inline OutputIteratorT find_format_copy(
59 OutputIteratorT Output,
60 const RangeT& Input,
61 FinderT Finder,
62 FormatterT Formatter )
63 {
64 // Concept check
65 BOOST_CONCEPT_ASSERT((
66 FinderConcept<
67 FinderT,
68 BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
69 ));
70 BOOST_CONCEPT_ASSERT((
71 FormatterConcept<
72 FormatterT,
73 FinderT,BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
74 ));
75
76 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
77
78 return detail::find_format_copy_impl(
79 Output,
80 lit_input,
81 Formatter,
82 Finder( ::boost::begin(lit_input), ::boost::end(lit_input) ) );
83 }
84
85 //! Generic replace algorithm
86 /*!
87 \overload
88 */
89 template<
90 typename SequenceT,
91 typename FinderT,
92 typename FormatterT>
93 inline SequenceT find_format_copy(
94 const SequenceT& Input,
95 FinderT Finder,
96 FormatterT Formatter )
97 {
98 // Concept check
99 BOOST_CONCEPT_ASSERT((
100 FinderConcept<
101 FinderT,
102 BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
103 ));
104 BOOST_CONCEPT_ASSERT((
105 FormatterConcept<
106 FormatterT,
107 FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
108 ));
109
110 return detail::find_format_copy_impl(
111 Input,
112 Formatter,
113 Finder(::boost::begin(Input), ::boost::end(Input)));
114 }
115
116 //! Generic replace algorithm
117 /*!
118 Use the Finder to search for a substring. Use the Formatter to format
119 this substring and replace it in the input. The input is modified in-place.
120
121 \param Input An input sequence
122 \param Finder A Finder object used to search for a match to be replaced
123 \param Formatter A Formatter object used to format a match
124 */
125 template<
126 typename SequenceT,
127 typename FinderT,
128 typename FormatterT>
129 inline void find_format(
130 SequenceT& Input,
131 FinderT Finder,
132 FormatterT Formatter)
133 {
134 // Concept check
135 BOOST_CONCEPT_ASSERT((
136 FinderConcept<
137 FinderT,
138 BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
139 ));
140 BOOST_CONCEPT_ASSERT((
141 FormatterConcept<
142 FormatterT,
143 FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
144 ));
145
146 detail::find_format_impl(
147 Input,
148 Formatter,
149 Finder(::boost::begin(Input), ::boost::end(Input)));
150 }
151
152
153// find_format_all generic ----------------------------------------------------------------//
154
155 //! Generic replace all algorithm
156 /*!
157 Use the Finder to search for a substring. Use the Formatter to format
158 this substring and replace it in the input. Repeat this for all matching
159 substrings.
160 The result is a modified copy of the input. It is returned as a sequence
161 or copied to the output iterator.
162
163 \param Output An output iterator to which the result will be copied
164 \param Input An input sequence
165 \param Finder A Finder object used to search for a match to be replaced
166 \param Formatter A Formatter object used to format a match
167 \return An output iterator pointing just after the last inserted character or
168 a modified copy of the input
169
170 \note The second variant of this function provides the strong exception-safety guarantee
171 */
172 template<
173 typename OutputIteratorT,
174 typename RangeT,
175 typename FinderT,
176 typename FormatterT>
177 inline OutputIteratorT find_format_all_copy(
178 OutputIteratorT Output,
179 const RangeT& Input,
180 FinderT Finder,
181 FormatterT Formatter)
182 {
183 // Concept check
184 BOOST_CONCEPT_ASSERT((
185 FinderConcept<
186 FinderT,
187 BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
188 ));
189 BOOST_CONCEPT_ASSERT((
190 FormatterConcept<
191 FormatterT,
192 FinderT,BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
193 ));
194
195 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::boost::as_literal(Input));
196
197 return detail::find_format_all_copy_impl(
198 Output,
199 lit_input,
200 Finder,
201 Formatter,
202 Finder(::boost::begin(lit_input), ::boost::end(lit_input)));
203 }
204
205 //! Generic replace all algorithm
206 /*!
207 \overload
208 */
209 template<
210 typename SequenceT,
211 typename FinderT,
212 typename FormatterT >
213 inline SequenceT find_format_all_copy(
214 const SequenceT& Input,
215 FinderT Finder,
216 FormatterT Formatter )
217 {
218 // Concept check
219 BOOST_CONCEPT_ASSERT((
220 FinderConcept<
221 FinderT,
222 BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
223 ));
224 BOOST_CONCEPT_ASSERT((
225 FormatterConcept<
226 FormatterT,
227 FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
228 ));
229
230 return detail::find_format_all_copy_impl(
231 Input,
232 Finder,
233 Formatter,
234 Finder( ::boost::begin(Input), ::boost::end(Input) ) );
235 }
236
237 //! Generic replace all algorithm
238 /*!
239 Use the Finder to search for a substring. Use the Formatter to format
240 this substring and replace it in the input. Repeat this for all matching
241 substrings.The input is modified in-place.
242
243 \param Input An input sequence
244 \param Finder A Finder object used to search for a match to be replaced
245 \param Formatter A Formatter object used to format a match
246 */
247 template<
248 typename SequenceT,
249 typename FinderT,
250 typename FormatterT >
251 inline void find_format_all(
252 SequenceT& Input,
253 FinderT Finder,
254 FormatterT Formatter )
255 {
256 // Concept check
257 BOOST_CONCEPT_ASSERT((
258 FinderConcept<
259 FinderT,
260 BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
261 ));
262 BOOST_CONCEPT_ASSERT((
263 FormatterConcept<
264 FormatterT,
265 FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
266 ));
267
268 detail::find_format_all_impl(
269 Input,
270 Finder,
271 Formatter,
272 Finder(::boost::begin(Input), ::boost::end(Input)));
273
274 }
275
276 } // namespace algorithm
277
278 // pull the names to the boost namespace
279 using algorithm::find_format_copy;
280 using algorithm::find_format;
281 using algorithm::find_format_all_copy;
282 using algorithm::find_format_all;
283
284} // namespace boost
285
286
287#endif // BOOST_STRING_FIND_FORMAT_HPP
288