Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- sstream ----------------------------------===// |
| 3 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_SSTREAM |
| 12 | #define _LIBCPP_SSTREAM |
| 13 | |
| 14 | /* |
| 15 | sstream synopsis |
| 16 | |
| 17 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 18 | class basic_stringbuf |
| 19 | : public basic_streambuf<charT, traits> |
| 20 | { |
| 21 | public: |
| 22 | typedef charT char_type; |
| 23 | typedef traits traits_type; |
| 24 | typedef typename traits_type::int_type int_type; |
| 25 | typedef typename traits_type::pos_type pos_type; |
| 26 | typedef typename traits_type::off_type off_type; |
| 27 | typedef Allocator allocator_type; |
| 28 | |
| 29 | // 27.8.1.1 Constructors: |
| 30 | explicit basic_stringbuf(ios_base::openmode which = ios_base::in | ios_base::out); |
| 31 | explicit basic_stringbuf(const basic_string<char_type, traits_type, allocator_type>& str, |
| 32 | ios_base::openmode which = ios_base::in | ios_base::out); |
| 33 | basic_stringbuf(basic_stringbuf&& rhs); |
| 34 | |
| 35 | // 27.8.1.2 Assign and swap: |
| 36 | basic_stringbuf& operator=(basic_stringbuf&& rhs); |
| 37 | void swap(basic_stringbuf& rhs); |
| 38 | |
| 39 | // 27.8.1.3 Get and set: |
| 40 | basic_string<char_type, traits_type, allocator_type> str() const; |
| 41 | void str(const basic_string<char_type, traits_type, allocator_type>& s); |
| 42 | |
| 43 | protected: |
| 44 | // 27.8.1.4 Overridden virtual functions: |
| 45 | virtual int_type underflow(); |
| 46 | virtual int_type pbackfail(int_type c = traits_type::eof()); |
| 47 | virtual int_type overflow (int_type c = traits_type::eof()); |
| 48 | virtual basic_streambuf<char_type, traits_type>* setbuf(char_type*, streamsize); |
| 49 | virtual pos_type seekoff(off_type off, ios_base::seekdir way, |
| 50 | ios_base::openmode which = ios_base::in | ios_base::out); |
| 51 | virtual pos_type seekpos(pos_type sp, |
| 52 | ios_base::openmode which = ios_base::in | ios_base::out); |
| 53 | }; |
| 54 | |
| 55 | template <class charT, class traits, class Allocator> |
| 56 | void swap(basic_stringbuf<charT, traits, Allocator>& x, |
| 57 | basic_stringbuf<charT, traits, Allocator>& y); |
| 58 | |
| 59 | typedef basic_stringbuf<char> stringbuf; |
| 60 | typedef basic_stringbuf<wchar_t> wstringbuf; |
| 61 | |
| 62 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 63 | class basic_istringstream |
| 64 | : public basic_istream<charT, traits> |
| 65 | { |
| 66 | public: |
| 67 | typedef charT char_type; |
| 68 | typedef traits traits_type; |
| 69 | typedef typename traits_type::int_type int_type; |
| 70 | typedef typename traits_type::pos_type pos_type; |
| 71 | typedef typename traits_type::off_type off_type; |
| 72 | typedef Allocator allocator_type; |
| 73 | |
| 74 | // 27.8.2.1 Constructors: |
| 75 | explicit basic_istringstream(ios_base::openmode which = ios_base::in); |
| 76 | explicit basic_istringstream(const basic_string<char_type, traits_type,allocator_type>& str, |
| 77 | ios_base::openmode which = ios_base::in); |
| 78 | basic_istringstream(basic_istringstream&& rhs); |
| 79 | |
| 80 | // 27.8.2.2 Assign and swap: |
| 81 | basic_istringstream& operator=(basic_istringstream&& rhs); |
| 82 | void swap(basic_istringstream& rhs); |
| 83 | |
| 84 | // 27.8.2.3 Members: |
| 85 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 86 | basic_string<char_type, traits_type, allocator_type> str() const; |
| 87 | void str(const basic_string<char_type, traits_type, allocator_type>& s); |
| 88 | }; |
| 89 | |
| 90 | template <class charT, class traits, class Allocator> |
| 91 | void swap(basic_istringstream<charT, traits, Allocator>& x, |
| 92 | basic_istringstream<charT, traits, Allocator>& y); |
| 93 | |
| 94 | typedef basic_istringstream<char> istringstream; |
| 95 | typedef basic_istringstream<wchar_t> wistringstream; |
| 96 | |
| 97 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 98 | class basic_ostringstream |
| 99 | : public basic_ostream<charT, traits> |
| 100 | { |
| 101 | public: |
| 102 | // types: |
| 103 | typedef charT char_type; |
| 104 | typedef traits traits_type; |
| 105 | typedef typename traits_type::int_type int_type; |
| 106 | typedef typename traits_type::pos_type pos_type; |
| 107 | typedef typename traits_type::off_type off_type; |
| 108 | typedef Allocator allocator_type; |
| 109 | |
| 110 | // 27.8.3.1 Constructors/destructor: |
| 111 | explicit basic_ostringstream(ios_base::openmode which = ios_base::out); |
| 112 | explicit basic_ostringstream(const basic_string<char_type, traits_type, allocator_type>& str, |
| 113 | ios_base::openmode which = ios_base::out); |
| 114 | basic_ostringstream(basic_ostringstream&& rhs); |
| 115 | |
| 116 | // 27.8.3.2 Assign/swap: |
| 117 | basic_ostringstream& operator=(basic_ostringstream&& rhs); |
| 118 | void swap(basic_ostringstream& rhs); |
| 119 | |
| 120 | // 27.8.3.3 Members: |
| 121 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 122 | basic_string<char_type, traits_type, allocator_type> str() const; |
| 123 | void str(const basic_string<char_type, traits_type, allocator_type>& s); |
| 124 | }; |
| 125 | |
| 126 | template <class charT, class traits, class Allocator> |
| 127 | void swap(basic_ostringstream<charT, traits, Allocator>& x, |
| 128 | basic_ostringstream<charT, traits, Allocator>& y); |
| 129 | |
| 130 | typedef basic_ostringstream<char> ostringstream; |
| 131 | typedef basic_ostringstream<wchar_t> wostringstream; |
| 132 | |
| 133 | template <class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > |
| 134 | class basic_stringstream |
| 135 | : public basic_iostream<charT, traits> |
| 136 | { |
| 137 | public: |
| 138 | // types: |
| 139 | typedef charT char_type; |
| 140 | typedef traits traits_type; |
| 141 | typedef typename traits_type::int_type int_type; |
| 142 | typedef typename traits_type::pos_type pos_type; |
| 143 | typedef typename traits_type::off_type off_type; |
| 144 | typedef Allocator allocator_type; |
| 145 | |
| 146 | // constructors/destructor |
| 147 | explicit basic_stringstream(ios_base::openmode which = ios_base::out|ios_base::in); |
| 148 | explicit basic_stringstream(const basic_string<char_type, traits_type, allocator_type>& str, |
| 149 | ios_base::openmode which = ios_base::out|ios_base::in); |
| 150 | basic_stringstream(basic_stringstream&& rhs); |
| 151 | |
| 152 | // 27.8.5.1 Assign/swap: |
| 153 | basic_stringstream& operator=(basic_stringstream&& rhs); |
| 154 | void swap(basic_stringstream& rhs); |
| 155 | |
| 156 | // Members: |
| 157 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 158 | basic_string<char_type, traits_type, allocator_type> str() const; |
| 159 | void str(const basic_string<char_type, traits_type, allocator_type>& str); |
| 160 | }; |
| 161 | |
| 162 | template <class charT, class traits, class Allocator> |
| 163 | void swap(basic_stringstream<charT, traits, Allocator>& x, |
| 164 | basic_stringstream<charT, traits, Allocator>& y); |
| 165 | |
| 166 | typedef basic_stringstream<char> stringstream; |
| 167 | typedef basic_stringstream<wchar_t> wstringstream; |
| 168 | |
| 169 | } // std |
| 170 | |
| 171 | */ |
| 172 | |
| 173 | #include <__config> |
| 174 | #include <ostream> |
| 175 | #include <istream> |
| 176 | #include <string> |
| 177 | |
| 178 | #pragma GCC system_header |
| 179 | |
| 180 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 181 | |
| 182 | // basic_stringbuf |
| 183 | |
| 184 | template <class _CharT, class _Traits, class _Allocator> |
| 185 | class basic_stringbuf |
| 186 | : public basic_streambuf<_CharT, _Traits> |
| 187 | { |
| 188 | public: |
| 189 | typedef _CharT char_type; |
| 190 | typedef _Traits traits_type; |
| 191 | typedef typename traits_type::int_type int_type; |
| 192 | typedef typename traits_type::pos_type pos_type; |
| 193 | typedef typename traits_type::off_type off_type; |
| 194 | typedef _Allocator allocator_type; |
| 195 | |
| 196 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
| 197 | |
| 198 | private: |
| 199 | |
| 200 | string_type __str_; |
| 201 | mutable char_type* __hm_; |
| 202 | ios_base::openmode __mode_; |
| 203 | |
| 204 | public: |
| 205 | // 27.8.1.1 Constructors: |
| 206 | explicit basic_stringbuf(ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 207 | explicit basic_stringbuf(const string_type& __s, |
| 208 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 209 | #ifdef _LIBCPP_MOVE |
| 210 | basic_stringbuf(basic_stringbuf&& __rhs); |
| 211 | #endif |
| 212 | |
| 213 | // 27.8.1.2 Assign and swap: |
| 214 | #ifdef _LIBCPP_MOVE |
| 215 | basic_stringbuf& operator=(basic_stringbuf&& __rhs); |
| 216 | #endif |
| 217 | void swap(basic_stringbuf& __rhs); |
| 218 | |
| 219 | // 27.8.1.3 Get and set: |
| 220 | string_type str() const; |
| 221 | void str(const string_type& __s); |
| 222 | |
| 223 | protected: |
| 224 | // 27.8.1.4 Overridden virtual functions: |
| 225 | virtual int_type underflow(); |
| 226 | virtual int_type pbackfail(int_type __c = traits_type::eof()); |
| 227 | virtual int_type overflow (int_type __c = traits_type::eof()); |
| 228 | virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, |
| 229 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 230 | virtual pos_type seekpos(pos_type __sp, |
| 231 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 232 | }; |
| 233 | |
| 234 | template <class _CharT, class _Traits, class _Allocator> |
| 235 | inline _LIBCPP_INLINE_VISIBILITY |
| 236 | basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(ios_base::openmode __wch) |
| 237 | : __hm_(0), |
| 238 | __mode_(__wch) |
| 239 | { |
| 240 | str(string_type()); |
| 241 | } |
| 242 | |
| 243 | template <class _CharT, class _Traits, class _Allocator> |
| 244 | inline _LIBCPP_INLINE_VISIBILITY |
| 245 | basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(const string_type& __s, |
| 246 | ios_base::openmode __wch) |
| 247 | : __hm_(0), |
| 248 | __mode_(__wch) |
| 249 | { |
| 250 | str(__s); |
| 251 | } |
| 252 | |
| 253 | #ifdef _LIBCPP_MOVE |
| 254 | |
| 255 | template <class _CharT, class _Traits, class _Allocator> |
| 256 | basic_stringbuf<_CharT, _Traits, _Allocator>::basic_stringbuf(basic_stringbuf&& __rhs) |
| 257 | : __mode_(__rhs.__mode_) |
| 258 | { |
| 259 | ptrdiff_t __ninp = __rhs.gptr() - __rhs.eback(); |
| 260 | ptrdiff_t __einp = __rhs.egptr() - __rhs.eback(); |
| 261 | ptrdiff_t __nout = __rhs.pptr() - __rhs.pbase(); |
| 262 | ptrdiff_t __eout = __rhs.epptr() - __rhs.pbase(); |
| 263 | ptrdiff_t __hm = __rhs.__hm_ - __rhs.pbase(); |
| 264 | __str_ = _STD::move(__rhs.__str_); |
| 265 | char_type* __p = const_cast<char_type*>(__str_.data()); |
| 266 | this->setg(__p, __p + __ninp, __p + __einp); |
| 267 | this->setp(__p, __p + __eout); |
| 268 | this->pbump(__nout); |
| 269 | __hm_ = __p + __hm; |
| 270 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
| 271 | __rhs.setg(__p, __p, __p); |
| 272 | __rhs.setp(__p, __p); |
| 273 | __rhs.__hm_ = __p; |
| 274 | this->pubimbue(__rhs.getloc()); |
| 275 | } |
| 276 | |
| 277 | template <class _CharT, class _Traits, class _Allocator> |
| 278 | basic_stringbuf<_CharT, _Traits, _Allocator>& |
| 279 | basic_stringbuf<_CharT, _Traits, _Allocator>::operator=(basic_stringbuf&& __rhs) |
| 280 | { |
| 281 | ptrdiff_t __ninp = __rhs.gptr() - __rhs.eback(); |
| 282 | ptrdiff_t __einp = __rhs.egptr() - __rhs.eback(); |
| 283 | ptrdiff_t __nout = __rhs.pptr() - __rhs.pbase(); |
| 284 | ptrdiff_t __eout = __rhs.epptr() - __rhs.pbase(); |
| 285 | ptrdiff_t __hm = __rhs.__hm_ - __rhs.pbase(); |
| 286 | __mode_ = __rhs.__mode_; |
| 287 | __str_ = _STD::move(__rhs.__str_); |
| 288 | char_type* __p = const_cast<char_type*>(__str_.data()); |
| 289 | this->setg(__p, __p + __ninp, __p + __einp); |
| 290 | this->setp(__p, __p + __eout); |
| 291 | this->pbump(__nout); |
| 292 | __hm_ = __p + __hm; |
| 293 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
| 294 | __rhs.setg(__p, __p, __p); |
| 295 | __rhs.setp(__p, __p); |
| 296 | __rhs.__hm_ = __p; |
| 297 | this->pubimbue(__rhs.getloc()); |
| 298 | return *this; |
| 299 | } |
| 300 | |
| 301 | #endif |
| 302 | |
| 303 | template <class _CharT, class _Traits, class _Allocator> |
| 304 | void |
| 305 | basic_stringbuf<_CharT, _Traits, _Allocator>::swap(basic_stringbuf& __rhs) |
| 306 | { |
| 307 | ptrdiff_t __rninp = __rhs.gptr() - __rhs.eback(); |
| 308 | ptrdiff_t __reinp = __rhs.egptr() - __rhs.eback(); |
| 309 | ptrdiff_t __rnout = __rhs.pptr() - __rhs.pbase(); |
| 310 | ptrdiff_t __reout = __rhs.epptr() - __rhs.pbase(); |
| 311 | ptrdiff_t __rhm = __rhs.__hm_ - __rhs.pbase(); |
| 312 | ptrdiff_t __lninp = this->gptr() - this->eback(); |
| 313 | ptrdiff_t __leinp = this->egptr() - this->eback(); |
| 314 | ptrdiff_t __lnout = this->pptr() - this->pbase(); |
| 315 | ptrdiff_t __leout = this->epptr() - this->pbase(); |
| 316 | ptrdiff_t __lhm = this->__hm_ - this->pbase(); |
| 317 | _STD::swap(__mode_, __rhs.__mode_); |
| 318 | __str_.swap(__rhs.__str_); |
| 319 | char_type* __p = const_cast<char_type*>(__str_.data()); |
| 320 | this->setg(__p, __p + __rninp, __p + __reinp); |
| 321 | this->setp(__p, __p + __reout); |
| 322 | this->pbump(__rnout); |
| 323 | __hm_ = __p + __rhm; |
| 324 | __p = const_cast<char_type*>(__rhs.__str_.data()); |
| 325 | __rhs.setg(__p, __p + __lninp, __p + __leinp); |
| 326 | __rhs.setp(__p, __p + __leout); |
| 327 | __rhs.pbump(__lnout); |
| 328 | __rhs.__hm_ = __p + __lhm; |
| 329 | locale __tl = __rhs.getloc(); |
| 330 | __rhs.pubimbue(this->getloc()); |
| 331 | this->pubimbue(__tl); |
| 332 | } |
| 333 | |
| 334 | template <class _CharT, class _Traits, class _Allocator> |
| 335 | inline _LIBCPP_INLINE_VISIBILITY |
| 336 | void |
| 337 | swap(basic_stringbuf<_CharT, _Traits, _Allocator>& __x, |
| 338 | basic_stringbuf<_CharT, _Traits, _Allocator>& __y) |
| 339 | { |
| 340 | __x.swap(__y); |
| 341 | } |
| 342 | |
| 343 | template <class _CharT, class _Traits, class _Allocator> |
| 344 | basic_string<_CharT, _Traits, _Allocator> |
| 345 | basic_stringbuf<_CharT, _Traits, _Allocator>::str() const |
| 346 | { |
| 347 | if (__mode_ & ios_base::out) |
| 348 | { |
| 349 | if (__hm_ < this->pptr()) |
| 350 | __hm_ = this->pptr(); |
| 351 | return string_type(this->pbase(), __hm_, __str_.get_allocator()); |
| 352 | } |
| 353 | else if (__mode_ & ios_base::in) |
| 354 | return string_type(this->eback(), this->egptr(), __str_.get_allocator()); |
| 355 | return string_type(__str_.get_allocator()); |
| 356 | } |
| 357 | |
| 358 | template <class _CharT, class _Traits, class _Allocator> |
| 359 | void |
| 360 | basic_stringbuf<_CharT, _Traits, _Allocator>::str(const string_type& __s) |
| 361 | { |
| 362 | __str_ = __s; |
| 363 | __hm_ = 0; |
| 364 | if (__mode_ & ios_base::in) |
| 365 | { |
| 366 | __hm_ = const_cast<char_type*>(__str_.data()) + __str_.size(); |
| 367 | this->setg(const_cast<char_type*>(__str_.data()), |
| 368 | const_cast<char_type*>(__str_.data()), |
| 369 | __hm_); |
| 370 | } |
| 371 | if (__mode_ & ios_base::out) |
| 372 | { |
| 373 | typename string_type::size_type __sz = __str_.size(); |
| 374 | __hm_ = const_cast<char_type*>(__str_.data()) + __sz; |
| 375 | __str_.resize(__str_.capacity()); |
| 376 | this->setp(const_cast<char_type*>(__str_.data()), |
| 377 | const_cast<char_type*>(__str_.data()) + __str_.size()); |
| 378 | if (__mode_ & (ios_base::app | ios_base::ate)) |
| 379 | this->pbump(__sz); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | template <class _CharT, class _Traits, class _Allocator> |
| 384 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
| 385 | basic_stringbuf<_CharT, _Traits, _Allocator>::underflow() |
| 386 | { |
| 387 | if (__hm_ < this->pptr()) |
| 388 | __hm_ = this->pptr(); |
| 389 | if (__mode_ & ios_base::in) |
| 390 | { |
| 391 | if (this->egptr() < __hm_) |
| 392 | this->setg(this->eback(), this->gptr(), __hm_); |
| 393 | if (this->gptr() < this->egptr()) |
| 394 | return traits_type::to_int_type(*this->gptr()); |
| 395 | } |
| 396 | return traits_type::eof(); |
| 397 | } |
| 398 | |
| 399 | template <class _CharT, class _Traits, class _Allocator> |
| 400 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
| 401 | basic_stringbuf<_CharT, _Traits, _Allocator>::pbackfail(int_type __c) |
| 402 | { |
| 403 | if (__hm_ < this->pptr()) |
| 404 | __hm_ = this->pptr(); |
| 405 | if (this->eback() < this->gptr()) |
| 406 | { |
| 407 | if (traits_type::eq_int_type(__c, traits_type::eof())) |
| 408 | { |
| 409 | this->setg(this->eback(), this->gptr()-1, __hm_); |
| 410 | return traits_type::not_eof(__c); |
| 411 | } |
| 412 | if ((__mode_ & ios_base::out) || |
| 413 | traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) |
| 414 | { |
| 415 | this->setg(this->eback(), this->gptr()-1, __hm_); |
| 416 | *this->gptr() = traits_type::to_char_type(__c); |
| 417 | return __c; |
| 418 | } |
| 419 | } |
| 420 | return traits_type::eof(); |
| 421 | } |
| 422 | |
| 423 | template <class _CharT, class _Traits, class _Allocator> |
| 424 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::int_type |
| 425 | basic_stringbuf<_CharT, _Traits, _Allocator>::overflow(int_type __c) |
| 426 | { |
| 427 | if (!traits_type::eq_int_type(__c, traits_type::eof())) |
| 428 | { |
| 429 | ptrdiff_t __ninp = this->gptr() - this->eback(); |
| 430 | if (this->pptr() == this->epptr()) |
| 431 | { |
| 432 | if (!(__mode_ & ios_base::out)) |
| 433 | return traits_type::eof(); |
| 434 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 435 | try |
| 436 | { |
| 437 | #endif |
| 438 | ptrdiff_t __nout = this->pptr() - this->pbase(); |
| 439 | ptrdiff_t __hm = __hm_ - this->pbase(); |
| 440 | __str_.push_back(char_type()); |
| 441 | __str_.resize(__str_.capacity()); |
| 442 | char_type* __p = const_cast<char_type*>(__str_.data()); |
| 443 | this->setp(__p, __p + __str_.size()); |
| 444 | this->pbump(__nout); |
| 445 | __hm_ = this->pbase() + __hm; |
| 446 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 447 | } |
| 448 | catch (...) |
| 449 | { |
| 450 | return traits_type::eof(); |
| 451 | } |
| 452 | #endif |
| 453 | } |
| 454 | __hm_ = max(this->pptr() + 1, __hm_); |
| 455 | if (__mode_ & ios_base::in) |
| 456 | { |
| 457 | char_type* __p = const_cast<char_type*>(__str_.data()); |
| 458 | this->setg(__p, __p + __ninp, __hm_); |
| 459 | } |
| 460 | return this->sputc(__c); |
| 461 | } |
| 462 | return traits_type::not_eof(__c); |
| 463 | } |
| 464 | |
| 465 | template <class _CharT, class _Traits, class _Allocator> |
| 466 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type |
| 467 | basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off, |
| 468 | ios_base::seekdir __way, |
| 469 | ios_base::openmode __wch) |
| 470 | { |
| 471 | if (__hm_ < this->pptr()) |
| 472 | __hm_ = this->pptr(); |
| 473 | if ((__wch & (ios_base::in | ios_base::out)) == 0) |
| 474 | return pos_type(-1); |
| 475 | if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out) |
| 476 | && __way == ios_base::cur) |
| 477 | return pos_type(-1); |
| 478 | off_type __noff; |
| 479 | switch (__way) |
| 480 | { |
| 481 | case ios_base::beg: |
| 482 | __noff = 0; |
| 483 | break; |
| 484 | case ios_base::cur: |
| 485 | if (__wch & ios_base::in) |
| 486 | __noff = this->gptr() - this->eback(); |
| 487 | else |
| 488 | __noff = this->pptr() - this->pbase(); |
| 489 | break; |
| 490 | case ios_base::end: |
| 491 | __noff = __hm_ - __str_.data(); |
| 492 | break; |
| 493 | default: |
| 494 | return pos_type(-1); |
| 495 | } |
| 496 | __noff += __off; |
| 497 | if (__noff < 0 || __hm_ - __str_.data() < __noff) |
| 498 | return pos_type(-1); |
| 499 | if (__noff != 0) |
| 500 | { |
| 501 | if ((__wch & ios_base::in) && this->gptr() == 0) |
| 502 | return pos_type(-1); |
| 503 | if ((__wch & ios_base::out) && this->pptr() == 0) |
| 504 | return pos_type(-1); |
| 505 | } |
| 506 | if (__wch & ios_base::in) |
| 507 | this->setg(this->eback(), this->eback() + __noff, __hm_); |
| 508 | if (__wch & ios_base::out) |
| 509 | { |
| 510 | this->setp(this->pbase(), this->epptr()); |
| 511 | this->pbump(__noff); |
| 512 | } |
| 513 | return pos_type(__noff); |
| 514 | } |
| 515 | |
| 516 | template <class _CharT, class _Traits, class _Allocator> |
| 517 | inline _LIBCPP_INLINE_VISIBILITY |
| 518 | typename basic_stringbuf<_CharT, _Traits, _Allocator>::pos_type |
| 519 | basic_stringbuf<_CharT, _Traits, _Allocator>::seekpos(pos_type __sp, |
| 520 | ios_base::openmode __wch) |
| 521 | { |
| 522 | return seekoff(__sp, ios_base::beg, __wch); |
| 523 | } |
| 524 | |
| 525 | // basic_istringstream |
| 526 | |
| 527 | template <class _CharT, class _Traits, class _Allocator> |
| 528 | class basic_istringstream |
| 529 | : public basic_istream<_CharT, _Traits> |
| 530 | { |
| 531 | public: |
| 532 | typedef _CharT char_type; |
| 533 | typedef _Traits traits_type; |
| 534 | typedef typename traits_type::int_type int_type; |
| 535 | typedef typename traits_type::pos_type pos_type; |
| 536 | typedef typename traits_type::off_type off_type; |
| 537 | typedef _Allocator allocator_type; |
| 538 | |
| 539 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
| 540 | |
| 541 | private: |
| 542 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
| 543 | |
| 544 | public: |
| 545 | // 27.8.2.1 Constructors: |
| 546 | explicit basic_istringstream(ios_base::openmode __wch = ios_base::in); |
| 547 | explicit basic_istringstream(const string_type& __s, |
| 548 | ios_base::openmode __wch = ios_base::in); |
| 549 | #ifdef _LIBCPP_MOVE |
| 550 | basic_istringstream(basic_istringstream&& __rhs); |
| 551 | |
| 552 | // 27.8.2.2 Assign and swap: |
| 553 | basic_istringstream& operator=(basic_istringstream&& __rhs); |
| 554 | #endif |
| 555 | void swap(basic_istringstream& __rhs); |
| 556 | |
| 557 | // 27.8.2.3 Members: |
| 558 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 559 | string_type str() const; |
| 560 | void str(const string_type& __s); |
| 561 | }; |
| 562 | |
| 563 | template <class _CharT, class _Traits, class _Allocator> |
| 564 | inline _LIBCPP_INLINE_VISIBILITY |
| 565 | basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(ios_base::openmode __wch) |
| 566 | : basic_istream<_CharT, _Traits>(&__sb_), |
| 567 | __sb_(__wch | ios_base::in) |
| 568 | { |
| 569 | } |
| 570 | |
| 571 | template <class _CharT, class _Traits, class _Allocator> |
| 572 | inline _LIBCPP_INLINE_VISIBILITY |
| 573 | basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(const string_type& __s, |
| 574 | ios_base::openmode __wch) |
| 575 | : basic_istream<_CharT, _Traits>(&__sb_), |
| 576 | __sb_(__s, __wch | ios_base::in) |
| 577 | { |
| 578 | } |
| 579 | |
| 580 | #ifdef _LIBCPP_MOVE |
| 581 | |
| 582 | template <class _CharT, class _Traits, class _Allocator> |
| 583 | inline _LIBCPP_INLINE_VISIBILITY |
| 584 | basic_istringstream<_CharT, _Traits, _Allocator>::basic_istringstream(basic_istringstream&& __rhs) |
| 585 | : basic_istream<_CharT, _Traits>(_STD::move(__rhs)), |
| 586 | __sb_(_STD::move(__rhs.__sb_)) |
| 587 | { |
| 588 | basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); |
| 589 | } |
| 590 | |
| 591 | template <class _CharT, class _Traits, class _Allocator> |
| 592 | basic_istringstream<_CharT, _Traits, _Allocator>& |
| 593 | basic_istringstream<_CharT, _Traits, _Allocator>::operator=(basic_istringstream&& __rhs) |
| 594 | { |
| 595 | basic_istream<char_type, traits_type>::operator=(_STD::move(__rhs)); |
| 596 | __sb_ = _STD::move(__rhs.__sb_); |
| 597 | return *this; |
| 598 | } |
| 599 | |
| 600 | #endif |
| 601 | |
| 602 | template <class _CharT, class _Traits, class _Allocator> |
| 603 | inline _LIBCPP_INLINE_VISIBILITY |
| 604 | void |
| 605 | basic_istringstream<_CharT, _Traits, _Allocator>::swap(basic_istringstream& __rhs) |
| 606 | { |
| 607 | basic_istream<char_type, traits_type>::swap(__rhs); |
| 608 | __sb_.swap(__rhs.__sb_); |
| 609 | } |
| 610 | |
| 611 | template <class _CharT, class _Traits, class _Allocator> |
| 612 | inline _LIBCPP_INLINE_VISIBILITY |
| 613 | void |
| 614 | swap(basic_istringstream<_CharT, _Traits, _Allocator>& __x, |
| 615 | basic_istringstream<_CharT, _Traits, _Allocator>& __y) |
| 616 | { |
| 617 | __x.swap(__y); |
| 618 | } |
| 619 | |
| 620 | template <class _CharT, class _Traits, class _Allocator> |
| 621 | inline _LIBCPP_INLINE_VISIBILITY |
| 622 | basic_stringbuf<_CharT, _Traits, _Allocator>* |
| 623 | basic_istringstream<_CharT, _Traits, _Allocator>::rdbuf() const |
| 624 | { |
| 625 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
| 626 | } |
| 627 | |
| 628 | template <class _CharT, class _Traits, class _Allocator> |
| 629 | inline _LIBCPP_INLINE_VISIBILITY |
| 630 | basic_string<_CharT, _Traits, _Allocator> |
| 631 | basic_istringstream<_CharT, _Traits, _Allocator>::str() const |
| 632 | { |
| 633 | return __sb_.str(); |
| 634 | } |
| 635 | |
| 636 | template <class _CharT, class _Traits, class _Allocator> |
| 637 | inline _LIBCPP_INLINE_VISIBILITY |
| 638 | void |
| 639 | basic_istringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s) |
| 640 | { |
| 641 | __sb_.str(__s); |
| 642 | } |
| 643 | |
| 644 | // basic_ostringstream |
| 645 | |
| 646 | template <class _CharT, class _Traits, class _Allocator> |
| 647 | class basic_ostringstream |
| 648 | : public basic_ostream<_CharT, _Traits> |
| 649 | { |
| 650 | public: |
| 651 | typedef _CharT char_type; |
| 652 | typedef _Traits traits_type; |
| 653 | typedef typename traits_type::int_type int_type; |
| 654 | typedef typename traits_type::pos_type pos_type; |
| 655 | typedef typename traits_type::off_type off_type; |
| 656 | typedef _Allocator allocator_type; |
| 657 | |
| 658 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
| 659 | |
| 660 | private: |
| 661 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
| 662 | |
| 663 | public: |
| 664 | // 27.8.2.1 Constructors: |
| 665 | explicit basic_ostringstream(ios_base::openmode __wch = ios_base::out); |
| 666 | explicit basic_ostringstream(const string_type& __s, |
| 667 | ios_base::openmode __wch = ios_base::out); |
| 668 | #ifdef _LIBCPP_MOVE |
| 669 | basic_ostringstream(basic_ostringstream&& __rhs); |
| 670 | |
| 671 | // 27.8.2.2 Assign and swap: |
| 672 | basic_ostringstream& operator=(basic_ostringstream&& __rhs); |
| 673 | #endif |
| 674 | void swap(basic_ostringstream& __rhs); |
| 675 | |
| 676 | // 27.8.2.3 Members: |
| 677 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 678 | string_type str() const; |
| 679 | void str(const string_type& __s); |
| 680 | }; |
| 681 | |
| 682 | template <class _CharT, class _Traits, class _Allocator> |
| 683 | inline _LIBCPP_INLINE_VISIBILITY |
| 684 | basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(ios_base::openmode __wch) |
| 685 | : basic_ostream<_CharT, _Traits>(&__sb_), |
| 686 | __sb_(__wch | ios_base::out) |
| 687 | { |
| 688 | } |
| 689 | |
| 690 | template <class _CharT, class _Traits, class _Allocator> |
| 691 | inline _LIBCPP_INLINE_VISIBILITY |
| 692 | basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(const string_type& __s, |
| 693 | ios_base::openmode __wch) |
| 694 | : basic_ostream<_CharT, _Traits>(&__sb_), |
| 695 | __sb_(__s, __wch | ios_base::out) |
| 696 | { |
| 697 | } |
| 698 | |
| 699 | #ifdef _LIBCPP_MOVE |
| 700 | |
| 701 | template <class _CharT, class _Traits, class _Allocator> |
| 702 | inline _LIBCPP_INLINE_VISIBILITY |
| 703 | basic_ostringstream<_CharT, _Traits, _Allocator>::basic_ostringstream(basic_ostringstream&& __rhs) |
| 704 | : basic_ostream<_CharT, _Traits>(_STD::move(__rhs)), |
| 705 | __sb_(_STD::move(__rhs.__sb_)) |
| 706 | { |
| 707 | basic_ostream<_CharT, _Traits>::set_rdbuf(&__sb_); |
| 708 | } |
| 709 | |
| 710 | template <class _CharT, class _Traits, class _Allocator> |
| 711 | basic_ostringstream<_CharT, _Traits, _Allocator>& |
| 712 | basic_ostringstream<_CharT, _Traits, _Allocator>::operator=(basic_ostringstream&& __rhs) |
| 713 | { |
| 714 | basic_ostream<char_type, traits_type>::operator=(_STD::move(__rhs)); |
| 715 | __sb_ = _STD::move(__rhs.__sb_); |
| 716 | return *this; |
| 717 | } |
| 718 | |
| 719 | #endif |
| 720 | |
| 721 | template <class _CharT, class _Traits, class _Allocator> |
| 722 | inline _LIBCPP_INLINE_VISIBILITY |
| 723 | void |
| 724 | basic_ostringstream<_CharT, _Traits, _Allocator>::swap(basic_ostringstream& __rhs) |
| 725 | { |
| 726 | basic_ostream<char_type, traits_type>::swap(__rhs); |
| 727 | __sb_.swap(__rhs.__sb_); |
| 728 | } |
| 729 | |
| 730 | template <class _CharT, class _Traits, class _Allocator> |
| 731 | inline _LIBCPP_INLINE_VISIBILITY |
| 732 | void |
| 733 | swap(basic_ostringstream<_CharT, _Traits, _Allocator>& __x, |
| 734 | basic_ostringstream<_CharT, _Traits, _Allocator>& __y) |
| 735 | { |
| 736 | __x.swap(__y); |
| 737 | } |
| 738 | |
| 739 | template <class _CharT, class _Traits, class _Allocator> |
| 740 | inline _LIBCPP_INLINE_VISIBILITY |
| 741 | basic_stringbuf<_CharT, _Traits, _Allocator>* |
| 742 | basic_ostringstream<_CharT, _Traits, _Allocator>::rdbuf() const |
| 743 | { |
| 744 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
| 745 | } |
| 746 | |
| 747 | template <class _CharT, class _Traits, class _Allocator> |
| 748 | inline _LIBCPP_INLINE_VISIBILITY |
| 749 | basic_string<_CharT, _Traits, _Allocator> |
| 750 | basic_ostringstream<_CharT, _Traits, _Allocator>::str() const |
| 751 | { |
| 752 | return __sb_.str(); |
| 753 | } |
| 754 | |
| 755 | template <class _CharT, class _Traits, class _Allocator> |
| 756 | inline _LIBCPP_INLINE_VISIBILITY |
| 757 | void |
| 758 | basic_ostringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s) |
| 759 | { |
| 760 | __sb_.str(__s); |
| 761 | } |
| 762 | |
| 763 | // basic_stringstream |
| 764 | |
| 765 | template <class _CharT, class _Traits, class _Allocator> |
| 766 | class basic_stringstream |
| 767 | : public basic_iostream<_CharT, _Traits> |
| 768 | { |
| 769 | public: |
| 770 | typedef _CharT char_type; |
| 771 | typedef _Traits traits_type; |
| 772 | typedef typename traits_type::int_type int_type; |
| 773 | typedef typename traits_type::pos_type pos_type; |
| 774 | typedef typename traits_type::off_type off_type; |
| 775 | typedef _Allocator allocator_type; |
| 776 | |
| 777 | typedef basic_string<char_type, traits_type, allocator_type> string_type; |
| 778 | |
| 779 | private: |
| 780 | basic_stringbuf<char_type, traits_type, allocator_type> __sb_; |
| 781 | |
| 782 | public: |
| 783 | // 27.8.2.1 Constructors: |
| 784 | explicit basic_stringstream(ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 785 | explicit basic_stringstream(const string_type& __s, |
| 786 | ios_base::openmode __wch = ios_base::in | ios_base::out); |
| 787 | #ifdef _LIBCPP_MOVE |
| 788 | basic_stringstream(basic_stringstream&& __rhs); |
| 789 | |
| 790 | // 27.8.2.2 Assign and swap: |
| 791 | basic_stringstream& operator=(basic_stringstream&& __rhs); |
| 792 | #endif |
| 793 | void swap(basic_stringstream& __rhs); |
| 794 | |
| 795 | // 27.8.2.3 Members: |
| 796 | basic_stringbuf<char_type, traits_type, allocator_type>* rdbuf() const; |
| 797 | string_type str() const; |
| 798 | void str(const string_type& __s); |
| 799 | }; |
| 800 | |
| 801 | template <class _CharT, class _Traits, class _Allocator> |
| 802 | inline _LIBCPP_INLINE_VISIBILITY |
| 803 | basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(ios_base::openmode __wch) |
| 804 | : basic_iostream<_CharT, _Traits>(&__sb_), |
| 805 | __sb_(__wch) |
| 806 | { |
| 807 | } |
| 808 | |
| 809 | template <class _CharT, class _Traits, class _Allocator> |
| 810 | inline _LIBCPP_INLINE_VISIBILITY |
| 811 | basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(const string_type& __s, |
| 812 | ios_base::openmode __wch) |
| 813 | : basic_iostream<_CharT, _Traits>(&__sb_), |
| 814 | __sb_(__s, __wch) |
| 815 | { |
| 816 | } |
| 817 | |
| 818 | #ifdef _LIBCPP_MOVE |
| 819 | |
| 820 | template <class _CharT, class _Traits, class _Allocator> |
| 821 | inline _LIBCPP_INLINE_VISIBILITY |
| 822 | basic_stringstream<_CharT, _Traits, _Allocator>::basic_stringstream(basic_stringstream&& __rhs) |
| 823 | : basic_iostream<_CharT, _Traits>(_STD::move(__rhs)), |
| 824 | __sb_(_STD::move(__rhs.__sb_)) |
| 825 | { |
| 826 | basic_istream<_CharT, _Traits>::set_rdbuf(&__sb_); |
| 827 | } |
| 828 | |
| 829 | template <class _CharT, class _Traits, class _Allocator> |
| 830 | basic_stringstream<_CharT, _Traits, _Allocator>& |
| 831 | basic_stringstream<_CharT, _Traits, _Allocator>::operator=(basic_stringstream&& __rhs) |
| 832 | { |
| 833 | basic_iostream<char_type, traits_type>::operator=(_STD::move(__rhs)); |
| 834 | __sb_ = _STD::move(__rhs.__sb_); |
| 835 | return *this; |
| 836 | } |
| 837 | |
| 838 | #endif |
| 839 | |
| 840 | template <class _CharT, class _Traits, class _Allocator> |
| 841 | inline _LIBCPP_INLINE_VISIBILITY |
| 842 | void |
| 843 | basic_stringstream<_CharT, _Traits, _Allocator>::swap(basic_stringstream& __rhs) |
| 844 | { |
| 845 | basic_iostream<char_type, traits_type>::swap(__rhs); |
| 846 | __sb_.swap(__rhs.__sb_); |
| 847 | } |
| 848 | |
| 849 | template <class _CharT, class _Traits, class _Allocator> |
| 850 | inline _LIBCPP_INLINE_VISIBILITY |
| 851 | void |
| 852 | swap(basic_stringstream<_CharT, _Traits, _Allocator>& __x, |
| 853 | basic_stringstream<_CharT, _Traits, _Allocator>& __y) |
| 854 | { |
| 855 | __x.swap(__y); |
| 856 | } |
| 857 | |
| 858 | template <class _CharT, class _Traits, class _Allocator> |
| 859 | inline _LIBCPP_INLINE_VISIBILITY |
| 860 | basic_stringbuf<_CharT, _Traits, _Allocator>* |
| 861 | basic_stringstream<_CharT, _Traits, _Allocator>::rdbuf() const |
| 862 | { |
| 863 | return const_cast<basic_stringbuf<char_type, traits_type, allocator_type>*>(&__sb_); |
| 864 | } |
| 865 | |
| 866 | template <class _CharT, class _Traits, class _Allocator> |
| 867 | inline _LIBCPP_INLINE_VISIBILITY |
| 868 | basic_string<_CharT, _Traits, _Allocator> |
| 869 | basic_stringstream<_CharT, _Traits, _Allocator>::str() const |
| 870 | { |
| 871 | return __sb_.str(); |
| 872 | } |
| 873 | |
| 874 | template <class _CharT, class _Traits, class _Allocator> |
| 875 | inline _LIBCPP_INLINE_VISIBILITY |
| 876 | void |
| 877 | basic_stringstream<_CharT, _Traits, _Allocator>::str(const string_type& __s) |
| 878 | { |
| 879 | __sb_.str(__s); |
| 880 | } |
| 881 | |
| 882 | _LIBCPP_END_NAMESPACE_STD |
| 883 | |
| 884 | #endif // _LIBCPP_SSTREAM |