string 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. // Debugging string implementation -*- C++ -*-
  2. // Copyright (C) 2003-2018 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 debug/string
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_STRING
  24. #define _GLIBCXX_DEBUG_STRING 1
  25. #pragma GCC system_header
  26. #include <string>
  27. #include <debug/safe_sequence.h>
  28. #include <debug/safe_container.h>
  29. #include <debug/safe_iterator.h>
  30. namespace __gnu_debug
  31. {
  32. /// Class std::basic_string with safety/checking/debug instrumentation.
  33. template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
  34. typename _Allocator = std::allocator<_CharT> >
  35. class basic_string
  36. : public __gnu_debug::_Safe_container<
  37. basic_string<_CharT, _Traits, _Allocator>,
  38. _Allocator, _Safe_sequence, bool(_GLIBCXX_USE_CXX11_ABI)>,
  39. public std::basic_string<_CharT, _Traits, _Allocator>
  40. {
  41. typedef std::basic_string<_CharT, _Traits, _Allocator> _Base;
  42. typedef __gnu_debug::_Safe_container<
  43. basic_string, _Allocator, _Safe_sequence, bool(_GLIBCXX_USE_CXX11_ABI)>
  44. _Safe;
  45. public:
  46. // types:
  47. typedef _Traits traits_type;
  48. typedef typename _Traits::char_type value_type;
  49. typedef _Allocator allocator_type;
  50. typedef typename _Base::size_type size_type;
  51. typedef typename _Base::difference_type difference_type;
  52. typedef typename _Base::reference reference;
  53. typedef typename _Base::const_reference const_reference;
  54. typedef typename _Base::pointer pointer;
  55. typedef typename _Base::const_pointer const_pointer;
  56. typedef __gnu_debug::_Safe_iterator<
  57. typename _Base::iterator, basic_string> iterator;
  58. typedef __gnu_debug::_Safe_iterator<
  59. typename _Base::const_iterator, basic_string> const_iterator;
  60. typedef std::reverse_iterator<iterator> reverse_iterator;
  61. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  62. using _Base::npos;
  63. basic_string()
  64. _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_default_constructible<_Base>::value)
  65. : _Base() { }
  66. // 21.3.1 construct/copy/destroy:
  67. explicit
  68. basic_string(const _Allocator& __a) _GLIBCXX_NOEXCEPT
  69. : _Base(__a) { }
  70. #if __cplusplus < 201103L
  71. basic_string(const basic_string& __str)
  72. : _Base(__str) { }
  73. ~basic_string() { }
  74. #else
  75. basic_string(const basic_string&) = default;
  76. basic_string(basic_string&&) = default;
  77. basic_string(std::initializer_list<_CharT> __l,
  78. const _Allocator& __a = _Allocator())
  79. : _Base(__l, __a)
  80. { }
  81. #if _GLIBCXX_USE_CXX11_ABI
  82. basic_string(const basic_string& __s, const _Allocator& __a)
  83. : _Base(__s, __a) { }
  84. basic_string(basic_string&& __s, const _Allocator& __a)
  85. : _Base(std::move(__s), __a) { }
  86. #endif
  87. ~basic_string() = default;
  88. // Provides conversion from a normal-mode string to a debug-mode string
  89. basic_string(_Base&& __base) noexcept
  90. : _Base(std::move(__base)) { }
  91. #endif // C++11
  92. // Provides conversion from a normal-mode string to a debug-mode string
  93. basic_string(const _Base& __base)
  94. : _Base(__base) { }
  95. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  96. // 42. string ctors specify wrong default allocator
  97. basic_string(const basic_string& __str, size_type __pos,
  98. size_type __n = _Base::npos,
  99. const _Allocator& __a = _Allocator())
  100. : _Base(__str, __pos, __n, __a) { }
  101. basic_string(const _CharT* __s, size_type __n,
  102. const _Allocator& __a = _Allocator())
  103. : _Base(__gnu_debug::__check_string(__s, __n), __n, __a) { }
  104. basic_string(const _CharT* __s, const _Allocator& __a = _Allocator())
  105. : _Base(__gnu_debug::__check_string(__s), __a)
  106. { this->assign(__s); }
  107. basic_string(size_type __n, _CharT __c,
  108. const _Allocator& __a = _Allocator())
  109. : _Base(__n, __c, __a) { }
  110. template<typename _InputIterator>
  111. basic_string(_InputIterator __begin, _InputIterator __end,
  112. const _Allocator& __a = _Allocator())
  113. : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__begin,
  114. __end)),
  115. __gnu_debug::__base(__end), __a) { }
  116. #if __cplusplus < 201103L
  117. basic_string&
  118. operator=(const basic_string& __str)
  119. {
  120. this->_M_safe() = __str;
  121. _M_base() = __str;
  122. return *this;
  123. }
  124. #else
  125. basic_string&
  126. operator=(const basic_string&) = default;
  127. basic_string&
  128. operator=(basic_string&&) = default;
  129. #endif
  130. basic_string&
  131. operator=(const _CharT* __s)
  132. {
  133. __glibcxx_check_string(__s);
  134. _M_base() = __s;
  135. this->_M_invalidate_all();
  136. return *this;
  137. }
  138. basic_string&
  139. operator=(_CharT __c)
  140. {
  141. _M_base() = __c;
  142. this->_M_invalidate_all();
  143. return *this;
  144. }
  145. #if __cplusplus >= 201103L
  146. basic_string&
  147. operator=(std::initializer_list<_CharT> __l)
  148. {
  149. _M_base() = __l;
  150. this->_M_invalidate_all();
  151. return *this;
  152. }
  153. #endif // C++11
  154. // 21.3.2 iterators:
  155. iterator
  156. begin() // _GLIBCXX_NOEXCEPT
  157. { return iterator(_Base::begin(), this); }
  158. const_iterator
  159. begin() const _GLIBCXX_NOEXCEPT
  160. { return const_iterator(_Base::begin(), this); }
  161. iterator
  162. end() // _GLIBCXX_NOEXCEPT
  163. { return iterator(_Base::end(), this); }
  164. const_iterator
  165. end() const _GLIBCXX_NOEXCEPT
  166. { return const_iterator(_Base::end(), this); }
  167. reverse_iterator
  168. rbegin() // _GLIBCXX_NOEXCEPT
  169. { return reverse_iterator(end()); }
  170. const_reverse_iterator
  171. rbegin() const _GLIBCXX_NOEXCEPT
  172. { return const_reverse_iterator(end()); }
  173. reverse_iterator
  174. rend() // _GLIBCXX_NOEXCEPT
  175. { return reverse_iterator(begin()); }
  176. const_reverse_iterator
  177. rend() const _GLIBCXX_NOEXCEPT
  178. { return const_reverse_iterator(begin()); }
  179. #if __cplusplus >= 201103L
  180. const_iterator
  181. cbegin() const noexcept
  182. { return const_iterator(_Base::begin(), this); }
  183. const_iterator
  184. cend() const noexcept
  185. { return const_iterator(_Base::end(), this); }
  186. const_reverse_iterator
  187. crbegin() const noexcept
  188. { return const_reverse_iterator(end()); }
  189. const_reverse_iterator
  190. crend() const noexcept
  191. { return const_reverse_iterator(begin()); }
  192. #endif
  193. // 21.3.3 capacity:
  194. using _Base::size;
  195. using _Base::length;
  196. using _Base::max_size;
  197. void
  198. resize(size_type __n, _CharT __c)
  199. {
  200. _Base::resize(__n, __c);
  201. this->_M_invalidate_all();
  202. }
  203. void
  204. resize(size_type __n)
  205. { this->resize(__n, _CharT()); }
  206. #if __cplusplus >= 201103L
  207. void
  208. shrink_to_fit() noexcept
  209. {
  210. if (capacity() > size())
  211. {
  212. __try
  213. {
  214. reserve(0);
  215. this->_M_invalidate_all();
  216. }
  217. __catch(...)
  218. { }
  219. }
  220. }
  221. #endif
  222. using _Base::capacity;
  223. using _Base::reserve;
  224. void
  225. clear() // _GLIBCXX_NOEXCEPT
  226. {
  227. _Base::clear();
  228. this->_M_invalidate_all();
  229. }
  230. using _Base::empty;
  231. // 21.3.4 element access:
  232. const_reference
  233. operator[](size_type __pos) const _GLIBCXX_NOEXCEPT
  234. {
  235. _GLIBCXX_DEBUG_VERIFY(__pos <= this->size(),
  236. _M_message(__gnu_debug::__msg_subscript_oob)
  237. ._M_sequence(*this, "this")
  238. ._M_integer(__pos, "__pos")
  239. ._M_integer(this->size(), "size"));
  240. return _M_base()[__pos];
  241. }
  242. reference
  243. operator[](size_type __pos) // _GLIBCXX_NOEXCEPT
  244. {
  245. #if __cplusplus < 201103L && defined(_GLIBCXX_DEBUG_PEDANTIC)
  246. __glibcxx_check_subscript(__pos);
  247. #else
  248. // as an extension v3 allows s[s.size()] when s is non-const.
  249. _GLIBCXX_DEBUG_VERIFY(__pos <= this->size(),
  250. _M_message(__gnu_debug::__msg_subscript_oob)
  251. ._M_sequence(*this, "this")
  252. ._M_integer(__pos, "__pos")
  253. ._M_integer(this->size(), "size"));
  254. #endif
  255. return _M_base()[__pos];
  256. }
  257. using _Base::at;
  258. #if __cplusplus >= 201103L
  259. using _Base::front;
  260. using _Base::back;
  261. #endif
  262. // 21.3.5 modifiers:
  263. basic_string&
  264. operator+=(const basic_string& __str)
  265. {
  266. _M_base() += __str;
  267. this->_M_invalidate_all();
  268. return *this;
  269. }
  270. basic_string&
  271. operator+=(const _CharT* __s)
  272. {
  273. __glibcxx_check_string(__s);
  274. _M_base() += __s;
  275. this->_M_invalidate_all();
  276. return *this;
  277. }
  278. basic_string&
  279. operator+=(_CharT __c)
  280. {
  281. _M_base() += __c;
  282. this->_M_invalidate_all();
  283. return *this;
  284. }
  285. #if __cplusplus >= 201103L
  286. basic_string&
  287. operator+=(std::initializer_list<_CharT> __l)
  288. {
  289. _M_base() += __l;
  290. this->_M_invalidate_all();
  291. return *this;
  292. }
  293. #endif // C++11
  294. basic_string&
  295. append(const basic_string& __str)
  296. {
  297. _Base::append(__str);
  298. this->_M_invalidate_all();
  299. return *this;
  300. }
  301. basic_string&
  302. append(const basic_string& __str, size_type __pos, size_type __n)
  303. {
  304. _Base::append(__str, __pos, __n);
  305. this->_M_invalidate_all();
  306. return *this;
  307. }
  308. basic_string&
  309. append(const _CharT* __s, size_type __n)
  310. {
  311. __glibcxx_check_string_len(__s, __n);
  312. _Base::append(__s, __n);
  313. this->_M_invalidate_all();
  314. return *this;
  315. }
  316. basic_string&
  317. append(const _CharT* __s)
  318. {
  319. __glibcxx_check_string(__s);
  320. _Base::append(__s);
  321. this->_M_invalidate_all();
  322. return *this;
  323. }
  324. basic_string&
  325. append(size_type __n, _CharT __c)
  326. {
  327. _Base::append(__n, __c);
  328. this->_M_invalidate_all();
  329. return *this;
  330. }
  331. template<typename _InputIterator>
  332. basic_string&
  333. append(_InputIterator __first, _InputIterator __last)
  334. {
  335. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  336. __glibcxx_check_valid_range2(__first, __last, __dist);
  337. if (__dist.second >= __dp_sign)
  338. _Base::append(__gnu_debug::__unsafe(__first),
  339. __gnu_debug::__unsafe(__last));
  340. else
  341. _Base::append(__first, __last);
  342. this->_M_invalidate_all();
  343. return *this;
  344. }
  345. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  346. // 7. string clause minor problems
  347. void
  348. push_back(_CharT __c)
  349. {
  350. _Base::push_back(__c);
  351. this->_M_invalidate_all();
  352. }
  353. basic_string&
  354. assign(const basic_string& __x)
  355. {
  356. _Base::assign(__x);
  357. this->_M_invalidate_all();
  358. return *this;
  359. }
  360. #if __cplusplus >= 201103L
  361. basic_string&
  362. assign(basic_string&& __x)
  363. noexcept(noexcept(std::declval<_Base&>().assign(std::move(__x))))
  364. {
  365. _Base::assign(std::move(__x));
  366. this->_M_invalidate_all();
  367. return *this;
  368. }
  369. #endif // C++11
  370. basic_string&
  371. assign(const basic_string& __str, size_type __pos, size_type __n)
  372. {
  373. _Base::assign(__str, __pos, __n);
  374. this->_M_invalidate_all();
  375. return *this;
  376. }
  377. basic_string&
  378. assign(const _CharT* __s, size_type __n)
  379. {
  380. __glibcxx_check_string_len(__s, __n);
  381. _Base::assign(__s, __n);
  382. this->_M_invalidate_all();
  383. return *this;
  384. }
  385. basic_string&
  386. assign(const _CharT* __s)
  387. {
  388. __glibcxx_check_string(__s);
  389. _Base::assign(__s);
  390. this->_M_invalidate_all();
  391. return *this;
  392. }
  393. basic_string&
  394. assign(size_type __n, _CharT __c)
  395. {
  396. _Base::assign(__n, __c);
  397. this->_M_invalidate_all();
  398. return *this;
  399. }
  400. template<typename _InputIterator>
  401. basic_string&
  402. assign(_InputIterator __first, _InputIterator __last)
  403. {
  404. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  405. __glibcxx_check_valid_range2(__first, __last, __dist);
  406. if (__dist.second >= __dp_sign)
  407. _Base::assign(__gnu_debug::__unsafe(__first),
  408. __gnu_debug::__unsafe(__last));
  409. else
  410. _Base::assign(__first, __last);
  411. this->_M_invalidate_all();
  412. return *this;
  413. }
  414. #if __cplusplus >= 201103L
  415. basic_string&
  416. assign(std::initializer_list<_CharT> __l)
  417. {
  418. _Base::assign(__l);
  419. this->_M_invalidate_all();
  420. return *this;
  421. }
  422. #endif // C++11
  423. basic_string&
  424. insert(size_type __pos1, const basic_string& __str)
  425. {
  426. _Base::insert(__pos1, __str);
  427. this->_M_invalidate_all();
  428. return *this;
  429. }
  430. basic_string&
  431. insert(size_type __pos1, const basic_string& __str,
  432. size_type __pos2, size_type __n)
  433. {
  434. _Base::insert(__pos1, __str, __pos2, __n);
  435. this->_M_invalidate_all();
  436. return *this;
  437. }
  438. basic_string&
  439. insert(size_type __pos, const _CharT* __s, size_type __n)
  440. {
  441. __glibcxx_check_string(__s);
  442. _Base::insert(__pos, __s, __n);
  443. this->_M_invalidate_all();
  444. return *this;
  445. }
  446. basic_string&
  447. insert(size_type __pos, const _CharT* __s)
  448. {
  449. __glibcxx_check_string(__s);
  450. _Base::insert(__pos, __s);
  451. this->_M_invalidate_all();
  452. return *this;
  453. }
  454. basic_string&
  455. insert(size_type __pos, size_type __n, _CharT __c)
  456. {
  457. _Base::insert(__pos, __n, __c);
  458. this->_M_invalidate_all();
  459. return *this;
  460. }
  461. iterator
  462. insert(iterator __p, _CharT __c)
  463. {
  464. __glibcxx_check_insert(__p);
  465. typename _Base::iterator __res = _Base::insert(__p.base(), __c);
  466. this->_M_invalidate_all();
  467. return iterator(__res, this);
  468. }
  469. void
  470. insert(iterator __p, size_type __n, _CharT __c)
  471. {
  472. __glibcxx_check_insert(__p);
  473. _Base::insert(__p.base(), __n, __c);
  474. this->_M_invalidate_all();
  475. }
  476. template<typename _InputIterator>
  477. void
  478. insert(iterator __p, _InputIterator __first, _InputIterator __last)
  479. {
  480. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  481. __glibcxx_check_insert_range(__p, __first, __last, __dist);
  482. if (__dist.second >= __dp_sign)
  483. _Base::insert(__p.base(), __gnu_debug::__unsafe(__first),
  484. __gnu_debug::__unsafe(__last));
  485. else
  486. _Base::insert(__p.base(), __first, __last);
  487. this->_M_invalidate_all();
  488. }
  489. #if __cplusplus >= 201103L
  490. void
  491. insert(iterator __p, std::initializer_list<_CharT> __l)
  492. {
  493. __glibcxx_check_insert(__p);
  494. _Base::insert(__p.base(), __l);
  495. this->_M_invalidate_all();
  496. }
  497. #endif // C++11
  498. basic_string&
  499. erase(size_type __pos = 0, size_type __n = _Base::npos)
  500. {
  501. _Base::erase(__pos, __n);
  502. this->_M_invalidate_all();
  503. return *this;
  504. }
  505. iterator
  506. erase(iterator __position)
  507. {
  508. __glibcxx_check_erase(__position);
  509. typename _Base::iterator __res = _Base::erase(__position.base());
  510. this->_M_invalidate_all();
  511. return iterator(__res, this);
  512. }
  513. iterator
  514. erase(iterator __first, iterator __last)
  515. {
  516. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  517. // 151. can't currently clear() empty container
  518. __glibcxx_check_erase_range(__first, __last);
  519. typename _Base::iterator __res = _Base::erase(__first.base(),
  520. __last.base());
  521. this->_M_invalidate_all();
  522. return iterator(__res, this);
  523. }
  524. #if __cplusplus >= 201103L
  525. void
  526. pop_back() // noexcept
  527. {
  528. __glibcxx_check_nonempty();
  529. _Base::pop_back();
  530. this->_M_invalidate_all();
  531. }
  532. #endif // C++11
  533. basic_string&
  534. replace(size_type __pos1, size_type __n1, const basic_string& __str)
  535. {
  536. _Base::replace(__pos1, __n1, __str);
  537. this->_M_invalidate_all();
  538. return *this;
  539. }
  540. basic_string&
  541. replace(size_type __pos1, size_type __n1, const basic_string& __str,
  542. size_type __pos2, size_type __n2)
  543. {
  544. _Base::replace(__pos1, __n1, __str, __pos2, __n2);
  545. this->_M_invalidate_all();
  546. return *this;
  547. }
  548. basic_string&
  549. replace(size_type __pos, size_type __n1, const _CharT* __s,
  550. size_type __n2)
  551. {
  552. __glibcxx_check_string_len(__s, __n2);
  553. _Base::replace(__pos, __n1, __s, __n2);
  554. this->_M_invalidate_all();
  555. return *this;
  556. }
  557. basic_string&
  558. replace(size_type __pos, size_type __n1, const _CharT* __s)
  559. {
  560. __glibcxx_check_string(__s);
  561. _Base::replace(__pos, __n1, __s);
  562. this->_M_invalidate_all();
  563. return *this;
  564. }
  565. basic_string&
  566. replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
  567. {
  568. _Base::replace(__pos, __n1, __n2, __c);
  569. this->_M_invalidate_all();
  570. return *this;
  571. }
  572. basic_string&
  573. replace(iterator __i1, iterator __i2, const basic_string& __str)
  574. {
  575. __glibcxx_check_erase_range(__i1, __i2);
  576. _Base::replace(__i1.base(), __i2.base(), __str);
  577. this->_M_invalidate_all();
  578. return *this;
  579. }
  580. basic_string&
  581. replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
  582. {
  583. __glibcxx_check_erase_range(__i1, __i2);
  584. __glibcxx_check_string_len(__s, __n);
  585. _Base::replace(__i1.base(), __i2.base(), __s, __n);
  586. this->_M_invalidate_all();
  587. return *this;
  588. }
  589. basic_string&
  590. replace(iterator __i1, iterator __i2, const _CharT* __s)
  591. {
  592. __glibcxx_check_erase_range(__i1, __i2);
  593. __glibcxx_check_string(__s);
  594. _Base::replace(__i1.base(), __i2.base(), __s);
  595. this->_M_invalidate_all();
  596. return *this;
  597. }
  598. basic_string&
  599. replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
  600. {
  601. __glibcxx_check_erase_range(__i1, __i2);
  602. _Base::replace(__i1.base(), __i2.base(), __n, __c);
  603. this->_M_invalidate_all();
  604. return *this;
  605. }
  606. template<typename _InputIterator>
  607. basic_string&
  608. replace(iterator __i1, iterator __i2,
  609. _InputIterator __j1, _InputIterator __j2)
  610. {
  611. __glibcxx_check_erase_range(__i1, __i2);
  612. typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
  613. __glibcxx_check_valid_range2(__j1, __j2, __dist);
  614. if (__dist.second >= __dp_sign)
  615. _Base::replace(__i1.base(), __i2.base(),
  616. __gnu_debug::__unsafe(__j1),
  617. __gnu_debug::__unsafe(__j2));
  618. else
  619. _Base::replace(__i1.base(), __i2.base(), __j1, __j2);
  620. this->_M_invalidate_all();
  621. return *this;
  622. }
  623. #if __cplusplus >= 201103L
  624. basic_string& replace(iterator __i1, iterator __i2,
  625. std::initializer_list<_CharT> __l)
  626. {
  627. __glibcxx_check_erase_range(__i1, __i2);
  628. _Base::replace(__i1.base(), __i2.base(), __l);
  629. this->_M_invalidate_all();
  630. return *this;
  631. }
  632. #endif // C++11
  633. size_type
  634. copy(_CharT* __s, size_type __n, size_type __pos = 0) const
  635. {
  636. __glibcxx_check_string_len(__s, __n);
  637. return _Base::copy(__s, __n, __pos);
  638. }
  639. void
  640. swap(basic_string& __x)
  641. _GLIBCXX_NOEXCEPT_IF(std::__is_nothrow_swappable<_Base>::value)
  642. {
  643. _Safe::_M_swap(__x);
  644. _Base::swap(__x);
  645. }
  646. // 21.3.6 string operations:
  647. const _CharT*
  648. c_str() const _GLIBCXX_NOEXCEPT
  649. {
  650. const _CharT* __res = _Base::c_str();
  651. this->_M_invalidate_all();
  652. return __res;
  653. }
  654. const _CharT*
  655. data() const _GLIBCXX_NOEXCEPT
  656. {
  657. const _CharT* __res = _Base::data();
  658. this->_M_invalidate_all();
  659. return __res;
  660. }
  661. using _Base::get_allocator;
  662. size_type
  663. find(const basic_string& __str, size_type __pos = 0) const
  664. _GLIBCXX_NOEXCEPT
  665. { return _Base::find(__str, __pos); }
  666. size_type
  667. find(const _CharT* __s, size_type __pos, size_type __n) const
  668. {
  669. __glibcxx_check_string(__s);
  670. return _Base::find(__s, __pos, __n);
  671. }
  672. size_type
  673. find(const _CharT* __s, size_type __pos = 0) const
  674. {
  675. __glibcxx_check_string(__s);
  676. return _Base::find(__s, __pos);
  677. }
  678. size_type
  679. find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  680. { return _Base::find(__c, __pos); }
  681. size_type
  682. rfind(const basic_string& __str, size_type __pos = _Base::npos) const
  683. _GLIBCXX_NOEXCEPT
  684. { return _Base::rfind(__str, __pos); }
  685. size_type
  686. rfind(const _CharT* __s, size_type __pos, size_type __n) const
  687. {
  688. __glibcxx_check_string_len(__s, __n);
  689. return _Base::rfind(__s, __pos, __n);
  690. }
  691. size_type
  692. rfind(const _CharT* __s, size_type __pos = _Base::npos) const
  693. {
  694. __glibcxx_check_string(__s);
  695. return _Base::rfind(__s, __pos);
  696. }
  697. size_type
  698. rfind(_CharT __c, size_type __pos = _Base::npos) const _GLIBCXX_NOEXCEPT
  699. { return _Base::rfind(__c, __pos); }
  700. size_type
  701. find_first_of(const basic_string& __str, size_type __pos = 0) const
  702. _GLIBCXX_NOEXCEPT
  703. { return _Base::find_first_of(__str, __pos); }
  704. size_type
  705. find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
  706. {
  707. __glibcxx_check_string(__s);
  708. return _Base::find_first_of(__s, __pos, __n);
  709. }
  710. size_type
  711. find_first_of(const _CharT* __s, size_type __pos = 0) const
  712. {
  713. __glibcxx_check_string(__s);
  714. return _Base::find_first_of(__s, __pos);
  715. }
  716. size_type
  717. find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  718. { return _Base::find_first_of(__c, __pos); }
  719. size_type
  720. find_last_of(const basic_string& __str,
  721. size_type __pos = _Base::npos) const _GLIBCXX_NOEXCEPT
  722. { return _Base::find_last_of(__str, __pos); }
  723. size_type
  724. find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
  725. {
  726. __glibcxx_check_string(__s);
  727. return _Base::find_last_of(__s, __pos, __n);
  728. }
  729. size_type
  730. find_last_of(const _CharT* __s, size_type __pos = _Base::npos) const
  731. {
  732. __glibcxx_check_string(__s);
  733. return _Base::find_last_of(__s, __pos);
  734. }
  735. size_type
  736. find_last_of(_CharT __c, size_type __pos = _Base::npos) const
  737. _GLIBCXX_NOEXCEPT
  738. { return _Base::find_last_of(__c, __pos); }
  739. size_type
  740. find_first_not_of(const basic_string& __str, size_type __pos = 0) const
  741. _GLIBCXX_NOEXCEPT
  742. { return _Base::find_first_not_of(__str, __pos); }
  743. size_type
  744. find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  745. {
  746. __glibcxx_check_string_len(__s, __n);
  747. return _Base::find_first_not_of(__s, __pos, __n);
  748. }
  749. size_type
  750. find_first_not_of(const _CharT* __s, size_type __pos = 0) const
  751. {
  752. __glibcxx_check_string(__s);
  753. return _Base::find_first_not_of(__s, __pos);
  754. }
  755. size_type
  756. find_first_not_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
  757. { return _Base::find_first_not_of(__c, __pos); }
  758. size_type
  759. find_last_not_of(const basic_string& __str,
  760. size_type __pos = _Base::npos) const
  761. _GLIBCXX_NOEXCEPT
  762. { return _Base::find_last_not_of(__str, __pos); }
  763. size_type
  764. find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  765. {
  766. __glibcxx_check_string(__s);
  767. return _Base::find_last_not_of(__s, __pos, __n);
  768. }
  769. size_type
  770. find_last_not_of(const _CharT* __s, size_type __pos = _Base::npos) const
  771. {
  772. __glibcxx_check_string(__s);
  773. return _Base::find_last_not_of(__s, __pos);
  774. }
  775. size_type
  776. find_last_not_of(_CharT __c, size_type __pos = _Base::npos) const
  777. _GLIBCXX_NOEXCEPT
  778. { return _Base::find_last_not_of(__c, __pos); }
  779. basic_string
  780. substr(size_type __pos = 0, size_type __n = _Base::npos) const
  781. { return basic_string(_Base::substr(__pos, __n)); }
  782. int
  783. compare(const basic_string& __str) const
  784. { return _Base::compare(__str); }
  785. int
  786. compare(size_type __pos1, size_type __n1,
  787. const basic_string& __str) const
  788. { return _Base::compare(__pos1, __n1, __str); }
  789. int
  790. compare(size_type __pos1, size_type __n1, const basic_string& __str,
  791. size_type __pos2, size_type __n2) const
  792. { return _Base::compare(__pos1, __n1, __str, __pos2, __n2); }
  793. int
  794. compare(const _CharT* __s) const
  795. {
  796. __glibcxx_check_string(__s);
  797. return _Base::compare(__s);
  798. }
  799. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  800. // 5. string::compare specification questionable
  801. int
  802. compare(size_type __pos1, size_type __n1, const _CharT* __s) const
  803. {
  804. __glibcxx_check_string(__s);
  805. return _Base::compare(__pos1, __n1, __s);
  806. }
  807. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  808. // 5. string::compare specification questionable
  809. int
  810. compare(size_type __pos1, size_type __n1,const _CharT* __s,
  811. size_type __n2) const
  812. {
  813. __glibcxx_check_string_len(__s, __n2);
  814. return _Base::compare(__pos1, __n1, __s, __n2);
  815. }
  816. _Base&
  817. _M_base() _GLIBCXX_NOEXCEPT { return *this; }
  818. const _Base&
  819. _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
  820. using _Safe::_M_invalidate_all;
  821. };
  822. template<typename _CharT, typename _Traits, typename _Allocator>
  823. inline basic_string<_CharT,_Traits,_Allocator>
  824. operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  825. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  826. { return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs; }
  827. template<typename _CharT, typename _Traits, typename _Allocator>
  828. inline basic_string<_CharT,_Traits,_Allocator>
  829. operator+(const _CharT* __lhs,
  830. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  831. {
  832. __glibcxx_check_string(__lhs);
  833. return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs;
  834. }
  835. template<typename _CharT, typename _Traits, typename _Allocator>
  836. inline basic_string<_CharT,_Traits,_Allocator>
  837. operator+(_CharT __lhs,
  838. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  839. { return basic_string<_CharT,_Traits,_Allocator>(1, __lhs) += __rhs; }
  840. template<typename _CharT, typename _Traits, typename _Allocator>
  841. inline basic_string<_CharT,_Traits,_Allocator>
  842. operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  843. const _CharT* __rhs)
  844. {
  845. __glibcxx_check_string(__rhs);
  846. return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs;
  847. }
  848. template<typename _CharT, typename _Traits, typename _Allocator>
  849. inline basic_string<_CharT,_Traits,_Allocator>
  850. operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  851. _CharT __rhs)
  852. { return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs; }
  853. template<typename _CharT, typename _Traits, typename _Allocator>
  854. inline bool
  855. operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  856. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  857. { return __lhs._M_base() == __rhs._M_base(); }
  858. template<typename _CharT, typename _Traits, typename _Allocator>
  859. inline bool
  860. operator==(const _CharT* __lhs,
  861. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  862. {
  863. __glibcxx_check_string(__lhs);
  864. return __lhs == __rhs._M_base();
  865. }
  866. template<typename _CharT, typename _Traits, typename _Allocator>
  867. inline bool
  868. operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  869. const _CharT* __rhs)
  870. {
  871. __glibcxx_check_string(__rhs);
  872. return __lhs._M_base() == __rhs;
  873. }
  874. template<typename _CharT, typename _Traits, typename _Allocator>
  875. inline bool
  876. operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  877. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  878. { return __lhs._M_base() != __rhs._M_base(); }
  879. template<typename _CharT, typename _Traits, typename _Allocator>
  880. inline bool
  881. operator!=(const _CharT* __lhs,
  882. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  883. {
  884. __glibcxx_check_string(__lhs);
  885. return __lhs != __rhs._M_base();
  886. }
  887. template<typename _CharT, typename _Traits, typename _Allocator>
  888. inline bool
  889. operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  890. const _CharT* __rhs)
  891. {
  892. __glibcxx_check_string(__rhs);
  893. return __lhs._M_base() != __rhs;
  894. }
  895. template<typename _CharT, typename _Traits, typename _Allocator>
  896. inline bool
  897. operator<(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  898. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  899. { return __lhs._M_base() < __rhs._M_base(); }
  900. template<typename _CharT, typename _Traits, typename _Allocator>
  901. inline bool
  902. operator<(const _CharT* __lhs,
  903. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  904. {
  905. __glibcxx_check_string(__lhs);
  906. return __lhs < __rhs._M_base();
  907. }
  908. template<typename _CharT, typename _Traits, typename _Allocator>
  909. inline bool
  910. operator<(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  911. const _CharT* __rhs)
  912. {
  913. __glibcxx_check_string(__rhs);
  914. return __lhs._M_base() < __rhs;
  915. }
  916. template<typename _CharT, typename _Traits, typename _Allocator>
  917. inline bool
  918. operator<=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  919. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  920. { return __lhs._M_base() <= __rhs._M_base(); }
  921. template<typename _CharT, typename _Traits, typename _Allocator>
  922. inline bool
  923. operator<=(const _CharT* __lhs,
  924. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  925. {
  926. __glibcxx_check_string(__lhs);
  927. return __lhs <= __rhs._M_base();
  928. }
  929. template<typename _CharT, typename _Traits, typename _Allocator>
  930. inline bool
  931. operator<=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  932. const _CharT* __rhs)
  933. {
  934. __glibcxx_check_string(__rhs);
  935. return __lhs._M_base() <= __rhs;
  936. }
  937. template<typename _CharT, typename _Traits, typename _Allocator>
  938. inline bool
  939. operator>=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  940. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  941. { return __lhs._M_base() >= __rhs._M_base(); }
  942. template<typename _CharT, typename _Traits, typename _Allocator>
  943. inline bool
  944. operator>=(const _CharT* __lhs,
  945. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  946. {
  947. __glibcxx_check_string(__lhs);
  948. return __lhs >= __rhs._M_base();
  949. }
  950. template<typename _CharT, typename _Traits, typename _Allocator>
  951. inline bool
  952. operator>=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  953. const _CharT* __rhs)
  954. {
  955. __glibcxx_check_string(__rhs);
  956. return __lhs._M_base() >= __rhs;
  957. }
  958. template<typename _CharT, typename _Traits, typename _Allocator>
  959. inline bool
  960. operator>(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  961. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  962. { return __lhs._M_base() > __rhs._M_base(); }
  963. template<typename _CharT, typename _Traits, typename _Allocator>
  964. inline bool
  965. operator>(const _CharT* __lhs,
  966. const basic_string<_CharT,_Traits,_Allocator>& __rhs)
  967. {
  968. __glibcxx_check_string(__lhs);
  969. return __lhs > __rhs._M_base();
  970. }
  971. template<typename _CharT, typename _Traits, typename _Allocator>
  972. inline bool
  973. operator>(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
  974. const _CharT* __rhs)
  975. {
  976. __glibcxx_check_string(__rhs);
  977. return __lhs._M_base() > __rhs;
  978. }
  979. // 21.3.7.8:
  980. template<typename _CharT, typename _Traits, typename _Allocator>
  981. inline void
  982. swap(basic_string<_CharT,_Traits,_Allocator>& __lhs,
  983. basic_string<_CharT,_Traits,_Allocator>& __rhs)
  984. { __lhs.swap(__rhs); }
  985. template<typename _CharT, typename _Traits, typename _Allocator>
  986. std::basic_ostream<_CharT, _Traits>&
  987. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  988. const basic_string<_CharT, _Traits, _Allocator>& __str)
  989. { return __os << __str._M_base(); }
  990. template<typename _CharT, typename _Traits, typename _Allocator>
  991. std::basic_istream<_CharT,_Traits>&
  992. operator>>(std::basic_istream<_CharT,_Traits>& __is,
  993. basic_string<_CharT,_Traits,_Allocator>& __str)
  994. {
  995. std::basic_istream<_CharT,_Traits>& __res = __is >> __str._M_base();
  996. __str._M_invalidate_all();
  997. return __res;
  998. }
  999. template<typename _CharT, typename _Traits, typename _Allocator>
  1000. std::basic_istream<_CharT,_Traits>&
  1001. getline(std::basic_istream<_CharT,_Traits>& __is,
  1002. basic_string<_CharT,_Traits,_Allocator>& __str, _CharT __delim)
  1003. {
  1004. std::basic_istream<_CharT,_Traits>& __res = getline(__is,
  1005. __str._M_base(),
  1006. __delim);
  1007. __str._M_invalidate_all();
  1008. return __res;
  1009. }
  1010. template<typename _CharT, typename _Traits, typename _Allocator>
  1011. std::basic_istream<_CharT,_Traits>&
  1012. getline(std::basic_istream<_CharT,_Traits>& __is,
  1013. basic_string<_CharT,_Traits,_Allocator>& __str)
  1014. {
  1015. std::basic_istream<_CharT,_Traits>& __res = getline(__is,
  1016. __str._M_base());
  1017. __str._M_invalidate_all();
  1018. return __res;
  1019. }
  1020. typedef basic_string<char> string;
  1021. #ifdef _GLIBCXX_USE_WCHAR_T
  1022. typedef basic_string<wchar_t> wstring;
  1023. #endif
  1024. template<typename _CharT, typename _Traits, typename _Allocator>
  1025. struct _Insert_range_from_self_is_safe<
  1026. __gnu_debug::basic_string<_CharT, _Traits, _Allocator> >
  1027. { enum { __value = 1 }; };
  1028. } // namespace __gnu_debug
  1029. #endif