regex_replace.hpp 2.5 KB

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