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