Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 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 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 11 | #ifndef NDNBOOST_STRING_FIND_FORMAT_HPP |
| 12 | #define NDNBOOST_STRING_FIND_FORMAT_HPP |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 13 | |
| 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 | |
| 32 | namespace 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 65 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 66 | FinderConcept< |
| 67 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 68 | NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 69 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 70 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 71 | FormatterConcept< |
| 72 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 73 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 74 | )); |
| 75 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 76 | iterator_range<NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::ndnboost::as_literal(Input)); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 77 | |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 99 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 100 | FinderConcept< |
| 101 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 102 | NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 103 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 104 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 105 | FormatterConcept< |
| 106 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 107 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 108 | )); |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 135 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 136 | FinderConcept< |
| 137 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 138 | NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 139 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 140 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 141 | FormatterConcept< |
| 142 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 143 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 144 | )); |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 184 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 185 | FinderConcept< |
| 186 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 187 | NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 188 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 189 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 190 | FormatterConcept< |
| 191 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 192 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 193 | )); |
| 194 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 195 | iterator_range<NDNBOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(::ndnboost::as_literal(Input)); |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 196 | |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 219 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 220 | FinderConcept< |
| 221 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 222 | NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 223 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 224 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 225 | FormatterConcept< |
| 226 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 227 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 228 | )); |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 257 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 258 | FinderConcept< |
| 259 | FinderT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 260 | NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 261 | )); |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 262 | NDNBOOST_CONCEPT_ASSERT(( |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 263 | FormatterConcept< |
| 264 | FormatterT, |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 265 | FinderT,NDNBOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> |
Jeff Thompson | ef2d5a4 | 2013-08-22 19:09:24 -0700 | [diff] [blame] | 266 | )); |
| 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 Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 287 | #endif // NDNBOOST_STRING_FIND_FORMAT_HPP |