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