Fix bug quoted_manip.hpp and in its tests. Thanks to Daniel James for a patch.

[SVN r64856]
This commit is contained in:
Beman Dawes
2010-08-16 21:53:33 +00:00
parent 1962b0b090
commit 961be4e927
2 changed files with 93 additions and 52 deletions

View File

@ -125,15 +125,15 @@ namespace boost
std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& is, std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& is,
const quoted_proxy<std::basic_string<Char, Traits, Alloc>&, Char>& proxy) const quoted_proxy<std::basic_string<Char, Traits, Alloc>&, Char>& proxy)
{ {
proxy.string.clear();
Char c; Char c;
is >> c; is >> c;
if (c != proxy.delim) if (c != proxy.delim)
{ {
proxy.string = c; is.unget();
is >> proxy.string; is >> proxy.string;
return is; return is;
} }
proxy.string.clear();
{ {
boost::io::ios_flags_saver ifs(is); boost::io::ios_flags_saver ifs(is);
is >> std::noskipws; is >> std::noskipws;

View File

@ -21,72 +21,113 @@ using std::wstring;
int main() int main()
{ {
std::stringstream ss;
std::wstringstream wss; std::wstringstream wss;
const string s1("foo\\bar, \" *");
string r; // test results string r; // test results
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");
}
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");
}
const string s1("foo\\bar, \" *");
{
std::stringstream ss;
ss << quoted(s1); ss << quoted(s1);
ss >> r; ss >> r;
BOOST_TEST_EQ(r, "\"foo\\\\bar, \\\" *\""); BOOST_TEST(r == "\"foo\\\\bar,");
}
ss << quoted(s1.c_str()); {
std::stringstream ss;
ss << quoted("foo\\bar, \" *");
ss >> r; ss >> r;
BOOST_TEST_EQ(r, "\"foo\\\\bar, \\\" *\""); BOOST_TEST(r == "\"foo\\\\bar,");
}
{
std::stringstream ss;
ss << quoted(s1); ss << quoted(s1);
ss >> quoted(r); ss >> quoted(r);
BOOST_TEST_EQ(r, s1); BOOST_TEST(r == s1);
}
{
std::stringstream ss;
ss << quoted(s1.c_str()); ss << quoted(s1.c_str());
ss >> quoted(r); ss >> quoted(r);
BOOST_TEST_EQ(r, s1); BOOST_TEST(r == s1);
}
string s2("'Jack & Jill'"); string s2("'Jack & Jill'");
{
ss << quoted(s2, '&', '\''); std::stringstream ss;
ss >> r;
BOOST_TEST_EQ(r, "'&'Jack && Jill&''");
ss << quoted(s2, '&', '\''); ss << quoted(s2, '&', '\'');
ss >> quoted(r, '&', '\''); ss >> quoted(r, '&', '\'');
BOOST_TEST_EQ(r, s2); BOOST_TEST(r == s2);
}
wstring ws1(L"foo$bar, \" *"); wstring ws1(L"foo$bar, \" *");
wstring wr; // test results wstring wr; // test results
{
wss << quoted(ws1, L'$'); std::wstringstream wss;
wss >> wr;
BOOST_TEST(wr == wstring(L"\"foo$$bar, $\" *\""));
wss << quoted(ws1, L'$'); wss << quoted(ws1, L'$');
wss >> quoted(wr, L'$'); wss >> quoted(wr, L'$');
BOOST_TEST(wr == ws1); BOOST_TEST(wr == ws1);
}
const string s3("const string"); const string s3("const string");
{
std::stringstream ss;
ss << quoted(s3); ss << quoted(s3);
ss >> quoted(r); ss >> quoted(r);
BOOST_TEST_EQ(r, s3); BOOST_TEST(r == s3);
}
{
// missing end delimiter test // missing end delimiter test
std::stringstream ss;
ss << "\"abc"; // load ss with faulty quoting ss << "\"abc"; // load ss with faulty quoting
ss >> quoted(r); // this loops if istream error/eof not detected ss >> quoted(r); // this loops if istream error/eof not detected
BOOST_TEST_EQ(r, "abc"); BOOST_TEST(r == "abc");
}
{
// no initial delmiter test // no initial delmiter test
std::stringstream ss;
ss << "abc"; ss << "abc";
ss >> quoted(r); ss >> quoted(r);
BOOST_TEST_EQ(r, "abc"); BOOST_TEST(r == "abc");
}
{
// no initial delmiter, space in ss // no initial delmiter, space in ss
std::stringstream ss;
ss << "abc def"; ss << "abc def";
ss >> quoted(r); ss >> quoted(r);
BOOST_TEST_EQ(r, "abc"); BOOST_TEST(r == "abc");
}
// these should fail to compile because the arguments are const: // these should fail to compile because the arguments are const:
// ss >> quoted(s1); // ss >> quoted(s1);
// ss >> quoted("foo"); // ss >> quoted("foo");
return 0; return boost::report_errors();
} }