blob: 7b7dd3e53d80f125a47a4c0fd17618e77608c451 [file] [log] [blame]
Jeff Thompsonef2d5a42013-08-22 19:09:24 -07001// 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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070011#ifndef NDNBOOST_STRING_FIND_FORMAT_HPP
12#define NDNBOOST_STRING_FIND_FORMAT_HPP
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070013
14#include <deque>
15#include <ndnboost/detail/iterator.hpp>
16#include <ndnboost/range/iterator_range.hpp>
17#include <ndnboost/range/begin.hpp>
18#include <ndnboost/range/end.hpp>
19#include <ndnboost/range/const_iterator.hpp>
20#include <ndnboost/range/as_literal.hpp>
21
22#include <ndnboost/algorithm/string/concept.hpp>
23#include <ndnboost/algorithm/string/detail/find_format.hpp>
24#include <ndnboost/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 ndnboost {
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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070065 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070066 FinderConcept<
67 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -070068 NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070069 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -070070 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070071 FormatterConcept<
72 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -070073 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070074 ));
75
Jeff Thompson3d613fd2013-10-15 15:39:04 -070076 iterator_range<NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::ndnboost::as_literal(Input));
Jeff Thompsonef2d5a42013-08-22 19:09:24 -070077
78 return detail::find_format_copy_impl(
79 Output,
80 lit_input,
81 Formatter,
82 Finder( ::ndnboost::begin(lit_input), ::ndnboost::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
Jeff Thompson3d613fd2013-10-15 15:39:04 -070099 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700100 FinderConcept<
101 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700102 NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700103 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700104 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700105 FormatterConcept<
106 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700107 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700108 ));
109
110 return detail::find_format_copy_impl(
111 Input,
112 Formatter,
113 Finder(::ndnboost::begin(Input), ::ndnboost::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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700135 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700136 FinderConcept<
137 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700138 NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700139 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700140 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700141 FormatterConcept<
142 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700143 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700144 ));
145
146 detail::find_format_impl(
147 Input,
148 Formatter,
149 Finder(::ndnboost::begin(Input), ::ndnboost::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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700184 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700185 FinderConcept<
186 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700187 NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700188 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700189 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700190 FormatterConcept<
191 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700192 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700193 ));
194
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700195 iterator_range<NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::ndnboost::as_literal(Input));
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700196
197 return detail::find_format_all_copy_impl(
198 Output,
199 lit_input,
200 Finder,
201 Formatter,
202 Finder(::ndnboost::begin(lit_input), ::ndnboost::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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700219 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700220 FinderConcept<
221 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700222 NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700223 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700224 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700225 FormatterConcept<
226 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700227 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700228 ));
229
230 return detail::find_format_all_copy_impl(
231 Input,
232 Finder,
233 Formatter,
234 Finder( ::ndnboost::begin(Input), ::ndnboost::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
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700257 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700258 FinderConcept<
259 FinderT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700260 NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700261 ));
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700262 NDNBOOST_CONCEPT_ASSERT((
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700263 FormatterConcept<
264 FormatterT,
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700265 FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type>
Jeff Thompsonef2d5a42013-08-22 19:09:24 -0700266 ));
267
268 detail::find_format_all_impl(
269 Input,
270 Finder,
271 Formatter,
272 Finder(::ndnboost::begin(Input), ::ndnboost::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 ndnboost
285
286
Jeff Thompson3d613fd2013-10-15 15:39:04 -0700287#endif // NDNBOOST_STRING_FIND_FORMAT_HPP