]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/xpressive/test/test_format.cpp
1 ///////////////////////////////////////////////////////////////////////////////
4 // Copyright 2008 Eric Niebler. Distributed under the Boost
5 // Software License, Version 1.0. (See accompanying file
6 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // Test all the different ways to call regex_replace with a formatter.
12 #include <boost/test/unit_test.hpp>
13 #include <boost/xpressive/xpressive.hpp>
15 using namespace boost::xpressive
;
17 std::map
<std::string
, std::string
> replacements
;
21 template<typename BidiIter
>
22 std::string
operator()(match_results
<BidiIter
> const &what
) const
24 return replacements
[what
[1].str()];
30 template<typename BidiIter
, typename OutIter
>
31 OutIter
operator()(match_results
<BidiIter
> const &what
, OutIter out
) const
33 std::map
<std::string
, std::string
>::const_iterator where
= replacements
.find(what
[1].str());
34 if(where
!= replacements
.end())
35 out
= std::copy((*where
).second
.begin(), (*where
).second
.end(), out
);
42 template<typename BidiIter
, typename OutIter
>
43 OutIter
operator()(match_results
<BidiIter
> const &what
, OutIter out
, regex_constants::match_flag_type
) const
45 std::map
<std::string
, std::string
>::const_iterator where
= replacements
.find(what
[1].str());
46 if(where
!= replacements
.end())
47 out
= std::copy((*where
).second
.begin(), (*where
).second
.end(), out
);
52 std::string
format_fun(smatch
const &what
)
54 return replacements
[what
[1].str()];
57 std::string
c_format_fun(cmatch
const &what
)
59 return replacements
[what
[1].str()];
64 replacements
["X"] = "this";
65 replacements
["Y"] = "that";
67 std::string
input("\"$(X)\" has the value \"$(Y)\""), output
;
68 sregex rx
= sregex::compile("\\$\\(([^\\)]+)\\)");
69 cregex crx
= cregex::compile("\\$\\(([^\\)]+)\\)");
71 std::string
result("\"this\" has the value \"that\"");
73 output
= regex_replace(input
, rx
, format1());
74 BOOST_CHECK_EQUAL(output
, result
);
76 output
= regex_replace(input
.c_str(), crx
, format1());
77 BOOST_CHECK_EQUAL(output
, result
);
79 output
= regex_replace(input
, rx
, format2());
80 BOOST_CHECK_EQUAL(output
, result
);
82 output
= regex_replace(input
.c_str(), crx
, format2());
83 BOOST_CHECK_EQUAL(output
, result
);
85 output
= regex_replace(input
, rx
, format3());
86 BOOST_CHECK_EQUAL(output
, result
);
88 output
= regex_replace(input
.c_str(), crx
, format3());
89 BOOST_CHECK_EQUAL(output
, result
);
91 output
= regex_replace(input
, rx
, format_fun
);
92 BOOST_CHECK_EQUAL(output
, result
);
94 output
= regex_replace(input
.c_str(), crx
, c_format_fun
);
95 BOOST_CHECK_EQUAL(output
, result
);
97 output
= regex_replace(input
, rx
, &format_fun
);
98 BOOST_CHECK_EQUAL(output
, result
);
100 output
= regex_replace(input
.c_str(), crx
, &c_format_fun
);
101 BOOST_CHECK_EQUAL(output
, result
);
104 regex_replace(std::back_inserter(output
), input
.begin(), input
.end(), rx
, format1());
105 BOOST_CHECK_EQUAL(output
, result
);
108 regex_replace(std::back_inserter(output
), input
.begin(), input
.end(), rx
, format2());
109 BOOST_CHECK_EQUAL(output
, result
);
112 regex_replace(std::back_inserter(output
), input
.begin(), input
.end(), rx
, format2());
113 BOOST_CHECK_EQUAL(output
, result
);
116 regex_replace(std::back_inserter(output
), input
.begin(), input
.end(), rx
, format_fun
);
117 BOOST_CHECK_EQUAL(output
, result
);
120 regex_replace(std::back_inserter(output
), input
.begin(), input
.end(), rx
, &format_fun
);
121 BOOST_CHECK_EQUAL(output
, result
);
124 using namespace boost::unit_test
;
125 ///////////////////////////////////////////////////////////////////////////////
126 // init_unit_test_suite
128 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
130 test_suite
*test
= BOOST_TEST_SUITE("test_format");
131 test
->add(BOOST_TEST_CASE(&test_main
));