From ca1ad064ff7fbbbc58f6ae4393fa10ef76bd914d Mon Sep 17 00:00:00 2001 From: Zach Laine Date: Mon, 19 Feb 2024 21:50:28 -0600 Subject: [PATCH] Use a more compelling example in the example code for replace(). Fixes #96. --- doc/tutorial.qbk | 15 ++++++++------- test/replace.cpp | 26 ++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/doc/tutorial.qbk b/doc/tutorial.qbk index b398275d..4b3952ed 100644 --- a/doc/tutorial.qbk +++ b/doc/tutorial.qbk @@ -3145,15 +3145,16 @@ Unlike _split_v_, _replace_v_ does not produce empty subranges, unless `replacement` is empty. namespace bp = boost::parser; - auto rng = "XYZaaXYZbaabaXYZXYZ" | bp::replace(bp::lit("XYZ"), "foo"); + auto card_number = bp::int_ >> bp::repeat(3)['-' >> bp::int_]; + auto rng = "My credit card number is 1234-5678-9012-3456." | bp::replace(card_number, "XXXX-XXXX-XXXX-XXXX"); int count = 0; - // Prints foo aa foo baaba foo foo. + // Prints My credit card number is XXXX-XXXX-XXXX-XXXX. for (auto subrange : rng) { - std::cout << std::string_view(subrange.begin(), subrange.end() - subrange.begin()) << " "; + std::cout << std::string_view(subrange.begin(), subrange.end() - subrange.begin()); ++count; } std::cout << "\n"; - assert(count == 6); + assert(count == 3); If the iterator types `Ir` and `Ireplacement` for the `r` and `replacement` @@ -3168,13 +3169,13 @@ subranges represented by _replace_v_ is easily joined back into a single range. namespace bp = boost::parser; - char const str[] = "XYZaaXYZbaabaXYZXYZ"; - auto rng = str | bp::replace(bp::lit("XYZ"), "foo") | std::views::join; + auto card_number = bp::int_ >> bp::repeat(3)['-' >> bp::int_]; + auto rng = "My credit card number is 1234-5678-9012-3456." | bp::replace(card_number, "XXXX-XXXX-XXXX-XXXX") | std::views::join; std::string replace_result; for (auto ch : rng) { replace_result.push_back(ch); } - assert(replace_result == "fooaafoobaabafoofoo"); + assert(replace_result == "My credit card number is XXXX-XXXX-XXXX-XXXX."); Note that we could *not* have written `std::string replace_result(r.begin(), r.end())`. This is ill-formed because the `std::string` range constructor diff --git a/test/replace.cpp b/test/replace.cpp index 038c8b4b..0c15506c 100644 --- a/test/replace.cpp +++ b/test/replace.cpp @@ -437,17 +437,35 @@ TEST(replace, join_compat) TEST(replace, doc_examples) { + // clang-format off { - auto rng = "XYZaaXYZbaabaXYZXYZ" | bp::replace(bp::lit("XYZ"), "foo"); + namespace bp = boost::parser; + auto card_number = bp::int_ >> bp::repeat(3)['-' >> bp::int_]; + auto rng = "My credit card number is 1234-5678-9012-3456." | bp::replace(card_number, "XXXX-XXXX-XXXX-XXXX"); int count = 0; - // Prints foo aa foo baaba foo foo. + // Prints My credit card number is XXXX-XXXX-XXXX-XXXX. for (auto subrange : rng) { - std::cout << std::string_view(subrange.begin(), subrange.end() - subrange.begin()) << " "; + std::cout << std::string_view(subrange.begin(), subrange.end() - subrange.begin()); ++count; } std::cout << "\n"; - assert(count == 6); + assert(count == 3); } +#if BOOST_PARSER_USE_CONCEPTS && (!defined(__GNUC__) || 12 <= __GNUC__) + { + namespace bp = boost::parser; + auto card_number = bp::int_ >> bp::repeat(3)['-' >> bp::int_]; + auto rng = "My credit card number is 1234-5678-9012-3456." | + bp::replace(card_number, "XXXX-XXXX-XXXX-XXXX") | + std::views::join; + std::string replace_result; + for (auto ch : rng) { + replace_result.push_back(ch); + } + assert(replace_result == "My credit card number is XXXX-XXXX-XXXX-XXXX."); + } +#endif + // clang-format on } #endif