1 // Boost string_algo library find_format_all.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003.
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)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP
12 #define BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP
14 #include <boost/algorithm/string/config.hpp>
15 #include <boost/range/iterator_range_core.hpp>
16 #include <boost/range/const_iterator.hpp>
17 #include <boost/range/value_type.hpp>
18 #include <boost/algorithm/string/detail/find_format_store.hpp>
19 #include <boost/algorithm/string/detail/replace_storage.hpp>
25 // find_format_all_copy (iterator variant) implementation ---------------------------//
28 typename OutputIteratorT,
33 typename FormatResultT >
34 inline OutputIteratorT find_format_all_copy_impl2(
35 OutputIteratorT Output,
39 const FindResultT& FindResult,
40 const FormatResultT& FormatResult )
42 typedef BOOST_STRING_TYPENAME
43 range_const_iterator<InputT>::type input_iterator_type;
45 typedef find_format_store<
48 FormatResultT > store_type;
50 // Create store for the find result
51 store_type M( FindResult, FormatResult, Formatter );
53 // Initialize last match
54 input_iterator_type LastMatch=::boost::begin(Input);
56 // Iterate through all matches
59 // Copy the beginning of the sequence
60 Output = std::copy( LastMatch, M.begin(), Output );
61 // Copy formatted result
62 Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
64 // Proceed to the next match
66 M=Finder( LastMatch, ::boost::end(Input) );
69 // Copy the rest of the sequence
70 Output = std::copy( LastMatch, ::boost::end(Input), Output );
76 typename OutputIteratorT,
80 typename FindResultT >
81 inline OutputIteratorT find_format_all_copy_impl(
82 OutputIteratorT Output,
86 const FindResultT& FindResult )
88 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
89 return ::boost::algorithm::detail::find_format_all_copy_impl2(
95 Formatter(FindResult) );
97 return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
101 // find_format_all_copy implementation ----------------------------------------------//
107 typename FindResultT,
108 typename FormatResultT >
109 inline InputT find_format_all_copy_impl2(
112 FormatterT Formatter,
113 const FindResultT& FindResult,
114 const FormatResultT& FormatResult)
116 typedef BOOST_STRING_TYPENAME
117 range_const_iterator<InputT>::type input_iterator_type;
119 typedef find_format_store<
122 FormatResultT > store_type;
124 // Create store for the find result
125 store_type M( FindResult, FormatResult, Formatter );
127 // Initialize last match
128 input_iterator_type LastMatch=::boost::begin(Input);
133 // Iterate through all matches
136 // Copy the beginning of the sequence
137 boost::algorithm::detail::insert( Output, ::boost::end(Output), LastMatch, M.begin() );
138 // Copy formatted result
139 boost::algorithm::detail::insert( Output, ::boost::end(Output), M.format_result() );
141 // Proceed to the next match
143 M=Finder( LastMatch, ::boost::end(Input) );
146 // Copy the rest of the sequence
147 ::boost::algorithm::detail::insert( Output, ::boost::end(Output), LastMatch, ::boost::end(Input) );
156 typename FindResultT >
157 inline InputT find_format_all_copy_impl(
160 FormatterT Formatter,
161 const FindResultT& FindResult)
163 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
164 return ::boost::algorithm::detail::find_format_all_copy_impl2(
169 Formatter(FindResult) );
175 // find_format_all implementation ------------------------------------------------//
181 typename FindResultT,
182 typename FormatResultT >
183 inline void find_format_all_impl2(
186 FormatterT Formatter,
187 FindResultT FindResult,
188 FormatResultT FormatResult)
190 typedef BOOST_STRING_TYPENAME
191 range_iterator<InputT>::type input_iterator_type;
192 typedef find_format_store<
195 FormatResultT > store_type;
197 // Create store for the find result
198 store_type M( FindResult, FormatResult, Formatter );
200 // Instantiate replacement storage
202 BOOST_STRING_TYPENAME range_value<InputT>::type> Storage;
204 // Initialize replacement iterators
205 input_iterator_type InsertIt=::boost::begin(Input);
206 input_iterator_type SearchIt=::boost::begin(Input);
210 // process the segment
211 InsertIt=process_segment(
218 // Adjust search iterator
221 // Copy formatted replace to the storage
222 ::boost::algorithm::detail::copy_to_storage( Storage, M.format_result() );
224 // Find range for a next match
225 M=Finder( SearchIt, ::boost::end(Input) );
228 // process the last segment
229 InsertIt=::boost::algorithm::detail::process_segment(
234 ::boost::end(Input) );
236 if ( Storage.empty() )
239 ::boost::algorithm::detail::erase( Input, InsertIt, ::boost::end(Input) );
243 // Copy remaining data to the end of input
244 ::boost::algorithm::detail::insert( Input, ::boost::end(Input), Storage.begin(), Storage.end() );
252 typename FindResultT >
253 inline void find_format_all_impl(
256 FormatterT Formatter,
257 FindResultT FindResult)
259 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
260 ::boost::algorithm::detail::find_format_all_impl2(
265 Formatter(FindResult) );
269 } // namespace detail
270 } // namespace algorithm
273 #endif // BOOST_STRING_FIND_FORMAT_ALL_DETAIL_HPP