You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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