rc_string_base.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. // Reference-counted versatile string base -*- C++ -*-
  2. // Copyright (C) 2005-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/rc_string_base.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ext/vstring.h}
  23. */
  24. #ifndef _RC_STRING_BASE_H
  25. #define _RC_STRING_BASE_H 1
  26. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  27. #include <ext/atomicity.h>
  28. #include <ext/alloc_traits.h>
  29. #include <bits/stl_iterator_base_funcs.h>
  30. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. /**
  34. * Documentation? What's that?
  35. * Nathan Myers <ncm@cantrip.org>.
  36. *
  37. * A string looks like this:
  38. *
  39. * @code
  40. * [_Rep]
  41. * _M_length
  42. * [__rc_string_base<char_type>] _M_capacity
  43. * _M_dataplus _M_refcount
  44. * _M_p ----------------> unnamed array of char_type
  45. * @endcode
  46. *
  47. * Where the _M_p points to the first character in the string, and
  48. * you cast it to a pointer-to-_Rep and subtract 1 to get a
  49. * pointer to the header.
  50. *
  51. * This approach has the enormous advantage that a string object
  52. * requires only one allocation. All the ugliness is confined
  53. * within a single pair of inline functions, which each compile to
  54. * a single @a add instruction: _Rep::_M_refdata(), and
  55. * __rc_string_base::_M_rep(); and the allocation function which gets a
  56. * block of raw bytes and with room enough and constructs a _Rep
  57. * object at the front.
  58. *
  59. * The reason you want _M_data pointing to the character array and
  60. * not the _Rep is so that the debugger can see the string
  61. * contents. (Probably we should add a non-inline member to get
  62. * the _Rep for the debugger to use, so users can check the actual
  63. * string length.)
  64. *
  65. * Note that the _Rep object is a POD so that you can have a
  66. * static <em>empty string</em> _Rep object already @a constructed before
  67. * static constructors have run. The reference-count encoding is
  68. * chosen so that a 0 indicates one reference, so you never try to
  69. * destroy the empty-string _Rep object.
  70. *
  71. * All but the last paragraph is considered pretty conventional
  72. * for a C++ string implementation.
  73. */
  74. template<typename _CharT, typename _Traits, typename _Alloc>
  75. class __rc_string_base
  76. : protected __vstring_utility<_CharT, _Traits, _Alloc>
  77. {
  78. public:
  79. typedef _Traits traits_type;
  80. typedef typename _Traits::char_type value_type;
  81. typedef _Alloc allocator_type;
  82. typedef __vstring_utility<_CharT, _Traits, _Alloc> _Util_Base;
  83. typedef typename _Util_Base::_CharT_alloc_type _CharT_alloc_type;
  84. typedef typename _CharT_alloc_type::size_type size_type;
  85. private:
  86. // _Rep: string representation
  87. // Invariants:
  88. // 1. String really contains _M_length + 1 characters: due to 21.3.4
  89. // must be kept null-terminated.
  90. // 2. _M_capacity >= _M_length
  91. // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
  92. // 3. _M_refcount has three states:
  93. // -1: leaked, one reference, no ref-copies allowed, non-const.
  94. // 0: one reference, non-const.
  95. // n>0: n + 1 references, operations require a lock, const.
  96. // 4. All fields == 0 is an empty string, given the extra storage
  97. // beyond-the-end for a null terminator; thus, the shared
  98. // empty string representation needs no constructor.
  99. struct _Rep
  100. {
  101. union
  102. {
  103. struct
  104. {
  105. size_type _M_length;
  106. size_type _M_capacity;
  107. _Atomic_word _M_refcount;
  108. } _M_info;
  109. // Only for alignment purposes.
  110. _CharT _M_align;
  111. };
  112. typedef typename __alloc_traits<_Alloc>::template rebind<_Rep>::other
  113. _Rep_alloc_type;
  114. _CharT*
  115. _M_refdata() throw()
  116. { return reinterpret_cast<_CharT*>(this + 1); }
  117. _CharT*
  118. _M_refcopy() throw()
  119. {
  120. __atomic_add_dispatch(&_M_info._M_refcount, 1);
  121. return _M_refdata();
  122. } // XXX MT
  123. void
  124. _M_set_length(size_type __n)
  125. {
  126. _M_info._M_refcount = 0; // One reference.
  127. _M_info._M_length = __n;
  128. // grrr. (per 21.3.4)
  129. // You cannot leave those LWG people alone for a second.
  130. traits_type::assign(_M_refdata()[__n], _CharT());
  131. }
  132. // Create & Destroy
  133. static _Rep*
  134. _S_create(size_type, size_type, const _Alloc&);
  135. void
  136. _M_destroy(const _Alloc&) throw();
  137. _CharT*
  138. _M_clone(const _Alloc&, size_type __res = 0);
  139. };
  140. struct _Rep_empty
  141. : public _Rep
  142. {
  143. _CharT _M_terminal;
  144. };
  145. static _Rep_empty _S_empty_rep;
  146. // The maximum number of individual char_type elements of an
  147. // individual string is determined by _S_max_size. This is the
  148. // value that will be returned by max_size(). (Whereas npos
  149. // is the maximum number of bytes the allocator can allocate.)
  150. // If one was to divvy up the theoretical largest size string,
  151. // with a terminating character and m _CharT elements, it'd
  152. // look like this:
  153. // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
  154. // + sizeof(_Rep) - 1
  155. // (NB: last two terms for rounding reasons, see _M_create below)
  156. // Solving for m:
  157. // m = ((npos - 2 * sizeof(_Rep) + 1) / sizeof(_CharT)) - 1
  158. // In addition, this implementation halves this amount.
  159. enum { _S_max_size = (((static_cast<size_type>(-1) - 2 * sizeof(_Rep)
  160. + 1) / sizeof(_CharT)) - 1) / 2 };
  161. // Data Member (private):
  162. mutable typename _Util_Base::template _Alloc_hider<_Alloc> _M_dataplus;
  163. void
  164. _M_data(_CharT* __p)
  165. { _M_dataplus._M_p = __p; }
  166. _Rep*
  167. _M_rep() const
  168. { return &((reinterpret_cast<_Rep*>(_M_data()))[-1]); }
  169. _CharT*
  170. _M_grab(const _Alloc& __alloc) const
  171. {
  172. return (!_M_is_leaked() && _M_get_allocator() == __alloc)
  173. ? _M_rep()->_M_refcopy() : _M_rep()->_M_clone(__alloc);
  174. }
  175. void
  176. _M_dispose()
  177. {
  178. // Be race-detector-friendly. For more info see bits/c++config.
  179. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_rep()->_M_info.
  180. _M_refcount);
  181. if (__exchange_and_add_dispatch(&_M_rep()->_M_info._M_refcount,
  182. -1) <= 0)
  183. {
  184. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_rep()->_M_info.
  185. _M_refcount);
  186. _M_rep()->_M_destroy(_M_get_allocator());
  187. }
  188. } // XXX MT
  189. bool
  190. _M_is_leaked() const
  191. { return _M_rep()->_M_info._M_refcount < 0; }
  192. void
  193. _M_set_sharable()
  194. { _M_rep()->_M_info._M_refcount = 0; }
  195. void
  196. _M_leak_hard();
  197. // _S_construct_aux is used to implement the 21.3.1 para 15 which
  198. // requires special behaviour if _InIterator is an integral type
  199. template<typename _InIterator>
  200. static _CharT*
  201. _S_construct_aux(_InIterator __beg, _InIterator __end,
  202. const _Alloc& __a, std::__false_type)
  203. {
  204. typedef typename std::iterator_traits<_InIterator>::iterator_category
  205. _Tag;
  206. return _S_construct(__beg, __end, __a, _Tag());
  207. }
  208. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  209. // 438. Ambiguity in the "do the right thing" clause
  210. template<typename _Integer>
  211. static _CharT*
  212. _S_construct_aux(_Integer __beg, _Integer __end,
  213. const _Alloc& __a, std::__true_type)
  214. { return _S_construct_aux_2(static_cast<size_type>(__beg),
  215. __end, __a); }
  216. static _CharT*
  217. _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
  218. { return _S_construct(__req, __c, __a); }
  219. template<typename _InIterator>
  220. static _CharT*
  221. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
  222. {
  223. typedef typename std::__is_integer<_InIterator>::__type _Integral;
  224. return _S_construct_aux(__beg, __end, __a, _Integral());
  225. }
  226. // For Input Iterators, used in istreambuf_iterators, etc.
  227. template<typename _InIterator>
  228. static _CharT*
  229. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  230. std::input_iterator_tag);
  231. // For forward_iterators up to random_access_iterators, used for
  232. // string::iterator, _CharT*, etc.
  233. template<typename _FwdIterator>
  234. static _CharT*
  235. _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
  236. std::forward_iterator_tag);
  237. static _CharT*
  238. _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
  239. public:
  240. size_type
  241. _M_max_size() const
  242. { return size_type(_S_max_size); }
  243. _CharT*
  244. _M_data() const
  245. { return _M_dataplus._M_p; }
  246. size_type
  247. _M_length() const
  248. { return _M_rep()->_M_info._M_length; }
  249. size_type
  250. _M_capacity() const
  251. { return _M_rep()->_M_info._M_capacity; }
  252. bool
  253. _M_is_shared() const
  254. { return _M_rep()->_M_info._M_refcount > 0; }
  255. void
  256. _M_set_leaked()
  257. { _M_rep()->_M_info._M_refcount = -1; }
  258. void
  259. _M_leak() // for use in begin() & non-const op[]
  260. {
  261. if (!_M_is_leaked())
  262. _M_leak_hard();
  263. }
  264. void
  265. _M_set_length(size_type __n)
  266. { _M_rep()->_M_set_length(__n); }
  267. __rc_string_base()
  268. : _M_dataplus(_S_empty_rep._M_refcopy()) { }
  269. __rc_string_base(const _Alloc& __a);
  270. __rc_string_base(const __rc_string_base& __rcs);
  271. #if __cplusplus >= 201103L
  272. __rc_string_base(__rc_string_base&& __rcs)
  273. : _M_dataplus(__rcs._M_dataplus)
  274. { __rcs._M_data(_S_empty_rep._M_refcopy()); }
  275. #endif
  276. __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a);
  277. template<typename _InputIterator>
  278. __rc_string_base(_InputIterator __beg, _InputIterator __end,
  279. const _Alloc& __a);
  280. ~__rc_string_base()
  281. { _M_dispose(); }
  282. allocator_type&
  283. _M_get_allocator()
  284. { return _M_dataplus; }
  285. const allocator_type&
  286. _M_get_allocator() const
  287. { return _M_dataplus; }
  288. void
  289. _M_swap(__rc_string_base& __rcs);
  290. void
  291. _M_assign(const __rc_string_base& __rcs);
  292. void
  293. _M_reserve(size_type __res);
  294. void
  295. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  296. size_type __len2);
  297. void
  298. _M_erase(size_type __pos, size_type __n);
  299. void
  300. _M_clear()
  301. {
  302. _M_dispose();
  303. _M_data(_S_empty_rep._M_refcopy());
  304. }
  305. bool
  306. _M_compare(const __rc_string_base&) const
  307. { return false; }
  308. };
  309. template<typename _CharT, typename _Traits, typename _Alloc>
  310. typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep_empty
  311. __rc_string_base<_CharT, _Traits, _Alloc>::_S_empty_rep;
  312. template<typename _CharT, typename _Traits, typename _Alloc>
  313. typename __rc_string_base<_CharT, _Traits, _Alloc>::_Rep*
  314. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  315. _S_create(size_type __capacity, size_type __old_capacity,
  316. const _Alloc& __alloc)
  317. {
  318. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  319. // 83. String::npos vs. string::max_size()
  320. if (__capacity > size_type(_S_max_size))
  321. std::__throw_length_error(__N("__rc_string_base::_Rep::_S_create"));
  322. // The standard places no restriction on allocating more memory
  323. // than is strictly needed within this layer at the moment or as
  324. // requested by an explicit application call to reserve().
  325. // Many malloc implementations perform quite poorly when an
  326. // application attempts to allocate memory in a stepwise fashion
  327. // growing each allocation size by only 1 char. Additionally,
  328. // it makes little sense to allocate less linear memory than the
  329. // natural blocking size of the malloc implementation.
  330. // Unfortunately, we would need a somewhat low-level calculation
  331. // with tuned parameters to get this perfect for any particular
  332. // malloc implementation. Fortunately, generalizations about
  333. // common features seen among implementations seems to suffice.
  334. // __pagesize need not match the actual VM page size for good
  335. // results in practice, thus we pick a common value on the low
  336. // side. __malloc_header_size is an estimate of the amount of
  337. // overhead per memory allocation (in practice seen N * sizeof
  338. // (void*) where N is 0, 2 or 4). According to folklore,
  339. // picking this value on the high side is better than
  340. // low-balling it (especially when this algorithm is used with
  341. // malloc implementations that allocate memory blocks rounded up
  342. // to a size which is a power of 2).
  343. const size_type __pagesize = 4096;
  344. const size_type __malloc_header_size = 4 * sizeof(void*);
  345. // The below implements an exponential growth policy, necessary to
  346. // meet amortized linear time requirements of the library: see
  347. // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
  348. if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
  349. {
  350. __capacity = 2 * __old_capacity;
  351. // Never allocate a string bigger than _S_max_size.
  352. if (__capacity > size_type(_S_max_size))
  353. __capacity = size_type(_S_max_size);
  354. }
  355. // NB: Need an array of char_type[__capacity], plus a terminating
  356. // null char_type() element, plus enough for the _Rep data structure,
  357. // plus sizeof(_Rep) - 1 to upper round to a size multiple of
  358. // sizeof(_Rep).
  359. // Whew. Seemingly so needy, yet so elemental.
  360. size_type __size = ((__capacity + 1) * sizeof(_CharT)
  361. + 2 * sizeof(_Rep) - 1);
  362. const size_type __adj_size = __size + __malloc_header_size;
  363. if (__adj_size > __pagesize && __capacity > __old_capacity)
  364. {
  365. const size_type __extra = __pagesize - __adj_size % __pagesize;
  366. __capacity += __extra / sizeof(_CharT);
  367. if (__capacity > size_type(_S_max_size))
  368. __capacity = size_type(_S_max_size);
  369. __size = (__capacity + 1) * sizeof(_CharT) + 2 * sizeof(_Rep) - 1;
  370. }
  371. // NB: Might throw, but no worries about a leak, mate: _Rep()
  372. // does not throw.
  373. _Rep* __place = _Rep_alloc_type(__alloc).allocate(__size / sizeof(_Rep));
  374. _Rep* __p = new (__place) _Rep;
  375. __p->_M_info._M_capacity = __capacity;
  376. return __p;
  377. }
  378. template<typename _CharT, typename _Traits, typename _Alloc>
  379. void
  380. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  381. _M_destroy(const _Alloc& __a) throw ()
  382. {
  383. const size_type __size = ((_M_info._M_capacity + 1) * sizeof(_CharT)
  384. + 2 * sizeof(_Rep) - 1);
  385. _Rep_alloc_type(__a).deallocate(this, __size / sizeof(_Rep));
  386. }
  387. template<typename _CharT, typename _Traits, typename _Alloc>
  388. _CharT*
  389. __rc_string_base<_CharT, _Traits, _Alloc>::_Rep::
  390. _M_clone(const _Alloc& __alloc, size_type __res)
  391. {
  392. // Requested capacity of the clone.
  393. const size_type __requested_cap = _M_info._M_length + __res;
  394. _Rep* __r = _Rep::_S_create(__requested_cap, _M_info._M_capacity,
  395. __alloc);
  396. if (_M_info._M_length)
  397. __rc_string_base::_S_copy(__r->_M_refdata(), _M_refdata(), _M_info._M_length);
  398. __r->_M_set_length(_M_info._M_length);
  399. return __r->_M_refdata();
  400. }
  401. template<typename _CharT, typename _Traits, typename _Alloc>
  402. __rc_string_base<_CharT, _Traits, _Alloc>::
  403. __rc_string_base(const _Alloc& __a)
  404. : _M_dataplus(__a, _S_construct(size_type(), _CharT(), __a)) { }
  405. template<typename _CharT, typename _Traits, typename _Alloc>
  406. __rc_string_base<_CharT, _Traits, _Alloc>::
  407. __rc_string_base(const __rc_string_base& __rcs)
  408. : _M_dataplus(__rcs._M_get_allocator(),
  409. __rcs._M_grab(__rcs._M_get_allocator())) { }
  410. template<typename _CharT, typename _Traits, typename _Alloc>
  411. __rc_string_base<_CharT, _Traits, _Alloc>::
  412. __rc_string_base(size_type __n, _CharT __c, const _Alloc& __a)
  413. : _M_dataplus(__a, _S_construct(__n, __c, __a)) { }
  414. template<typename _CharT, typename _Traits, typename _Alloc>
  415. template<typename _InputIterator>
  416. __rc_string_base<_CharT, _Traits, _Alloc>::
  417. __rc_string_base(_InputIterator __beg, _InputIterator __end,
  418. const _Alloc& __a)
  419. : _M_dataplus(__a, _S_construct(__beg, __end, __a)) { }
  420. template<typename _CharT, typename _Traits, typename _Alloc>
  421. void
  422. __rc_string_base<_CharT, _Traits, _Alloc>::
  423. _M_leak_hard()
  424. {
  425. if (_M_is_shared())
  426. _M_erase(0, 0);
  427. _M_set_leaked();
  428. }
  429. // NB: This is the special case for Input Iterators, used in
  430. // istreambuf_iterators, etc.
  431. // Input Iterators have a cost structure very different from
  432. // pointers, calling for a different coding style.
  433. template<typename _CharT, typename _Traits, typename _Alloc>
  434. template<typename _InIterator>
  435. _CharT*
  436. __rc_string_base<_CharT, _Traits, _Alloc>::
  437. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  438. std::input_iterator_tag)
  439. {
  440. if (__beg == __end && __a == _Alloc())
  441. return _S_empty_rep._M_refcopy();
  442. // Avoid reallocation for common case.
  443. _CharT __buf[128];
  444. size_type __len = 0;
  445. while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
  446. {
  447. __buf[__len++] = *__beg;
  448. ++__beg;
  449. }
  450. _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
  451. _S_copy(__r->_M_refdata(), __buf, __len);
  452. __try
  453. {
  454. while (__beg != __end)
  455. {
  456. if (__len == __r->_M_info._M_capacity)
  457. {
  458. // Allocate more space.
  459. _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
  460. _S_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
  461. __r->_M_destroy(__a);
  462. __r = __another;
  463. }
  464. __r->_M_refdata()[__len++] = *__beg;
  465. ++__beg;
  466. }
  467. }
  468. __catch(...)
  469. {
  470. __r->_M_destroy(__a);
  471. __throw_exception_again;
  472. }
  473. __r->_M_set_length(__len);
  474. return __r->_M_refdata();
  475. }
  476. template<typename _CharT, typename _Traits, typename _Alloc>
  477. template<typename _InIterator>
  478. _CharT*
  479. __rc_string_base<_CharT, _Traits, _Alloc>::
  480. _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
  481. std::forward_iterator_tag)
  482. {
  483. if (__beg == __end && __a == _Alloc())
  484. return _S_empty_rep._M_refcopy();
  485. // NB: Not required, but considered best practice.
  486. if (__is_null_pointer(__beg) && __beg != __end)
  487. std::__throw_logic_error(__N("__rc_string_base::"
  488. "_S_construct null not valid"));
  489. const size_type __dnew = static_cast<size_type>(std::distance(__beg,
  490. __end));
  491. // Check for out_of_range and length_error exceptions.
  492. _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
  493. __try
  494. { __rc_string_base::_S_copy_chars(__r->_M_refdata(), __beg, __end); }
  495. __catch(...)
  496. {
  497. __r->_M_destroy(__a);
  498. __throw_exception_again;
  499. }
  500. __r->_M_set_length(__dnew);
  501. return __r->_M_refdata();
  502. }
  503. template<typename _CharT, typename _Traits, typename _Alloc>
  504. _CharT*
  505. __rc_string_base<_CharT, _Traits, _Alloc>::
  506. _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
  507. {
  508. if (__n == 0 && __a == _Alloc())
  509. return _S_empty_rep._M_refcopy();
  510. // Check for out_of_range and length_error exceptions.
  511. _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
  512. if (__n)
  513. __rc_string_base::_S_assign(__r->_M_refdata(), __n, __c);
  514. __r->_M_set_length(__n);
  515. return __r->_M_refdata();
  516. }
  517. template<typename _CharT, typename _Traits, typename _Alloc>
  518. void
  519. __rc_string_base<_CharT, _Traits, _Alloc>::
  520. _M_swap(__rc_string_base& __rcs)
  521. {
  522. if (_M_is_leaked())
  523. _M_set_sharable();
  524. if (__rcs._M_is_leaked())
  525. __rcs._M_set_sharable();
  526. _CharT* __tmp = _M_data();
  527. _M_data(__rcs._M_data());
  528. __rcs._M_data(__tmp);
  529. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  530. // 431. Swapping containers with unequal allocators.
  531. std::__alloc_swap<allocator_type>::_S_do_it(_M_get_allocator(),
  532. __rcs._M_get_allocator());
  533. }
  534. template<typename _CharT, typename _Traits, typename _Alloc>
  535. void
  536. __rc_string_base<_CharT, _Traits, _Alloc>::
  537. _M_assign(const __rc_string_base& __rcs)
  538. {
  539. if (_M_rep() != __rcs._M_rep())
  540. {
  541. _CharT* __tmp = __rcs._M_grab(_M_get_allocator());
  542. _M_dispose();
  543. _M_data(__tmp);
  544. }
  545. }
  546. template<typename _CharT, typename _Traits, typename _Alloc>
  547. void
  548. __rc_string_base<_CharT, _Traits, _Alloc>::
  549. _M_reserve(size_type __res)
  550. {
  551. // Make sure we don't shrink below the current size.
  552. if (__res < _M_length())
  553. __res = _M_length();
  554. if (__res != _M_capacity() || _M_is_shared())
  555. {
  556. _CharT* __tmp = _M_rep()->_M_clone(_M_get_allocator(),
  557. __res - _M_length());
  558. _M_dispose();
  559. _M_data(__tmp);
  560. }
  561. }
  562. template<typename _CharT, typename _Traits, typename _Alloc>
  563. void
  564. __rc_string_base<_CharT, _Traits, _Alloc>::
  565. _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
  566. size_type __len2)
  567. {
  568. const size_type __how_much = _M_length() - __pos - __len1;
  569. _Rep* __r = _Rep::_S_create(_M_length() + __len2 - __len1,
  570. _M_capacity(), _M_get_allocator());
  571. if (__pos)
  572. this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
  573. if (__s && __len2)
  574. this->_S_copy(__r->_M_refdata() + __pos, __s, __len2);
  575. if (__how_much)
  576. this->_S_copy(__r->_M_refdata() + __pos + __len2,
  577. _M_data() + __pos + __len1, __how_much);
  578. _M_dispose();
  579. _M_data(__r->_M_refdata());
  580. }
  581. template<typename _CharT, typename _Traits, typename _Alloc>
  582. void
  583. __rc_string_base<_CharT, _Traits, _Alloc>::
  584. _M_erase(size_type __pos, size_type __n)
  585. {
  586. const size_type __new_size = _M_length() - __n;
  587. const size_type __how_much = _M_length() - __pos - __n;
  588. if (_M_is_shared())
  589. {
  590. // Must reallocate.
  591. _Rep* __r = _Rep::_S_create(__new_size, _M_capacity(),
  592. _M_get_allocator());
  593. if (__pos)
  594. this->_S_copy(__r->_M_refdata(), _M_data(), __pos);
  595. if (__how_much)
  596. this->_S_copy(__r->_M_refdata() + __pos,
  597. _M_data() + __pos + __n, __how_much);
  598. _M_dispose();
  599. _M_data(__r->_M_refdata());
  600. }
  601. else if (__how_much && __n)
  602. {
  603. // Work in-place.
  604. this->_S_move(_M_data() + __pos,
  605. _M_data() + __pos + __n, __how_much);
  606. }
  607. _M_rep()->_M_set_length(__new_size);
  608. }
  609. template<>
  610. inline bool
  611. __rc_string_base<char, std::char_traits<char>,
  612. std::allocator<char> >::
  613. _M_compare(const __rc_string_base& __rcs) const
  614. {
  615. if (_M_rep() == __rcs._M_rep())
  616. return true;
  617. return false;
  618. }
  619. template<>
  620. inline bool
  621. __rc_string_base<wchar_t, std::char_traits<wchar_t>,
  622. std::allocator<wchar_t> >::
  623. _M_compare(const __rc_string_base& __rcs) const
  624. {
  625. if (_M_rep() == __rcs._M_rep())
  626. return true;
  627. return false;
  628. }
  629. _GLIBCXX_END_NAMESPACE_VERSION
  630. } // namespace
  631. #endif /* _RC_STRING_BASE_H */