#pragma once
#ifndef _SSTREAM_
#define _SSTREAM_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <istream>
#include <string>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
_STD_BEGIN
template <class _Elem, class _Traits, class _Alloc>
class basic_stringbuf
: public basic_streambuf<_Elem, _Traits> {
public:
using allocator_type = _Alloc;
using _Mysb = basic_streambuf<_Elem, _Traits>;
using _Mystr = basic_string<_Elem, _Traits, _Alloc>;
using _Mysize_type = typename _Mystr::size_type;
explicit basic_stringbuf(ios_base::openmode _Mode = ios_base::in | ios_base::out)
: _Seekhigh(nullptr), _Mystate(_Getstate(_Mode)), _Al() {}
explicit basic_stringbuf(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in | ios_base::out)
: _Al(_Str.get_allocator()) {
_Init(_Str.c_str(), _Str.size(), _Getstate(_Mode));
}
basic_stringbuf(basic_stringbuf&& _Right) {
_Assign_rv(_STD move(_Right));
}
basic_stringbuf& operator=(basic_stringbuf&& _Right) {
_Assign_rv(_STD move(_Right));
return *this;
}
void _Assign_rv(basic_stringbuf&& _Right) {
if (this != _STD addressof(_Right)) {
_Tidy();
this->swap(_Right);
}
}
void swap(basic_stringbuf& _Right) {
if (this != _STD addressof(_Right)) {
_Mysb::swap(_Right);
_STD swap(_Seekhigh, _Right._Seekhigh);
_STD swap(_Mystate, _Right._Mystate);
_Swap_adl(_Al, _Right._Al);
}
}
basic_stringbuf(const basic_stringbuf&) = delete;
basic_stringbuf& operator=(const basic_stringbuf&) = delete;
virtual ~basic_stringbuf() noexcept {
_Tidy();
}
enum {
_Allocated = 1,
_Constant = 2,
_Noread = 4,
_Append = 8,
_Atend = 16
};
using int_type = typename _Traits::int_type;
using pos_type = typename _Traits::pos_type;
using off_type = typename _Traits::off_type;
_NODISCARD _Mystr str() const {
_Mystr _Result(_Al);
if (!(_Mystate & _Constant) && _Mysb::pptr()) {
const auto _Base = _Mysb::pbase();
_Result.assign(_Base, static_cast<_Mysize_type>(_Max_value(_Mysb::pptr(), _Seekhigh) - _Base));
} else if (!(_Mystate & _Noread) && _Mysb::gptr()) {
const auto _Base = _Mysb::eback();
_Result.assign(_Base, static_cast<_Mysize_type>(_Mysb::egptr() - _Base));
}
return _Result;
}
void str(const _Mystr& _Newstr) {
_Tidy();
_Init(_Newstr.c_str(), _Newstr.size(), _Mystate);
}
protected:
virtual int_type overflow(int_type _Meta = _Traits::eof()) {
if (_Mystate & _Constant) {
return _Traits::eof();
}
if (_Traits::eq_int_type(_Traits::eof(), _Meta)) {
return _Traits::not_eof(_Meta);
}
const auto _Pptr = _Mysb::pptr();
const auto _Epptr = _Mysb::epptr();
if (_Pptr && _Pptr < _Epptr) {
*_Mysb::_Pninc() = _Traits::to_char_type(_Meta);
_Seekhigh = _Pptr + 1;
return _Meta;
}
size_t _Oldsize = 0;
const auto _Oldptr = _Mysb::eback();
if (_Pptr) {
_Oldsize = static_cast<size_t>(_Epptr - _Oldptr);
}
size_t _Newsize;
if (_Oldsize < _MINSIZE) {
_Newsize = _MINSIZE;
} else if (_Oldsize < INT_MAX / 2) {
_Newsize = _Oldsize << 1;
} else if (_Oldsize < INT_MAX) {
_Newsize = INT_MAX;
} else {
return _Traits::eof();
}
const auto _Newptr = _Unfancy(_Al.allocate(_Newsize));
_Traits::copy(_Newptr, _Oldptr, _Oldsize);
const auto _New_pnext = _Newptr + _Oldsize;
_Seekhigh = _New_pnext + 1;
_Mysb::setp(_Newptr, _New_pnext, _Newptr + _Newsize);
if (_Mystate & _Noread) {
_Mysb::setg(_Newptr, nullptr, _Newptr);
} else {
_Mysb::setg(_Newptr, _Newptr + (_Mysb::gptr() - _Oldptr), _Seekhigh);
}
if (_Mystate & _Allocated) {
_Al.deallocate(_Ptr_traits::pointer_to(*_Oldptr), _Oldsize);
}
_Mystate |= _Allocated;
*_Mysb::_Pninc() = _Traits::to_char_type(_Meta);
return _Meta;
}
virtual int_type pbackfail(int_type _Meta = _Traits::eof()) {
const auto _Gptr = _Mysb::gptr();
if (!_Gptr || _Gptr <= _Mysb::eback()
|| (!_Traits::eq_int_type(_Traits::eof(), _Meta) && !_Traits::eq(_Traits::to_char_type(_Meta), _Gptr[-1])
&& (_Mystate & _Constant))) {
return _Traits::eof();
}
_Mysb::gbump(-1);
if (!_Traits::eq_int_type(_Traits::eof(), _Meta)) {
*_Mysb::gptr() = _Traits::to_char_type(_Meta);
}
return _Traits::not_eof(_Meta);
}
virtual int_type underflow() {
const auto _Gptr = _Mysb::gptr();
if (!_Gptr) {
return _Traits::eof();
}
if (_Gptr < _Mysb::egptr()) {
return _Traits::to_int_type(*_Gptr);
}
const auto _Pptr = _Mysb::pptr();
if (!_Pptr || (_Mystate & _Noread)) {
return _Traits::eof();
}
const auto _Local_highwater = _Max_value(_Seekhigh, _Pptr);
if (_Local_highwater <= _Gptr) {
return _Traits::eof();
}
_Seekhigh = _Local_highwater;
_Mysb::setg(_Mysb::eback(), _Mysb::gptr(), _Local_highwater);
return _Traits::to_int_type(*_Mysb::gptr());
}
virtual pos_type seekoff(
off_type _Off, ios_base::seekdir _Way, ios_base::openmode _Mode = ios_base::in | ios_base::out) {
const auto _Gptr_old = _Mysb::gptr();
const auto _Pptr_old = _Mysb::pptr();
if (_Pptr_old && _Seekhigh < _Pptr_old) {
_Seekhigh = _Pptr_old;
}
const auto _Seeklow = _Mysb::eback();
const auto _Seekdist = _Seekhigh - _Seeklow;
off_type _Newoff;
switch (_Way) {
case ios_base::beg:
_Newoff = 0;
break;
case ios_base::end:
_Newoff = _Seekdist;
break;
case ios_base::cur: {
constexpr auto _Both = ios_base::in | ios_base::out;
if ((_Mode & _Both) != _Both) {
if (_Mode & ios_base::in) {
if (_Gptr_old || !_Seeklow) {
_Newoff = _Gptr_old - _Seeklow;
break;
}
} else if ((_Mode & ios_base::out) && (_Pptr_old || !_Seeklow)) {
_Newoff = _Pptr_old - _Seeklow;
break;
}
}
}
default:
return pos_type(off_type(-1));
}
if (static_cast<unsigned long long>(_Off) + _Newoff > static_cast<unsigned long long>(_Seekdist)) {
return pos_type(off_type(-1));
}
_Off += _Newoff;
if (_Off != 0 && (((_Mode & ios_base::in) && !_Gptr_old) || ((_Mode & ios_base::out) && !_Pptr_old))) {
return pos_type(off_type(-1));
}
const auto _Newptr = _Seeklow + _Off;
if ((_Mode & ios_base::in) && _Gptr_old) {
_Mysb::setg(_Seeklow, _Newptr, _Seekhigh);
}
if ((_Mode & ios_base::out) && _Pptr_old) {
_Mysb::setp(_Seeklow, _Newptr, _Mysb::epptr());
}
return pos_type(_Off);
}
virtual pos_type seekpos(pos_type _Pos, ios_base::openmode _Mode = ios_base::in | ios_base::out) {
const auto _Off = static_cast<streamoff>(_Pos);
const auto _Gptr_old = _Mysb::gptr();
const auto _Pptr_old = _Mysb::pptr();
if (_Pptr_old && _Seekhigh < _Pptr_old) {
_Seekhigh = _Pptr_old;
}
const auto _Seeklow = _Mysb::eback();
const auto _Seekdist = _Seekhigh - _Seeklow;
if (static_cast<unsigned long long>(_Off) > static_cast<unsigned long long>(_Seekdist)) {
return pos_type(off_type(-1));
}
if (_Off != 0 && (((_Mode & ios_base::in) && !_Gptr_old) || ((_Mode & ios_base::out) && !_Pptr_old))) {
return pos_type(off_type(-1));
}
const auto _Newptr = _Seeklow + _Off;
if ((_Mode & ios_base::in) && _Gptr_old) {
_Mysb::setg(_Seeklow, _Newptr, _Seekhigh);
}
if ((_Mode & ios_base::out) && _Pptr_old) {
_Mysb::setp(_Seeklow, _Newptr, _Mysb::epptr());
}
return pos_type(_Off);
}
protected:
void _Init(const _Elem* _Ptr, _Mysize_type _Count, int _State) {
if (_Count > INT_MAX) {
_Xbad_alloc();
}
if (_Count != 0
&& (_State & (_Noread | _Constant))
!= (_Noread | _Constant)) {
const auto _Pnew = _Unfancy(_Al.allocate(_Count));
_Traits::copy(_Pnew, _Ptr, _Count);
_Seekhigh = _Pnew + _Count;
if (!(_State & _Noread)) {
_Mysb::setg(_Pnew, _Pnew, _Seekhigh);
}
if (!(_State & _Constant)) {
_Mysb::setp(_Pnew, (_State & (_Atend | _Append)) ? _Seekhigh : _Pnew, _Seekhigh);
if (_State & _Noread) {
_Mysb::setg(_Pnew, nullptr, _Pnew);
}
}
_State |= _Allocated;
} else {
_Seekhigh = nullptr;
}
_Mystate = _State;
}
void _Tidy() noexcept {
if (_Mystate & _Allocated) {
_Al.deallocate(_Ptr_traits::pointer_to(*_Mysb::eback()),
static_cast<typename allocator_traits<allocator_type>::size_type>(
(_Mysb::pptr() ? _Mysb::epptr() : _Mysb::egptr()) - _Mysb::eback()));
}
_Mysb::setg(nullptr, nullptr, nullptr);
_Mysb::setp(nullptr, nullptr);
_Seekhigh = nullptr;
_Mystate &= ~_Allocated;
}
private:
using _Ptr_traits = pointer_traits<typename allocator_traits<allocator_type>::pointer>;
enum {
_MINSIZE = 32
};
static int _Getstate(ios_base::openmode _Mode) {
int _State = 0;
if (!(_Mode & ios_base::in)) {
_State |= _Noread;
}
if (!(_Mode & ios_base::out)) {
_State |= _Constant;
}
if (_Mode & ios_base::app) {
_State |= _Append;
}
if (_Mode & ios_base::ate) {
_State |= _Atend;
}
return _State;
}
_Elem* _Seekhigh;
int _Mystate;
allocator_type _Al;
};
template <class _Elem, class _Traits, class _Alloc>
void swap(basic_stringbuf<_Elem, _Traits, _Alloc>& _Left, basic_stringbuf<_Elem, _Traits, _Alloc>& _Right) {
_Left.swap(_Right);
}
template <class _Elem, class _Traits, class _Alloc>
class basic_istringstream : public basic_istream<_Elem, _Traits> {
public:
using _Mybase = basic_istream<_Elem, _Traits>;
using allocator_type = _Alloc;
using _Mysb = basic_stringbuf<_Elem, _Traits, _Alloc>;
using _Mystr = basic_string<_Elem, _Traits, _Alloc>;
explicit basic_istringstream(ios_base::openmode _Mode = ios_base::in)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Mode | ios_base::in) {}
explicit basic_istringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Str, _Mode | ios_base::in) {}
basic_istringstream(basic_istringstream&& _Right) : _Mybase(_STD addressof(_Stringbuffer)) {
_Assign_rv(_STD move(_Right));
}
basic_istringstream& operator=(basic_istringstream&& _Right) {
_Assign_rv(_STD move(_Right));
return *this;
}
void _Assign_rv(basic_istringstream&& _Right) {
if (this != _STD addressof(_Right)) {
_Stringbuffer.str(_Mystr());
this->swap(_Right);
}
}
void swap(basic_istringstream& _Right) {
if (this != _STD addressof(_Right)) {
_Mybase::swap(_Right);
_Stringbuffer.swap(_Right._Stringbuffer);
}
}
basic_istringstream(const basic_istringstream&) = delete;
basic_istringstream& operator=(const basic_istringstream&) = delete;
virtual ~basic_istringstream() noexcept {}
_NODISCARD _Mysb* rdbuf() const {
return const_cast<_Mysb*>(_STD addressof(_Stringbuffer));
}
_NODISCARD _Mystr str() const {
return _Stringbuffer.str();
}
void str(const _Mystr& _Newstr) {
_Stringbuffer.str(_Newstr);
}
private:
_Mysb _Stringbuffer;
};
template <class _Elem, class _Traits, class _Alloc>
void swap(basic_istringstream<_Elem, _Traits, _Alloc>& _Left, basic_istringstream<_Elem, _Traits, _Alloc>& _Right) {
_Left.swap(_Right);
}
template <class _Elem, class _Traits, class _Alloc>
class basic_ostringstream : public basic_ostream<_Elem, _Traits> {
public:
using _Mybase = basic_ostream<_Elem, _Traits>;
using allocator_type = _Alloc;
using _Mysb = basic_stringbuf<_Elem, _Traits, _Alloc>;
using _Mystr = basic_string<_Elem, _Traits, _Alloc>;
explicit basic_ostringstream(ios_base::openmode _Mode = ios_base::out)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Mode | ios_base::out) {}
explicit basic_ostringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::out)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Str, _Mode | ios_base::out) {}
basic_ostringstream(basic_ostringstream&& _Right) : _Mybase(_STD addressof(_Stringbuffer)) {
_Assign_rv(_STD move(_Right));
}
basic_ostringstream& operator=(basic_ostringstream&& _Right) {
_Assign_rv(_STD move(_Right));
return *this;
}
void _Assign_rv(basic_ostringstream&& _Right) {
if (this != _STD addressof(_Right)) {
_Stringbuffer.str(_Mystr());
this->swap(_Right);
}
}
void swap(basic_ostringstream& _Right) {
if (this != _STD addressof(_Right)) {
_Mybase::swap(_Right);
_Stringbuffer.swap(_Right._Stringbuffer);
}
}
basic_ostringstream(const basic_ostringstream&) = delete;
basic_ostringstream& operator=(const basic_ostringstream&) = delete;
virtual ~basic_ostringstream() noexcept {}
_NODISCARD _Mysb* rdbuf() const {
return const_cast<_Mysb*>(_STD addressof(_Stringbuffer));
}
_NODISCARD _Mystr str() const {
return _Stringbuffer.str();
}
void str(const _Mystr& _Newstr) {
_Stringbuffer.str(_Newstr);
}
private:
_Mysb _Stringbuffer;
};
template <class _Elem, class _Traits, class _Alloc>
void swap(basic_ostringstream<_Elem, _Traits, _Alloc>& _Left, basic_ostringstream<_Elem, _Traits, _Alloc>& _Right) {
_Left.swap(_Right);
}
template <class _Elem, class _Traits, class _Alloc>
class basic_stringstream
: public basic_iostream<_Elem, _Traits> {
public:
using _Mybase = basic_iostream<_Elem, _Traits>;
using char_type = _Elem;
using traits_type = _Traits;
using allocator_type = _Alloc;
using int_type = typename _Traits::int_type;
using pos_type = typename _Traits::pos_type;
using off_type = typename _Traits::off_type;
using _Mysb = basic_stringbuf<_Elem, _Traits, _Alloc>;
using _Mystr = basic_string<_Elem, _Traits, _Alloc>;
explicit basic_stringstream(ios_base::openmode _Mode = ios_base::in | ios_base::out)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Mode) {}
explicit basic_stringstream(const _Mystr& _Str, ios_base::openmode _Mode = ios_base::in | ios_base::out)
: _Mybase(_STD addressof(_Stringbuffer)), _Stringbuffer(_Str, _Mode) {}
basic_stringstream(basic_stringstream&& _Right) : _Mybase(_STD addressof(_Stringbuffer)) {
_Assign_rv(_STD move(_Right));
}
basic_stringstream& operator=(basic_stringstream&& _Right) {
_Assign_rv(_STD move(_Right));
return *this;
}
void _Assign_rv(basic_stringstream&& _Right) {
if (this != _STD addressof(_Right)) {
_Stringbuffer.str(_Mystr());
this->swap(_Right);
}
}
void swap(basic_stringstream& _Right) {
if (this != _STD addressof(_Right)) {
_Mybase::swap(_Right);
_Stringbuffer.swap(_Right._Stringbuffer);
}
}
basic_stringstream(const basic_stringstream&) = delete;
basic_stringstream& operator=(const basic_stringstream&) = delete;
virtual ~basic_stringstream() noexcept {}
_NODISCARD _Mysb* rdbuf() const {
return const_cast<_Mysb*>(_STD addressof(_Stringbuffer));
}
_NODISCARD _Mystr str() const {
return _Stringbuffer.str();
}
void str(const _Mystr& _Newstr) {
_Stringbuffer.str(_Newstr);
}
private:
_Mysb _Stringbuffer;
};
template <class _Elem, class _Traits, class _Alloc>
void swap(basic_stringstream<_Elem, _Traits, _Alloc>& _Left, basic_stringstream<_Elem, _Traits, _Alloc>& _Right) {
_Left.swap(_Right);
}
_STD_END
#pragma pop_macro("new")
_STL_RESTORE_CLANG_WARNINGS
#pragma warning(pop)
#pragma pack(pop)
#endif
#endif
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
· 【ESP32】两种模拟 USB 鼠标的方法
· 设计模式脉络