regex_replace.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_V5_REGEX_REPLACE_HPP
  20. #define BOOST_REGEX_V5_REGEX_REPLACE_HPP
  21. #include <boost/regex/v5/basic_regex.hpp>
  22. #include <boost/regex/v5/match_flags.hpp>
  23. #include <boost/regex/v5/regex_iterator.hpp>
  24. namespace boost{
  25. BOOST_REGEX_MODULE_EXPORT template <class OutputIterator, class BidirectionalIterator, class traits, class charT, class Formatter>
  26. OutputIterator regex_replace(OutputIterator out,
  27. BidirectionalIterator first,
  28. BidirectionalIterator last,
  29. const basic_regex<charT, traits>& e,
  30. Formatter fmt,
  31. match_flag_type flags = match_default)
  32. {
  33. regex_iterator<BidirectionalIterator, charT, traits> i(first, last, e, flags);
  34. regex_iterator<BidirectionalIterator, charT, traits> j;
  35. if(i == j)
  36. {
  37. if(!(flags & regex_constants::format_no_copy))
  38. out = BOOST_REGEX_DETAIL_NS::copy(first, last, out);
  39. }
  40. else
  41. {
  42. BidirectionalIterator last_m(first);
  43. while(i != j)
  44. {
  45. if(!(flags & regex_constants::format_no_copy))
  46. out = BOOST_REGEX_DETAIL_NS::copy(i->prefix().first, i->prefix().second, out);
  47. out = i->format(out, fmt, flags, e);
  48. last_m = (*i)[0].second;
  49. if(flags & regex_constants::format_first_only)
  50. break;
  51. ++i;
  52. }
  53. if(!(flags & regex_constants::format_no_copy))
  54. out = BOOST_REGEX_DETAIL_NS::copy(last_m, last, out);
  55. }
  56. return out;
  57. }
  58. BOOST_REGEX_MODULE_EXPORT template <class traits, class charT, class Formatter>
  59. std::basic_string<charT> regex_replace(const std::basic_string<charT>& s,
  60. const basic_regex<charT, traits>& e,
  61. Formatter fmt,
  62. match_flag_type flags = match_default)
  63. {
  64. std::basic_string<charT> result;
  65. BOOST_REGEX_DETAIL_NS::string_out_iterator<std::basic_string<charT> > i(result);
  66. regex_replace(i, s.begin(), s.end(), e, fmt, flags);
  67. return result;
  68. }
  69. } // namespace boost
  70. #endif // BOOST_REGEX_V5_REGEX_REPLACE_HPP