summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/o3tl/string_view.hxx11
-rw-r--r--o3tl/qa/test-string_view.cxx9
2 files changed, 14 insertions, 6 deletions
diff --git a/include/o3tl/string_view.hxx b/include/o3tl/string_view.hxx
index b605da65ac90..2bc182034342 100644
--- a/include/o3tl/string_view.hxx
+++ b/include/o3tl/string_view.hxx
@@ -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;
}
diff --git a/o3tl/qa/test-string_view.cxx b/o3tl/qa/test-string_view.cxx
index 977cfebc460a..fb6239fca379 100644
--- a/o3tl/qa/test-string_view.cxx
+++ b/o3tl/qa/test-string_view.cxx
@@ -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);