find_format.hpp 10 KB

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