From 7c3790caa0c3d69c1a1b2193786470c84ba02ef7 Mon Sep 17 00:00:00 2001 From: Nicholas Ormrod Date: Tue, 10 Dec 2013 14:14:00 -0800 Subject: [PATCH] FBString iomanip fix. Summary: D1090936 noticed some problems with fbstring iomanip behavior. The root cause is that os.write(ostream, char*, size_t) is an UnformattedOutputFunction, so disregards setw(), setfill(), and left/right alignment. The use of os.write instead of os << str.data() is intentional: D367009 switched from the latter to the former so that strings containing a '\0' are printed properly. There does not seem to be a public function to write with formatting. Where needed in libgcc, the function __ostream_insert is used. Since FBString already uses such 'private' functions, __ostream_insert is an appropriate solution. @override-unit-failures Test Plan: Added test cases to FBStringTest.cpp to cover iomanip. fbconfig -r folly && fbmake opt && fbmake runtests_opt Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D1091474 --- folly/FBString.h | 2 +- folly/test/FBStringTest.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/folly/FBString.h b/folly/FBString.h index 6f5ea131..b75db3d4 100644 --- a/folly/FBString.h +++ b/folly/FBString.h @@ -2309,7 +2309,7 @@ operator<<( std::basic_ostream::value_type, typename basic_fbstring::traits_type>& os, const basic_fbstring& str) { - os.write(str.data(), str.size()); + std::__ostream_insert(os, str.data(), str.size()); return os; } diff --git a/folly/test/FBStringTest.cpp b/folly/test/FBStringTest.cpp index 4f0aa89a..d3b32118 100644 --- a/folly/test/FBStringTest.cpp +++ b/folly/test/FBStringTest.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -1181,6 +1182,31 @@ TEST(FBString, noexcept) { #endif } +TEST(FBString, iomanip) { + stringstream ss; + fbstring fbstr("Hello"); + + ss << setw(6) << fbstr; + EXPECT_EQ(ss.str(), " Hello"); + ss.str(""); + + ss << left << setw(6) << fbstr; + EXPECT_EQ(ss.str(), "Hello "); + ss.str(""); + + ss << right << setw(6) << fbstr; + EXPECT_EQ(ss.str(), " Hello"); + ss.str(""); + + ss << setw(4) << fbstr; + EXPECT_EQ(ss.str(), "Hello"); + ss.str(""); + + ss << setfill('^') << setw(6) << fbstr; + EXPECT_EQ(ss.str(), "^Hello"); + ss.str(""); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1