-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++] Inline basic_streambuf functions #123379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
✅ With the latest revision this PR passed the C/C++ code formatter. |
c752280
to
a4f5179
Compare
@llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesMost of the Full diff: https://github.com/llvm/llvm-project/pull/123379.diff 1 Files Affected:
diff --git a/libcxx/include/streambuf b/libcxx/include/streambuf
index 7f02a9b3314110..3c4e9086e05ecb 100644
--- a/libcxx/include/streambuf
+++ b/libcxx/include/streambuf
@@ -146,7 +146,7 @@ public:
static_assert(is_same<_CharT, typename traits_type::char_type>::value,
"traits_type::char_type must be the same type as CharT");
- virtual ~basic_streambuf();
+ virtual ~basic_streambuf() {}
// 27.6.2.2.1 locales:
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 locale pubimbue(const locale& __loc) {
@@ -229,10 +229,36 @@ public:
}
protected:
- basic_streambuf();
- basic_streambuf(const basic_streambuf& __rhs);
- basic_streambuf& operator=(const basic_streambuf& __rhs);
- void swap(basic_streambuf& __rhs);
+ basic_streambuf() {}
+ basic_streambuf(const basic_streambuf& __sb)
+ : __loc_(__sb.__loc_),
+ __binp_(__sb.__binp_),
+ __ninp_(__sb.__ninp_),
+ __einp_(__sb.__einp_),
+ __bout_(__sb.__bout_),
+ __nout_(__sb.__nout_),
+ __eout_(__sb.__eout_) {}
+
+ basic_streambuf& operator=(const basic_streambuf& __sb) {
+ __loc_ = __sb.__loc_;
+ __binp_ = __sb.__binp_;
+ __ninp_ = __sb.__ninp_;
+ __einp_ = __sb.__einp_;
+ __bout_ = __sb.__bout_;
+ __nout_ = __sb.__nout_;
+ __eout_ = __sb.__eout_;
+ return *this;
+ }
+
+ void swap(basic_streambuf& __sb) {
+ std::swap(__loc_, __sb.__loc_);
+ std::swap(__binp_, __sb.__binp_);
+ std::swap(__ninp_, __sb.__ninp_);
+ std::swap(__einp_, __sb.__einp_);
+ std::swap(__bout_, __sb.__bout_);
+ std::swap(__nout_, __sb.__nout_);
+ std::swap(__eout_, __sb.__eout_);
+ }
// 27.6.2.3.2 Get area:
_LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
@@ -267,173 +293,85 @@ protected:
// 27.6.2.4 virtual functions:
// 27.6.2.4.1 Locales:
- virtual void imbue(const locale& __loc);
+ virtual void imbue(const locale&) {}
// 27.6.2.4.2 Buffer management and positioning:
- virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
- virtual pos_type
- seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out);
- virtual pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out);
- virtual int sync();
+ virtual basic_streambuf* setbuf(char_type*, streamsize) { return this; }
+ virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out) {
+ return pos_type(off_type(-1));
+ }
+ virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out) {
+ return pos_type(off_type(-1));
+ }
+ virtual int sync() { return 0; }
// 27.6.2.4.3 Get area:
- virtual streamsize showmanyc();
- virtual streamsize xsgetn(char_type* __s, streamsize __n);
- virtual int_type underflow();
- virtual int_type uflow();
+ virtual streamsize showmanyc() { return 0; }
+
+ virtual streamsize xsgetn(char_type* __s, streamsize __n) {
+ const int_type __eof = traits_type::eof();
+ int_type __c;
+ streamsize __i = 0;
+ while (__i < __n) {
+ if (__ninp_ < __einp_) {
+ const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
+ traits_type::copy(__s, __ninp_, __len);
+ __s += __len;
+ __i += __len;
+ this->gbump(__len);
+ } else if ((__c = uflow()) != __eof) {
+ *__s = traits_type::to_char_type(__c);
+ ++__s;
+ ++__i;
+ } else
+ break;
+ }
+ return __i;
+ }
+
+ virtual int_type underflow() { return traits_type::eof(); }
+ virtual int_type uflow() {
+ if (underflow() == traits_type::eof())
+ return traits_type::eof();
+ return traits_type::to_int_type(*__ninp_++);
+ }
// 27.6.2.4.4 Putback:
- virtual int_type pbackfail(int_type __c = traits_type::eof());
+ virtual int_type pbackfail(int_type = traits_type::eof()) { return traits_type::eof(); }
// 27.6.2.4.5 Put area:
- virtual streamsize xsputn(const char_type* __s, streamsize __n);
- virtual int_type overflow(int_type __c = traits_type::eof());
+ virtual streamsize xsputn(const char_type* __s, streamsize __n) {
+ streamsize __i = 0;
+ int_type __eof = traits_type::eof();
+ while (__i < __n) {
+ if (__nout_ >= __eout_) {
+ if (overflow(traits_type::to_int_type(*__s)) == __eof)
+ break;
+ ++__s;
+ ++__i;
+ } else {
+ streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
+ traits_type::copy(__nout_, __s, __chunk_size);
+ __nout_ += __chunk_size;
+ __s += __chunk_size;
+ __i += __chunk_size;
+ }
+ }
+ return __i;
+ }
+
+ virtual int_type overflow(int_type = traits_type::eof()) { return traits_type::eof(); }
private:
locale __loc_;
- char_type* __binp_;
- char_type* __ninp_;
- char_type* __einp_;
- char_type* __bout_;
- char_type* __nout_;
- char_type* __eout_;
+ char_type* __binp_ = nullptr;
+ char_type* __ninp_ = nullptr;
+ char_type* __einp_ = nullptr;
+ char_type* __bout_ = nullptr;
+ char_type* __nout_ = nullptr;
+ char_type* __eout_ = nullptr;
};
-template <class _CharT, class _Traits>
-basic_streambuf<_CharT, _Traits>::~basic_streambuf() {}
-
-template <class _CharT, class _Traits>
-basic_streambuf<_CharT, _Traits>::basic_streambuf()
- : __binp_(nullptr), __ninp_(nullptr), __einp_(nullptr), __bout_(nullptr), __nout_(nullptr), __eout_(nullptr) {}
-
-template <class _CharT, class _Traits>
-basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
- : __loc_(__sb.__loc_),
- __binp_(__sb.__binp_),
- __ninp_(__sb.__ninp_),
- __einp_(__sb.__einp_),
- __bout_(__sb.__bout_),
- __nout_(__sb.__nout_),
- __eout_(__sb.__eout_) {}
-
-template <class _CharT, class _Traits>
-basic_streambuf<_CharT, _Traits>& basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb) {
- __loc_ = __sb.__loc_;
- __binp_ = __sb.__binp_;
- __ninp_ = __sb.__ninp_;
- __einp_ = __sb.__einp_;
- __bout_ = __sb.__bout_;
- __nout_ = __sb.__nout_;
- __eout_ = __sb.__eout_;
- return *this;
-}
-
-template <class _CharT, class _Traits>
-void basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb) {
- std::swap(__loc_, __sb.__loc_);
- std::swap(__binp_, __sb.__binp_);
- std::swap(__ninp_, __sb.__ninp_);
- std::swap(__einp_, __sb.__einp_);
- std::swap(__bout_, __sb.__bout_);
- std::swap(__nout_, __sb.__nout_);
- std::swap(__eout_, __sb.__eout_);
-}
-
-template <class _CharT, class _Traits>
-void basic_streambuf<_CharT, _Traits>::imbue(const locale&) {}
-
-template <class _CharT, class _Traits>
-basic_streambuf<_CharT, _Traits>* basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize) {
- return this;
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::pos_type
-basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir, ios_base::openmode) {
- return pos_type(off_type(-1));
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::pos_type
-basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode) {
- return pos_type(off_type(-1));
-}
-
-template <class _CharT, class _Traits>
-int basic_streambuf<_CharT, _Traits>::sync() {
- return 0;
-}
-
-template <class _CharT, class _Traits>
-streamsize basic_streambuf<_CharT, _Traits>::showmanyc() {
- return 0;
-}
-
-template <class _CharT, class _Traits>
-streamsize basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n) {
- const int_type __eof = traits_type::eof();
- int_type __c;
- streamsize __i = 0;
- while (__i < __n) {
- if (__ninp_ < __einp_) {
- const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
- traits_type::copy(__s, __ninp_, __len);
- __s += __len;
- __i += __len;
- this->gbump(__len);
- } else if ((__c = uflow()) != __eof) {
- *__s = traits_type::to_char_type(__c);
- ++__s;
- ++__i;
- } else
- break;
- }
- return __i;
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::underflow() {
- return traits_type::eof();
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::uflow() {
- if (underflow() == traits_type::eof())
- return traits_type::eof();
- return traits_type::to_int_type(*__ninp_++);
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::pbackfail(int_type) {
- return traits_type::eof();
-}
-
-template <class _CharT, class _Traits>
-streamsize basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n) {
- streamsize __i = 0;
- int_type __eof = traits_type::eof();
- while (__i < __n) {
- if (__nout_ >= __eout_) {
- if (overflow(traits_type::to_int_type(*__s)) == __eof)
- break;
- ++__s;
- ++__i;
- } else {
- streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
- traits_type::copy(__nout_, __s, __chunk_size);
- __nout_ += __chunk_size;
- __s += __chunk_size;
- __i += __chunk_size;
- }
- }
- return __i;
-}
-
-template <class _CharT, class _Traits>
-typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::overflow(int_type) {
- return traits_type::eof();
-}
-
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
# if _LIBCPP_HAS_WIDE_CHARACTERS
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Most of the
basic_streambuf
functions are really simple, which makes most of the implementation when they are out of line boilerplate.