From 961be4e9270b43709fa36c64daa75f6cf8ec47ec Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Mon, 16 Aug 2010 21:53:33 +0000 Subject: [PATCH] Fix bug quoted_manip.hpp and in its tests. Thanks to Daniel James for a patch. [SVN r64856] --- include/boost/io/detail/quoted_manip.hpp | 4 +- test/quoted_manip_test.cpp | 141 +++++++++++++++-------- 2 files changed, 93 insertions(+), 52 deletions(-) diff --git a/include/boost/io/detail/quoted_manip.hpp b/include/boost/io/detail/quoted_manip.hpp index 13cfc35..502f422 100644 --- a/include/boost/io/detail/quoted_manip.hpp +++ b/include/boost/io/detail/quoted_manip.hpp @@ -125,15 +125,15 @@ namespace boost std::basic_istream& operator>>(std::basic_istream& is, const quoted_proxy&, Char>& proxy) { + proxy.string.clear(); Char c; is >> c; if (c != proxy.delim) { - proxy.string = c; + is.unget(); is >> proxy.string; return is; } - proxy.string.clear(); { boost::io::ios_flags_saver ifs(is); is >> std::noskipws; diff --git a/test/quoted_manip_test.cpp b/test/quoted_manip_test.cpp index eb481b7..31c6f05 100644 --- a/test/quoted_manip_test.cpp +++ b/test/quoted_manip_test.cpp @@ -21,72 +21,113 @@ using std::wstring; int main() { - std::stringstream ss; std::wstringstream wss; - const string s1("foo\\bar, \" *"); string r; // test results - ss << quoted(s1); - ss >> r; - BOOST_TEST_EQ(r, "\"foo\\\\bar, \\\" *\""); + const string s0("foo"); + { + std::stringstream ss; + ss << quoted(s0); + ss >> r; + BOOST_TEST(r == "\"foo\""); + } + { + std::stringstream ss; + ss << quoted(s0); + ss >> quoted(r); + BOOST_TEST(r == "foo"); + } - ss << quoted(s1.c_str()); - ss >> r; - BOOST_TEST_EQ(r, "\"foo\\\\bar, \\\" *\""); + const string s0s("foo bar"); + { + std::stringstream ss; + ss << quoted(s0s); + ss >> r; + BOOST_TEST(r == "\"foo"); + } + { + std::stringstream ss; + ss << quoted(s0s); + ss >> quoted(r); + BOOST_TEST(r == "foo bar"); + } - ss << quoted(s1); - ss >> quoted(r); - BOOST_TEST_EQ(r, s1); - - ss << quoted(s1.c_str()); - ss >> quoted(r); - BOOST_TEST_EQ(r, s1); + const string s1("foo\\bar, \" *"); + { + std::stringstream ss; + ss << quoted(s1); + ss >> r; + BOOST_TEST(r == "\"foo\\\\bar,"); + } + { + std::stringstream ss; + ss << quoted("foo\\bar, \" *"); + ss >> r; + BOOST_TEST(r == "\"foo\\\\bar,"); + } + { + std::stringstream ss; + ss << quoted(s1); + ss >> quoted(r); + BOOST_TEST(r == s1); + } + { + std::stringstream ss; + ss << quoted(s1.c_str()); + ss >> quoted(r); + BOOST_TEST(r == s1); + } string s2("'Jack & Jill'"); - - ss << quoted(s2, '&', '\''); - ss >> r; - BOOST_TEST_EQ(r, "'&'Jack && Jill&''"); - - ss << quoted(s2, '&', '\''); - ss >> quoted(r, '&', '\''); - BOOST_TEST_EQ(r, s2); + { + std::stringstream ss; + ss << quoted(s2, '&', '\''); + ss >> quoted(r, '&', '\''); + BOOST_TEST(r == s2); + } wstring ws1(L"foo$bar, \" *"); wstring wr; // test results - - wss << quoted(ws1, L'$'); - wss >> wr; - BOOST_TEST(wr == wstring(L"\"foo$$bar, $\" *\"")); - - wss << quoted(ws1, L'$'); - wss >> quoted(wr, L'$'); - BOOST_TEST(wr == ws1); + { + std::wstringstream wss; + wss << quoted(ws1, L'$'); + wss >> quoted(wr, L'$'); + BOOST_TEST(wr == ws1); + } const string s3("const string"); - ss << quoted(s3); - ss >> quoted(r); - BOOST_TEST_EQ(r, s3); - - // missing end delimiter test - ss << "\"abc"; // load ss with faulty quoting - ss >> quoted(r); // this loops if istream error/eof not detected - BOOST_TEST_EQ(r, "abc"); - - // no initial delmiter test - ss << "abc"; - ss >> quoted(r); - BOOST_TEST_EQ(r, "abc"); - - // no initial delmiter, space in ss - ss << "abc def"; - ss >> quoted(r); - BOOST_TEST_EQ(r, "abc"); + { + std::stringstream ss; + ss << quoted(s3); + ss >> quoted(r); + BOOST_TEST(r == s3); + } + { + // missing end delimiter test + std::stringstream ss; + ss << "\"abc"; // load ss with faulty quoting + ss >> quoted(r); // this loops if istream error/eof not detected + BOOST_TEST(r == "abc"); + } + { + // no initial delmiter test + std::stringstream ss; + ss << "abc"; + ss >> quoted(r); + BOOST_TEST(r == "abc"); + } + { + // no initial delmiter, space in ss + std::stringstream ss; + ss << "abc def"; + ss >> quoted(r); + BOOST_TEST(r == "abc"); + } // these should fail to compile because the arguments are const: // ss >> quoted(s1); // ss >> quoted("foo"); - return 0; + return boost::report_errors(); }