find_format.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.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. function_requires<
  55. FinderConcept<FinderT,
  56. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> >();
  57. function_requires<
  58. FormatterConcept<
  59. FormatterT,
  60. FinderT,BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> >();
  61. iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(as_literal(Input));
  62. return detail::find_format_copy_impl(
  63. Output,
  64. lit_input,
  65. Formatter,
  66. Finder( ::boost::begin(lit_input), ::boost::end(lit_input) ) );
  67. }
  68. //! Generic replace algorithm
  69. /*!
  70. \overload
  71. */
  72. template<
  73. typename SequenceT,
  74. typename FinderT,
  75. typename FormatterT>
  76. inline SequenceT find_format_copy(
  77. const SequenceT& Input,
  78. FinderT Finder,
  79. FormatterT Formatter )
  80. {
  81. // Concept check
  82. function_requires<
  83. FinderConcept<FinderT,
  84. BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  85. function_requires<
  86. FormatterConcept<
  87. FormatterT,
  88. FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  89. return detail::find_format_copy_impl(
  90. Input,
  91. Formatter,
  92. Finder(::boost::begin(Input), ::boost::end(Input)));
  93. }
  94. //! Generic replace algorithm
  95. /*!
  96. Use the Finder to search for a substring. Use the Formatter to format
  97. this substring and replace it in the input. The input is modified in-place.
  98. \param Input An input sequence
  99. \param Finder A Finder object used to search for a match to be replaced
  100. \param Formatter A Formatter object used to format a match
  101. */
  102. template<
  103. typename SequenceT,
  104. typename FinderT,
  105. typename FormatterT>
  106. inline void find_format(
  107. SequenceT& Input,
  108. FinderT Finder,
  109. FormatterT Formatter)
  110. {
  111. // Concept check
  112. function_requires<
  113. FinderConcept<FinderT,
  114. BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  115. function_requires<
  116. FormatterConcept<
  117. FormatterT,
  118. FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  119. detail::find_format_impl(
  120. Input,
  121. Formatter,
  122. Finder(::boost::begin(Input), ::boost::end(Input)));
  123. }
  124. // find_format_all generic ----------------------------------------------------------------//
  125. //! Generic replace all algorithm
  126. /*!
  127. Use the Finder to search for a substring. Use the Formatter to format
  128. this substring and replace it in the input. Repeat this for all matching
  129. substrings.
  130. The result is a modified copy of the input. It is returned as a sequence
  131. or copied to the output iterator.
  132. \param Output An output iterator to which the result will be copied
  133. \param Input An input sequence
  134. \param Finder A Finder object used to search for a match to be replaced
  135. \param Formatter A Formatter object used to format a match
  136. \return An output iterator pointing just after the last inserted character or
  137. a modified copy of the input
  138. \note The second variant of this function provides the strong exception-safety guarantee
  139. */
  140. template<
  141. typename OutputIteratorT,
  142. typename RangeT,
  143. typename FinderT,
  144. typename FormatterT>
  145. inline OutputIteratorT find_format_all_copy(
  146. OutputIteratorT Output,
  147. const RangeT& Input,
  148. FinderT Finder,
  149. FormatterT Formatter)
  150. {
  151. // Concept check
  152. function_requires<
  153. FinderConcept<FinderT,
  154. BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> >();
  155. function_requires<
  156. FormatterConcept<
  157. FormatterT,
  158. FinderT,BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> >();
  159. iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_input(as_literal(Input));
  160. return detail::find_format_all_copy_impl(
  161. Output,
  162. lit_input,
  163. Finder,
  164. Formatter,
  165. Finder(::boost::begin(lit_input), ::boost::end(lit_input)));
  166. }
  167. //! Generic replace all algorithm
  168. /*!
  169. \overload
  170. */
  171. template<
  172. typename SequenceT,
  173. typename FinderT,
  174. typename FormatterT >
  175. inline SequenceT find_format_all_copy(
  176. const SequenceT& Input,
  177. FinderT Finder,
  178. FormatterT Formatter )
  179. {
  180. // Concept check
  181. function_requires<
  182. FinderConcept<FinderT,
  183. BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  184. function_requires<
  185. FormatterConcept<
  186. FormatterT,
  187. FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  188. return detail::find_format_all_copy_impl(
  189. Input,
  190. Finder,
  191. Formatter,
  192. Finder( ::boost::begin(Input), ::boost::end(Input) ) );
  193. }
  194. //! Generic replace all algorithm
  195. /*!
  196. Use the Finder to search for a substring. Use the Formatter to format
  197. this substring and replace it in the input. Repeat this for all matching
  198. substrings.The input is modified in-place.
  199. \param Input An input sequence
  200. \param Finder A Finder object used to search for a match to be replaced
  201. \param Formatter A Formatter object used to format a match
  202. */
  203. template<
  204. typename SequenceT,
  205. typename FinderT,
  206. typename FormatterT >
  207. inline void find_format_all(
  208. SequenceT& Input,
  209. FinderT Finder,
  210. FormatterT Formatter )
  211. {
  212. // Concept check
  213. function_requires<
  214. FinderConcept<FinderT,
  215. BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  216. function_requires<
  217. FormatterConcept<
  218. FormatterT,
  219. FinderT,BOOST_STRING_TYPENAME range_const_iterator<SequenceT>::type> >();
  220. detail::find_format_all_impl(
  221. Input,
  222. Finder,
  223. Formatter,
  224. Finder(::boost::begin(Input), ::boost::end(Input)));
  225. }
  226. } // namespace algorithm
  227. // pull the names to the boost namespace
  228. using algorithm::find_format_copy;
  229. using algorithm::find_format;
  230. using algorithm::find_format_all_copy;
  231. using algorithm::find_format_all;
  232. } // namespace boost
  233. #endif // BOOST_STRING_FIND_FORMAT_HPP