regex_replace.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. *
  3. * Copyright (c) 1998-2009
  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, class Formatter>
  33. OutputIterator regex_replace(OutputIterator out,
  34. BidirectionalIterator first,
  35. BidirectionalIterator last,
  36. const basic_regex<charT, traits>& e,
  37. Formatter 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 = BOOST_REGEX_DETAIL_NS::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 = BOOST_REGEX_DETAIL_NS::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 = BOOST_REGEX_DETAIL_NS::copy(last_m, last, out);
  62. }
  63. return out;
  64. }
  65. template <class traits, class charT, class Formatter>
  66. std::basic_string<charT> regex_replace(const std::basic_string<charT>& s,
  67. const basic_regex<charT, traits>& e,
  68. Formatter fmt,
  69. match_flag_type flags = match_default)
  70. {
  71. std::basic_string<charT> result;
  72. BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<charT> > i(result);
  73. regex_replace(i, s.begin(), s.end(), e, fmt, flags);
  74. return result;
  75. }
  76. #ifdef BOOST_MSVC
  77. #pragma warning(push)
  78. #pragma warning(disable: 4103)
  79. #endif
  80. #ifdef BOOST_HAS_ABI_HEADERS
  81. # include BOOST_ABI_SUFFIX
  82. #endif
  83. #ifdef BOOST_MSVC
  84. #pragma warning(pop)
  85. #endif
  86. } // namespace boost
  87. #endif // BOOST_REGEX_V4_REGEX_REPLACE_HPP