annotate DEPENDENCIES/generic/include/boost/container/string.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/container for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_CONTAINER_STRING_HPP
Chris@16 12 #define BOOST_CONTAINER_STRING_HPP
Chris@16 13
Chris@16 14 #include <boost/container/detail/config_begin.hpp>
Chris@16 15 #include <boost/container/detail/workaround.hpp>
Chris@16 16
Chris@16 17 #include <boost/container/detail/workaround.hpp>
Chris@16 18 #include <boost/container/container_fwd.hpp>
Chris@16 19 #include <boost/container/throw_exception.hpp>
Chris@16 20 #include <boost/container/detail/utilities.hpp>
Chris@16 21 #include <boost/container/detail/iterators.hpp>
Chris@16 22 #include <boost/container/detail/algorithms.hpp>
Chris@16 23 #include <boost/container/detail/version_type.hpp>
Chris@16 24 #include <boost/container/detail/allocation_type.hpp>
Chris@16 25 #include <boost/container/allocator_traits.hpp>
Chris@16 26 #include <boost/container/detail/allocator_version_traits.hpp>
Chris@16 27 #include <boost/container/detail/mpl.hpp>
Chris@16 28 #include <boost/move/utility.hpp>
Chris@16 29 #include <boost/static_assert.hpp>
Chris@16 30 #include <boost/functional/hash.hpp>
Chris@16 31 #include <boost/intrusive/pointer_traits.hpp>
Chris@16 32 #include <boost/detail/no_exceptions_support.hpp>
Chris@16 33
Chris@16 34 #include <functional>
Chris@16 35 #include <string>
Chris@16 36 #include <utility>
Chris@16 37 #include <iterator>
Chris@16 38 #include <memory>
Chris@16 39 #include <algorithm>
Chris@16 40 #include <iosfwd>
Chris@16 41 #include <istream>
Chris@16 42 #include <ostream>
Chris@16 43 #include <ios>
Chris@16 44 #include <locale>
Chris@16 45 #include <cstddef>
Chris@16 46 #include <climits>
Chris@16 47 #include <boost/container/detail/type_traits.hpp>
Chris@16 48 #include <boost/detail/no_exceptions_support.hpp>
Chris@16 49 #include <boost/type_traits/has_trivial_destructor.hpp>
Chris@16 50 #include <boost/aligned_storage.hpp>
Chris@16 51
Chris@16 52 namespace boost {
Chris@16 53 namespace container {
Chris@16 54
Chris@16 55 /// @cond
Chris@16 56 namespace container_detail {
Chris@16 57 // ------------------------------------------------------------
Chris@16 58 // Class basic_string_base.
Chris@16 59
Chris@16 60 // basic_string_base is a helper class that makes it it easier to write
Chris@16 61 // an exception-safe version of basic_string. The constructor allocates,
Chris@16 62 // but does not initialize, a block of memory. The destructor
Chris@16 63 // deallocates, but does not destroy elements within, a block of
Chris@16 64 // memory. The destructor assumes that the memory either is the internal buffer,
Chris@16 65 // or else points to a block of memory that was allocated using string_base's
Chris@16 66 // allocator and whose size is this->m_storage.
Chris@16 67 template <class Allocator>
Chris@16 68 class basic_string_base
Chris@16 69 {
Chris@16 70 BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_string_base)
Chris@16 71
Chris@16 72 typedef allocator_traits<Allocator> allocator_traits_type;
Chris@16 73 public:
Chris@16 74 typedef Allocator allocator_type;
Chris@16 75 typedef allocator_type stored_allocator_type;
Chris@16 76 typedef typename allocator_traits_type::pointer pointer;
Chris@16 77 typedef typename allocator_traits_type::value_type value_type;
Chris@16 78 typedef typename allocator_traits_type::size_type size_type;
Chris@16 79 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
Chris@16 80
Chris@16 81 basic_string_base()
Chris@16 82 : members_()
Chris@16 83 { init(); }
Chris@16 84
Chris@16 85 basic_string_base(const allocator_type& a)
Chris@16 86 : members_(a)
Chris@16 87 { init(); }
Chris@16 88
Chris@16 89 basic_string_base(const allocator_type& a, size_type n)
Chris@16 90 : members_(a)
Chris@16 91 {
Chris@16 92 this->init();
Chris@16 93 this->allocate_initial_block(n);
Chris@16 94 }
Chris@16 95
Chris@16 96 basic_string_base(BOOST_RV_REF(basic_string_base) b)
Chris@16 97 : members_(boost::move(b.alloc()))
Chris@16 98 {
Chris@16 99 this->init();
Chris@16 100 this->swap_data(b);
Chris@16 101 }
Chris@16 102
Chris@16 103 ~basic_string_base()
Chris@16 104 {
Chris@16 105 if(!this->is_short()){
Chris@16 106 this->deallocate_block();
Chris@16 107 this->is_short(true);
Chris@16 108 }
Chris@16 109 }
Chris@16 110
Chris@16 111 private:
Chris@16 112
Chris@16 113 //This is the structure controlling a long string
Chris@16 114 struct long_t
Chris@16 115 {
Chris@16 116 size_type is_short : 1;
Chris@16 117 size_type length : (sizeof(size_type)*CHAR_BIT - 1);
Chris@16 118 size_type storage;
Chris@16 119 pointer start;
Chris@16 120
Chris@16 121 long_t()
Chris@16 122 {}
Chris@16 123
Chris@16 124 long_t(const long_t &other)
Chris@16 125 {
Chris@16 126 this->is_short = other.is_short;
Chris@16 127 length = other.length;
Chris@16 128 storage = other.storage;
Chris@16 129 start = other.start;
Chris@16 130 }
Chris@16 131
Chris@16 132 long_t &operator =(const long_t &other)
Chris@16 133 {
Chris@16 134 this->is_short = other.is_short;
Chris@16 135 length = other.length;
Chris@16 136 storage = other.storage;
Chris@16 137 start = other.start;
Chris@16 138 return *this;
Chris@16 139 }
Chris@16 140 };
Chris@16 141
Chris@16 142 //This type is the first part of the structure controlling a short string
Chris@16 143 //The "data" member stores
Chris@16 144 struct short_header
Chris@16 145 {
Chris@16 146 unsigned char is_short : 1;
Chris@16 147 unsigned char length : (CHAR_BIT - 1);
Chris@16 148 };
Chris@16 149
Chris@16 150 //This type has the same alignment and size as long_t but it's POD
Chris@16 151 //so, unlike long_t, it can be placed in a union
Chris@16 152
Chris@16 153 typedef typename boost::aligned_storage< sizeof(long_t),
Chris@16 154 container_detail::alignment_of<long_t>::value>::type long_raw_t;
Chris@16 155
Chris@16 156 protected:
Chris@16 157 static const size_type MinInternalBufferChars = 8;
Chris@16 158 static const size_type AlignmentOfValueType =
Chris@16 159 alignment_of<value_type>::value;
Chris@16 160 static const size_type ShortDataOffset =
Chris@16 161 container_detail::ct_rounded_size<sizeof(short_header), AlignmentOfValueType>::value;
Chris@16 162 static const size_type ZeroCostInternalBufferChars =
Chris@16 163 (sizeof(long_t) - ShortDataOffset)/sizeof(value_type);
Chris@16 164 static const size_type UnalignedFinalInternalBufferChars =
Chris@16 165 (ZeroCostInternalBufferChars > MinInternalBufferChars) ?
Chris@16 166 ZeroCostInternalBufferChars : MinInternalBufferChars;
Chris@16 167
Chris@16 168 struct short_t
Chris@16 169 {
Chris@16 170 short_header h;
Chris@16 171 value_type data[UnalignedFinalInternalBufferChars];
Chris@16 172 };
Chris@16 173
Chris@16 174 union repr_t
Chris@16 175 {
Chris@16 176 long_raw_t r;
Chris@16 177 short_t s;
Chris@16 178
Chris@16 179 const short_t &short_repr() const
Chris@16 180 { return s; }
Chris@16 181
Chris@16 182 const long_t &long_repr() const
Chris@16 183 { return *static_cast<const long_t*>(static_cast<const void*>(&r)); }
Chris@16 184
Chris@16 185 short_t &short_repr()
Chris@16 186 { return s; }
Chris@16 187
Chris@16 188 long_t &long_repr()
Chris@16 189 { return *static_cast<long_t*>(static_cast<void*>(&r)); }
Chris@16 190 };
Chris@16 191
Chris@16 192 struct members_holder
Chris@16 193 : public Allocator
Chris@16 194 {
Chris@16 195 members_holder()
Chris@16 196 : Allocator()
Chris@16 197 {}
Chris@16 198
Chris@16 199 template<class AllocatorConvertible>
Chris@16 200 explicit members_holder(BOOST_FWD_REF(AllocatorConvertible) a)
Chris@16 201 : Allocator(boost::forward<AllocatorConvertible>(a))
Chris@16 202 {}
Chris@16 203
Chris@16 204 repr_t m_repr;
Chris@16 205 } members_;
Chris@16 206
Chris@16 207 const Allocator &alloc() const
Chris@16 208 { return members_; }
Chris@16 209
Chris@16 210 Allocator &alloc()
Chris@16 211 { return members_; }
Chris@16 212
Chris@16 213 static const size_type InternalBufferChars = (sizeof(repr_t) - ShortDataOffset)/sizeof(value_type);
Chris@16 214
Chris@16 215 private:
Chris@16 216
Chris@16 217 static const size_type MinAllocation = InternalBufferChars*2;
Chris@16 218
Chris@16 219 protected:
Chris@16 220 bool is_short() const
Chris@16 221 { return static_cast<bool>(this->members_.m_repr.s.h.is_short != 0); }
Chris@16 222
Chris@16 223 void is_short(bool yes)
Chris@16 224 {
Chris@16 225 const bool was_short = this->is_short();
Chris@16 226 if(yes && !was_short){
Chris@16 227 allocator_traits_type::destroy
Chris@16 228 ( this->alloc()
Chris@16 229 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
Chris@16 230 );
Chris@16 231 this->members_.m_repr.s.h.is_short = true;
Chris@16 232 }
Chris@16 233 else if(!yes && was_short){
Chris@16 234 allocator_traits_type::construct
Chris@16 235 ( this->alloc()
Chris@16 236 , static_cast<long_t*>(static_cast<void*>(&this->members_.m_repr.r))
Chris@16 237 );
Chris@16 238 this->members_.m_repr.s.h.is_short = false;
Chris@16 239 }
Chris@16 240 }
Chris@16 241
Chris@16 242 private:
Chris@16 243 void init()
Chris@16 244 {
Chris@16 245 this->members_.m_repr.s.h.is_short = 1;
Chris@16 246 this->members_.m_repr.s.h.length = 0;
Chris@16 247 }
Chris@16 248
Chris@16 249 protected:
Chris@16 250
Chris@16 251 typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
Chris@16 252 typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
Chris@16 253 typedef container_detail::integral_constant<unsigned,
Chris@16 254 boost::container::container_detail::version<Allocator>::value> alloc_version;
Chris@16 255
Chris@16 256 std::pair<pointer, bool>
Chris@16 257 allocation_command(allocation_type command,
Chris@16 258 size_type limit_size,
Chris@16 259 size_type preferred_size,
Chris@16 260 size_type &received_size, pointer reuse = 0)
Chris@16 261 {
Chris@16 262 if(this->is_short() && (command & (expand_fwd | expand_bwd)) ){
Chris@16 263 reuse = pointer();
Chris@16 264 command &= ~(expand_fwd | expand_bwd);
Chris@16 265 }
Chris@16 266 return container_detail::allocator_version_traits<Allocator>::allocation_command
Chris@16 267 (this->alloc(), command, limit_size, preferred_size, received_size, reuse);
Chris@16 268 }
Chris@16 269
Chris@16 270 size_type next_capacity(size_type additional_objects) const
Chris@16 271 { return get_next_capacity(allocator_traits_type::max_size(this->alloc()), this->priv_storage(), additional_objects); }
Chris@16 272
Chris@16 273 void deallocate(pointer p, size_type n)
Chris@16 274 {
Chris@16 275 if (p && (n > InternalBufferChars))
Chris@16 276 this->alloc().deallocate(p, n);
Chris@16 277 }
Chris@16 278
Chris@16 279 void construct(pointer p, const value_type &value = value_type())
Chris@16 280 {
Chris@16 281 allocator_traits_type::construct
Chris@16 282 ( this->alloc()
Chris@16 283 , container_detail::to_raw_pointer(p)
Chris@16 284 , value
Chris@16 285 );
Chris@16 286 }
Chris@16 287
Chris@16 288 void destroy(pointer p, size_type n)
Chris@16 289 {
Chris@16 290 value_type *raw_p = container_detail::to_raw_pointer(p);
Chris@16 291 for(; n--; ++raw_p){
Chris@16 292 allocator_traits_type::destroy( this->alloc(), raw_p);
Chris@16 293 }
Chris@16 294 }
Chris@16 295
Chris@16 296 void destroy(pointer p)
Chris@16 297 {
Chris@16 298 allocator_traits_type::destroy
Chris@16 299 ( this->alloc()
Chris@16 300 , container_detail::to_raw_pointer(p)
Chris@16 301 );
Chris@16 302 }
Chris@16 303
Chris@16 304 void allocate_initial_block(size_type n)
Chris@16 305 {
Chris@16 306 if (n <= this->max_size()) {
Chris@16 307 if(n > InternalBufferChars){
Chris@16 308 size_type new_cap = this->next_capacity(n);
Chris@16 309 pointer p = this->allocation_command(allocate_new, n, new_cap, new_cap).first;
Chris@16 310 this->is_short(false);
Chris@16 311 this->priv_long_addr(p);
Chris@16 312 this->priv_long_size(0);
Chris@16 313 this->priv_storage(new_cap);
Chris@16 314 }
Chris@16 315 }
Chris@16 316 else{
Chris@16 317 throw_length_error("basic_string::allocate_initial_block max_size() exceeded");
Chris@16 318 }
Chris@16 319 }
Chris@16 320
Chris@16 321 void deallocate_block()
Chris@16 322 { this->deallocate(this->priv_addr(), this->priv_storage()); }
Chris@16 323
Chris@16 324 size_type max_size() const
Chris@16 325 { return allocator_traits_type::max_size(this->alloc()) - 1; }
Chris@16 326
Chris@16 327 protected:
Chris@16 328 size_type priv_capacity() const
Chris@16 329 { return this->priv_storage() - 1; }
Chris@16 330
Chris@16 331 pointer priv_short_addr() const
Chris@16 332 { return pointer_traits::pointer_to(const_cast<value_type&>(this->members_.m_repr.short_repr().data[0])); }
Chris@16 333
Chris@16 334 pointer priv_long_addr() const
Chris@16 335 { return this->members_.m_repr.long_repr().start; }
Chris@16 336
Chris@16 337 pointer priv_addr() const
Chris@16 338 {
Chris@16 339 return this->is_short()
Chris@16 340 ? priv_short_addr()
Chris@16 341 : priv_long_addr()
Chris@16 342 ;
Chris@16 343 }
Chris@16 344
Chris@16 345 pointer priv_end_addr() const
Chris@16 346 {
Chris@16 347 return this->is_short()
Chris@16 348 ? this->priv_short_addr() + this->priv_short_size()
Chris@16 349 : this->priv_long_addr() + this->priv_long_size()
Chris@16 350 ;
Chris@16 351 }
Chris@16 352
Chris@16 353 void priv_long_addr(pointer addr)
Chris@16 354 { this->members_.m_repr.long_repr().start = addr; }
Chris@16 355
Chris@16 356 size_type priv_storage() const
Chris@16 357 { return this->is_short() ? priv_short_storage() : priv_long_storage(); }
Chris@16 358
Chris@16 359 size_type priv_short_storage() const
Chris@16 360 { return InternalBufferChars; }
Chris@16 361
Chris@16 362 size_type priv_long_storage() const
Chris@16 363 { return this->members_.m_repr.long_repr().storage; }
Chris@16 364
Chris@16 365 void priv_storage(size_type storage)
Chris@16 366 {
Chris@16 367 if(!this->is_short())
Chris@16 368 this->priv_long_storage(storage);
Chris@16 369 }
Chris@16 370
Chris@16 371 void priv_long_storage(size_type storage)
Chris@16 372 {
Chris@16 373 this->members_.m_repr.long_repr().storage = storage;
Chris@16 374 }
Chris@16 375
Chris@16 376 size_type priv_size() const
Chris@16 377 { return this->is_short() ? this->priv_short_size() : this->priv_long_size(); }
Chris@16 378
Chris@16 379 size_type priv_short_size() const
Chris@16 380 { return this->members_.m_repr.short_repr().h.length; }
Chris@16 381
Chris@16 382 size_type priv_long_size() const
Chris@16 383 { return this->members_.m_repr.long_repr().length; }
Chris@16 384
Chris@16 385 void priv_size(size_type sz)
Chris@16 386 {
Chris@16 387 if(this->is_short())
Chris@16 388 this->priv_short_size(sz);
Chris@16 389 else
Chris@16 390 this->priv_long_size(sz);
Chris@16 391 }
Chris@16 392
Chris@16 393 void priv_short_size(size_type sz)
Chris@16 394 {
Chris@16 395 this->members_.m_repr.s.h.length = (unsigned char)sz;
Chris@16 396 }
Chris@16 397
Chris@16 398 void priv_long_size(size_type sz)
Chris@16 399 {
Chris@16 400 this->members_.m_repr.long_repr().length = sz;
Chris@16 401 }
Chris@16 402
Chris@16 403 void swap_data(basic_string_base& other)
Chris@16 404 {
Chris@16 405 if(this->is_short()){
Chris@16 406 if(other.is_short()){
Chris@16 407 std::swap(this->members_.m_repr, other.members_.m_repr);
Chris@16 408 }
Chris@16 409 else{
Chris@16 410 short_t short_backup(this->members_.m_repr.short_repr());
Chris@16 411 long_t long_backup (other.members_.m_repr.long_repr());
Chris@16 412 other.members_.m_repr.long_repr().~long_t();
Chris@16 413 ::new(&this->members_.m_repr.long_repr()) long_t;
Chris@16 414 this->members_.m_repr.long_repr() = long_backup;
Chris@16 415 other.members_.m_repr.short_repr() = short_backup;
Chris@16 416 }
Chris@16 417 }
Chris@16 418 else{
Chris@16 419 if(other.is_short()){
Chris@16 420 short_t short_backup(other.members_.m_repr.short_repr());
Chris@16 421 long_t long_backup (this->members_.m_repr.long_repr());
Chris@16 422 this->members_.m_repr.long_repr().~long_t();
Chris@16 423 ::new(&other.members_.m_repr.long_repr()) long_t;
Chris@16 424 other.members_.m_repr.long_repr() = long_backup;
Chris@16 425 this->members_.m_repr.short_repr() = short_backup;
Chris@16 426 }
Chris@16 427 else{
Chris@16 428 boost::container::swap_dispatch(this->members_.m_repr.long_repr(), other.members_.m_repr.long_repr());
Chris@16 429 }
Chris@16 430 }
Chris@16 431 }
Chris@16 432 };
Chris@16 433
Chris@16 434 } //namespace container_detail {
Chris@16 435
Chris@16 436 /// @endcond
Chris@16 437
Chris@16 438 //! The basic_string class represents a Sequence of characters. It contains all the
Chris@16 439 //! usual operations of a Sequence, and, additionally, it contains standard string
Chris@16 440 //! operations such as search and concatenation.
Chris@16 441 //!
Chris@16 442 //! The basic_string class is parameterized by character type, and by that type's
Chris@16 443 //! Character Traits.
Chris@16 444 //!
Chris@16 445 //! This class has performance characteristics very much like vector<>, meaning,
Chris@16 446 //! for example, that it does not perform reference-count or copy-on-write, and that
Chris@16 447 //! concatenation of two strings is an O(N) operation.
Chris@16 448 //!
Chris@16 449 //! Some of basic_string's member functions use an unusual method of specifying positions
Chris@16 450 //! and ranges. In addition to the conventional method using iterators, many of
Chris@16 451 //! basic_string's member functions use a single value pos of type size_type to represent a
Chris@16 452 //! position (in which case the position is begin() + pos, and many of basic_string's
Chris@16 453 //! member functions use two values, pos and n, to represent a range. In that case pos is
Chris@16 454 //! the beginning of the range and n is its size. That is, the range is
Chris@16 455 //! [begin() + pos, begin() + pos + n).
Chris@16 456 //!
Chris@16 457 //! Note that the C++ standard does not specify the complexity of basic_string operations.
Chris@16 458 //! In this implementation, basic_string has performance characteristics very similar to
Chris@16 459 //! those of vector: access to a single character is O(1), while copy and concatenation
Chris@16 460 //! are O(N).
Chris@16 461 //!
Chris@16 462 //! In this implementation, begin(),
Chris@16 463 //! end(), rbegin(), rend(), operator[], c_str(), and data() do not invalidate iterators.
Chris@16 464 //! In this implementation, iterators are only invalidated by member functions that
Chris@16 465 //! explicitly change the string's contents.
Chris@16 466 #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
Chris@16 467 template <class CharT, class Traits = std::char_traits<CharT>, class Allocator = std::allocator<CharT> >
Chris@16 468 #else
Chris@16 469 template <class CharT, class Traits, class Allocator>
Chris@16 470 #endif
Chris@16 471 class basic_string
Chris@16 472 : private container_detail::basic_string_base<Allocator>
Chris@16 473 {
Chris@16 474 /// @cond
Chris@16 475 private:
Chris@16 476 typedef allocator_traits<Allocator> allocator_traits_type;
Chris@16 477 BOOST_COPYABLE_AND_MOVABLE(basic_string)
Chris@16 478 typedef container_detail::basic_string_base<Allocator> base_t;
Chris@16 479 static const typename base_t::size_type InternalBufferChars = base_t::InternalBufferChars;
Chris@16 480
Chris@16 481 protected:
Chris@16 482 // Allocator helper class to use a char_traits as a function object.
Chris@16 483
Chris@16 484 template <class Tr>
Chris@16 485 struct Eq_traits
Chris@16 486 : public std::binary_function<typename Tr::char_type,
Chris@16 487 typename Tr::char_type,
Chris@16 488 bool>
Chris@16 489 {
Chris@16 490 bool operator()(const typename Tr::char_type& x,
Chris@16 491 const typename Tr::char_type& y) const
Chris@16 492 { return Tr::eq(x, y); }
Chris@16 493 };
Chris@16 494
Chris@16 495 template <class Tr>
Chris@16 496 struct Not_within_traits
Chris@16 497 : public std::unary_function<typename Tr::char_type, bool>
Chris@16 498 {
Chris@16 499 typedef const typename Tr::char_type* Pointer;
Chris@16 500 const Pointer m_first;
Chris@16 501 const Pointer m_last;
Chris@16 502
Chris@16 503 Not_within_traits(Pointer f, Pointer l)
Chris@16 504 : m_first(f), m_last(l) {}
Chris@16 505
Chris@16 506 bool operator()(const typename Tr::char_type& x) const
Chris@16 507 {
Chris@16 508 return std::find_if(m_first, m_last,
Chris@16 509 std::bind1st(Eq_traits<Tr>(), x)) == m_last;
Chris@16 510 }
Chris@16 511 };
Chris@16 512 /// @endcond
Chris@16 513
Chris@16 514 public:
Chris@16 515 //////////////////////////////////////////////
Chris@16 516 //
Chris@16 517 // types
Chris@16 518 //
Chris@16 519 //////////////////////////////////////////////
Chris@16 520 typedef Traits traits_type;
Chris@16 521 typedef CharT value_type;
Chris@16 522 typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
Chris@16 523 typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
Chris@16 524 typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
Chris@16 525 typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
Chris@16 526 typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
Chris@16 527 typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
Chris@16 528 typedef Allocator allocator_type;
Chris@16 529 typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
Chris@16 530 typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
Chris@16 531 typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
Chris@16 532 typedef BOOST_CONTAINER_IMPDEF(std::reverse_iterator<iterator>) reverse_iterator;
Chris@16 533 typedef BOOST_CONTAINER_IMPDEF(std::reverse_iterator<const_iterator>) const_reverse_iterator;
Chris@16 534 static const size_type npos = size_type(-1);
Chris@16 535
Chris@16 536 /// @cond
Chris@16 537 private:
Chris@16 538 typedef constant_iterator<CharT, difference_type> cvalue_iterator;
Chris@16 539 typedef typename base_t::allocator_v1 allocator_v1;
Chris@16 540 typedef typename base_t::allocator_v2 allocator_v2;
Chris@16 541 typedef typename base_t::alloc_version alloc_version;
Chris@16 542 typedef ::boost::intrusive::pointer_traits<pointer> pointer_traits;
Chris@16 543 /// @endcond
Chris@16 544
Chris@16 545 public: // Constructor, destructor, assignment.
Chris@16 546 //////////////////////////////////////////////
Chris@16 547 //
Chris@16 548 // construct/copy/destroy
Chris@16 549 //
Chris@16 550 //////////////////////////////////////////////
Chris@16 551 /// @cond
Chris@16 552 struct reserve_t {};
Chris@16 553
Chris@16 554 basic_string(reserve_t, size_type n,
Chris@16 555 const allocator_type& a = allocator_type())
Chris@16 556 //Select allocator as in copy constructor as reserve_t-based constructors
Chris@16 557 //are two step copies optimized for capacity
Chris@16 558 : base_t( allocator_traits_type::select_on_container_copy_construction(a)
Chris@16 559 , n + 1)
Chris@16 560 { this->priv_terminate_string(); }
Chris@16 561
Chris@16 562 /// @endcond
Chris@16 563
Chris@16 564 //! <b>Effects</b>: Default constructs a basic_string.
Chris@16 565 //!
Chris@16 566 //! <b>Throws</b>: If allocator_type's default constructor throws.
Chris@16 567 basic_string()
Chris@16 568 : base_t()
Chris@16 569 { this->priv_terminate_string(); }
Chris@16 570
Chris@16 571
Chris@16 572 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter.
Chris@16 573 //!
Chris@16 574 //! <b>Throws</b>: Nothing
Chris@16 575 explicit basic_string(const allocator_type& a) BOOST_CONTAINER_NOEXCEPT
Chris@16 576 : base_t(a)
Chris@16 577 { this->priv_terminate_string(); }
Chris@16 578
Chris@16 579 //! <b>Effects</b>: Copy constructs a basic_string.
Chris@16 580 //!
Chris@16 581 //! <b>Postcondition</b>: x == *this.
Chris@16 582 //!
Chris@16 583 //! <b>Throws</b>: If allocator_type's default constructor throws.
Chris@16 584 basic_string(const basic_string& s)
Chris@16 585 : base_t(allocator_traits_type::select_on_container_copy_construction(s.alloc()))
Chris@16 586 {
Chris@16 587 this->priv_terminate_string();
Chris@16 588 this->assign(s.begin(), s.end());
Chris@16 589 }
Chris@16 590
Chris@16 591 //! <b>Effects</b>: Move constructor. Moves s's resources to *this.
Chris@16 592 //!
Chris@16 593 //! <b>Throws</b>: Nothing.
Chris@16 594 //!
Chris@16 595 //! <b>Complexity</b>: Constant.
Chris@16 596 basic_string(BOOST_RV_REF(basic_string) s) BOOST_CONTAINER_NOEXCEPT
Chris@16 597 : base_t(boost::move((base_t&)s))
Chris@16 598 {}
Chris@16 599
Chris@16 600 //! <b>Effects</b>: Copy constructs a basic_string using the specified allocator.
Chris@16 601 //!
Chris@16 602 //! <b>Postcondition</b>: x == *this.
Chris@16 603 //!
Chris@16 604 //! <b>Throws</b>: If allocation throws.
Chris@16 605 basic_string(const basic_string& s, const allocator_type &a)
Chris@16 606 : base_t(a)
Chris@16 607 {
Chris@16 608 this->priv_terminate_string();
Chris@16 609 this->assign(s.begin(), s.end());
Chris@16 610 }
Chris@16 611
Chris@16 612 //! <b>Effects</b>: Move constructor using the specified allocator.
Chris@16 613 //! Moves s's resources to *this.
Chris@16 614 //!
Chris@16 615 //! <b>Throws</b>: If allocation throws.
Chris@16 616 //!
Chris@16 617 //! <b>Complexity</b>: Constant if a == s.get_allocator(), linear otherwise.
Chris@16 618 basic_string(BOOST_RV_REF(basic_string) s, const allocator_type &a)
Chris@16 619 : base_t(a)
Chris@16 620 {
Chris@16 621 this->priv_terminate_string();
Chris@16 622 if(a == this->alloc()){
Chris@16 623 this->swap_data(s);
Chris@16 624 }
Chris@16 625 else{
Chris@16 626 this->assign(s.begin(), s.end());
Chris@16 627 }
Chris@16 628 }
Chris@16 629
Chris@16 630 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Chris@16 631 //! and is initialized by a specific number of characters of the s string.
Chris@16 632 basic_string(const basic_string& s, size_type pos, size_type n = npos,
Chris@16 633 const allocator_type& a = allocator_type())
Chris@16 634 : base_t(a)
Chris@16 635 {
Chris@16 636 this->priv_terminate_string();
Chris@16 637 if (pos > s.size())
Chris@16 638 throw_out_of_range("basic_string::basic_string out of range position");
Chris@16 639 else
Chris@16 640 this->assign
Chris@16 641 (s.begin() + pos, s.begin() + pos + container_detail::min_value(n, s.size() - pos));
Chris@16 642 }
Chris@16 643
Chris@16 644 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Chris@16 645 //! and is initialized by a specific number of characters of the s c-string.
Chris@16 646 basic_string(const CharT* s, size_type n, const allocator_type& a = allocator_type())
Chris@16 647 : base_t(a)
Chris@16 648 {
Chris@16 649 this->priv_terminate_string();
Chris@16 650 this->assign(s, s + n);
Chris@16 651 }
Chris@16 652
Chris@16 653 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Chris@16 654 //! and is initialized by the null-terminated s c-string.
Chris@16 655 basic_string(const CharT* s, const allocator_type& a = allocator_type())
Chris@16 656 : base_t(a)
Chris@16 657 {
Chris@16 658 this->priv_terminate_string();
Chris@16 659 this->assign(s, s + Traits::length(s));
Chris@16 660 }
Chris@16 661
Chris@16 662 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Chris@16 663 //! and is initialized by n copies of c.
Chris@16 664 basic_string(size_type n, CharT c, const allocator_type& a = allocator_type())
Chris@16 665 : base_t(a)
Chris@16 666 {
Chris@16 667 this->priv_terminate_string();
Chris@16 668 this->assign(n, c);
Chris@16 669 }
Chris@16 670
Chris@16 671 //! <b>Effects</b>: Constructs a basic_string taking the allocator as parameter,
Chris@16 672 //! and a range of iterators.
Chris@16 673 template <class InputIterator>
Chris@16 674 basic_string(InputIterator f, InputIterator l, const allocator_type& a = allocator_type())
Chris@16 675 : base_t(a)
Chris@16 676 {
Chris@16 677 this->priv_terminate_string();
Chris@16 678 this->assign(f, l);
Chris@16 679 }
Chris@16 680
Chris@16 681 //! <b>Effects</b>: Destroys the basic_string. All used memory is deallocated.
Chris@16 682 //!
Chris@16 683 //! <b>Throws</b>: Nothing.
Chris@16 684 //!
Chris@16 685 //! <b>Complexity</b>: Constant.
Chris@16 686 ~basic_string() BOOST_CONTAINER_NOEXCEPT
Chris@16 687 {}
Chris@16 688
Chris@16 689 //! <b>Effects</b>: Copy constructs a string.
Chris@16 690 //!
Chris@16 691 //! <b>Postcondition</b>: x == *this.
Chris@16 692 //!
Chris@16 693 //! <b>Complexity</b>: Linear to the elements x contains.
Chris@16 694 basic_string& operator=(BOOST_COPY_ASSIGN_REF(basic_string) x)
Chris@16 695 {
Chris@16 696 if (&x != this){
Chris@16 697 allocator_type &this_alloc = this->alloc();
Chris@16 698 const allocator_type &x_alloc = x.alloc();
Chris@16 699 container_detail::bool_<allocator_traits_type::
Chris@16 700 propagate_on_container_copy_assignment::value> flag;
Chris@16 701 if(flag && this_alloc != x_alloc){
Chris@16 702 if(!this->is_short()){
Chris@16 703 this->deallocate_block();
Chris@16 704 this->is_short(true);
Chris@16 705 Traits::assign(*this->priv_addr(), CharT(0));
Chris@16 706 this->priv_short_size(0);
Chris@16 707 }
Chris@16 708 }
Chris@16 709 container_detail::assign_alloc(this->alloc(), x.alloc(), flag);
Chris@16 710 this->assign(x.begin(), x.end());
Chris@16 711 }
Chris@16 712 return *this;
Chris@16 713 }
Chris@16 714
Chris@16 715 //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.
Chris@16 716 //!
Chris@16 717 //! <b>Throws</b>: If allocator_type's copy constructor throws.
Chris@16 718 //!
Chris@16 719 //! <b>Complexity</b>: Constant.
Chris@16 720 basic_string& operator=(BOOST_RV_REF(basic_string) x) BOOST_CONTAINER_NOEXCEPT
Chris@16 721 {
Chris@16 722 if (&x != this){
Chris@16 723 allocator_type &this_alloc = this->alloc();
Chris@16 724 allocator_type &x_alloc = x.alloc();
Chris@16 725 //If allocators are equal we can just swap pointers
Chris@16 726 if(this_alloc == x_alloc){
Chris@16 727 //Destroy objects but retain memory in case x reuses it in the future
Chris@16 728 this->clear();
Chris@16 729 this->swap_data(x);
Chris@16 730 //Move allocator if needed
Chris@16 731 container_detail::bool_<allocator_traits_type::
Chris@16 732 propagate_on_container_move_assignment::value> flag;
Chris@16 733 container_detail::move_alloc(this_alloc, x_alloc, flag);
Chris@16 734 }
Chris@16 735 //If unequal allocators, then do a one by one move
Chris@16 736 else{
Chris@16 737 this->assign( x.begin(), x.end());
Chris@16 738 }
Chris@16 739 }
Chris@16 740 return *this;
Chris@16 741 }
Chris@16 742
Chris@16 743 //! <b>Effects</b>: Assignment from a null-terminated c-string.
Chris@16 744 basic_string& operator=(const CharT* s)
Chris@16 745 { return this->assign(s, s + Traits::length(s)); }
Chris@16 746
Chris@16 747 //! <b>Effects</b>: Assignment from character.
Chris@16 748 basic_string& operator=(CharT c)
Chris@16 749 { return this->assign(static_cast<size_type>(1), c); }
Chris@16 750
Chris@16 751 //! <b>Effects</b>: Returns a copy of the internal allocator.
Chris@16 752 //!
Chris@16 753 //! <b>Throws</b>: If allocator's copy constructor throws.
Chris@16 754 //!
Chris@16 755 //! <b>Complexity</b>: Constant.
Chris@16 756 allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
Chris@16 757 { return this->alloc(); }
Chris@16 758
Chris@16 759 //! <b>Effects</b>: Returns a reference to the internal allocator.
Chris@16 760 //!
Chris@16 761 //! <b>Throws</b>: Nothing
Chris@16 762 //!
Chris@16 763 //! <b>Complexity</b>: Constant.
Chris@16 764 //!
Chris@16 765 //! <b>Note</b>: Non-standard extension.
Chris@16 766 stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
Chris@16 767 { return this->alloc(); }
Chris@16 768
Chris@16 769 //! <b>Effects</b>: Returns a reference to the internal allocator.
Chris@16 770 //!
Chris@16 771 //! <b>Throws</b>: Nothing
Chris@16 772 //!
Chris@16 773 //! <b>Complexity</b>: Constant.
Chris@16 774 //!
Chris@16 775 //! <b>Note</b>: Non-standard extension.
Chris@16 776 const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
Chris@16 777 { return this->alloc(); }
Chris@16 778
Chris@16 779 //////////////////////////////////////////////
Chris@16 780 //
Chris@16 781 // iterators
Chris@16 782 //
Chris@16 783 //////////////////////////////////////////////
Chris@16 784
Chris@16 785 //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
Chris@16 786 //!
Chris@16 787 //! <b>Throws</b>: Nothing.
Chris@16 788 //!
Chris@16 789 //! <b>Complexity</b>: Constant.
Chris@16 790 iterator begin() BOOST_CONTAINER_NOEXCEPT
Chris@16 791 { return this->priv_addr(); }
Chris@16 792
Chris@16 793 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
Chris@16 794 //!
Chris@16 795 //! <b>Throws</b>: Nothing.
Chris@16 796 //!
Chris@16 797 //! <b>Complexity</b>: Constant.
Chris@16 798 const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
Chris@16 799 { return this->priv_addr(); }
Chris@16 800
Chris@16 801 //! <b>Effects</b>: Returns an iterator to the end of the vector.
Chris@16 802 //!
Chris@16 803 //! <b>Throws</b>: Nothing.
Chris@16 804 //!
Chris@16 805 //! <b>Complexity</b>: Constant.
Chris@16 806 iterator end() BOOST_CONTAINER_NOEXCEPT
Chris@16 807 { return this->priv_end_addr(); }
Chris@16 808
Chris@16 809 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
Chris@16 810 //!
Chris@16 811 //! <b>Throws</b>: Nothing.
Chris@16 812 //!
Chris@16 813 //! <b>Complexity</b>: Constant.
Chris@16 814 const_iterator end() const BOOST_CONTAINER_NOEXCEPT
Chris@16 815 { return this->priv_end_addr(); }
Chris@16 816
Chris@16 817 //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
Chris@16 818 //! of the reversed vector.
Chris@16 819 //!
Chris@16 820 //! <b>Throws</b>: Nothing.
Chris@16 821 //!
Chris@16 822 //! <b>Complexity</b>: Constant.
Chris@16 823 reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
Chris@16 824 { return reverse_iterator(this->priv_end_addr()); }
Chris@16 825
Chris@16 826 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
Chris@16 827 //! of the reversed vector.
Chris@16 828 //!
Chris@16 829 //! <b>Throws</b>: Nothing.
Chris@16 830 //!
Chris@16 831 //! <b>Complexity</b>: Constant.
Chris@16 832 const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
Chris@16 833 { return this->crbegin(); }
Chris@16 834
Chris@16 835 //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
Chris@16 836 //! of the reversed vector.
Chris@16 837 //!
Chris@16 838 //! <b>Throws</b>: Nothing.
Chris@16 839 //!
Chris@16 840 //! <b>Complexity</b>: Constant.
Chris@16 841 reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
Chris@16 842 { return reverse_iterator(this->priv_addr()); }
Chris@16 843
Chris@16 844 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
Chris@16 845 //! of the reversed vector.
Chris@16 846 //!
Chris@16 847 //! <b>Throws</b>: Nothing.
Chris@16 848 //!
Chris@16 849 //! <b>Complexity</b>: Constant.
Chris@16 850 const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
Chris@16 851 { return this->crend(); }
Chris@16 852
Chris@16 853 //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
Chris@16 854 //!
Chris@16 855 //! <b>Throws</b>: Nothing.
Chris@16 856 //!
Chris@16 857 //! <b>Complexity</b>: Constant.
Chris@16 858 const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
Chris@16 859 { return this->priv_addr(); }
Chris@16 860
Chris@16 861 //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
Chris@16 862 //!
Chris@16 863 //! <b>Throws</b>: Nothing.
Chris@16 864 //!
Chris@16 865 //! <b>Complexity</b>: Constant.
Chris@16 866 const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
Chris@16 867 { return this->priv_end_addr(); }
Chris@16 868
Chris@16 869 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
Chris@16 870 //! of the reversed vector.
Chris@16 871 //!
Chris@16 872 //! <b>Throws</b>: Nothing.
Chris@16 873 //!
Chris@16 874 //! <b>Complexity</b>: Constant.
Chris@16 875 const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
Chris@16 876 { return const_reverse_iterator(this->priv_end_addr()); }
Chris@16 877
Chris@16 878 //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
Chris@16 879 //! of the reversed vector.
Chris@16 880 //!
Chris@16 881 //! <b>Throws</b>: Nothing.
Chris@16 882 //!
Chris@16 883 //! <b>Complexity</b>: Constant.
Chris@16 884 const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
Chris@16 885 { return const_reverse_iterator(this->priv_addr()); }
Chris@16 886
Chris@16 887 //////////////////////////////////////////////
Chris@16 888 //
Chris@16 889 // capacity
Chris@16 890 //
Chris@16 891 //////////////////////////////////////////////
Chris@16 892
Chris@16 893 //! <b>Effects</b>: Returns true if the vector contains no elements.
Chris@16 894 //!
Chris@16 895 //! <b>Throws</b>: Nothing.
Chris@16 896 //!
Chris@16 897 //! <b>Complexity</b>: Constant.
Chris@16 898 bool empty() const BOOST_CONTAINER_NOEXCEPT
Chris@16 899 { return !this->priv_size(); }
Chris@16 900
Chris@16 901 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
Chris@16 902 //!
Chris@16 903 //! <b>Throws</b>: Nothing.
Chris@16 904 //!
Chris@16 905 //! <b>Complexity</b>: Constant.
Chris@16 906 size_type size() const BOOST_CONTAINER_NOEXCEPT
Chris@16 907 { return this->priv_size(); }
Chris@16 908
Chris@16 909 //! <b>Effects</b>: Returns the number of the elements contained in the vector.
Chris@16 910 //!
Chris@16 911 //! <b>Throws</b>: Nothing.
Chris@16 912 //!
Chris@16 913 //! <b>Complexity</b>: Constant.
Chris@16 914 size_type length() const BOOST_CONTAINER_NOEXCEPT
Chris@16 915 { return this->size(); }
Chris@16 916
Chris@16 917 //! <b>Effects</b>: Returns the largest possible size of the vector.
Chris@16 918 //!
Chris@16 919 //! <b>Throws</b>: Nothing.
Chris@16 920 //!
Chris@16 921 //! <b>Complexity</b>: Constant.
Chris@16 922 size_type max_size() const BOOST_CONTAINER_NOEXCEPT
Chris@16 923 { return base_t::max_size(); }
Chris@16 924
Chris@16 925 //! <b>Effects</b>: Inserts or erases elements at the end such that
Chris@16 926 //! the size becomes n. New elements are copy constructed from x.
Chris@16 927 //!
Chris@16 928 //! <b>Throws</b>: If memory allocation throws
Chris@16 929 //!
Chris@16 930 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
Chris@16 931 void resize(size_type n, CharT c)
Chris@16 932 {
Chris@16 933 if (n <= this->size())
Chris@16 934 this->erase(this->begin() + n, this->end());
Chris@16 935 else
Chris@16 936 this->append(n - this->size(), c);
Chris@16 937 }
Chris@16 938
Chris@16 939 //! <b>Effects</b>: Inserts or erases elements at the end such that
Chris@16 940 //! the size becomes n. New elements are value initialized.
Chris@16 941 //!
Chris@16 942 //! <b>Throws</b>: If memory allocation throws
Chris@16 943 //!
Chris@16 944 //! <b>Complexity</b>: Linear to the difference between size() and new_size.
Chris@16 945 void resize(size_type n)
Chris@16 946 { resize(n, CharT()); }
Chris@16 947
Chris@16 948 //! <b>Effects</b>: Number of elements for which memory has been allocated.
Chris@16 949 //! capacity() is always greater than or equal to size().
Chris@16 950 //!
Chris@16 951 //! <b>Throws</b>: Nothing.
Chris@16 952 //!
Chris@16 953 //! <b>Complexity</b>: Constant.
Chris@16 954 size_type capacity() const BOOST_CONTAINER_NOEXCEPT
Chris@16 955 { return this->priv_capacity(); }
Chris@16 956
Chris@16 957 //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
Chris@16 958 //! effect. Otherwise, it is a request for allocation of additional memory.
Chris@16 959 //! If the request is successful, then capacity() is greater than or equal to
Chris@16 960 //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
Chris@16 961 //!
Chris@16 962 //! <b>Throws</b>: If memory allocation allocation throws
Chris@16 963 void reserve(size_type res_arg)
Chris@16 964 {
Chris@16 965 if (res_arg > this->max_size()){
Chris@16 966 throw_length_error("basic_string::reserve max_size() exceeded");
Chris@16 967 }
Chris@16 968
Chris@16 969 if (this->capacity() < res_arg){
Chris@16 970 size_type n = container_detail::max_value(res_arg, this->size()) + 1;
Chris@16 971 size_type new_cap = this->next_capacity(n);
Chris@16 972 pointer new_start = this->allocation_command
Chris@16 973 (allocate_new, n, new_cap, new_cap).first;
Chris@16 974 size_type new_length = 0;
Chris@16 975
Chris@16 976 const pointer addr = this->priv_addr();
Chris@16 977 new_length += priv_uninitialized_copy
Chris@16 978 (addr, addr + this->priv_size(), new_start);
Chris@16 979 this->priv_construct_null(new_start + new_length);
Chris@16 980 this->deallocate_block();
Chris@16 981 this->is_short(false);
Chris@16 982 this->priv_long_addr(new_start);
Chris@16 983 this->priv_long_size(new_length);
Chris@16 984 this->priv_storage(new_cap);
Chris@16 985 }
Chris@16 986 }
Chris@16 987
Chris@16 988 //! <b>Effects</b>: Tries to deallocate the excess of memory created
Chris@16 989 //! with previous allocations. The size of the string is unchanged
Chris@16 990 //!
Chris@16 991 //! <b>Throws</b>: Nothing
Chris@16 992 //!
Chris@16 993 //! <b>Complexity</b>: Linear to size().
Chris@16 994 void shrink_to_fit()
Chris@16 995 {
Chris@16 996 //Check if shrinking is possible
Chris@16 997 if(this->priv_storage() > InternalBufferChars){
Chris@16 998 //Check if we should pass from dynamically allocated buffer
Chris@16 999 //to the internal storage
Chris@16 1000 if(this->priv_size() < InternalBufferChars){
Chris@16 1001 //Dynamically allocated buffer attributes
Chris@16 1002 pointer long_addr = this->priv_long_addr();
Chris@16 1003 size_type long_storage = this->priv_long_storage();
Chris@16 1004 size_type long_size = this->priv_long_size();
Chris@16 1005 //Shrink from allocated buffer to the internal one, including trailing null
Chris@16 1006 Traits::copy( container_detail::to_raw_pointer(this->priv_short_addr())
Chris@16 1007 , container_detail::to_raw_pointer(long_addr)
Chris@16 1008 , long_size+1);
Chris@16 1009 this->is_short(true);
Chris@16 1010 this->alloc().deallocate(long_addr, long_storage);
Chris@16 1011 }
Chris@16 1012 else{
Chris@16 1013 //Shrinking in dynamic buffer
Chris@16 1014 this->priv_shrink_to_fit_dynamic_buffer(alloc_version());
Chris@16 1015 }
Chris@16 1016 }
Chris@16 1017 }
Chris@16 1018
Chris@16 1019 //////////////////////////////////////////////
Chris@16 1020 //
Chris@16 1021 // element access
Chris@16 1022 //
Chris@16 1023 //////////////////////////////////////////////
Chris@16 1024
Chris@16 1025 //! <b>Requires</b>: size() > n.
Chris@16 1026 //!
Chris@16 1027 //! <b>Effects</b>: Returns a reference to the nth element
Chris@16 1028 //! from the beginning of the container.
Chris@16 1029 //!
Chris@16 1030 //! <b>Throws</b>: Nothing.
Chris@16 1031 //!
Chris@16 1032 //! <b>Complexity</b>: Constant.
Chris@16 1033 reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT
Chris@16 1034 { return *(this->priv_addr() + n); }
Chris@16 1035
Chris@16 1036 //! <b>Requires</b>: size() > n.
Chris@16 1037 //!
Chris@16 1038 //! <b>Effects</b>: Returns a const reference to the nth element
Chris@16 1039 //! from the beginning of the container.
Chris@16 1040 //!
Chris@16 1041 //! <b>Throws</b>: Nothing.
Chris@16 1042 //!
Chris@16 1043 //! <b>Complexity</b>: Constant.
Chris@16 1044 const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT
Chris@16 1045 { return *(this->priv_addr() + n); }
Chris@16 1046
Chris@16 1047 //! <b>Requires</b>: size() > n.
Chris@16 1048 //!
Chris@16 1049 //! <b>Effects</b>: Returns a reference to the nth element
Chris@16 1050 //! from the beginning of the container.
Chris@16 1051 //!
Chris@16 1052 //! <b>Throws</b>: std::range_error if n >= size()
Chris@16 1053 //!
Chris@16 1054 //! <b>Complexity</b>: Constant.
Chris@16 1055 reference at(size_type n)
Chris@16 1056 {
Chris@16 1057 if (n >= this->size())
Chris@16 1058 throw_out_of_range("basic_string::at invalid subscript");
Chris@16 1059 return *(this->priv_addr() + n);
Chris@16 1060 }
Chris@16 1061
Chris@16 1062 //! <b>Requires</b>: size() > n.
Chris@16 1063 //!
Chris@16 1064 //! <b>Effects</b>: Returns a const reference to the nth element
Chris@16 1065 //! from the beginning of the container.
Chris@16 1066 //!
Chris@16 1067 //! <b>Throws</b>: std::range_error if n >= size()
Chris@16 1068 //!
Chris@16 1069 //! <b>Complexity</b>: Constant.
Chris@16 1070 const_reference at(size_type n) const {
Chris@16 1071 if (n >= this->size())
Chris@16 1072 throw_out_of_range("basic_string::at invalid subscript");
Chris@16 1073 return *(this->priv_addr() + n);
Chris@16 1074 }
Chris@16 1075
Chris@16 1076 //////////////////////////////////////////////
Chris@16 1077 //
Chris@16 1078 // modifiers
Chris@16 1079 //
Chris@16 1080 //////////////////////////////////////////////
Chris@16 1081
Chris@16 1082 //! <b>Effects</b>: Calls append(str.data, str.size()).
Chris@16 1083 //!
Chris@16 1084 //! <b>Returns</b>: *this
Chris@16 1085 basic_string& operator+=(const basic_string& s)
Chris@16 1086 { return this->append(s); }
Chris@16 1087
Chris@16 1088 //! <b>Effects</b>: Calls append(s).
Chris@16 1089 //!
Chris@16 1090 //! <b>Returns</b>: *this
Chris@16 1091 basic_string& operator+=(const CharT* s)
Chris@16 1092 { return this->append(s); }
Chris@16 1093
Chris@16 1094 //! <b>Effects</b>: Calls append(1, c).
Chris@16 1095 //!
Chris@16 1096 //! <b>Returns</b>: *this
Chris@16 1097 basic_string& operator+=(CharT c)
Chris@16 1098 { this->push_back(c); return *this; }
Chris@16 1099
Chris@16 1100 //! <b>Effects</b>: Calls append(str.data(), str.size()).
Chris@16 1101 //!
Chris@16 1102 //! <b>Returns</b>: *this
Chris@16 1103 basic_string& append(const basic_string& s)
Chris@16 1104 { return this->append(s.begin(), s.end()); }
Chris@16 1105
Chris@16 1106 //! <b>Requires</b>: pos <= str.size()
Chris@16 1107 //!
Chris@16 1108 //! <b>Effects</b>: Determines the effective length rlen of the string to append
Chris@16 1109 //! as the smaller of n and str.size() - pos and calls append(str.data() + pos, rlen).
Chris@16 1110 //!
Chris@16 1111 //! <b>Throws</b>: If memory allocation throws and out_of_range if pos > str.size()
Chris@16 1112 //!
Chris@16 1113 //! <b>Returns</b>: *this
Chris@16 1114 basic_string& append(const basic_string& s, size_type pos, size_type n)
Chris@16 1115 {
Chris@16 1116 if (pos > s.size())
Chris@16 1117 throw_out_of_range("basic_string::append out of range position");
Chris@16 1118 return this->append(s.begin() + pos,
Chris@16 1119 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
Chris@16 1120 }
Chris@16 1121
Chris@16 1122 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 1123 //!
Chris@16 1124 //! <b>Effects</b>: The function replaces the string controlled by *this with
Chris@16 1125 //! a string of length size() + n whose irst size() elements are a copy of the
Chris@16 1126 //! original string controlled by *this and whose remaining
Chris@16 1127 //! elements are a copy of the initial n elements of s.
Chris@16 1128 //!
Chris@16 1129 //! <b>Throws</b>: If memory allocation throws length_error if size() + n > max_size().
Chris@16 1130 //!
Chris@16 1131 //! <b>Returns</b>: *this
Chris@16 1132 basic_string& append(const CharT* s, size_type n)
Chris@16 1133 { return this->append(s, s + n); }
Chris@16 1134
Chris@16 1135 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 1136 //!
Chris@16 1137 //! <b>Effects</b>: Calls append(s, traits::length(s)).
Chris@16 1138 //!
Chris@16 1139 //! <b>Returns</b>: *this
Chris@16 1140 basic_string& append(const CharT* s)
Chris@16 1141 { return this->append(s, s + Traits::length(s)); }
Chris@16 1142
Chris@16 1143 //! <b>Effects</b>: Equivalent to append(basic_string(n, c)).
Chris@16 1144 //!
Chris@16 1145 //! <b>Returns</b>: *this
Chris@16 1146 basic_string& append(size_type n, CharT c)
Chris@16 1147 { return this->append(cvalue_iterator(c, n), cvalue_iterator()); }
Chris@16 1148
Chris@16 1149 //! <b>Requires</b>: [first,last) is a valid range.
Chris@16 1150 //!
Chris@16 1151 //! <b>Effects</b>: Equivalent to append(basic_string(first, last)).
Chris@16 1152 //!
Chris@16 1153 //! <b>Returns</b>: *this
Chris@16 1154 template <class InputIter>
Chris@16 1155 basic_string& append(InputIter first, InputIter last)
Chris@16 1156 { this->insert(this->end(), first, last); return *this; }
Chris@16 1157
Chris@16 1158 //! <b>Effects</b>: Equivalent to append(static_cast<size_type>(1), c).
Chris@16 1159 void push_back(CharT c)
Chris@16 1160 {
Chris@16 1161 const size_type old_size = this->priv_size();
Chris@16 1162 if (old_size < this->capacity()){
Chris@16 1163 const pointer addr = this->priv_addr();
Chris@16 1164 this->priv_construct_null(addr + old_size + 1);
Chris@16 1165 Traits::assign(addr[old_size], c);
Chris@16 1166 this->priv_size(old_size+1);
Chris@16 1167 }
Chris@16 1168 else{
Chris@16 1169 //No enough memory, insert a new object at the end
Chris@16 1170 this->append(size_type(1), c);
Chris@16 1171 }
Chris@16 1172 }
Chris@16 1173
Chris@16 1174 //! <b>Effects</b>: Equivalent to assign(str, 0, npos).
Chris@16 1175 //!
Chris@16 1176 //! <b>Returns</b>: *this
Chris@16 1177 basic_string& assign(const basic_string& s)
Chris@16 1178 { return this->operator=(s); }
Chris@16 1179
Chris@16 1180 //! <b>Effects</b>: The function replaces the string controlled by *this
Chris@16 1181 //! with a string of length str.size() whose elements are a copy of the string
Chris@16 1182 //! controlled by str. Leaves str in a valid but unspecified state.
Chris@16 1183 //!
Chris@16 1184 //! <b>Throws</b>: Nothing
Chris@16 1185 //!
Chris@16 1186 //! <b>Returns</b>: *this
Chris@16 1187 basic_string& assign(BOOST_RV_REF(basic_string) ms) BOOST_CONTAINER_NOEXCEPT
Chris@16 1188 { return this->swap_data(ms), *this; }
Chris@16 1189
Chris@16 1190 //! <b>Requires</b>: pos <= str.size()
Chris@16 1191 //!
Chris@16 1192 //! <b>Effects</b>: Determines the effective length rlen of the string to assign as
Chris@16 1193 //! the smaller of n and str.size() - pos and calls assign(str.data() + pos rlen).
Chris@16 1194 //!
Chris@16 1195 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > str.size().
Chris@16 1196 //!
Chris@16 1197 //! <b>Returns</b>: *this
Chris@16 1198 basic_string& assign(const basic_string& s, size_type pos, size_type n)
Chris@16 1199 {
Chris@16 1200 if (pos > s.size())
Chris@16 1201 throw_out_of_range("basic_string::assign out of range position");
Chris@16 1202 return this->assign(s.begin() + pos,
Chris@16 1203 s.begin() + pos + container_detail::min_value(n, s.size() - pos));
Chris@16 1204 }
Chris@16 1205
Chris@16 1206 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 1207 //!
Chris@16 1208 //! <b>Effects</b>: Replaces the string controlled by *this with a string of
Chris@16 1209 //! length n whose elements are a copy of those pointed to by s.
Chris@16 1210 //!
Chris@16 1211 //! <b>Throws</b>: If memory allocation throws or length_error if n > max_size().
Chris@16 1212 //!
Chris@16 1213 //! <b>Returns</b>: *this
Chris@16 1214 basic_string& assign(const CharT* s, size_type n)
Chris@16 1215 { return this->assign(s, s + n); }
Chris@16 1216
Chris@16 1217 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 1218 //!
Chris@16 1219 //! <b>Effects</b>: Calls assign(s, traits::length(s)).
Chris@16 1220 //!
Chris@16 1221 //! <b>Returns</b>: *this
Chris@16 1222 basic_string& assign(const CharT* s)
Chris@16 1223 { return this->assign(s, s + Traits::length(s)); }
Chris@16 1224
Chris@16 1225 //! <b>Effects</b>: Equivalent to assign(basic_string(n, c)).
Chris@16 1226 //!
Chris@16 1227 //! <b>Returns</b>: *this
Chris@16 1228 basic_string& assign(size_type n, CharT c)
Chris@16 1229 { return this->assign(cvalue_iterator(c, n), cvalue_iterator()); }
Chris@16 1230
Chris@16 1231 //! <b>Effects</b>: Equivalent to assign(basic_string(first, last)).
Chris@16 1232 //!
Chris@16 1233 //! <b>Returns</b>: *this
Chris@16 1234 template <class InputIter>
Chris@16 1235 basic_string& assign(InputIter first, InputIter last
Chris@16 1236 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
Chris@16 1237 , typename container_detail::enable_if_c
Chris@16 1238 < !container_detail::is_convertible<InputIter, size_type>::value
Chris@16 1239 >::type * = 0
Chris@16 1240 #endif
Chris@16 1241 )
Chris@16 1242 {
Chris@16 1243 size_type cur = 0;
Chris@16 1244 const pointer addr = this->priv_addr();
Chris@16 1245 CharT *ptr = container_detail::to_raw_pointer(addr);
Chris@16 1246 const size_type old_size = this->priv_size();
Chris@16 1247 while (first != last && cur != old_size) {
Chris@16 1248 Traits::assign(*ptr, *first);
Chris@16 1249 ++first;
Chris@16 1250 ++cur;
Chris@16 1251 ++ptr;
Chris@16 1252 }
Chris@16 1253 if (first == last)
Chris@16 1254 this->erase(addr + cur, addr + old_size);
Chris@16 1255 else
Chris@16 1256 this->append(first, last);
Chris@16 1257 return *this;
Chris@16 1258 }
Chris@16 1259
Chris@16 1260 //! <b>Requires</b>: pos <= size().
Chris@16 1261 //!
Chris@16 1262 //! <b>Effects</b>: Calls insert(pos, str.data(), str.size()).
Chris@16 1263 //!
Chris@16 1264 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
Chris@16 1265 //!
Chris@16 1266 //! <b>Returns</b>: *this
Chris@16 1267 basic_string& insert(size_type pos, const basic_string& s)
Chris@16 1268 {
Chris@16 1269 const size_type sz = this->size();
Chris@16 1270 if (pos > sz)
Chris@16 1271 throw_out_of_range("basic_string::insert out of range position");
Chris@16 1272 if (sz > this->max_size() - s.size())
Chris@16 1273 throw_length_error("basic_string::insert max_size() exceeded");
Chris@16 1274 this->insert(this->priv_addr() + pos, s.begin(), s.end());
Chris@16 1275 return *this;
Chris@16 1276 }
Chris@16 1277
Chris@16 1278 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
Chris@16 1279 //!
Chris@16 1280 //! <b>Effects</b>: Determines the effective length rlen of the string to insert as
Chris@16 1281 //! the smaller of n and str.size() - pos2 and calls insert(pos1, str.data() + pos2, rlen).
Chris@16 1282 //!
Chris@16 1283 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos1 > size() or pos2 > str.size().
Chris@16 1284 //!
Chris@16 1285 //! <b>Returns</b>: *this
Chris@16 1286 basic_string& insert(size_type pos1, const basic_string& s, size_type pos2, size_type n)
Chris@16 1287 {
Chris@16 1288 const size_type sz = this->size();
Chris@16 1289 const size_type str_size = s.size();
Chris@16 1290 if (pos1 > sz || pos2 > str_size)
Chris@16 1291 throw_out_of_range("basic_string::insert out of range position");
Chris@16 1292 size_type len = container_detail::min_value(n, str_size - pos2);
Chris@16 1293 if (sz > this->max_size() - len)
Chris@16 1294 throw_length_error("basic_string::insert max_size() exceeded");
Chris@16 1295 const CharT *beg_ptr = container_detail::to_raw_pointer(s.begin()) + pos2;
Chris@16 1296 const CharT *end_ptr = beg_ptr + len;
Chris@16 1297 this->insert(this->priv_addr() + pos1, beg_ptr, end_ptr);
Chris@16 1298 return *this;
Chris@16 1299 }
Chris@16 1300
Chris@16 1301 //! <b>Requires</b>: s points to an array of at least n elements of CharT and pos <= size().
Chris@16 1302 //!
Chris@16 1303 //! <b>Effects</b>: Replaces the string controlled by *this with a string of length size() + n
Chris@16 1304 //! whose first pos elements are a copy of the initial elements of the original string
Chris@16 1305 //! controlled by *this and whose next n elements are a copy of the elements in s and whose
Chris@16 1306 //! remaining elements are a copy of the remaining elements of the original string controlled by *this.
Chris@16 1307 //!
Chris@16 1308 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size() or
Chris@16 1309 //! length_error if size() + n > max_size().
Chris@16 1310 //!
Chris@16 1311 //! <b>Returns</b>: *this
Chris@16 1312 basic_string& insert(size_type pos, const CharT* s, size_type n)
Chris@16 1313 {
Chris@16 1314 if (pos > this->size())
Chris@16 1315 throw_out_of_range("basic_string::insert out of range position");
Chris@16 1316 if (this->size() > this->max_size() - n)
Chris@16 1317 throw_length_error("basic_string::insert max_size() exceeded");
Chris@16 1318 this->insert(this->priv_addr() + pos, s, s + n);
Chris@16 1319 return *this;
Chris@16 1320 }
Chris@16 1321
Chris@16 1322 //! <b>Requires</b>: pos <= size() and s points to an array of at least traits::length(s) + 1 elements of CharT
Chris@16 1323 //!
Chris@16 1324 //! <b>Effects</b>: Calls insert(pos, s, traits::length(s)).
Chris@16 1325 //!
Chris@16 1326 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
Chris@16 1327 //! length_error if size() > max_size() - Traits::length(s)
Chris@16 1328 //!
Chris@16 1329 //! <b>Returns</b>: *this
Chris@16 1330 basic_string& insert(size_type pos, const CharT* s)
Chris@16 1331 {
Chris@16 1332 if (pos > this->size())
Chris@16 1333 throw_out_of_range("basic_string::insert out of range position");
Chris@16 1334 size_type len = Traits::length(s);
Chris@16 1335 if (this->size() > this->max_size() - len)
Chris@16 1336 throw_length_error("basic_string::insert max_size() exceeded");
Chris@16 1337 this->insert(this->priv_addr() + pos, s, s + len);
Chris@16 1338 return *this;
Chris@16 1339 }
Chris@16 1340
Chris@16 1341 //! <b>Effects</b>: Equivalent to insert(pos, basic_string(n, c)).
Chris@16 1342 //!
Chris@16 1343 //! <b>Throws</b>: If memory allocation throws, out_of_range if pos > size()
Chris@16 1344 //! length_error if size() > max_size() - n
Chris@16 1345 //!
Chris@16 1346 //! <b>Returns</b>: *this
Chris@16 1347 basic_string& insert(size_type pos, size_type n, CharT c)
Chris@16 1348 {
Chris@16 1349 if (pos > this->size())
Chris@16 1350 throw_out_of_range("basic_string::insert out of range position");
Chris@16 1351 if (this->size() > this->max_size() - n)
Chris@16 1352 throw_length_error("basic_string::insert max_size() exceeded");
Chris@16 1353 this->insert(const_iterator(this->priv_addr() + pos), n, c);
Chris@16 1354 return *this;
Chris@16 1355 }
Chris@16 1356
Chris@16 1357 //! <b>Requires</b>: p is a valid iterator on *this.
Chris@16 1358 //!
Chris@16 1359 //! <b>Effects</b>: inserts a copy of c before the character referred to by p.
Chris@16 1360 //!
Chris@16 1361 //! <b>Returns</b>: An iterator which refers to the copy of the inserted character.
Chris@16 1362 iterator insert(const_iterator p, CharT c)
Chris@16 1363 {
Chris@16 1364 size_type new_offset = p - this->priv_addr();
Chris@16 1365 this->insert(p, cvalue_iterator(c, 1), cvalue_iterator());
Chris@16 1366 return this->priv_addr() + new_offset;
Chris@16 1367 }
Chris@16 1368
Chris@16 1369
Chris@16 1370 //! <b>Requires</b>: p is a valid iterator on *this.
Chris@16 1371 //!
Chris@16 1372 //! <b>Effects</b>: Inserts n copies of c before the character referred to by p.
Chris@16 1373 //!
Chris@16 1374 //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
Chris@16 1375 iterator insert(const_iterator p, size_type n, CharT c)
Chris@16 1376 { return this->insert(p, cvalue_iterator(c, n), cvalue_iterator()); }
Chris@16 1377
Chris@16 1378 //! <b>Requires</b>: p is a valid iterator on *this. [first,last) is a valid range.
Chris@16 1379 //!
Chris@16 1380 //! <b>Effects</b>: Equivalent to insert(p - begin(), basic_string(first, last)).
Chris@16 1381 //!
Chris@16 1382 //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
Chris@16 1383 template <class InputIter>
Chris@16 1384 iterator insert(const_iterator p, InputIter first, InputIter last
Chris@16 1385 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
Chris@16 1386 , typename container_detail::enable_if_c
Chris@16 1387 < !container_detail::is_convertible<InputIter, size_type>::value
Chris@16 1388 && container_detail::is_input_iterator<InputIter>::value
Chris@16 1389 >::type * = 0
Chris@16 1390 #endif
Chris@16 1391 )
Chris@16 1392 {
Chris@16 1393 const size_type n_pos = p - this->cbegin();
Chris@16 1394 for ( ; first != last; ++first, ++p) {
Chris@16 1395 p = this->insert(p, *first);
Chris@16 1396 }
Chris@16 1397 return this->begin() + n_pos;
Chris@16 1398 }
Chris@16 1399
Chris@16 1400 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
Chris@16 1401 template <class ForwardIter>
Chris@16 1402 iterator insert(const_iterator p, ForwardIter first, ForwardIter last
Chris@16 1403 , typename container_detail::enable_if_c
Chris@16 1404 < !container_detail::is_convertible<ForwardIter, size_type>::value
Chris@16 1405 && !container_detail::is_input_iterator<ForwardIter>::value
Chris@16 1406 >::type * = 0
Chris@16 1407 )
Chris@16 1408 {
Chris@16 1409 const size_type n_pos = p - this->cbegin();
Chris@16 1410 if (first != last) {
Chris@16 1411 const size_type n = std::distance(first, last);
Chris@16 1412 const size_type old_size = this->priv_size();
Chris@16 1413 const size_type remaining = this->capacity() - old_size;
Chris@16 1414 const pointer old_start = this->priv_addr();
Chris@16 1415 bool enough_capacity = false;
Chris@16 1416 std::pair<pointer, bool> allocation_ret;
Chris@16 1417 size_type new_cap = 0;
Chris@16 1418
Chris@16 1419 //Check if we have enough capacity
Chris@16 1420 if (remaining >= n){
Chris@16 1421 enough_capacity = true;
Chris@16 1422 }
Chris@16 1423 else {
Chris@16 1424 //Otherwise expand current buffer or allocate new storage
Chris@16 1425 new_cap = this->next_capacity(n);
Chris@16 1426 allocation_ret = this->allocation_command
Chris@16 1427 (allocate_new | expand_fwd | expand_bwd, old_size + n + 1,
Chris@16 1428 new_cap, new_cap, old_start);
Chris@16 1429
Chris@16 1430 //Check forward expansion
Chris@16 1431 if(old_start == allocation_ret.first){
Chris@16 1432 enough_capacity = true;
Chris@16 1433 this->priv_storage(new_cap);
Chris@16 1434 }
Chris@16 1435 }
Chris@16 1436
Chris@16 1437 //Reuse same buffer
Chris@16 1438 if(enough_capacity){
Chris@16 1439 const size_type elems_after = old_size - (p - old_start);
Chris@16 1440 const size_type old_length = old_size;
Chris@16 1441 if (elems_after >= n) {
Chris@16 1442 const pointer pointer_past_last = old_start + old_size + 1;
Chris@16 1443 priv_uninitialized_copy(old_start + (old_size - n + 1),
Chris@16 1444 pointer_past_last, pointer_past_last);
Chris@16 1445
Chris@16 1446 this->priv_size(old_size+n);
Chris@16 1447 Traits::move(const_cast<CharT*>(container_detail::to_raw_pointer(p + n)),
Chris@16 1448 container_detail::to_raw_pointer(p),
Chris@16 1449 (elems_after - n) + 1);
Chris@16 1450 this->priv_copy(first, last, const_cast<CharT*>(container_detail::to_raw_pointer(p)));
Chris@16 1451 }
Chris@16 1452 else {
Chris@16 1453 ForwardIter mid = first;
Chris@16 1454 std::advance(mid, elems_after + 1);
Chris@16 1455
Chris@16 1456 priv_uninitialized_copy(mid, last, old_start + old_size + 1);
Chris@16 1457 const size_type newer_size = old_size + (n - elems_after);
Chris@16 1458 this->priv_size(newer_size);
Chris@16 1459 priv_uninitialized_copy
Chris@16 1460 (p, const_iterator(old_start + old_length + 1),
Chris@16 1461 old_start + newer_size);
Chris@16 1462 this->priv_size(newer_size + elems_after);
Chris@16 1463 this->priv_copy(first, mid, const_cast<CharT*>(container_detail::to_raw_pointer(p)));
Chris@16 1464 }
Chris@16 1465 }
Chris@16 1466 else{
Chris@16 1467 pointer new_start = allocation_ret.first;
Chris@16 1468 if(!allocation_ret.second){
Chris@16 1469 //Copy data to new buffer
Chris@16 1470 size_type new_length = 0;
Chris@16 1471 //This can't throw, since characters are POD
Chris@16 1472 new_length += priv_uninitialized_copy
Chris@16 1473 (const_iterator(old_start), p, new_start);
Chris@16 1474 new_length += priv_uninitialized_copy
Chris@16 1475 (first, last, new_start + new_length);
Chris@16 1476 new_length += priv_uninitialized_copy
Chris@16 1477 (p, const_iterator(old_start + old_size),
Chris@16 1478 new_start + new_length);
Chris@16 1479 this->priv_construct_null(new_start + new_length);
Chris@16 1480
Chris@16 1481 this->deallocate_block();
Chris@16 1482 this->is_short(false);
Chris@16 1483 this->priv_long_addr(new_start);
Chris@16 1484 this->priv_long_size(new_length);
Chris@16 1485 this->priv_long_storage(new_cap);
Chris@16 1486 }
Chris@16 1487 else{
Chris@16 1488 //value_type is POD, so backwards expansion is much easier
Chris@16 1489 //than with vector<T>
Chris@16 1490 value_type * const oldbuf = container_detail::to_raw_pointer(old_start);
Chris@16 1491 value_type * const newbuf = container_detail::to_raw_pointer(new_start);
Chris@16 1492 const value_type *const pos = container_detail::to_raw_pointer(p);
Chris@16 1493 const size_type before = pos - oldbuf;
Chris@16 1494
Chris@16 1495 //First move old data
Chris@16 1496 Traits::move(newbuf, oldbuf, before);
Chris@16 1497 Traits::move(newbuf + before + n, pos, old_size - before);
Chris@16 1498 //Now initialize the new data
Chris@16 1499 priv_uninitialized_copy(first, last, new_start + before);
Chris@16 1500 this->priv_construct_null(new_start + (old_size + n));
Chris@16 1501 this->is_short(false);
Chris@16 1502 this->priv_long_addr(new_start);
Chris@16 1503 this->priv_long_size(old_size + n);
Chris@16 1504 this->priv_long_storage(new_cap);
Chris@16 1505 }
Chris@16 1506 }
Chris@16 1507 }
Chris@16 1508 return this->begin() + n_pos;
Chris@16 1509 }
Chris@16 1510 #endif
Chris@16 1511
Chris@16 1512 //! <b>Requires</b>: pos <= size()
Chris@16 1513 //!
Chris@16 1514 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller of n and size() - pos.
Chris@16 1515 //! The function then replaces the string controlled by *this with a string of length size() - xlen
Chris@16 1516 //! whose first pos elements are a copy of the initial elements of the original string controlled by *this,
Chris@16 1517 //! and whose remaining elements are a copy of the elements of the original string controlled by *this
Chris@16 1518 //! beginning at position pos + xlen.
Chris@16 1519 //!
Chris@16 1520 //! <b>Throws</b>: out_of_range if pos > size().
Chris@16 1521 //!
Chris@16 1522 //! <b>Returns</b>: *this
Chris@16 1523 basic_string& erase(size_type pos = 0, size_type n = npos)
Chris@16 1524 {
Chris@16 1525 if (pos > this->size())
Chris@16 1526 throw_out_of_range("basic_string::erase out of range position");
Chris@16 1527 const pointer addr = this->priv_addr();
Chris@16 1528 erase(addr + pos, addr + pos + container_detail::min_value(n, this->size() - pos));
Chris@16 1529 return *this;
Chris@16 1530 }
Chris@16 1531
Chris@16 1532 //! <b>Effects</b>: Removes the character referred to by p.
Chris@16 1533 //!
Chris@16 1534 //! <b>Throws</b>: Nothing
Chris@16 1535 //!
Chris@16 1536 //! <b>Returns</b>: An iterator which points to the element immediately following p prior to the element being
Chris@16 1537 //! erased. If no such element exists, end() is returned.
Chris@16 1538 iterator erase(const_iterator p) BOOST_CONTAINER_NOEXCEPT
Chris@16 1539 {
Chris@16 1540 // The move includes the terminating null.
Chris@16 1541 CharT * const ptr = const_cast<CharT*>(container_detail::to_raw_pointer(p));
Chris@16 1542 const size_type old_size = this->priv_size();
Chris@16 1543 Traits::move(ptr,
Chris@16 1544 container_detail::to_raw_pointer(p + 1),
Chris@16 1545 old_size - (p - this->priv_addr()));
Chris@16 1546 this->priv_size(old_size-1);
Chris@16 1547 return iterator(ptr);
Chris@16 1548 }
Chris@16 1549
Chris@16 1550 //! <b>Requires</b>: first and last are valid iterators on *this, defining a range [first,last).
Chris@16 1551 //!
Chris@16 1552 //! <b>Effects</b>: Removes the characters in the range [first,last).
Chris@16 1553 //!
Chris@16 1554 //! <b>Throws</b>: Nothing
Chris@16 1555 //!
Chris@16 1556 //! <b>Returns</b>: An iterator which points to the element pointed to by last prior to
Chris@16 1557 //! the other elements being erased. If no such element exists, end() is returned.
Chris@16 1558 iterator erase(const_iterator first, const_iterator last) BOOST_CONTAINER_NOEXCEPT
Chris@16 1559 {
Chris@16 1560 CharT * f = const_cast<CharT*>(container_detail::to_raw_pointer(first));
Chris@16 1561 if (first != last) { // The move includes the terminating null.
Chris@16 1562 const size_type num_erased = last - first;
Chris@16 1563 const size_type old_size = this->priv_size();
Chris@16 1564 Traits::move(f,
Chris@16 1565 container_detail::to_raw_pointer(last),
Chris@16 1566 (old_size + 1)-(last - this->priv_addr()));
Chris@16 1567 const size_type new_length = old_size - num_erased;
Chris@16 1568 this->priv_size(new_length);
Chris@16 1569 }
Chris@16 1570 return iterator(f);
Chris@16 1571 }
Chris@16 1572
Chris@16 1573 //! <b>Requires</b>: !empty()
Chris@16 1574 //!
Chris@16 1575 //! <b>Throws</b>: Nothing
Chris@16 1576 //!
Chris@16 1577 //! <b>Effects</b>: Equivalent to erase(size() - 1, 1).
Chris@16 1578 void pop_back() BOOST_CONTAINER_NOEXCEPT
Chris@16 1579 {
Chris@16 1580 const size_type old_size = this->priv_size();
Chris@16 1581 Traits::assign(this->priv_addr()[old_size-1], CharT(0));
Chris@16 1582 this->priv_size(old_size-1);;
Chris@16 1583 }
Chris@16 1584
Chris@16 1585 //! <b>Effects</b>: Erases all the elements of the vector.
Chris@16 1586 //!
Chris@16 1587 //! <b>Throws</b>: Nothing.
Chris@16 1588 //!
Chris@16 1589 //! <b>Complexity</b>: Linear to the number of elements in the vector.
Chris@16 1590 void clear() BOOST_CONTAINER_NOEXCEPT
Chris@16 1591 {
Chris@16 1592 if (!this->empty()) {
Chris@16 1593 Traits::assign(*this->priv_addr(), CharT(0));
Chris@16 1594 this->priv_size(0);
Chris@16 1595 }
Chris@16 1596 }
Chris@16 1597
Chris@16 1598 //! <b>Requires</b>: pos1 <= size().
Chris@16 1599 //!
Chris@16 1600 //! <b>Effects</b>: Calls replace(pos1, n1, str.data(), str.size()).
Chris@16 1601 //!
Chris@16 1602 //! <b>Throws</b>: if memory allocation throws or out_of_range if pos1 > size().
Chris@16 1603 //!
Chris@16 1604 //! <b>Returns</b>: *this
Chris@16 1605 basic_string& replace(size_type pos1, size_type n1, const basic_string& str)
Chris@16 1606 {
Chris@16 1607 if (pos1 > this->size())
Chris@16 1608 throw_out_of_range("basic_string::replace out of range position");
Chris@16 1609 const size_type len = container_detail::min_value(n1, this->size() - pos1);
Chris@16 1610 if (this->size() - len >= this->max_size() - str.size())
Chris@16 1611 throw_length_error("basic_string::replace max_size() exceeded");
Chris@16 1612 const pointer addr = this->priv_addr();
Chris@16 1613 return this->replace( const_iterator(addr + pos1)
Chris@16 1614 , const_iterator(addr + pos1 + len)
Chris@16 1615 , str.begin(), str.end());
Chris@16 1616 }
Chris@16 1617
Chris@16 1618 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size().
Chris@16 1619 //!
Chris@16 1620 //! <b>Effects</b>: Determines the effective length rlen of the string to be
Chris@16 1621 //! inserted as the smaller of n2 and str.size() - pos2 and calls
Chris@16 1622 //! replace(pos1, n1, str.data() + pos2, rlen).
Chris@16 1623 //!
Chris@16 1624 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or pos2 > str.size().
Chris@16 1625 //!
Chris@16 1626 //! <b>Returns</b>: *this
Chris@16 1627 basic_string& replace(size_type pos1, size_type n1,
Chris@16 1628 const basic_string& str, size_type pos2, size_type n2)
Chris@16 1629 {
Chris@16 1630 if (pos1 > this->size() || pos2 > str.size())
Chris@16 1631 throw_out_of_range("basic_string::replace out of range position");
Chris@16 1632 const size_type len1 = container_detail::min_value(n1, this->size() - pos1);
Chris@16 1633 const size_type len2 = container_detail::min_value(n2, str.size() - pos2);
Chris@16 1634 if (this->size() - len1 >= this->max_size() - len2)
Chris@16 1635 throw_length_error("basic_string::replace max_size() exceeded");
Chris@16 1636 const pointer addr = this->priv_addr();
Chris@16 1637 const pointer straddr = str.priv_addr();
Chris@16 1638 return this->replace(addr + pos1, addr + pos1 + len1,
Chris@16 1639 straddr + pos2, straddr + pos2 + len2);
Chris@16 1640 }
Chris@16 1641
Chris@16 1642 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
Chris@16 1643 //!
Chris@16 1644 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the
Chris@16 1645 //! smaller of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error.
Chris@16 1646 //! Otherwise, the function replaces the string controlled by *this with a string of
Chris@16 1647 //! length size() - xlen + n2 whose first pos1 elements are a copy of the initial elements
Chris@16 1648 //! of the original string controlled by *this, whose next n2 elements are a copy of the
Chris@16 1649 //! initial n2 elements of s, and whose remaining elements are a copy of the elements of
Chris@16 1650 //! the original string controlled by *this beginning at position pos + xlen.
Chris@16 1651 //!
Chris@16 1652 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
Chris@16 1653 //! if the length of the resulting string would exceed max_size()
Chris@16 1654 //!
Chris@16 1655 //! <b>Returns</b>: *this
Chris@16 1656 basic_string& replace(size_type pos1, size_type n1, const CharT* s, size_type n2)
Chris@16 1657 {
Chris@16 1658 if (pos1 > this->size())
Chris@16 1659 throw_out_of_range("basic_string::replace out of range position");
Chris@16 1660 const size_type len = container_detail::min_value(n1, this->size() - pos1);
Chris@16 1661 if (n2 > this->max_size() || size() - len >= this->max_size() - n2)
Chris@16 1662 throw_length_error("basic_string::replace max_size() exceeded");
Chris@16 1663 const pointer addr = this->priv_addr();
Chris@16 1664 return this->replace(addr + pos1, addr + pos1 + len, s, s + n2);
Chris@16 1665 }
Chris@16 1666
Chris@16 1667 //! <b>Requires</b>: pos1 <= size() and s points to an array of at least n2 elements of CharT.
Chris@16 1668 //!
Chris@16 1669 //! <b>Effects</b>: Determines the effective length xlen of the string to be removed as the smaller
Chris@16 1670 //! of n1 and size() - pos1. If size() - xlen >= max_size() - n2 throws length_error. Otherwise,
Chris@16 1671 //! the function replaces the string controlled by *this with a string of length size() - xlen + n2
Chris@16 1672 //! whose first pos1 elements are a copy of the initial elements of the original string controlled
Chris@16 1673 //! by *this, whose next n2 elements are a copy of the initial n2 elements of s, and whose
Chris@16 1674 //! remaining elements are a copy of the elements of the original string controlled by *this
Chris@16 1675 //! beginning at position pos + xlen.
Chris@16 1676 //!
Chris@16 1677 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
Chris@16 1678 //! if the length of the resulting string would exceed max_size()
Chris@16 1679 //!
Chris@16 1680 //! <b>Returns</b>: *this
Chris@16 1681 basic_string& replace(size_type pos, size_type n1, const CharT* s)
Chris@16 1682 {
Chris@16 1683 if (pos > this->size())
Chris@16 1684 throw_out_of_range("basic_string::replace out of range position");
Chris@16 1685 const size_type len = container_detail::min_value(n1, this->size() - pos);
Chris@16 1686 const size_type n2 = Traits::length(s);
Chris@16 1687 if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
Chris@16 1688 throw_length_error("basic_string::replace max_size() exceeded");
Chris@16 1689 const pointer addr = this->priv_addr();
Chris@16 1690 return this->replace(addr + pos, addr + pos + len,
Chris@16 1691 s, s + Traits::length(s));
Chris@16 1692 }
Chris@16 1693
Chris@16 1694 //! <b>Requires</b>: pos1 <= size().
Chris@16 1695 //!
Chris@16 1696 //! <b>Effects</b>: Equivalent to replace(pos1, n1, basic_string(n2, c)).
Chris@16 1697 //!
Chris@16 1698 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos1 > size() or length_error
Chris@16 1699 //! if the length of the resulting string would exceed max_size()
Chris@16 1700 //!
Chris@16 1701 //! <b>Returns</b>: *this
Chris@16 1702 basic_string& replace(size_type pos1, size_type n1, size_type n2, CharT c)
Chris@16 1703 {
Chris@16 1704 if (pos1 > this->size())
Chris@16 1705 throw_out_of_range("basic_string::replace out of range position");
Chris@16 1706 const size_type len = container_detail::min_value(n1, this->size() - pos1);
Chris@16 1707 if (n2 > this->max_size() || this->size() - len >= this->max_size() - n2)
Chris@16 1708 throw_length_error("basic_string::replace max_size() exceeded");
Chris@16 1709 const pointer addr = this->priv_addr();
Chris@16 1710 return this->replace(addr + pos1, addr + pos1 + len, n2, c);
Chris@16 1711 }
Chris@16 1712
Chris@16 1713 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
Chris@16 1714 //!
Chris@16 1715 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, str).
Chris@16 1716 //!
Chris@16 1717 //! <b>Throws</b>: if memory allocation throws
Chris@16 1718 //!
Chris@16 1719 //! <b>Returns</b>: *this
Chris@16 1720 basic_string& replace(const_iterator i1, const_iterator i2, const basic_string& str)
Chris@16 1721 { return this->replace(i1, i2, str.begin(), str.end()); }
Chris@16 1722
Chris@16 1723 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and
Chris@16 1724 //! s points to an array of at least n elements
Chris@16 1725 //!
Chris@16 1726 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, n).
Chris@16 1727 //!
Chris@16 1728 //! <b>Throws</b>: if memory allocation throws
Chris@16 1729 //!
Chris@16 1730 //! <b>Returns</b>: *this
Chris@16 1731 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s, size_type n)
Chris@16 1732 { return this->replace(i1, i2, s, s + n); }
Chris@16 1733
Chris@16 1734 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges and s points to an
Chris@16 1735 //! array of at least traits::length(s) + 1 elements of CharT.
Chris@16 1736 //!
Chris@16 1737 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, s, traits::length(s)).
Chris@16 1738 //!
Chris@16 1739 //! <b>Throws</b>: if memory allocation throws
Chris@16 1740 //!
Chris@16 1741 //! <b>Returns</b>: *this
Chris@16 1742 basic_string& replace(const_iterator i1, const_iterator i2, const CharT* s)
Chris@16 1743 { return this->replace(i1, i2, s, s + Traits::length(s)); }
Chris@16 1744
Chris@16 1745 //! <b>Requires</b>: [begin(),i1) and [i1,i2) are valid ranges.
Chris@16 1746 //!
Chris@16 1747 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(n, c)).
Chris@16 1748 //!
Chris@16 1749 //! <b>Throws</b>: if memory allocation throws
Chris@16 1750 //!
Chris@16 1751 //! <b>Returns</b>: *this
Chris@16 1752 basic_string& replace(const_iterator i1, const_iterator i2, size_type n, CharT c)
Chris@16 1753 {
Chris@16 1754 const size_type len = static_cast<size_type>(i2 - i1);
Chris@16 1755 if (len >= n) {
Chris@16 1756 Traits::assign(const_cast<CharT*>(container_detail::to_raw_pointer(i1)), n, c);
Chris@16 1757 erase(i1 + n, i2);
Chris@16 1758 }
Chris@16 1759 else {
Chris@16 1760 Traits::assign(const_cast<CharT*>(container_detail::to_raw_pointer(i1)), len, c);
Chris@16 1761 insert(i2, n - len, c);
Chris@16 1762 }
Chris@16 1763 return *this;
Chris@16 1764 }
Chris@16 1765
Chris@16 1766 //! <b>Requires</b>: [begin(),i1), [i1,i2) and [j1,j2) are valid ranges.
Chris@16 1767 //!
Chris@16 1768 //! <b>Effects</b>: Calls replace(i1 - begin(), i2 - i1, basic_string(j1, j2)).
Chris@16 1769 //!
Chris@16 1770 //! <b>Throws</b>: if memory allocation throws
Chris@16 1771 //!
Chris@16 1772 //! <b>Returns</b>: *this
Chris@16 1773 template <class InputIter>
Chris@16 1774 basic_string& replace(const_iterator i1, const_iterator i2, InputIter j1, InputIter j2
Chris@16 1775 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
Chris@16 1776 , typename container_detail::enable_if_c
Chris@16 1777 < !container_detail::is_convertible<InputIter, size_type>::value
Chris@16 1778 && container_detail::is_input_iterator<InputIter>::value
Chris@16 1779 >::type * = 0
Chris@16 1780 #endif
Chris@16 1781 )
Chris@16 1782 {
Chris@16 1783 for ( ; i1 != i2 && j1 != j2; ++i1, ++j1){
Chris@16 1784 Traits::assign(*const_cast<CharT*>(container_detail::to_raw_pointer(i1)), *j1);
Chris@16 1785 }
Chris@16 1786
Chris@16 1787 if (j1 == j2)
Chris@16 1788 this->erase(i1, i2);
Chris@16 1789 else
Chris@16 1790 this->insert(i2, j1, j2);
Chris@16 1791 return *this;
Chris@16 1792 }
Chris@16 1793
Chris@16 1794 #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
Chris@16 1795 template <class ForwardIter>
Chris@16 1796 basic_string& replace(const_iterator i1, const_iterator i2, ForwardIter j1, ForwardIter j2
Chris@16 1797 , typename container_detail::enable_if_c
Chris@16 1798 < !container_detail::is_convertible<ForwardIter, size_type>::value
Chris@16 1799 && !container_detail::is_input_iterator<ForwardIter>::value
Chris@16 1800 >::type * = 0
Chris@16 1801 )
Chris@16 1802 {
Chris@16 1803 difference_type n = std::distance(j1, j2);
Chris@16 1804 const difference_type len = i2 - i1;
Chris@16 1805 if (len >= n) {
Chris@16 1806 this->priv_copy(j1, j2, const_cast<CharT*>(container_detail::to_raw_pointer(i1)));
Chris@16 1807 this->erase(i1 + n, i2);
Chris@16 1808 }
Chris@16 1809 else {
Chris@16 1810 ForwardIter m = j1;
Chris@16 1811 std::advance(m, len);
Chris@16 1812 this->priv_copy(j1, m, const_cast<CharT*>(container_detail::to_raw_pointer(i1)));
Chris@16 1813 this->insert(i2, m, j2);
Chris@16 1814 }
Chris@16 1815 return *this;
Chris@16 1816 }
Chris@16 1817 #endif
Chris@16 1818
Chris@16 1819 //! <b>Requires</b>: pos <= size()
Chris@16 1820 //!
Chris@16 1821 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as the
Chris@16 1822 //! smaller of n and size() - pos. s shall designate an array of at least rlen elements.
Chris@16 1823 //! The function then replaces the string designated by s with a string of length rlen
Chris@16 1824 //! whose elements are a copy of the string controlled by *this beginning at position pos.
Chris@16 1825 //! The function does not append a null object to the string designated by s.
Chris@16 1826 //!
Chris@16 1827 //! <b>Throws</b>: if memory allocation throws, out_of_range if pos > size().
Chris@16 1828 //!
Chris@16 1829 //! <b>Returns</b>: rlen
Chris@16 1830 size_type copy(CharT* s, size_type n, size_type pos = 0) const
Chris@16 1831 {
Chris@16 1832 if (pos > this->size())
Chris@16 1833 throw_out_of_range("basic_string::copy out of range position");
Chris@16 1834 const size_type len = container_detail::min_value(n, this->size() - pos);
Chris@16 1835 Traits::copy(s, container_detail::to_raw_pointer(this->priv_addr() + pos), len);
Chris@16 1836 return len;
Chris@16 1837 }
Chris@16 1838
Chris@16 1839 //! <b>Effects</b>: *this contains the same sequence of characters that was in s,
Chris@16 1840 //! s contains the same sequence of characters that was in *this.
Chris@16 1841 //!
Chris@16 1842 //! <b>Throws</b>: Nothing
Chris@16 1843 void swap(basic_string& x)
Chris@16 1844 {
Chris@16 1845 this->base_t::swap_data(x);
Chris@16 1846 container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
Chris@16 1847 container_detail::swap_alloc(this->alloc(), x.alloc(), flag);
Chris@16 1848 }
Chris@16 1849
Chris@16 1850 //////////////////////////////////////////////
Chris@16 1851 //
Chris@16 1852 // data access
Chris@16 1853 //
Chris@16 1854 //////////////////////////////////////////////
Chris@16 1855
Chris@16 1856 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
Chris@16 1857 //!
Chris@16 1858 //! <b>Returns</b>: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()].
Chris@16 1859 //!
Chris@16 1860 //! <b>Complexity</b>: constant time.
Chris@16 1861 const CharT* c_str() const BOOST_CONTAINER_NOEXCEPT
Chris@16 1862 { return container_detail::to_raw_pointer(this->priv_addr()); }
Chris@16 1863
Chris@16 1864 //! <b>Requires</b>: The program shall not alter any of the values stored in the character array.
Chris@16 1865 //!
Chris@16 1866 //! <b>Returns</b>: Allocator pointer p such that p + i == &operator[](i) for each i in [0,size()].
Chris@16 1867 //!
Chris@16 1868 //! <b>Complexity</b>: constant time.
Chris@16 1869 const CharT* data() const BOOST_CONTAINER_NOEXCEPT
Chris@16 1870 { return container_detail::to_raw_pointer(this->priv_addr()); }
Chris@16 1871
Chris@16 1872 //////////////////////////////////////////////
Chris@16 1873 //
Chris@16 1874 // string operations
Chris@16 1875 //
Chris@16 1876 //////////////////////////////////////////////
Chris@16 1877
Chris@16 1878 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both
Chris@16 1879 //! of the following conditions obtain: 19 pos <= xpos and xpos + str.size() <= size();
Chris@16 1880 //! 2) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
Chris@16 1881 //!
Chris@16 1882 //! <b>Throws</b>: Nothing
Chris@16 1883 //!
Chris@16 1884 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 1885 size_type find(const basic_string& s, size_type pos = 0) const
Chris@16 1886 { return find(s.c_str(), pos, s.size()); }
Chris@16 1887
Chris@16 1888 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 1889 //!
Chris@16 1890 //! <b>Throws</b>: Nothing
Chris@16 1891 //!
Chris@16 1892 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(s,n),pos).
Chris@16 1893 size_type find(const CharT* s, size_type pos, size_type n) const
Chris@16 1894 {
Chris@16 1895 if (pos + n > this->size())
Chris@16 1896 return npos;
Chris@16 1897 else {
Chris@16 1898 const pointer addr = this->priv_addr();
Chris@16 1899 pointer finish = addr + this->priv_size();
Chris@16 1900 const const_iterator result =
Chris@16 1901 std::search(container_detail::to_raw_pointer(addr + pos),
Chris@16 1902 container_detail::to_raw_pointer(finish),
Chris@16 1903 s, s + n, Eq_traits<Traits>());
Chris@16 1904 return result != finish ? result - begin() : npos;
Chris@16 1905 }
Chris@16 1906 }
Chris@16 1907
Chris@16 1908 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 1909 //!
Chris@16 1910 //! <b>Throws</b>: Nothing
Chris@16 1911 //!
Chris@16 1912 //! <b>Returns</b>: find(basic_string(s), pos).
Chris@16 1913 size_type find(const CharT* s, size_type pos = 0) const
Chris@16 1914 { return this->find(s, pos, Traits::length(s)); }
Chris@16 1915
Chris@16 1916 //! <b>Throws</b>: Nothing
Chris@16 1917 //!
Chris@16 1918 //! <b>Returns</b>: find(basic_string<CharT,traits,Allocator>(1,c), pos).
Chris@16 1919 size_type find(CharT c, size_type pos = 0) const
Chris@16 1920 {
Chris@16 1921 const size_type sz = this->size();
Chris@16 1922 if (pos >= sz)
Chris@16 1923 return npos;
Chris@16 1924 else {
Chris@16 1925 const pointer addr = this->priv_addr();
Chris@16 1926 pointer finish = addr + sz;
Chris@16 1927 const const_iterator result =
Chris@16 1928 std::find_if(addr + pos, finish,
Chris@16 1929 std::bind2nd(Eq_traits<Traits>(), c));
Chris@16 1930 return result != finish ? result - begin() : npos;
Chris@16 1931 }
Chris@16 1932 }
Chris@16 1933
Chris@16 1934 //! <b>Effects</b>: Determines the highest position xpos, if possible, such
Chris@16 1935 //! that both of the following conditions obtain:
Chris@16 1936 //! a) xpos <= pos and xpos + str.size() <= size();
Chris@16 1937 //! b) traits::eq(at(xpos+I), str.at(I)) for all elements I of the string controlled by str.
Chris@16 1938 //!
Chris@16 1939 //! <b>Throws</b>: Nothing
Chris@16 1940 //!
Chris@16 1941 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 1942 size_type rfind(const basic_string& str, size_type pos = npos) const
Chris@16 1943 { return rfind(str.c_str(), pos, str.size()); }
Chris@16 1944
Chris@16 1945 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 1946 //!
Chris@16 1947 //! <b>Throws</b>: Nothing
Chris@16 1948 //!
Chris@16 1949 //! <b>Returns</b>: rfind(basic_string(s, n), pos).
Chris@16 1950 size_type rfind(const CharT* s, size_type pos, size_type n) const
Chris@16 1951 {
Chris@16 1952 const size_type len = this->size();
Chris@16 1953
Chris@16 1954 if (n > len)
Chris@16 1955 return npos;
Chris@16 1956 else if (n == 0)
Chris@16 1957 return container_detail::min_value(len, pos);
Chris@16 1958 else {
Chris@16 1959 const const_iterator last = begin() + container_detail::min_value(len - n, pos) + n;
Chris@16 1960 const const_iterator result = find_end(begin(), last,
Chris@16 1961 s, s + n,
Chris@16 1962 Eq_traits<Traits>());
Chris@16 1963 return result != last ? result - begin() : npos;
Chris@16 1964 }
Chris@16 1965 }
Chris@16 1966
Chris@16 1967 //! <b>Requires</b>: pos <= size() and s points to an array of at least
Chris@16 1968 //! traits::length(s) + 1 elements of CharT.
Chris@16 1969 //!
Chris@16 1970 //! <b>Throws</b>: Nothing
Chris@16 1971 //!
Chris@16 1972 //! <b>Returns</b>: rfind(basic_string(s), pos).
Chris@16 1973 size_type rfind(const CharT* s, size_type pos = npos) const
Chris@16 1974 { return rfind(s, pos, Traits::length(s)); }
Chris@16 1975
Chris@16 1976 //! <b>Throws</b>: Nothing
Chris@16 1977 //!
Chris@16 1978 //! <b>Returns</b>: rfind(basic_string<CharT,traits,Allocator>(1,c),pos).
Chris@16 1979 size_type rfind(CharT c, size_type pos = npos) const
Chris@16 1980 {
Chris@16 1981 const size_type len = this->size();
Chris@16 1982
Chris@16 1983 if (len < 1)
Chris@16 1984 return npos;
Chris@16 1985 else {
Chris@16 1986 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
Chris@16 1987 const_reverse_iterator rresult =
Chris@16 1988 std::find_if(const_reverse_iterator(last), rend(),
Chris@16 1989 std::bind2nd(Eq_traits<Traits>(), c));
Chris@16 1990 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
Chris@16 1991 }
Chris@16 1992 }
Chris@16 1993
Chris@16 1994 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that both of the
Chris@16 1995 //! following conditions obtain: a) pos <= xpos and xpos < size();
Chris@16 1996 //! b) traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
Chris@16 1997 //!
Chris@16 1998 //! <b>Throws</b>: Nothing
Chris@16 1999 //!
Chris@16 2000 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 2001 size_type find_first_of(const basic_string& s, size_type pos = 0) const
Chris@16 2002 { return find_first_of(s.c_str(), pos, s.size()); }
Chris@16 2003
Chris@16 2004 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 2005 //!
Chris@16 2006 //! <b>Throws</b>: Nothing
Chris@16 2007 //!
Chris@16 2008 //! <b>Returns</b>: find_first_of(basic_string(s, n), pos).
Chris@16 2009 size_type find_first_of(const CharT* s, size_type pos, size_type n) const
Chris@16 2010 {
Chris@16 2011 const size_type sz = this->size();
Chris@16 2012 if (pos >= sz)
Chris@16 2013 return npos;
Chris@16 2014 else {
Chris@16 2015 const pointer addr = this->priv_addr();
Chris@16 2016 pointer finish = addr + sz;
Chris@16 2017 const_iterator result = std::find_first_of
Chris@16 2018 (addr + pos, finish, s, s + n, Eq_traits<Traits>());
Chris@16 2019 return result != finish ? result - this->begin() : npos;
Chris@16 2020 }
Chris@16 2021 }
Chris@16 2022
Chris@16 2023 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2024 //!
Chris@16 2025 //! <b>Throws</b>: Nothing
Chris@16 2026 //!
Chris@16 2027 //! <b>Returns</b>: find_first_of(basic_string(s), pos).
Chris@16 2028 size_type find_first_of(const CharT* s, size_type pos = 0) const
Chris@16 2029 { return find_first_of(s, pos, Traits::length(s)); }
Chris@16 2030
Chris@16 2031 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2032 //!
Chris@16 2033 //! <b>Throws</b>: Nothing
Chris@16 2034 //!
Chris@16 2035 //! <b>Returns</b>: find_first_of(basic_string<CharT,traits,Allocator>(1,c), pos).
Chris@16 2036 size_type find_first_of(CharT c, size_type pos = 0) const
Chris@16 2037 { return find(c, pos); }
Chris@16 2038
Chris@16 2039 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that both of
Chris@16 2040 //! the following conditions obtain: a) xpos <= pos and xpos < size(); b)
Chris@16 2041 //! traits::eq(at(xpos), str.at(I)) for some element I of the string controlled by str.
Chris@16 2042 //!
Chris@16 2043 //! <b>Throws</b>: Nothing
Chris@16 2044 //!
Chris@16 2045 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 2046 size_type find_last_of(const basic_string& str, size_type pos = npos) const
Chris@16 2047 { return find_last_of(str.c_str(), pos, str.size()); }
Chris@16 2048
Chris@16 2049 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 2050 //!
Chris@16 2051 //! <b>Throws</b>: Nothing
Chris@16 2052 //!
Chris@16 2053 //! <b>Returns</b>: find_last_of(basic_string(s, n), pos).
Chris@16 2054 size_type find_last_of(const CharT* s, size_type pos, size_type n) const
Chris@16 2055 {
Chris@16 2056 const size_type len = this->size();
Chris@16 2057
Chris@16 2058 if (len < 1)
Chris@16 2059 return npos;
Chris@16 2060 else {
Chris@16 2061 const pointer addr = this->priv_addr();
Chris@16 2062 const const_iterator last = addr + container_detail::min_value(len - 1, pos) + 1;
Chris@16 2063 const const_reverse_iterator rresult =
Chris@16 2064 std::find_first_of(const_reverse_iterator(last), rend(),
Chris@16 2065 s, s + n, Eq_traits<Traits>());
Chris@16 2066 return rresult != rend() ? (rresult.base() - 1) - addr : npos;
Chris@16 2067 }
Chris@16 2068 }
Chris@16 2069
Chris@16 2070 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2071 //!
Chris@16 2072 //! <b>Throws</b>: Nothing
Chris@16 2073 //!
Chris@16 2074 //! <b>Returns</b>: find_last_of(basic_string<CharT,traits,Allocator>(1,c),pos).
Chris@16 2075 size_type find_last_of(const CharT* s, size_type pos = npos) const
Chris@16 2076 { return find_last_of(s, pos, Traits::length(s)); }
Chris@16 2077
Chris@16 2078 //! <b>Throws</b>: Nothing
Chris@16 2079 //!
Chris@16 2080 //! <b>Returns</b>: find_last_of(basic_string(s), pos).
Chris@16 2081 size_type find_last_of(CharT c, size_type pos = npos) const
Chris@16 2082 { return rfind(c, pos); }
Chris@16 2083
Chris@16 2084 //! <b>Effects</b>: Determines the lowest position xpos, if possible, such that
Chris@16 2085 //! both of the following conditions obtain:
Chris@16 2086 //! a) pos <= xpos and xpos < size(); b) traits::eq(at(xpos), str.at(I)) for no
Chris@16 2087 //! element I of the string controlled by str.
Chris@16 2088 //!
Chris@16 2089 //! <b>Throws</b>: Nothing
Chris@16 2090 //!
Chris@16 2091 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 2092 size_type find_first_not_of(const basic_string& str, size_type pos = 0) const
Chris@16 2093 { return find_first_not_of(str.c_str(), pos, str.size()); }
Chris@16 2094
Chris@16 2095 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2096 //!
Chris@16 2097 //! <b>Throws</b>: Nothing
Chris@16 2098 //!
Chris@16 2099 //! <b>Returns</b>: find_first_not_of(basic_string(s, n), pos).
Chris@16 2100 size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const
Chris@16 2101 {
Chris@16 2102 if (pos > this->size())
Chris@16 2103 return npos;
Chris@16 2104 else {
Chris@16 2105 const pointer addr = this->priv_addr();
Chris@16 2106 const pointer finish = addr + this->priv_size();
Chris@16 2107 const const_iterator result = std::find_if
Chris@16 2108 (addr + pos, finish, Not_within_traits<Traits>(s, s + n));
Chris@16 2109 return result != finish ? result - addr : npos;
Chris@16 2110 }
Chris@16 2111 }
Chris@16 2112
Chris@16 2113 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2114 //!
Chris@16 2115 //! <b>Throws</b>: Nothing
Chris@16 2116 //!
Chris@16 2117 //! <b>Returns</b>: find_first_not_of(basic_string(s), pos).
Chris@16 2118 size_type find_first_not_of(const CharT* s, size_type pos = 0) const
Chris@16 2119 { return find_first_not_of(s, pos, Traits::length(s)); }
Chris@16 2120
Chris@16 2121 //! <b>Throws</b>: Nothing
Chris@16 2122 //!
Chris@16 2123 //! <b>Returns</b>: find_first_not_of(basic_string(1, c), pos).
Chris@16 2124 size_type find_first_not_of(CharT c, size_type pos = 0) const
Chris@16 2125 {
Chris@16 2126 if (pos > this->size())
Chris@16 2127 return npos;
Chris@16 2128 else {
Chris@16 2129 const pointer addr = this->priv_addr();
Chris@16 2130 const pointer finish = addr + this->priv_size();
Chris@16 2131 const const_iterator result
Chris@16 2132 = std::find_if(addr + pos, finish,
Chris@16 2133 std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
Chris@16 2134 return result != finish ? result - begin() : npos;
Chris@16 2135 }
Chris@16 2136 }
Chris@16 2137
Chris@16 2138 //! <b>Effects</b>: Determines the highest position xpos, if possible, such that
Chris@16 2139 //! both of the following conditions obtain: a) xpos <= pos and xpos < size();
Chris@16 2140 //! b) traits::eq(at(xpos), str.at(I)) for no element I of the string controlled by str.
Chris@16 2141 //!
Chris@16 2142 //! <b>Throws</b>: Nothing
Chris@16 2143 //!
Chris@16 2144 //! <b>Returns</b>: xpos if the function can determine such a value for xpos. Otherwise, returns npos.
Chris@16 2145 size_type find_last_not_of(const basic_string& str, size_type pos = npos) const
Chris@16 2146 { return find_last_not_of(str.c_str(), pos, str.size()); }
Chris@16 2147
Chris@16 2148 //! <b>Requires</b>: s points to an array of at least n elements of CharT.
Chris@16 2149 //!
Chris@16 2150 //! <b>Throws</b>: Nothing
Chris@16 2151 //!
Chris@16 2152 //! <b>Returns</b>: find_last_not_of(basic_string(s, n), pos).
Chris@16 2153 size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const
Chris@16 2154 {
Chris@16 2155 const size_type len = this->size();
Chris@16 2156
Chris@16 2157 if (len < 1)
Chris@16 2158 return npos;
Chris@16 2159 else {
Chris@16 2160 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
Chris@16 2161 const const_reverse_iterator rresult =
Chris@16 2162 std::find_if(const_reverse_iterator(last), rend(),
Chris@16 2163 Not_within_traits<Traits>(s, s + n));
Chris@16 2164 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
Chris@16 2165 }
Chris@16 2166 }
Chris@16 2167
Chris@16 2168 //! <b>Requires</b>: s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2169 //!
Chris@16 2170 //! <b>Throws</b>: Nothing
Chris@16 2171 //!
Chris@16 2172 //! <b>Returns</b>: find_last_not_of(basic_string(s), pos).
Chris@16 2173 size_type find_last_not_of(const CharT* s, size_type pos = npos) const
Chris@16 2174 { return find_last_not_of(s, pos, Traits::length(s)); }
Chris@16 2175
Chris@16 2176 //! <b>Throws</b>: Nothing
Chris@16 2177 //!
Chris@16 2178 //! <b>Returns</b>: find_last_not_of(basic_string(1, c), pos).
Chris@16 2179 size_type find_last_not_of(CharT c, size_type pos = npos) const
Chris@16 2180 {
Chris@16 2181 const size_type len = this->size();
Chris@16 2182
Chris@16 2183 if (len < 1)
Chris@16 2184 return npos;
Chris@16 2185 else {
Chris@16 2186 const const_iterator last = begin() + container_detail::min_value(len - 1, pos) + 1;
Chris@16 2187 const const_reverse_iterator rresult =
Chris@16 2188 std::find_if(const_reverse_iterator(last), rend(),
Chris@16 2189 std::not1(std::bind2nd(Eq_traits<Traits>(), c)));
Chris@16 2190 return rresult != rend() ? (rresult.base() - 1) - begin() : npos;
Chris@16 2191 }
Chris@16 2192 }
Chris@16 2193
Chris@16 2194 //! <b>Requires</b>: Requires: pos <= size()
Chris@16 2195 //!
Chris@16 2196 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
Chris@16 2197 //! the smaller of n and size() - pos.
Chris@16 2198 //!
Chris@16 2199 //! <b>Throws</b>: If memory allocation throws or out_of_range if pos > size().
Chris@16 2200 //!
Chris@16 2201 //! <b>Returns</b>: basic_string<CharT,traits,Allocator>(data()+pos,rlen).
Chris@16 2202 basic_string substr(size_type pos = 0, size_type n = npos) const
Chris@16 2203 {
Chris@16 2204 if (pos > this->size())
Chris@16 2205 throw_out_of_range("basic_string::substr out of range position");
Chris@16 2206 const pointer addr = this->priv_addr();
Chris@16 2207 return basic_string(addr + pos,
Chris@16 2208 addr + pos + container_detail::min_value(n, size() - pos), this->alloc());
Chris@16 2209 }
Chris@16 2210
Chris@16 2211 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
Chris@16 2212 //! the smaller of size() and str.size(). The function then compares the two strings by
Chris@16 2213 //! calling traits::compare(data(), str.data(), rlen).
Chris@16 2214 //!
Chris@16 2215 //! <b>Throws</b>: Nothing
Chris@16 2216 //!
Chris@16 2217 //! <b>Returns</b>: The nonzero result if the result of the comparison is nonzero.
Chris@16 2218 //! Otherwise, returns a value < 0 if size() < str.size(), a 0 value if size() == str.size(),
Chris@16 2219 //! and value > 0 if size() > str.size()
Chris@16 2220 int compare(const basic_string& str) const
Chris@16 2221 {
Chris@16 2222 const pointer addr = this->priv_addr();
Chris@16 2223 const pointer str_addr = str.priv_addr();
Chris@16 2224 return s_compare(addr, addr + this->priv_size(), str_addr, str_addr + str.priv_size());
Chris@16 2225 }
Chris@16 2226
Chris@16 2227 //! <b>Requires</b>: pos1 <= size()
Chris@16 2228 //!
Chris@16 2229 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
Chris@16 2230 //! the smaller of
Chris@16 2231 //!
Chris@16 2232 //! <b>Throws</b>: out_of_range if pos1 > size()
Chris@16 2233 //!
Chris@16 2234 //! <b>Returns</b>:basic_string(*this,pos1,n1).compare(str).
Chris@16 2235 int compare(size_type pos1, size_type n1, const basic_string& str) const
Chris@16 2236 {
Chris@16 2237 if (pos1 > this->size())
Chris@16 2238 throw_out_of_range("basic_string::compare out of range position");
Chris@16 2239 const pointer addr = this->priv_addr();
Chris@16 2240 const pointer str_addr = str.priv_addr();
Chris@16 2241 return s_compare(addr + pos1,
Chris@16 2242 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
Chris@16 2243 str_addr, str_addr + str.priv_size());
Chris@16 2244 }
Chris@16 2245
Chris@16 2246 //! <b>Requires</b>: pos1 <= size() and pos2 <= str.size()
Chris@16 2247 //!
Chris@16 2248 //! <b>Effects</b>: Determines the effective length rlen of the string to copy as
Chris@16 2249 //! the smaller of
Chris@16 2250 //!
Chris@16 2251 //! <b>Throws</b>: out_of_range if pos1 > size() or pos2 > str.size()
Chris@16 2252 //!
Chris@16 2253 //! <b>Returns</b>: basic_string(*this, pos1, n1).compare(basic_string(str, pos2, n2)).
Chris@16 2254 int compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2) const
Chris@16 2255 {
Chris@16 2256 if (pos1 > this->size() || pos2 > str.size())
Chris@16 2257 throw_out_of_range("basic_string::compare out of range position");
Chris@16 2258 const pointer addr = this->priv_addr();
Chris@16 2259 const pointer str_addr = str.priv_addr();
Chris@16 2260 return s_compare(addr + pos1,
Chris@16 2261 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
Chris@16 2262 str_addr + pos2,
Chris@16 2263 str_addr + pos2 + container_detail::min_value(n2, str.size() - pos2));
Chris@16 2264 }
Chris@16 2265
Chris@16 2266 //! <b>Throws</b>: Nothing
Chris@16 2267 //!
Chris@16 2268 //! <b>Returns</b>: compare(basic_string(s)).
Chris@16 2269 int compare(const CharT* s) const
Chris@16 2270 {
Chris@16 2271 const pointer addr = this->priv_addr();
Chris@16 2272 return s_compare(addr, addr + this->priv_size(), s, s + Traits::length(s));
Chris@16 2273 }
Chris@16 2274
Chris@16 2275
Chris@16 2276 //! <b>Requires</b>: pos1 > size() and s points to an array of at least n2 elements of CharT.
Chris@16 2277 //!
Chris@16 2278 //! <b>Throws</b>: out_of_range if pos1 > size()
Chris@16 2279 //!
Chris@16 2280 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
Chris@16 2281 int compare(size_type pos1, size_type n1, const CharT* s, size_type n2) const
Chris@16 2282 {
Chris@16 2283 if (pos1 > this->size())
Chris@16 2284 throw_out_of_range("basic_string::compare out of range position");
Chris@16 2285 const pointer addr = this->priv_addr();
Chris@16 2286 return s_compare( addr + pos1,
Chris@16 2287 addr + pos1 + container_detail::min_value(n1, this->size() - pos1),
Chris@16 2288 s, s + n2);
Chris@16 2289 }
Chris@16 2290
Chris@16 2291 //! <b>Requires</b>: pos1 > size() and s points to an array of at least traits::length(s) + 1 elements of CharT.
Chris@16 2292 //!
Chris@16 2293 //! <b>Throws</b>: out_of_range if pos1 > size()
Chris@16 2294 //!
Chris@16 2295 //! <b>Returns</b>: basic_string(*this, pos, n1).compare(basic_string(s, n2)).
Chris@16 2296 int compare(size_type pos1, size_type n1, const CharT* s) const
Chris@16 2297 { return this->compare(pos1, n1, s, Traits::length(s)); }
Chris@16 2298
Chris@16 2299 /// @cond
Chris@16 2300 private:
Chris@16 2301 static int s_compare(const_pointer f1, const_pointer l1,
Chris@16 2302 const_pointer f2, const_pointer l2)
Chris@16 2303 {
Chris@16 2304 const difference_type n1 = l1 - f1;
Chris@16 2305 const difference_type n2 = l2 - f2;
Chris@16 2306 const int cmp = Traits::compare(container_detail::to_raw_pointer(f1),
Chris@16 2307 container_detail::to_raw_pointer(f2),
Chris@16 2308 container_detail::min_value(n1, n2));
Chris@16 2309 return cmp != 0 ? cmp : (n1 < n2 ? -1 : (n1 > n2 ? 1 : 0));
Chris@16 2310 }
Chris@16 2311
Chris@16 2312 template<class AllocVersion>
Chris@16 2313 void priv_shrink_to_fit_dynamic_buffer
Chris@16 2314 ( AllocVersion
Chris@16 2315 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, allocator_v1> >::type* = 0)
Chris@16 2316 {
Chris@16 2317 //Allocate a new buffer.
Chris@16 2318 size_type real_cap = 0;
Chris@16 2319 const pointer long_addr = this->priv_long_addr();
Chris@16 2320 const size_type long_size = this->priv_long_size();
Chris@16 2321 const size_type long_storage = this->priv_long_storage();
Chris@16 2322 //We can make this nothrow as chars are always NoThrowCopyables
Chris@16 2323 BOOST_TRY{
Chris@16 2324 const std::pair<pointer, bool> ret = this->allocation_command
Chris@16 2325 (allocate_new, long_size+1, long_size+1, real_cap, long_addr);
Chris@16 2326 //Copy and update
Chris@16 2327 Traits::copy( container_detail::to_raw_pointer(ret.first)
Chris@16 2328 , container_detail::to_raw_pointer(this->priv_long_addr())
Chris@16 2329 , long_size+1);
Chris@16 2330 this->priv_long_addr(ret.first);
Chris@16 2331 this->priv_storage(real_cap);
Chris@16 2332 //And release old buffer
Chris@16 2333 this->alloc().deallocate(long_addr, long_storage);
Chris@16 2334 }
Chris@16 2335 BOOST_CATCH(...){
Chris@16 2336 return;
Chris@16 2337 }
Chris@16 2338 BOOST_CATCH_END
Chris@16 2339 }
Chris@16 2340
Chris@16 2341 template<class AllocVersion>
Chris@16 2342 void priv_shrink_to_fit_dynamic_buffer
Chris@16 2343 ( AllocVersion
Chris@16 2344 , typename container_detail::enable_if<container_detail::is_same<AllocVersion, allocator_v2> >::type* = 0)
Chris@16 2345 {
Chris@16 2346 size_type received_size;
Chris@16 2347 if(this->alloc().allocation_command
Chris@16 2348 ( shrink_in_place | nothrow_allocation
Chris@16 2349 , this->priv_long_storage(), this->priv_long_size()+1
Chris@16 2350 , received_size, this->priv_long_addr()).first){
Chris@16 2351 this->priv_storage(received_size);
Chris@16 2352 }
Chris@16 2353 }
Chris@16 2354
Chris@16 2355 void priv_construct_null(pointer p)
Chris@16 2356 { this->construct(p, CharT(0)); }
Chris@16 2357
Chris@16 2358 // Helper functions used by constructors. It is a severe error for
Chris@16 2359 // any of them to be called anywhere except from within constructors.
Chris@16 2360 void priv_terminate_string()
Chris@16 2361 { this->priv_construct_null(this->priv_end_addr()); }
Chris@16 2362
Chris@16 2363 template<class FwdIt, class Count> inline
Chris@16 2364 void priv_uninitialized_fill_n(FwdIt first, Count count, const CharT val)
Chris@16 2365 {
Chris@16 2366 //Save initial position
Chris@16 2367 FwdIt init = first;
Chris@16 2368
Chris@16 2369 BOOST_TRY{
Chris@16 2370 //Construct objects
Chris@16 2371 for (; count--; ++first){
Chris@16 2372 this->construct(first, val);
Chris@16 2373 }
Chris@16 2374 }
Chris@16 2375 BOOST_CATCH(...){
Chris@16 2376 //Call destructors
Chris@16 2377 for (; init != first; ++init){
Chris@16 2378 this->destroy(init);
Chris@16 2379 }
Chris@16 2380 BOOST_RETHROW
Chris@16 2381 }
Chris@16 2382 BOOST_CATCH_END
Chris@16 2383 }
Chris@16 2384
Chris@16 2385 template<class InpIt, class FwdIt> inline
Chris@16 2386 size_type priv_uninitialized_copy(InpIt first, InpIt last, FwdIt dest)
Chris@16 2387 {
Chris@16 2388 //Save initial destination position
Chris@16 2389 FwdIt dest_init = dest;
Chris@16 2390 size_type constructed = 0;
Chris@16 2391
Chris@16 2392 BOOST_TRY{
Chris@16 2393 //Try to build objects
Chris@16 2394 for (; first != last; ++dest, ++first, ++constructed){
Chris@16 2395 this->construct(dest, *first);
Chris@16 2396 }
Chris@16 2397 }
Chris@16 2398 BOOST_CATCH(...){
Chris@16 2399 //Call destructors
Chris@16 2400 for (; constructed--; ++dest_init){
Chris@16 2401 this->destroy(dest_init);
Chris@16 2402 }
Chris@16 2403 BOOST_RETHROW
Chris@16 2404 }
Chris@16 2405 BOOST_CATCH_END
Chris@16 2406 return (constructed);
Chris@16 2407 }
Chris@16 2408
Chris@16 2409 template <class InputIterator, class OutIterator>
Chris@16 2410 void priv_copy(InputIterator first, InputIterator last, OutIterator result)
Chris@16 2411 {
Chris@16 2412 for ( ; first != last; ++first, ++result)
Chris@16 2413 Traits::assign(*result, *first);
Chris@16 2414 }
Chris@16 2415
Chris@16 2416 void priv_copy(const CharT* first, const CharT* last, CharT* result)
Chris@16 2417 { Traits::copy(result, first, last - first); }
Chris@16 2418
Chris@16 2419 template <class Integer>
Chris@16 2420 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
Chris@16 2421 Integer n, Integer x,
Chris@16 2422 container_detail::true_)
Chris@16 2423 { return this->replace(first, last, (size_type) n, (CharT) x); }
Chris@16 2424
Chris@16 2425 template <class InputIter>
Chris@16 2426 basic_string& priv_replace_dispatch(const_iterator first, const_iterator last,
Chris@16 2427 InputIter f, InputIter l,
Chris@16 2428 container_detail::false_)
Chris@16 2429 {
Chris@16 2430 typedef typename std::iterator_traits<InputIter>::iterator_category Category;
Chris@16 2431 return this->priv_replace(first, last, f, l, Category());
Chris@16 2432 }
Chris@16 2433
Chris@16 2434 /// @endcond
Chris@16 2435 };
Chris@16 2436
Chris@16 2437 //!Typedef for a basic_string of
Chris@16 2438 //!narrow characters
Chris@16 2439 typedef basic_string
Chris@16 2440 <char
Chris@16 2441 ,std::char_traits<char>
Chris@16 2442 ,std::allocator<char> >
Chris@16 2443 string;
Chris@16 2444
Chris@16 2445 //!Typedef for a basic_string of
Chris@16 2446 //!narrow characters
Chris@16 2447 typedef basic_string
Chris@16 2448 <wchar_t
Chris@16 2449 ,std::char_traits<wchar_t>
Chris@16 2450 ,std::allocator<wchar_t> >
Chris@16 2451 wstring;
Chris@16 2452
Chris@16 2453 // ------------------------------------------------------------
Chris@16 2454 // Non-member functions.
Chris@16 2455
Chris@16 2456 // Operator+
Chris@16 2457
Chris@16 2458 template <class CharT, class Traits, class Allocator> inline
Chris@16 2459 basic_string<CharT,Traits,Allocator>
Chris@16 2460 operator+(const basic_string<CharT,Traits,Allocator>& x
Chris@16 2461 ,const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2462 {
Chris@16 2463 typedef basic_string<CharT,Traits,Allocator> str_t;
Chris@16 2464 typedef typename str_t::reserve_t reserve_t;
Chris@16 2465 reserve_t reserve;
Chris@16 2466 str_t result(reserve, x.size() + y.size(), x.get_stored_allocator());
Chris@16 2467 result.append(x);
Chris@16 2468 result.append(y);
Chris@16 2469 return result;
Chris@16 2470 }
Chris@16 2471
Chris@16 2472 template <class CharT, class Traits, class Allocator> inline
Chris@16 2473 basic_string<CharT, Traits, Allocator> operator+
Chris@16 2474 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END mx
Chris@16 2475 , BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END my)
Chris@16 2476 {
Chris@16 2477 mx += my;
Chris@16 2478 return boost::move(mx);
Chris@16 2479 }
Chris@16 2480
Chris@16 2481 template <class CharT, class Traits, class Allocator> inline
Chris@16 2482 basic_string<CharT, Traits, Allocator> operator+
Chris@16 2483 ( BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END mx
Chris@16 2484 , const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2485 {
Chris@16 2486 mx += y;
Chris@16 2487 return boost::move(mx);
Chris@16 2488 }
Chris@16 2489
Chris@16 2490 template <class CharT, class Traits, class Allocator> inline
Chris@16 2491 basic_string<CharT, Traits, Allocator> operator+
Chris@16 2492 (const basic_string<CharT,Traits,Allocator>& x
Chris@16 2493 ,BOOST_RV_REF_BEG basic_string<CharT, Traits, Allocator> BOOST_RV_REF_END my)
Chris@16 2494 {
Chris@16 2495 my.insert(my.begin(), x.begin(), x.end());
Chris@16 2496 return boost::move(my);
Chris@16 2497 }
Chris@16 2498
Chris@16 2499 template <class CharT, class Traits, class Allocator> inline
Chris@16 2500 basic_string<CharT, Traits, Allocator> operator+
Chris@16 2501 (const CharT* s, basic_string<CharT, Traits, Allocator> y)
Chris@16 2502 {
Chris@16 2503 y.insert(y.begin(), s, s + Traits::length(s));
Chris@16 2504 return y;
Chris@16 2505 }
Chris@16 2506
Chris@16 2507 template <class CharT, class Traits, class Allocator> inline
Chris@16 2508 basic_string<CharT,Traits,Allocator> operator+
Chris@16 2509 (basic_string<CharT,Traits,Allocator> x, const CharT* s)
Chris@16 2510 {
Chris@16 2511 x += s;
Chris@16 2512 return x;
Chris@16 2513 }
Chris@16 2514
Chris@16 2515 template <class CharT, class Traits, class Allocator> inline
Chris@16 2516 basic_string<CharT,Traits,Allocator> operator+
Chris@16 2517 (CharT c, basic_string<CharT,Traits,Allocator> y)
Chris@16 2518 {
Chris@16 2519 y.insert(y.begin(), c);
Chris@16 2520 return y;
Chris@16 2521 }
Chris@16 2522
Chris@16 2523 template <class CharT, class Traits, class Allocator> inline
Chris@16 2524 basic_string<CharT,Traits,Allocator> operator+
Chris@16 2525 (basic_string<CharT,Traits,Allocator> x, const CharT c)
Chris@16 2526 {
Chris@16 2527 x += c;
Chris@16 2528 return x;
Chris@16 2529 }
Chris@16 2530
Chris@16 2531 // Operator== and operator!=
Chris@16 2532
Chris@16 2533 template <class CharT, class Traits, class Allocator>
Chris@16 2534 inline bool
Chris@16 2535 operator==(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2536 const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2537 {
Chris@16 2538 return x.size() == y.size() &&
Chris@16 2539 Traits::compare(x.data(), y.data(), x.size()) == 0;
Chris@16 2540 }
Chris@16 2541
Chris@16 2542 template <class CharT, class Traits, class Allocator>
Chris@16 2543 inline bool
Chris@16 2544 operator==(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2545 {
Chris@16 2546 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
Chris@16 2547 return n == y.size() && Traits::compare(s, y.data(), n) == 0;
Chris@16 2548 }
Chris@16 2549
Chris@16 2550 template <class CharT, class Traits, class Allocator>
Chris@16 2551 inline bool
Chris@16 2552 operator==(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
Chris@16 2553 {
Chris@16 2554 typename basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
Chris@16 2555 return x.size() == n && Traits::compare(x.data(), s, n) == 0;
Chris@16 2556 }
Chris@16 2557
Chris@16 2558 template <class CharT, class Traits, class Allocator>
Chris@16 2559 inline bool
Chris@16 2560 operator!=(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2561 const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2562 { return !(x == y); }
Chris@16 2563
Chris@16 2564 template <class CharT, class Traits, class Allocator>
Chris@16 2565 inline bool
Chris@16 2566 operator!=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2567 { return !(s == y); }
Chris@16 2568
Chris@16 2569 template <class CharT, class Traits, class Allocator>
Chris@16 2570 inline bool
Chris@16 2571 operator!=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
Chris@16 2572 { return !(x == s); }
Chris@16 2573
Chris@16 2574
Chris@16 2575 // Operator< (and also >, <=, and >=).
Chris@16 2576
Chris@16 2577 template <class CharT, class Traits, class Allocator>
Chris@16 2578 inline bool
Chris@16 2579 operator<(const basic_string<CharT,Traits,Allocator>& x, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2580 {
Chris@16 2581 return x.compare(y) < 0;
Chris@16 2582 // return basic_string<CharT,Traits,Allocator>
Chris@16 2583 // ::s_compare(x.begin(), x.end(), y.begin(), y.end()) < 0;
Chris@16 2584 }
Chris@16 2585
Chris@16 2586 template <class CharT, class Traits, class Allocator>
Chris@16 2587 inline bool
Chris@16 2588 operator<(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2589 {
Chris@16 2590 return y.compare(s) > 0;
Chris@16 2591 // basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
Chris@16 2592 // return basic_string<CharT,Traits,Allocator>
Chris@16 2593 // ::s_compare(s, s + n, y.begin(), y.end()) < 0;
Chris@16 2594 }
Chris@16 2595
Chris@16 2596 template <class CharT, class Traits, class Allocator>
Chris@16 2597 inline bool
Chris@16 2598 operator<(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2599 const CharT* s)
Chris@16 2600 {
Chris@16 2601 return x.compare(s) < 0;
Chris@16 2602 // basic_string<CharT,Traits,Allocator>::size_type n = Traits::length(s);
Chris@16 2603 // return basic_string<CharT,Traits,Allocator>
Chris@16 2604 // ::s_compare(x.begin(), x.end(), s, s + n) < 0;
Chris@16 2605 }
Chris@16 2606
Chris@16 2607 template <class CharT, class Traits, class Allocator>
Chris@16 2608 inline bool
Chris@16 2609 operator>(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2610 const basic_string<CharT,Traits,Allocator>& y) {
Chris@16 2611 return y < x;
Chris@16 2612 }
Chris@16 2613
Chris@16 2614 template <class CharT, class Traits, class Allocator>
Chris@16 2615 inline bool
Chris@16 2616 operator>(const CharT* s, const basic_string<CharT,Traits,Allocator>& y) {
Chris@16 2617 return y < s;
Chris@16 2618 }
Chris@16 2619
Chris@16 2620 template <class CharT, class Traits, class Allocator>
Chris@16 2621 inline bool
Chris@16 2622 operator>(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
Chris@16 2623 {
Chris@16 2624 return s < x;
Chris@16 2625 }
Chris@16 2626
Chris@16 2627 template <class CharT, class Traits, class Allocator>
Chris@16 2628 inline bool
Chris@16 2629 operator<=(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2630 const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2631 {
Chris@16 2632 return !(y < x);
Chris@16 2633 }
Chris@16 2634
Chris@16 2635 template <class CharT, class Traits, class Allocator>
Chris@16 2636 inline bool
Chris@16 2637 operator<=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2638 { return !(y < s); }
Chris@16 2639
Chris@16 2640 template <class CharT, class Traits, class Allocator>
Chris@16 2641 inline bool
Chris@16 2642 operator<=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
Chris@16 2643 { return !(s < x); }
Chris@16 2644
Chris@16 2645 template <class CharT, class Traits, class Allocator>
Chris@16 2646 inline bool
Chris@16 2647 operator>=(const basic_string<CharT,Traits,Allocator>& x,
Chris@16 2648 const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2649 { return !(x < y); }
Chris@16 2650
Chris@16 2651 template <class CharT, class Traits, class Allocator>
Chris@16 2652 inline bool
Chris@16 2653 operator>=(const CharT* s, const basic_string<CharT,Traits,Allocator>& y)
Chris@16 2654 { return !(s < y); }
Chris@16 2655
Chris@16 2656 template <class CharT, class Traits, class Allocator>
Chris@16 2657 inline bool
Chris@16 2658 operator>=(const basic_string<CharT,Traits,Allocator>& x, const CharT* s)
Chris@16 2659 { return !(x < s); }
Chris@16 2660
Chris@16 2661 // Swap.
Chris@16 2662 template <class CharT, class Traits, class Allocator>
Chris@16 2663 inline void swap(basic_string<CharT,Traits,Allocator>& x, basic_string<CharT,Traits,Allocator>& y)
Chris@16 2664 { x.swap(y); }
Chris@16 2665
Chris@16 2666 /// @cond
Chris@16 2667 // I/O.
Chris@16 2668 namespace container_detail {
Chris@16 2669
Chris@16 2670 template <class CharT, class Traits>
Chris@16 2671 inline bool
Chris@16 2672 string_fill(std::basic_ostream<CharT, Traits>& os,
Chris@16 2673 std::basic_streambuf<CharT, Traits>* buf,
Chris@16 2674 std::size_t n)
Chris@16 2675 {
Chris@16 2676 CharT f = os.fill();
Chris@16 2677 std::size_t i;
Chris@16 2678 bool ok = true;
Chris@16 2679
Chris@16 2680 for (i = 0; i < n; i++)
Chris@16 2681 ok = ok && !Traits::eq_int_type(buf->sputc(f), Traits::eof());
Chris@16 2682 return ok;
Chris@16 2683 }
Chris@16 2684
Chris@16 2685 } //namespace container_detail {
Chris@16 2686 /// @endcond
Chris@16 2687
Chris@16 2688 template <class CharT, class Traits, class Allocator>
Chris@16 2689 std::basic_ostream<CharT, Traits>&
Chris@16 2690 operator<<(std::basic_ostream<CharT, Traits>& os, const basic_string<CharT,Traits,Allocator>& s)
Chris@16 2691 {
Chris@16 2692 typename std::basic_ostream<CharT, Traits>::sentry sentry(os);
Chris@16 2693 bool ok = false;
Chris@16 2694
Chris@16 2695 if (sentry) {
Chris@16 2696 ok = true;
Chris@16 2697 typename basic_string<CharT,Traits,Allocator>::size_type n = s.size();
Chris@16 2698 typename basic_string<CharT,Traits,Allocator>::size_type pad_len = 0;
Chris@16 2699 const bool left = (os.flags() & std::ios::left) != 0;
Chris@16 2700 const std::size_t w = os.width(0);
Chris@16 2701 std::basic_streambuf<CharT, Traits>* buf = os.rdbuf();
Chris@16 2702
Chris@16 2703 if (w != 0 && n < w)
Chris@16 2704 pad_len = w - n;
Chris@16 2705
Chris@16 2706 if (!left)
Chris@16 2707 ok = container_detail::string_fill(os, buf, pad_len);
Chris@16 2708
Chris@16 2709 ok = ok &&
Chris@16 2710 buf->sputn(s.data(), std::streamsize(n)) == std::streamsize(n);
Chris@16 2711
Chris@16 2712 if (left)
Chris@16 2713 ok = ok && container_detail::string_fill(os, buf, pad_len);
Chris@16 2714 }
Chris@16 2715
Chris@16 2716 if (!ok)
Chris@16 2717 os.setstate(std::ios_base::failbit);
Chris@16 2718
Chris@16 2719 return os;
Chris@16 2720 }
Chris@16 2721
Chris@16 2722
Chris@16 2723 template <class CharT, class Traits, class Allocator>
Chris@16 2724 std::basic_istream<CharT, Traits>&
Chris@16 2725 operator>>(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
Chris@16 2726 {
Chris@16 2727 typename std::basic_istream<CharT, Traits>::sentry sentry(is);
Chris@16 2728
Chris@16 2729 if (sentry) {
Chris@16 2730 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
Chris@16 2731 const std::ctype<CharT>& ctype = std::use_facet<std::ctype<CharT> >(is.getloc());
Chris@16 2732
Chris@16 2733 s.clear();
Chris@16 2734 std::size_t n = is.width(0);
Chris@16 2735 if (n == 0)
Chris@16 2736 n = static_cast<std::size_t>(-1);
Chris@16 2737 else
Chris@16 2738 s.reserve(n);
Chris@16 2739
Chris@16 2740 while (n-- > 0) {
Chris@16 2741 typename Traits::int_type c1 = buf->sbumpc();
Chris@16 2742
Chris@16 2743 if (Traits::eq_int_type(c1, Traits::eof())) {
Chris@16 2744 is.setstate(std::ios_base::eofbit);
Chris@16 2745 break;
Chris@16 2746 }
Chris@16 2747 else {
Chris@16 2748 CharT c = Traits::to_char_type(c1);
Chris@16 2749
Chris@16 2750 if (ctype.is(std::ctype<CharT>::space, c)) {
Chris@16 2751 if (Traits::eq_int_type(buf->sputbackc(c), Traits::eof()))
Chris@16 2752 is.setstate(std::ios_base::failbit);
Chris@16 2753 break;
Chris@16 2754 }
Chris@16 2755 else
Chris@16 2756 s.push_back(c);
Chris@16 2757 }
Chris@16 2758 }
Chris@16 2759
Chris@16 2760 // If we have read no characters, then set failbit.
Chris@16 2761 if (s.size() == 0)
Chris@16 2762 is.setstate(std::ios_base::failbit);
Chris@16 2763 }
Chris@16 2764 else
Chris@16 2765 is.setstate(std::ios_base::failbit);
Chris@16 2766
Chris@16 2767 return is;
Chris@16 2768 }
Chris@16 2769
Chris@16 2770 template <class CharT, class Traits, class Allocator>
Chris@16 2771 std::basic_istream<CharT, Traits>&
Chris@16 2772 getline(std::istream& is, basic_string<CharT,Traits,Allocator>& s,CharT delim)
Chris@16 2773 {
Chris@16 2774 typename basic_string<CharT,Traits,Allocator>::size_type nread = 0;
Chris@16 2775 typename std::basic_istream<CharT, Traits>::sentry sentry(is, true);
Chris@16 2776 if (sentry) {
Chris@16 2777 std::basic_streambuf<CharT, Traits>* buf = is.rdbuf();
Chris@16 2778 s.clear();
Chris@16 2779
Chris@16 2780 while (nread < s.max_size()) {
Chris@16 2781 int c1 = buf->sbumpc();
Chris@16 2782 if (Traits::eq_int_type(c1, Traits::eof())) {
Chris@16 2783 is.setstate(std::ios_base::eofbit);
Chris@16 2784 break;
Chris@16 2785 }
Chris@16 2786 else {
Chris@16 2787 ++nread;
Chris@16 2788 CharT c = Traits::to_char_type(c1);
Chris@16 2789 if (!Traits::eq(c, delim))
Chris@16 2790 s.push_back(c);
Chris@16 2791 else
Chris@16 2792 break; // Character is extracted but not appended.
Chris@16 2793 }
Chris@16 2794 }
Chris@16 2795 }
Chris@16 2796 if (nread == 0 || nread >= s.max_size())
Chris@16 2797 is.setstate(std::ios_base::failbit);
Chris@16 2798
Chris@16 2799 return is;
Chris@16 2800 }
Chris@16 2801
Chris@16 2802 template <class CharT, class Traits, class Allocator>
Chris@16 2803 inline std::basic_istream<CharT, Traits>&
Chris@16 2804 getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT,Traits,Allocator>& s)
Chris@16 2805 {
Chris@16 2806 return getline(is, s, '\n');
Chris@16 2807 }
Chris@16 2808
Chris@16 2809 template <class Ch, class Allocator>
Chris@16 2810 inline std::size_t hash_value(basic_string<Ch, std::char_traits<Ch>, Allocator> const& v)
Chris@16 2811 {
Chris@16 2812 return hash_range(v.begin(), v.end());
Chris@16 2813 }
Chris@16 2814
Chris@16 2815 }}
Chris@16 2816
Chris@16 2817 /// @cond
Chris@16 2818
Chris@16 2819 namespace boost {
Chris@16 2820
Chris@16 2821 //!has_trivial_destructor_after_move<> == true_type
Chris@16 2822 //!specialization for optimizations
Chris@16 2823 template <class C, class T, class Allocator>
Chris@16 2824 struct has_trivial_destructor_after_move<boost::container::basic_string<C, T, Allocator> >
Chris@16 2825 : public ::boost::has_trivial_destructor_after_move<Allocator>
Chris@16 2826 {};
Chris@16 2827
Chris@16 2828 }
Chris@16 2829
Chris@16 2830 /// @endcond
Chris@16 2831
Chris@16 2832 #include <boost/container/detail/config_end.hpp>
Chris@16 2833
Chris@16 2834 #endif // BOOST_CONTAINER_STRING_HPP