regex_replace.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. *
  3. * Copyright (c) 1998-2002
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_format.hpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Provides formatting output routines for search and replace
  16. * operations. Note this is an internal header file included
  17. * by regex.hpp, do not include on its own.
  18. */
  19. #ifndef BOOST_REGEX_V4_REGEX_REPLACE_HPP
  20. #define BOOST_REGEX_V4_REGEX_REPLACE_HPP
  21. namespace boost{
  22. #ifdef BOOST_MSVC
  23. #pragma warning(push)
  24. #pragma warning(disable: 4103)
  25. #endif
  26. #ifdef BOOST_HAS_ABI_HEADERS
  27. # include BOOST_ABI_PREFIX
  28. #endif
  29. #ifdef BOOST_MSVC
  30. #pragma warning(pop)
  31. #endif
  32. template <class OutputIterator, class BidirectionalIterator, class traits, class charT>
  33. OutputIterator regex_replace(OutputIterator out,
  34. BidirectionalIterator first,
  35. BidirectionalIterator last,
  36. const basic_regex<charT, traits>& e,
  37. const charT* fmt,
  38. match_flag_type flags = match_default)
  39. {
  40. regex_iterator<BidirectionalIterator, charT, traits> i(first, last, e, flags);
  41. regex_iterator<BidirectionalIterator, charT, traits> j;
  42. if(i == j)
  43. {
  44. if(!(flags & regex_constants::format_no_copy))
  45. out = re_detail::copy(first, last, out);
  46. }
  47. else
  48. {
  49. BidirectionalIterator last_m(first);
  50. while(i != j)
  51. {
  52. if(!(flags & regex_constants::format_no_copy))
  53. out = re_detail::copy(i->prefix().first, i->prefix().second, out);
  54. out = i->format(out, fmt, flags, e);
  55. last_m = (*i)[0].second;
  56. if(flags & regex_constants::format_first_only)
  57. break;
  58. ++i;
  59. }
  60. if(!(flags & regex_constants::format_no_copy))
  61. out = re_detail::copy(last_m, last, out);
  62. }
  63. return out;
  64. }
  65. template <class OutputIterator, class Iterator, class traits, class charT>
  66. inline OutputIterator regex_replace(OutputIterator out,
  67. Iterator first,
  68. Iterator last,
  69. const basic_regex<charT, traits>& e,
  70. const std::basic_string<charT>& fmt,
  71. match_flag_type flags = match_default)
  72. {
  73. return regex_replace(out, first, last, e, fmt.c_str(), flags);
  74. }
  75. template <class traits, class charT>
  76. std::basic_string<charT> regex_replace(const std::basic_string<charT>& s,
  77. const basic_regex<charT, traits>& e,
  78. const charT* fmt,
  79. match_flag_type flags = match_default)
  80. {
  81. std::basic_string<charT> result;
  82. re_detail::string_out_iterator<std::basic_string<charT> > i(result);
  83. regex_replace(i, s.begin(), s.end(), e, fmt, flags);
  84. return result;
  85. }
  86. template <class traits, class charT>
  87. std::basic_string<charT> regex_replace(const std::basic_string<charT>& s,
  88. const basic_regex<charT, traits>& e,
  89. const std::basic_string<charT>& fmt,
  90. match_flag_type flags = match_default)
  91. {
  92. std::basic_string<charT> result;
  93. re_detail::string_out_iterator<std::basic_string<charT> > i(result);
  94. regex_replace(i, s.begin(), s.end(), e, fmt.c_str(), flags);
  95. return result;
  96. }
  97. #ifdef BOOST_MSVC
  98. #pragma warning(push)
  99. #pragma warning(disable: 4103)
  100. #endif
  101. #ifdef BOOST_HAS_ABI_HEADERS
  102. # include BOOST_ABI_SUFFIX
  103. #endif
  104. #ifdef BOOST_MSVC
  105. #pragma warning(pop)
  106. #endif
  107. } // namespace boost
  108. #endif // BOOST_REGEX_V4_REGEX_REPLACE_HPP