Index: libcxx/include/sstream =================================================================== --- libcxx/include/sstream +++ libcxx/include/sstream @@ -41,6 +41,7 @@ // [stringbuf.members] Member functions: basic_string str() const; void str(const basic_string& s); + basic_string_view view() const noexcept; // C++20 protected: // [stringbuf.virtuals] Overridden virtual functions: @@ -92,6 +93,7 @@ basic_stringbuf* rdbuf() const; basic_string str() const; void str(const basic_string& s); + basic_string_view view() const noexcept; // C++20 }; template @@ -132,6 +134,7 @@ basic_stringbuf* rdbuf() const; basic_string str() const; void str(const basic_string& s); + basic_string_view view() const noexcept; // C++20 }; template @@ -172,6 +175,7 @@ basic_stringbuf* rdbuf() const; basic_string str() const; void str(const basic_string& str); + basic_string_view view() const noexcept; // C++20 }; template @@ -191,6 +195,7 @@ #include #include #include +#include #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) @@ -252,6 +257,7 @@ // [stringbuf.members] Member functions: string_type str() const; void str(const string_type& __s); + basic_string_view view() const noexcept; // C++20 protected: // [stringbuf.virtuals] Overridden virtual functions: @@ -446,15 +452,7 @@ basic_string<_CharT, _Traits, _Allocator> basic_stringbuf<_CharT, _Traits, _Allocator>::str() const { - if (__mode_ & ios_base::out) - { - if (__hm_ < this->pptr()) - __hm_ = this->pptr(); - return string_type(this->pbase(), __hm_, __str_.get_allocator()); - } - else if (__mode_ & ios_base::in) - return string_type(this->eback(), this->egptr(), __str_.get_allocator()); - return string_type(__str_.get_allocator()); + return string_type(this->view(), __str_.get_allocator()); } template @@ -490,6 +488,21 @@ } } +template +basic_string_view<_CharT, _Traits> +basic_stringbuf<_CharT, _Traits, _Allocator>::view() const noexcept +{ + if (__mode_ & ios_base::out) + { + if (__hm_ < this->pptr()) + __hm_ = this->pptr(); + return basic_string_view<_CharT, _Traits>(this->pbase(), __hm_ - this->pbase()); + } + else if (__mode_ & ios_base::in) + return basic_string_view<_CharT, _Traits>(this->eback(), this->egptr() - this->eback()); + return basic_string_view<_CharT, _Traits>(); +} + template typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type basic_stringbuf<_CharT, _Traits, _Allocator>::underflow() @@ -693,6 +706,10 @@ void str(const string_type& __s) { __sb_.str(__s); } + _LIBCPP_HIDE_FROM_ABI + basic_string_view view() const noexcept { + return __sb_.view(); + } }; template @@ -774,6 +791,10 @@ void str(const string_type& __s) { __sb_.str(__s); } + _LIBCPP_HIDE_FROM_ABI + basic_string_view view() const noexcept { + return __sb_.view(); + } }; template @@ -854,6 +875,10 @@ void str(const string_type& __s) { __sb_.str(__s); } + _LIBCPP_HIDE_FROM_ABI + basic_string_view view() const noexcept { + return __sb_.view(); + } }; template Index: libcxx/test/libcxx/input.output/string.streams/view.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/libcxx/input.output/string.streams/view.pass.cpp @@ -0,0 +1,26 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// + +#include +#include + +int main(int, char**) { + std::istringstream iss{"foo"}; + assert(iss.view() == "foo"); + + std::ostringstream oss; + oss << "foo"; + assert(oss.view() == "foo"); + + std::stringstream ss; + ss << "foo"; + assert(ss.view() == "foo"); + return 0; +}