test2.pass.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <regex>
  9. // template <class OutputIterator, class BidirectionalIterator,
  10. // class traits, class charT, class ST, class SA>
  11. // OutputIterator
  12. // regex_replace(OutputIterator out,
  13. // BidirectionalIterator first, BidirectionalIterator last,
  14. // const basic_regex<charT, traits>& e,
  15. // const charT* fmt,
  16. // regex_constants::match_flag_type flags =
  17. // regex_constants::match_default);
  18. #include <regex>
  19. #include <cassert>
  20. #include "test_macros.h"
  21. #include "test_iterators.h"
  22. int main(int, char**)
  23. {
  24. {
  25. std::regex phone_numbers("\\d{3}-\\d{4}");
  26. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  27. typedef output_iterator<char*> Out;
  28. typedef bidirectional_iterator<const char*> Bi;
  29. char buf[100] = {0};
  30. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  31. Bi(std::end(phone_book)-1), phone_numbers,
  32. "123-$&");
  33. assert(r.base() == buf+40);
  34. assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
  35. }
  36. {
  37. std::regex phone_numbers("\\d{3}-\\d{4}");
  38. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  39. typedef output_iterator<char*> Out;
  40. typedef bidirectional_iterator<const char*> Bi;
  41. char buf[100] = {0};
  42. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  43. Bi(std::end(phone_book)-1), phone_numbers,
  44. "123-$&",
  45. std::regex_constants::format_sed);
  46. assert(r.base() == buf+43);
  47. assert(buf == std::string("123-$555-1234, 123-$555-2345, 123-$555-3456"));
  48. }
  49. {
  50. std::regex phone_numbers("\\d{3}-\\d{4}");
  51. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  52. typedef output_iterator<char*> Out;
  53. typedef bidirectional_iterator<const char*> Bi;
  54. char buf[100] = {0};
  55. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  56. Bi(std::end(phone_book)-1), phone_numbers,
  57. "123-&",
  58. std::regex_constants::format_sed);
  59. assert(r.base() == buf+40);
  60. assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
  61. }
  62. {
  63. std::regex phone_numbers("\\d{3}-\\d{4}");
  64. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  65. typedef output_iterator<char*> Out;
  66. typedef bidirectional_iterator<const char*> Bi;
  67. char buf[100] = {0};
  68. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  69. Bi(std::end(phone_book)-1), phone_numbers,
  70. "123-$&",
  71. std::regex_constants::format_no_copy);
  72. assert(r.base() == buf+36);
  73. assert(buf == std::string("123-555-1234123-555-2345123-555-3456"));
  74. }
  75. {
  76. std::regex phone_numbers("\\d{3}-\\d{4}");
  77. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  78. typedef output_iterator<char*> Out;
  79. typedef bidirectional_iterator<const char*> Bi;
  80. char buf[100] = {0};
  81. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  82. Bi(std::end(phone_book)-1), phone_numbers,
  83. "123-$&",
  84. std::regex_constants::format_first_only);
  85. assert(r.base() == buf+32);
  86. assert(buf == std::string("123-555-1234, 555-2345, 555-3456"));
  87. }
  88. {
  89. std::regex phone_numbers("\\d{3}-\\d{4}");
  90. const char phone_book[] = "555-1234, 555-2345, 555-3456";
  91. typedef output_iterator<char*> Out;
  92. typedef bidirectional_iterator<const char*> Bi;
  93. char buf[100] = {0};
  94. Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
  95. Bi(std::end(phone_book)-1), phone_numbers,
  96. "123-$&",
  97. std::regex_constants::format_first_only |
  98. std::regex_constants::format_no_copy);
  99. assert(r.base() == buf+12);
  100. assert(buf == std::string("123-555-1234"));
  101. }
  102. return 0;
  103. }