180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//===----------------------------------------------------------------------===//
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                     The LLVM Compiler Infrastructure
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// This file is dual licensed under the MIT and the University of Illinois Open
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// Source Licenses. See LICENSE.TXT for details.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//===----------------------------------------------------------------------===//
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// <regex>
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// template <class OutputIterator, class BidirectionalIterator,
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//           class traits, class charT, class ST, class SA>
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//     OutputIterator
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//     regex_replace(OutputIterator out,
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                   BidirectionalIterator first, BidirectionalIterator last,
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                   const basic_regex<charT, traits>& e,
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                   const basic_string<charT, ST, SA>& fmt,
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                   regex_constants::match_flag_type flags =
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//                                              regex_constants::match_default);
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <regex>
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <cassert>
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "test_iterators.h"
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruint main()
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
30363e546ed626b6dbbc42f5db87b3594bc0b5944bDerek Sollenberger        std::regex phone_numbers("\\d{3}-\\d{4}");
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char phone_book[] = "555-1234, 555-2345, 555-3456";
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef output_iterator<char*> Out;
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef bidirectional_iterator<const char*> Bi;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        char buf[100] = {0};
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    Bi(std::end(phone_book)-1), phone_numbers,
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    std::string("123-$&"));
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        assert(r.base() == buf+40);
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        std::regex phone_numbers("\\d{3}-\\d{4}");
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char phone_book[] = "555-1234, 555-2345, 555-3456";
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef output_iterator<char*> Out;
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef bidirectional_iterator<const char*> Bi;
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        char buf[100] = {0};
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    Bi(std::end(phone_book)-1), phone_numbers,
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    std::string("123-$&"),
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                                    std::regex_constants::format_sed);
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        assert(r.base() == buf+43);
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        assert(buf == std::string("123-$555-1234, 123-$555-2345, 123-$555-3456"));
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        std::regex phone_numbers("\\d{3}-\\d{4}");
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        const char phone_book[] = "555-1234, 555-2345, 555-3456";
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef output_iterator<char*> Out;
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        typedef bidirectional_iterator<const char*> Bi;
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        char buf[100] = {0};
60        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
61                                    Bi(std::end(phone_book)-1), phone_numbers,
62                                    std::string("123-&"),
63                                    std::regex_constants::format_sed);
64        assert(r.base() == buf+40);
65        assert(buf == std::string("123-555-1234, 123-555-2345, 123-555-3456"));
66    }
67    {
68        std::regex phone_numbers("\\d{3}-\\d{4}");
69        const char phone_book[] = "555-1234, 555-2345, 555-3456";
70        typedef output_iterator<char*> Out;
71        typedef bidirectional_iterator<const char*> Bi;
72        char buf[100] = {0};
73        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
74                                    Bi(std::end(phone_book)-1), phone_numbers,
75                                    std::string("123-$&"),
76                                    std::regex_constants::format_no_copy);
77        assert(r.base() == buf+36);
78        assert(buf == std::string("123-555-1234123-555-2345123-555-3456"));
79    }
80    {
81        std::regex phone_numbers("\\d{3}-\\d{4}");
82        const char phone_book[] = "555-1234, 555-2345, 555-3456";
83        typedef output_iterator<char*> Out;
84        typedef bidirectional_iterator<const char*> Bi;
85        char buf[100] = {0};
86        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
87                                    Bi(std::end(phone_book)-1), phone_numbers,
88                                    std::string("123-$&"),
89                                    std::regex_constants::format_first_only);
90        assert(r.base() == buf+32);
91        assert(buf == std::string("123-555-1234, 555-2345, 555-3456"));
92    }
93    {
94        std::regex phone_numbers("\\d{3}-\\d{4}");
95        const char phone_book[] = "555-1234, 555-2345, 555-3456";
96        typedef output_iterator<char*> Out;
97        typedef bidirectional_iterator<const char*> Bi;
98        char buf[100] = {0};
99        Out r = std::regex_replace(Out(buf), Bi(std::begin(phone_book)),
100                                    Bi(std::end(phone_book)-1), phone_numbers,
101                                    std::string("123-$&"),
102                                    std::regex_constants::format_first_only |
103                                    std::regex_constants::format_no_copy);
104        assert(r.base() == buf+12);
105        assert(buf == std::string("123-555-1234"));
106    }
107}
108