string 31 KB

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