xref: /llvm-project/libcxx/test/std/re/re.alg/re.alg.replace/test2.pass.cpp (revision 5f26d8636f506b487962cd2a9b0e32940e93fa9b)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <regex>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template <class OutputIterator, class BidirectionalIterator,
125a83710eSEric Fiselier //           class traits, class charT, class ST, class SA>
135a83710eSEric Fiselier //     OutputIterator
145a83710eSEric Fiselier //     regex_replace(OutputIterator out,
155a83710eSEric Fiselier //                   BidirectionalIterator first, BidirectionalIterator last,
165a83710eSEric Fiselier //                   const basic_regex<charT, traits>& e,
175a83710eSEric Fiselier //                   const charT* fmt,
185a83710eSEric Fiselier //                   regex_constants::match_flag_type flags =
195a83710eSEric Fiselier //                                              regex_constants::match_default);
205a83710eSEric Fiselier 
215a83710eSEric Fiselier #include <regex>
225a83710eSEric Fiselier #include <cassert>
235a83710eSEric Fiselier 
24fd5ceb22SMarshall Clow #include "test_macros.h"
255a83710eSEric Fiselier #include "test_iterators.h"
265a83710eSEric Fiselier 
main(int,char **)272df59c50SJF Bastien int main(int, char**)
285a83710eSEric Fiselier {
295a83710eSEric Fiselier     {
305a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
315a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
325e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
335a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
345a83710eSEric Fiselier         char buf[100] = {0};
355a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
365a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
375a83710eSEric Fiselier                                     "123-$&");
38*5f26d863SMark de Wever         assert(base(r) == buf+40);
395a83710eSEric Fiselier         assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
405a83710eSEric Fiselier     }
415a83710eSEric Fiselier     {
425a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
435a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
445e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
455a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
465a83710eSEric Fiselier         char buf[100] = {0};
475a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
485a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
495a83710eSEric Fiselier                                     "123-$&",
505a83710eSEric Fiselier                                     std::regex_constants::format_sed);
51*5f26d863SMark de Wever         assert(base(r) == buf+43);
525a83710eSEric Fiselier         assert(buf == std::string("123-$555-1234, 123-$555-2345, 123-$555-3456"));
535a83710eSEric Fiselier     }
545a83710eSEric Fiselier     {
555a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
565a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
575e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
585a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
595a83710eSEric Fiselier         char buf[100] = {0};
605a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
615a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
625a83710eSEric Fiselier                                     "123-&",
635a83710eSEric Fiselier                                     std::regex_constants::format_sed);
64*5f26d863SMark de Wever         assert(base(r) == buf+40);
655a83710eSEric Fiselier         assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
665a83710eSEric Fiselier     }
675a83710eSEric Fiselier     {
685a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
695a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
705e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
715a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
725a83710eSEric Fiselier         char buf[100] = {0};
735a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
745a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
755a83710eSEric Fiselier                                     "123-$&",
765a83710eSEric Fiselier                                     std::regex_constants::format_no_copy);
77*5f26d863SMark de Wever         assert(base(r) == buf+36);
785a83710eSEric Fiselier         assert(buf == std::string("123-555-1234123-555-2345123-555-3456"));
795a83710eSEric Fiselier     }
805a83710eSEric Fiselier     {
815a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
825a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
835e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
845a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
855a83710eSEric Fiselier         char buf[100] = {0};
865a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
875a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
885a83710eSEric Fiselier                                     "123-$&",
895a83710eSEric Fiselier                                     std::regex_constants::format_first_only);
90*5f26d863SMark de Wever         assert(base(r) == buf+32);
915a83710eSEric Fiselier         assert(buf == std::string("123-555-1234, 555-2345, 555-3456"));
925a83710eSEric Fiselier     }
935a83710eSEric Fiselier     {
945a83710eSEric Fiselier         std::regex phone_numbers("\\d{3}-\\d{4}");
955a83710eSEric Fiselier         const char phone_book[] = "555-1234, 555-2345, 555-3456";
965e97d37bSMark de Wever         typedef cpp17_output_iterator<char*> Out;
975a83710eSEric Fiselier         typedef bidirectional_iterator<const char*> Bi;
985a83710eSEric Fiselier         char buf[100] = {0};
995a83710eSEric Fiselier         Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
1005a83710eSEric Fiselier                                     Bi(std::end(phone_book)-1), phone_numbers,
1015a83710eSEric Fiselier                                     "123-$&",
1025a83710eSEric Fiselier                                     std::regex_constants::format_first_only |
1035a83710eSEric Fiselier                                     std::regex_constants::format_no_copy);
104*5f26d863SMark de Wever         assert(base(r) == buf+12);
1055a83710eSEric Fiselier         assert(buf == std::string("123-555-1234"));
1065a83710eSEric Fiselier     }
1072df59c50SJF Bastien 
1082df59c50SJF Bastien   return 0;
1095a83710eSEric Fiselier }
110