diff --git a/libcxx/include/ios b/libcxx/include/ios --- a/libcxx/include/ios +++ b/libcxx/include/ios @@ -678,6 +678,7 @@ private: basic_ostream* __tie_; mutable int_type __fill_; + bool __fill_set; }; template @@ -700,6 +701,7 @@ ios_base::init(__sb); __tie_ = nullptr; __fill_ = traits_type::eof(); + __fill_set = false; } template @@ -771,7 +773,7 @@ _CharT basic_ios<_CharT, _Traits>::fill() const { - if (traits_type::eq_int_type(traits_type::eof(), __fill_)) + if (!__fill_set && traits_type::eq_int_type(traits_type::eof(), __fill_)) __fill_ = widen(' '); return __fill_; } @@ -781,8 +783,11 @@ _CharT basic_ios<_CharT, _Traits>::fill(char_type __ch) { + if (!__fill_set && traits_type::eq_int_type(traits_type::eof(), __fill_)) + __fill_ = widen(' '); char_type __r = __fill_; __fill_ = __ch; + __fill_set = true; return __r; } @@ -796,6 +801,7 @@ ios_base::copyfmt(__rhs); __tie_ = __rhs.__tie_; __fill_ = __rhs.__fill_; + __fill_set = __rhs.__fill_set; __call_callbacks(copyfmt_event); exceptions(__rhs.exceptions()); } @@ -811,6 +817,7 @@ __tie_ = __rhs.__tie_; __rhs.__tie_ = nullptr; __fill_ = __rhs.__fill_; + __fill_set = __rhs.__fill_set; } template @@ -821,6 +828,7 @@ ios_base::swap(__rhs); _VSTD::swap(__tie_, __rhs.__tie_); _VSTD::swap(__fill_, __rhs.__fill_); + _VSTD::swap(__fill_set, __rhs.__fill_set); } template diff --git a/libcxx/test/std/input.output/iostreams.base/ios/basic.ios.members/fill_init.pass.cpp b/libcxx/test/std/input.output/iostreams.base/ios/basic.ios.members/fill_init.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/input.output/iostreams.base/ios/basic.ios.members/fill_init.pass.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// + +// template class basic_ios + +// char_type fill() const; +// char_type fill(char_type fillch); + +// Make sure fill() returns the right initialized value of __fill_. + +#include +#include +#include + +#include "test_macros.h" + +int main(int, char**) { + { + std::basic_ios ios(0); + assert(ios.fill() == ' '); + } + { + std::basic_ios ios(0); + assert(ios.fill('x') == ' '); + } + { + std::basic_ios ios(0); + ios.fill('x'); + char ch = ios.fill(std::char_traits::eof()); + assert(ch == 'x'); + } +#ifndef TEST_HAS_NO_WIDE_CHARACTERS + { + std::basic_ios ios(0); + assert(ios.fill() == ' '); + } + { + std::basic_ios ios(0); + assert(ios.fill('x') == ' '); + } + { + std::basic_ios ios(0); + ios.fill('x'); + wchar_t ch = ios.fill(std::char_traits::eof()); + assert(ch == 'x'); + } +#endif + return 0; +}