find_format.hpp 10 KB

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