Kaydet (Commit) 0d2ac93f authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Fix o3tl::string_view streaming operator <<

(The unnecessary os.setstate(std::ios_base::failbit) was due to a misreading of
C++17 [ostream.formatted.reqmts]/1.)

Change-Id: I7d8285230cb316c7af45c76029e9629517d05d56
Reviewed-on: https://gerrit.libreoffice.org/65217
Tested-by: Jenkins
Reviewed-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 9bc346dc
......@@ -732,12 +732,13 @@ operator <<(
std::basic_ostream<charT, traits> & os,
basic_string_view<charT, traits> str)
{
typename std::basic_ostream<charT, traits>::sentry sentry;
typename std::basic_ostream<charT, traits>::sentry sentry(os);
if (sentry) {
auto const w = os.width();
auto const pad
= std::max<std::make_unsigned<decltype(w + str.size())>::type>(
w < 0 ? 0 : w, str.size());
auto pad
= std::max<typename std::make_unsigned<decltype(w + str.size())>::type>(
w < 0 ? 0 : w, str.size())
- str.size();
auto const after = (os.flags() & std::ios_base::adjustfield)
== std::ios_base::left;
if (pad != 0 && !after) {
......@@ -754,8 +755,6 @@ operator <<(
}
}
os.width(0);
} else {
os.setstate(std::ios_base::failbit);
}
return os;
}
......
......@@ -9,7 +9,9 @@
#include <sal/config.h>
#include <sstream>
#include <stdexcept>
#include <string>
#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
......@@ -27,6 +29,7 @@ private:
CPPUNIT_TEST(testChar32Literal);
CPPUNIT_TEST(testWcharLiteral);
CPPUNIT_TEST(testOperations);
CPPUNIT_TEST(testOutput);
CPPUNIT_TEST_SUITE_END();
void testCharLiteral() {
......@@ -203,6 +206,12 @@ private:
v.find_last_not_of("fxo", o3tl::string_view::npos, 2));
CPPUNIT_ASSERT_EQUAL(npos, v.find_last_not_of("fxo"));
}
void testOutput() {
std::ostringstream s;
s << o3tl::string_view("foo");
CPPUNIT_ASSERT_EQUAL(std::string("foo"), s.str());
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(Test);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment