diff --git a/include/boost/core/string_view.hpp b/include/boost/core/string_view.hpp index 0ee3916..d9704bd 100644 --- a/include/boost/core/string_view.hpp +++ b/include/boost/core/string_view.hpp @@ -928,7 +928,38 @@ public: template std::basic_ostream& operator<<( std::basic_ostream& os, basic_string_view str ) { - os.write( str.data(), str.size() ); + Ch const* p = str.data(); + std::size_t n = str.size(); + + if( n == 0 ) + { + os << ""; + } + else + { + std::size_t m = os.width(); + + if( n >= m ) + { + os.write( p, n ); + } + else if( ( os.flags() & std::ios_base::adjustfield ) == std::ios_base::left ) + { + os.write( p, n - 1 ); + + os.width( m - n + 1 ); + os << p[ n-1 ]; + } + else + { + os.width( m - n + 1 ); + os << p[0]; + + os.write( p + 1, n - 1 ); + } + } + + os.width( 0 ); return os; } diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7286df2..b6c89b5 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -266,6 +266,7 @@ run sv_find_last_not_of_test.cpp ; run sv_contains_test.cpp ; run sv_eq_test.cpp ; run sv_lt_test.cpp ; +run sv_stream_insert_test.cpp ; use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/sv_stream_insert_test.cpp b/test/sv_stream_insert_test.cpp new file mode 100644 index 0000000..cebdf01 --- /dev/null +++ b/test/sv_stream_insert_test.cpp @@ -0,0 +1,111 @@ +// Copyright 2021 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include + +int main() +{ + using boost::core::string_view; + + { + std::ostringstream os; + + os << string_view( "" ); + + BOOST_TEST_EQ( os.str(), std::string( "" ) ); + } + + { + std::ostringstream os; + + os << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( "123" ) ); + } + + { + std::ostringstream os; + + os << std::setw( 5 ) << string_view( "" ); + + BOOST_TEST_EQ( os.str(), std::string( " " ) ); + } + + { + std::ostringstream os; + + os << std::setfill( '-' ) << std::setw( 5 ) << string_view( "" ); + + BOOST_TEST_EQ( os.str(), std::string( "-----" ) ); + } + + { + std::ostringstream os; + + os << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( " 123" ) ); + } + + { + std::ostringstream os; + + os << std::left << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( "123 " ) ); + } + + { + std::ostringstream os; + + os << std::right << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( " 123" ) ); + } + + { + std::ostringstream os; + + os << std::setfill( '-' ) << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( "--123" ) ); + } + + { + std::ostringstream os; + + os << std::setfill( '-' ) << std::left << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( "123--" ) ); + } + + { + std::ostringstream os; + + os << std::setfill( '-' ) << std::right << std::setw( 5 ) << string_view( "123" ); + + BOOST_TEST_EQ( os.str(), std::string( "--123" ) ); + } + + { + std::ostringstream os; + + os << std::setw( 5 ) << string_view( "12345" ); + + BOOST_TEST_EQ( os.str(), std::string( "12345" ) ); + } + + { + std::ostringstream os; + + os << std::setw( 5 ) << string_view( "1234567" ); + + BOOST_TEST_EQ( os.str(), std::string( "1234567" ) ); + } + + return boost::report_errors(); +}