diff --git a/src/value_semantic.cpp b/src/value_semantic.cpp index c4feb688e..80c1c5130 100644 --- a/src/value_semantic.cpp +++ b/src/value_semantic.cpp @@ -272,13 +272,11 @@ namespace boost { namespace program_options { void error_with_option_name::replace_token(const string& from, const string& to) const { - for (;;) + std::size_t pos = 0; + while ((pos = m_message.find(from, pos)) != std::string::npos) { - std::size_t pos = m_message.find(from.c_str(), 0, from.length()); - // not found: all replaced - if (pos == std::string::npos) - return; m_message.replace(pos, from.length(), to); + pos += to.length(); } } diff --git a/test/exception_test.cpp b/test/exception_test.cpp index 438f68db6..ba9eedc97 100644 --- a/test/exception_test.cpp +++ b/test/exception_test.cpp @@ -117,6 +117,12 @@ void test_unknown_option() } } +void test_unknown_option_placeholder() +{ + unknown_option err("-%canonical_option%"); + BOOST_CHECK_EQUAL(string(err.what()), "unrecognised option '-%canonical_option%'"); +} + void test_multiple_values() @@ -253,6 +259,7 @@ int main(int /*ac*/, char** /*av*/) test_ambiguous_long(); test_ambiguous_multiple_long_names(); test_unknown_option(); + test_unknown_option_placeholder(); test_multiple_values(); test_multiple_occurrences(); test_multiple_occurrences_with_different_names(); @@ -261,4 +268,3 @@ int main(int /*ac*/, char** /*av*/) return 0; } -