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_DETAIL_HPP
12 #define BOOST_STRING_FIND_FORMAT_DETAIL_HPP
13 
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/iterator.hpp>
18 #include <boost/algorithm/string/detail/find_format_store.hpp>
19 #include <boost/algorithm/string/detail/replace_storage.hpp>
20 
21 namespace boost {
22     namespace algorithm {
23         namespace detail {
24 
25 // find_format_copy (iterator variant) implementation -------------------------------//
26 
27            template<
28                 typename OutputIteratorT,
29                 typename InputT,
30                 typename FormatterT,
31                 typename FindResultT,
32                 typename FormatResultT >
find_format_copy_impl2(OutputIteratorT Output,const InputT & Input,FormatterT Formatter,const FindResultT & FindResult,const FormatResultT & FormatResult)33             inline OutputIteratorT find_format_copy_impl2(
34                 OutputIteratorT Output,
35                 const InputT& Input,
36                 FormatterT Formatter,
37                 const FindResultT& FindResult,
38                 const FormatResultT& FormatResult )
39             {
40                 typedef find_format_store<
41                     BOOST_STRING_TYPENAME
42                         range_const_iterator<InputT>::type,
43                         FormatterT,
44                         FormatResultT > store_type;
45 
46                 // Create store for the find result
47                 store_type M( FindResult, FormatResult, Formatter );
48 
49                 if ( !M )
50                 {
51                     // Match not found - return original sequence
52                     Output = std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
53                     return Output;
54                 }
55 
56                 // Copy the beginning of the sequence
57                 Output = std::copy( ::boost::begin(Input), ::boost::begin(M), Output );
58                 // Format find result
59                 // Copy formatted result
60                 Output = std::copy( ::boost::begin(M.format_result()), ::boost::end(M.format_result()), Output );
61                 // Copy the rest of the sequence
62                 Output = std::copy( M.end(), ::boost::end(Input), Output );
63 
64                 return Output;
65             }
66 
67             template<
68                 typename OutputIteratorT,
69                 typename InputT,
70                 typename FormatterT,
71                 typename FindResultT >
find_format_copy_impl(OutputIteratorT Output,const InputT & Input,FormatterT Formatter,const FindResultT & FindResult)72             inline OutputIteratorT find_format_copy_impl(
73                 OutputIteratorT Output,
74                 const InputT& Input,
75                 FormatterT Formatter,
76                 const FindResultT& FindResult )
77             {
78                 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
79                     return ::boost::algorithm::detail::find_format_copy_impl2(
80                         Output,
81                         Input,
82                         Formatter,
83                         FindResult,
84                         Formatter(FindResult) );
85                 } else {
86                     return std::copy( ::boost::begin(Input), ::boost::end(Input), Output );
87                 }
88             }
89 
90 
91 // find_format_copy implementation --------------------------------------------------//
92 
93            template<
94                 typename InputT,
95                 typename FormatterT,
96                 typename FindResultT,
97                 typename FormatResultT >
find_format_copy_impl2(const InputT & Input,FormatterT Formatter,const FindResultT & FindResult,const FormatResultT & FormatResult)98             inline InputT find_format_copy_impl2(
99                 const InputT& Input,
100                 FormatterT Formatter,
101                 const FindResultT& FindResult,
102                 const FormatResultT& FormatResult)
103             {
104                 typedef find_format_store<
105                     BOOST_STRING_TYPENAME
106                         range_const_iterator<InputT>::type,
107                         FormatterT,
108                         FormatResultT > store_type;
109 
110                 // Create store for the find result
111                 store_type M( FindResult, FormatResult, Formatter );
112 
113                 if ( !M )
114                 {
115                     // Match not found - return original sequence
116                     return InputT( Input );
117                 }
118 
119                 InputT Output;
120                 // Copy the beginning of the sequence
121                 boost::algorithm::detail::insert( Output, ::boost::end(Output), ::boost::begin(Input), M.begin() );
122                 // Copy formatted result
123                 boost::algorithm::detail::insert( Output, ::boost::end(Output), M.format_result() );
124                 // Copy the rest of the sequence
125                 boost::algorithm::detail::insert( Output, ::boost::end(Output), M.end(), ::boost::end(Input) );
126 
127                 return Output;
128             }
129 
130             template<
131                 typename InputT,
132                 typename FormatterT,
133                 typename FindResultT >
find_format_copy_impl(const InputT & Input,FormatterT Formatter,const FindResultT & FindResult)134             inline InputT find_format_copy_impl(
135                 const InputT& Input,
136                 FormatterT Formatter,
137                 const FindResultT& FindResult)
138             {
139                 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
140                     return ::boost::algorithm::detail::find_format_copy_impl2(
141                         Input,
142                         Formatter,
143                         FindResult,
144                         Formatter(FindResult) );
145                 } else {
146                     return Input;
147                 }
148             }
149 
150  // replace implementation ----------------------------------------------------//
151 
152             template<
153                 typename InputT,
154                 typename FormatterT,
155                 typename FindResultT,
156                 typename FormatResultT >
find_format_impl2(InputT & Input,FormatterT Formatter,const FindResultT & FindResult,const FormatResultT & FormatResult)157             inline void find_format_impl2(
158                 InputT& Input,
159                 FormatterT Formatter,
160                 const FindResultT& FindResult,
161                 const FormatResultT& FormatResult)
162             {
163                 typedef find_format_store<
164                     BOOST_STRING_TYPENAME
165                         range_iterator<InputT>::type,
166                         FormatterT,
167                         FormatResultT > store_type;
168 
169                 // Create store for the find result
170                 store_type M( FindResult, FormatResult, Formatter );
171 
172                 if ( !M )
173                 {
174                     // Search not found - return original sequence
175                     return;
176                 }
177 
178                 // Replace match
179                 ::boost::algorithm::detail::replace( Input, M.begin(), M.end(), M.format_result() );
180             }
181 
182             template<
183                 typename InputT,
184                 typename FormatterT,
185                 typename FindResultT >
find_format_impl(InputT & Input,FormatterT Formatter,const FindResultT & FindResult)186             inline void find_format_impl(
187                 InputT& Input,
188                 FormatterT Formatter,
189                 const FindResultT& FindResult)
190             {
191                 if( ::boost::algorithm::detail::check_find_result(Input, FindResult) ) {
192                     ::boost::algorithm::detail::find_format_impl2(
193                         Input,
194                         Formatter,
195                         FindResult,
196                         Formatter(FindResult) );
197                 }
198             }
199 
200         } // namespace detail
201     } // namespace algorithm
202 } // namespace boost
203 
204 #endif  // BOOST_STRING_FIND_FORMAT_DETAIL_HPP
205