_string.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright (c) 1994
  3. * Hewlett-Packard Company
  4. *
  5. * Copyright (c) 1996,1997
  6. * Silicon Graphics Computer Systems, Inc.
  7. *
  8. * Copyright (c) 1997
  9. * Moscow Center for SPARC Technology
  10. *
  11. * Copyright (c) 1999
  12. * Boris Fomitchev
  13. *
  14. * This material is provided "as is", with absolutely no warranty expressed
  15. * or implied. Any use is at your own risk.
  16. *
  17. * Permission to use or copy this software for any purpose is hereby granted
  18. * without fee, provided the above notices are retained on all copies.
  19. * Permission to modify the code and to distribute modified code is granted,
  20. * provided the above notices are retained, and a notice that the code was
  21. * modified is included with the above copyright notice.
  22. */
  23. #ifndef _STLP_STRING_C
  24. #define _STLP_STRING_C
  25. #ifndef _STLP_INTERNAL_STRING_H
  26. # include <stl/_string.h>
  27. #endif
  28. #ifndef _STLP_INTERNAL_CTRAITS_FUNCTIONS_H
  29. # include <stl/_ctraits_fns.h>
  30. #endif
  31. #ifndef _STLP_INTERNAL_FUNCTION_H
  32. # include <stl/_function.h>
  33. #endif
  34. #if defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
  35. # define basic_string _STLP_NO_MEM_T_NAME(str)
  36. #elif defined (_STLP_DEBUG)
  37. # define basic_string _STLP_NON_DBG_NAME(str)
  38. #endif
  39. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  40. # define __size_type__ size_t
  41. # define size_type size_t
  42. # define iterator _CharT*
  43. #else
  44. # define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_string<_CharT,_Traits,_Alloc>::size_type
  45. #endif
  46. _STLP_BEGIN_NAMESPACE
  47. _STLP_MOVE_TO_PRIV_NAMESPACE
  48. // A helper class to use a char_traits as a function object.
  49. template <class _Traits>
  50. struct _Not_within_traits : public unary_function<typename _Traits::char_type, bool> {
  51. typedef typename _Traits::char_type _CharT;
  52. const _CharT* _M_first;
  53. const _CharT* _M_last;
  54. _Not_within_traits(const _CharT* __f, const _CharT* __l)
  55. : _M_first(__f), _M_last(__l) {}
  56. bool operator()(const _CharT& __x) const {
  57. return find_if(_M_first, _M_last,
  58. _STLP_PRIV _Eq_char_bound<_Traits>(__x)) == _M_last;
  59. }
  60. };
  61. template <class _InputIter, class _CharT, class _Traits>
  62. inline _InputIter __str_find_first_of_aux(_InputIter __first1, _InputIter __last1,
  63. const _CharT* __first2, const _CharT* __last2,
  64. _Traits*, const __true_type& /* _STLportTraits */)
  65. { return __find_first_of(__first1, __last1, __first2, __last2); }
  66. template <class _InputIter, class _CharT, class _Traits>
  67. inline _InputIter __str_find_first_of_aux(_InputIter __first1, _InputIter __last1,
  68. const _CharT* __first2, const _CharT* __last2,
  69. _Traits*, const __false_type& /* _STLportTraits */)
  70. { return __find_first_of(__first1, __last1, __first2, __last2, _STLP_PRIV _Eq_traits<_Traits>()); }
  71. template <class _InputIter, class _CharT, class _Traits>
  72. inline _InputIter __str_find_first_of(_InputIter __first1, _InputIter __last1,
  73. const _CharT* __first2, const _CharT* __last2,
  74. _Traits* __traits) {
  75. #if !defined (__BORLANDC__)
  76. typedef typename _IsSTLportClass<_Traits>::_Ret _STLportTraits;
  77. #else
  78. enum { _Is = _IsSTLportClass<_Traits>::_Is };
  79. typedef typename __bool2type<_Is>::_Ret _STLportTraits;
  80. #endif
  81. return __str_find_first_of_aux(__first1, __last1, __first2, __last2, __traits, _STLportTraits());
  82. }
  83. template <class _InputIter, class _CharT, class _Traits>
  84. inline _InputIter __str_find_first_not_of_aux3(_InputIter __first1, _InputIter __last1,
  85. const _CharT* __first2, const _CharT* __last2,
  86. _Traits* /* __traits */, const __true_type& __useStrcspnLikeAlgo)
  87. { return __find_first_of_aux2(__first1, __last1, __first2, __last2, __first2, not1(_Identity<bool>()), __useStrcspnLikeAlgo); }
  88. template <class _InputIter, class _CharT, class _Traits>
  89. inline _InputIter __str_find_first_not_of_aux3(_InputIter __first1, _InputIter __last1,
  90. const _CharT* __first2, const _CharT* __last2,
  91. _Traits* /* __traits */, const __false_type& /* _UseStrcspnLikeAlgo */)
  92. { return _STLP_STD::find_if(__first1, __last1, _STLP_PRIV _Not_within_traits<_Traits>(__first2, __last2)); }
  93. template <class _InputIter, class _CharT, class _Tp, class _Traits>
  94. inline _InputIter __str_find_first_not_of_aux2(_InputIter __first1, _InputIter __last1,
  95. const _CharT* __first2, const _CharT* __last2,
  96. _Tp* __pt, _Traits* __traits) {
  97. typedef typename _IsIntegral<_Tp>::_Ret _IsIntegral;
  98. typedef typename _IsCharLikeType<_CharT>::_Ret _IsCharLike;
  99. typedef typename _Land2<_IsIntegral, _IsCharLike>::_Ret _UseStrcspnLikeAlgo;
  100. _STLP_MARK_PARAMETER_AS_UNUSED(__pt);
  101. return __str_find_first_not_of_aux3(__first1, __last1, __first2, __last2, __traits, _UseStrcspnLikeAlgo());
  102. }
  103. template <class _InputIter, class _CharT, class _Traits>
  104. inline _InputIter __str_find_first_not_of_aux1(_InputIter __first1, _InputIter __last1,
  105. const _CharT* __first2, const _CharT* __last2,
  106. _Traits* __traits, const __true_type& /* _STLportTraits */)
  107. { return __str_find_first_not_of_aux2(__first1, __last1, __first2, __last2,
  108. _STLP_VALUE_TYPE(__first1, _InputIter), __traits); }
  109. template <class _InputIter, class _CharT, class _Traits>
  110. inline _InputIter __str_find_first_not_of_aux1(_InputIter __first1, _InputIter __last1,
  111. const _CharT* __first2, const _CharT* __last2,
  112. _Traits*, const __false_type& /* _STLportTraits */)
  113. { return _STLP_STD::find_if(__first1, __last1, _STLP_PRIV _Not_within_traits<_Traits>(__first2, __last2)); }
  114. template <class _InputIter, class _CharT, class _Traits>
  115. inline _InputIter __str_find_first_not_of(_InputIter __first1, _InputIter __last1,
  116. const _CharT* __first2, const _CharT* __last2,
  117. _Traits* __traits) {
  118. #if !defined (__BORLANDC__)
  119. typedef typename _IsSTLportClass<_Traits>::_Ret _STLportTraits;
  120. #else
  121. enum { _Is = _IsSTLportClass<_Traits>::_Is };
  122. typedef typename __bool2type<_Is>::_Ret _STLportTraits;
  123. #endif
  124. return __str_find_first_not_of_aux1(__first1, __last1, __first2, __last2, __traits, _STLportTraits());
  125. }
  126. // ------------------------------------------------------------
  127. // Non-inline declarations.
  128. #if !defined (basic_string)
  129. _STLP_MOVE_TO_STD_NAMESPACE
  130. #endif
  131. // Change the string's capacity so that it is large enough to hold
  132. // at least __res_arg elements, plus the terminating _CharT(). Note that,
  133. // if __res_arg < capacity(), this member function may actually decrease
  134. // the string's capacity.
  135. template <class _CharT, class _Traits, class _Alloc>
  136. void basic_string<_CharT,_Traits,_Alloc>::reserve(size_type __res_arg) {
  137. if (__res_arg > max_size())
  138. this->_M_throw_length_error();
  139. size_type __n = (max)(__res_arg, size()) + 1;
  140. if (__n < this->_M_capacity())
  141. return;
  142. _M_reserve(__n);
  143. }
  144. template <class _CharT, class _Traits, class _Alloc>
  145. void basic_string<_CharT,_Traits,_Alloc>::_M_reserve(size_type __n) {
  146. pointer __new_start = this->_M_start_of_storage.allocate(__n, __n);
  147. pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
  148. _M_construct_null(__new_finish);
  149. this->_M_deallocate_block();
  150. this->_M_reset(__new_start, __new_finish, __new_start + __n);
  151. }
  152. template <class _CharT, class _Traits, class _Alloc>
  153. basic_string<_CharT,_Traits,_Alloc>&
  154. basic_string<_CharT,_Traits,_Alloc>::append(size_type __n, _CharT __c) {
  155. if (__n > 0) {
  156. if (__n > max_size() - size())
  157. this->_M_throw_length_error();
  158. if (__n >= this->_M_rest())
  159. _M_reserve(_M_compute_next_size(__n));
  160. _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - 1, __c);
  161. _M_construct_null(this->_M_finish + __n);
  162. _Traits::assign(*end(), __c);
  163. this->_M_finish += __n;
  164. }
  165. return *this;
  166. }
  167. template <class _CharT, class _Traits, class _Alloc>
  168. basic_string<_CharT, _Traits, _Alloc>&
  169. basic_string<_CharT, _Traits, _Alloc>::_M_append(const _CharT* __first, const _CharT* __last) {
  170. if (__first != __last) {
  171. size_type __n = __STATIC_CAST(size_type, __last - __first);
  172. if (__n >= this->_M_rest()) {
  173. size_type __len = _M_compute_next_size(__n);
  174. pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
  175. pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), this->_M_Finish(), __new_start);
  176. __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
  177. _M_construct_null(__new_finish);
  178. this->_M_deallocate_block();
  179. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  180. }
  181. else {
  182. const _CharT* __f1 = __first;
  183. ++__f1;
  184. _STLP_PRIV __ucopy(__f1, __last, this->_M_finish + 1);
  185. _M_construct_null(this->_M_finish + __n);
  186. _Traits::assign(*end(), *__first);
  187. this->_M_finish += __n;
  188. }
  189. }
  190. return *this;
  191. }
  192. template <class _CharT, class _Traits, class _Alloc>
  193. basic_string<_CharT,_Traits,_Alloc>&
  194. basic_string<_CharT,_Traits,_Alloc>::assign(size_type __n, _CharT __c) {
  195. if (__n <= size()) {
  196. _Traits::assign(this->_M_Start(), __n, __c);
  197. erase(begin() + __n, end());
  198. }
  199. else {
  200. if (__n < capacity()) {
  201. _Traits::assign(this->_M_Start(), size(), __c);
  202. append(__n - size(), __c);
  203. }
  204. else {
  205. _Self __str(__n, __c);
  206. this->swap(__str);
  207. }
  208. }
  209. return *this;
  210. }
  211. template <class _CharT, class _Traits, class _Alloc>
  212. basic_string<_CharT,_Traits,_Alloc>&
  213. basic_string<_CharT,_Traits,_Alloc>::_M_assign(const _CharT* __f, const _CharT* __l) {
  214. ptrdiff_t __n = __l - __f;
  215. if (__STATIC_CAST(size_type, __n) <= size()) {
  216. _Traits::move(this->_M_Start(), __f, __n);
  217. erase(begin() + __n, end());
  218. }
  219. else {
  220. _Traits::move(this->_M_Start(), __f, size());
  221. _M_append(__f + size(), __l);
  222. }
  223. return *this;
  224. }
  225. template <class _CharT, class _Traits, class _Alloc>
  226. _CharT* basic_string<_CharT,_Traits,_Alloc> ::_M_insert_aux(_CharT* __p,
  227. _CharT __c) {
  228. pointer __new_pos = __p;
  229. if (this->_M_rest() > 1 ) {
  230. _M_construct_null(this->_M_finish + 1);
  231. _Traits::move(__p + 1, __p, this->_M_finish - __p);
  232. _Traits::assign(*__p, __c);
  233. ++this->_M_finish;
  234. }
  235. else {
  236. size_type __len = _M_compute_next_size(1);
  237. pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
  238. __new_pos = _STLP_PRIV __ucopy(this->_M_Start(), __p, __new_start);
  239. _Traits::assign(*__new_pos, __c);
  240. pointer __new_finish = __new_pos + 1;
  241. __new_finish = _STLP_PRIV __ucopy(__p, this->_M_finish, __new_finish);
  242. _M_construct_null(__new_finish);
  243. this->_M_deallocate_block();
  244. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  245. }
  246. return __new_pos;
  247. }
  248. template <class _CharT, class _Traits, class _Alloc>
  249. void basic_string<_CharT,_Traits,_Alloc>::insert(iterator __pos,
  250. size_t __n, _CharT __c) {
  251. if (__n != 0) {
  252. if (this->_M_rest() > __n) {
  253. const size_type __elems_after = this->_M_finish - __pos;
  254. pointer __old_finish = this->_M_finish;
  255. if (__elems_after >= __n) {
  256. _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
  257. this->_M_finish += __n;
  258. _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
  259. _Traits::assign(__pos, __n, __c);
  260. }
  261. else {
  262. _STLP_PRIV __uninitialized_fill_n(this->_M_finish + 1, __n - __elems_after - 1, __c);
  263. this->_M_finish += __n - __elems_after;
  264. _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
  265. this->_M_finish += __elems_after;
  266. _Traits::assign(__pos, __elems_after + 1, __c);
  267. }
  268. }
  269. else {
  270. size_type __len = _M_compute_next_size(__n);
  271. pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
  272. pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
  273. __new_finish = _STLP_PRIV __uninitialized_fill_n(__new_finish, __n, __c);
  274. __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
  275. _M_construct_null(__new_finish);
  276. this->_M_deallocate_block();
  277. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  278. }
  279. }
  280. }
  281. template <class _CharT, class _Traits, class _Alloc>
  282. void basic_string<_CharT,_Traits,_Alloc>::_M_insert(iterator __pos,
  283. const _CharT* __first, const _CharT* __last,
  284. bool __self_ref) {
  285. //this version has to take care about the auto referencing
  286. if (__first != __last) {
  287. const size_t __n = __last - __first;
  288. if (this->_M_rest() > __n) {
  289. const size_t __elems_after = this->_M_finish - __pos;
  290. pointer __old_finish = this->_M_finish;
  291. if (__elems_after >= __n) {
  292. _STLP_PRIV __ucopy((this->_M_finish - __n) + 1, this->_M_finish + 1, this->_M_finish + 1);
  293. this->_M_finish += __n;
  294. _Traits::move(__pos + __n, __pos, (__elems_after - __n) + 1);
  295. if (!__self_ref || __last < __pos) {
  296. _M_copy(__first, __last, __pos);
  297. }
  298. else {
  299. //We have to check that the source buffer hasn't move
  300. if (__first >= __pos) {
  301. //The source buffer has move
  302. __first += __n;
  303. __last += __n;
  304. _M_copy(__first, __last, __pos);
  305. }
  306. else {
  307. //The source buffer hasn't move, it has been duplicated
  308. _M_move(__first, __last, __pos);
  309. }
  310. }
  311. }
  312. else {
  313. const_iterator __mid = __first;
  314. __mid += __elems_after + 1;
  315. _STLP_PRIV __ucopy(__mid, __last, this->_M_finish + 1);
  316. this->_M_finish += __n - __elems_after;
  317. _STLP_PRIV __ucopy(__pos, __old_finish + 1, this->_M_finish);
  318. this->_M_finish += __elems_after;
  319. if (!__self_ref)
  320. _M_copy(__first, __mid, __pos);
  321. else
  322. _M_move(__first, __mid, __pos);
  323. }
  324. }
  325. else {
  326. size_type __len = _M_compute_next_size(__n);
  327. pointer __new_start = this->_M_start_of_storage.allocate(__len, __len);
  328. pointer __new_finish = _STLP_PRIV __ucopy(this->_M_Start(), __pos, __new_start);
  329. __new_finish = _STLP_PRIV __ucopy(__first, __last, __new_finish);
  330. __new_finish = _STLP_PRIV __ucopy(__pos, this->_M_finish, __new_finish);
  331. _M_construct_null(__new_finish);
  332. this->_M_deallocate_block();
  333. this->_M_reset(__new_start, __new_finish, __new_start + __len);
  334. }
  335. }
  336. }
  337. template <class _CharT, class _Traits, class _Alloc>
  338. basic_string<_CharT,_Traits,_Alloc>&
  339. basic_string<_CharT,_Traits,_Alloc> ::replace(iterator __first, iterator __last,
  340. size_type __n, _CharT __c) {
  341. size_type __len = (size_type)(__last - __first);
  342. if (__len >= __n) {
  343. _Traits::assign(__first, __n, __c);
  344. erase(__first + __n, __last);
  345. }
  346. else {
  347. _Traits::assign(__first, __len, __c);
  348. insert(__last, __n - __len, __c);
  349. }
  350. return *this;
  351. }
  352. template <class _CharT, class _Traits, class _Alloc>
  353. basic_string<_CharT,_Traits,_Alloc>&
  354. basic_string<_CharT,_Traits,_Alloc> ::_M_replace(iterator __first, iterator __last,
  355. const _CharT* __f, const _CharT* __l,
  356. bool __self_ref) {
  357. const ptrdiff_t __n = __l - __f;
  358. const difference_type __len = __last - __first;
  359. if (__len >= __n) {
  360. if (!__self_ref || __l < __first || __f >= __last)
  361. _M_copy(__f, __l, __first);
  362. else
  363. _M_move(__f, __l, __first);
  364. erase(__first + __n, __last);
  365. } else if (!__self_ref || (__f >= __last) || (__l <= __first)) { // no overlap
  366. const_iterator __m = __f + __len;
  367. _M_copy(__f, __m, __first);
  368. _M_insert(__last, __m, __l, __self_ref );
  369. } else if (__f < __first) { // we have to take care of overlaping
  370. const_iterator __m = __f + __len;
  371. // We have to deal with possible reallocation because we do insert first.
  372. const difference_type __off_dest = __first - this->begin();
  373. const difference_type __off_src = __f - this->begin();
  374. _M_insert(__last, __m, __l, true);
  375. _Traits::move(begin() + __off_dest, begin() + __off_src, __len);
  376. } else {
  377. const_iterator __m = __f + __len;
  378. _Traits::move(__first, __f, __len);
  379. _M_insert(__last, __m, __l, true);
  380. }
  381. return *this;
  382. }
  383. template <class _CharT, class _Traits, class _Alloc>
  384. __size_type__ basic_string<_CharT,_Traits,_Alloc>::find( const _CharT* __s, size_type __pos,
  385. size_type __n) const
  386. {
  387. const size_t __len = size();
  388. if (__pos >= __len || __pos + __n > __len) {
  389. if ( __n == 0 && __pos <= __len ) { // marginal case
  390. return __pos;
  391. }
  392. return npos;
  393. }
  394. const_pointer __result =
  395. _STLP_STD::search(this->_M_Start() + __pos, this->_M_Finish(),
  396. __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
  397. return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
  398. }
  399. template <class _CharT, class _Traits, class _Alloc>
  400. __size_type__ basic_string<_CharT,_Traits,_Alloc>::find(_CharT __c, size_type __pos) const
  401. {
  402. if (__pos >= size()) { /*__pos + 1 > size()*/
  403. return npos;
  404. }
  405. const_pointer __result =
  406. _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
  407. _STLP_PRIV _Eq_char_bound<_Traits>(__c));
  408. return __result != this->_M_Finish() ? __result - this->_M_Start() : npos;
  409. }
  410. template <class _CharT, class _Traits, class _Alloc>
  411. __size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(const _CharT* __s, size_type __pos,
  412. size_type __n) const
  413. {
  414. const size_type __len = size();
  415. if ( __len < __n ) {
  416. return npos;
  417. }
  418. const_pointer __last = this->_M_Start() + (min)( __len - __n, __pos) + __n;
  419. if ( __n == 0 ) { // marginal case
  420. return __last - this->_M_Start();
  421. }
  422. const_pointer __result = _STLP_STD::find_end(this->_M_Start(), __last,
  423. __s, __s + __n, _STLP_PRIV _Eq_traits<_Traits>());
  424. return __result != __last ? __result - this->_M_Start() : npos;
  425. }
  426. template <class _CharT, class _Traits, class _Alloc>
  427. __size_type__ basic_string<_CharT,_Traits,_Alloc>::rfind(_CharT __c, size_type __pos) const
  428. {
  429. const size_type __len = size();
  430. if ( __len < 1 ) {
  431. return npos;
  432. }
  433. const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
  434. const_reverse_iterator __rresult =
  435. _STLP_STD::find_if(const_reverse_iterator(__last), rend(),
  436. _STLP_PRIV _Eq_char_bound<_Traits>(__c));
  437. return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  438. }
  439. template <class _CharT, class _Traits, class _Alloc> __size_type__
  440. basic_string<_CharT,_Traits,_Alloc> ::find_first_of(const _CharT* __s, size_type __pos,
  441. size_type __n) const {
  442. if (__pos >= size()) /*__pos + 1 > size()*/
  443. return npos;
  444. else {
  445. const_iterator __result = _STLP_PRIV __str_find_first_of(begin() + __pos, end(),
  446. __s, __s + __n,
  447. __STATIC_CAST(_Traits*, 0));
  448. return __result != end() ? __result - begin() : npos;
  449. }
  450. }
  451. template <class _CharT, class _Traits, class _Alloc>
  452. __size_type__
  453. basic_string<_CharT,_Traits,_Alloc> ::find_last_of(const _CharT* __s, size_type __pos,
  454. size_type __n) const
  455. {
  456. const size_type __len = size();
  457. if ( __len < 1 ) {
  458. return npos;
  459. }
  460. const const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
  461. const const_reverse_iterator __rresult =
  462. _STLP_PRIV __str_find_first_of(const_reverse_iterator(__last), rend(),
  463. __s, __s + __n,
  464. __STATIC_CAST(_Traits*, 0));
  465. return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  466. }
  467. template <class _CharT, class _Traits, class _Alloc> __size_type__
  468. basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(const _CharT* __s, size_type __pos,
  469. size_type __n) const {
  470. typedef typename _Traits::char_type _CharType;
  471. if (__pos >= size()) /*__pos + 1 >= size()*/
  472. return npos;
  473. else {
  474. const_pointer __result = _STLP_PRIV __str_find_first_not_of(this->_M_Start() + __pos, this->_M_Finish(),
  475. __STATIC_CAST(const _CharType*, __s),
  476. __STATIC_CAST(const _CharType*, __s) + __n,
  477. __STATIC_CAST(_Traits*, 0));
  478. return __result != this->_M_finish ? __result - this->_M_Start() : npos;
  479. }
  480. }
  481. template <class _CharT, class _Traits, class _Alloc> __size_type__
  482. basic_string<_CharT,_Traits,_Alloc> ::find_first_not_of(_CharT __c, size_type __pos) const {
  483. if (1 > size())
  484. return npos;
  485. else {
  486. const_pointer __result = _STLP_STD::find_if(this->_M_Start() + __pos, this->_M_Finish(),
  487. _STLP_PRIV _Neq_char_bound<_Traits>(__c));
  488. return __result != this->_M_finish ? __result - this->_M_Start() : npos;
  489. }
  490. }
  491. template <class _CharT, class _Traits, class _Alloc>
  492. __size_type__
  493. basic_string<_CharT,_Traits,_Alloc>::find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
  494. {
  495. typedef typename _Traits::char_type _CharType;
  496. const size_type __len = size();
  497. if ( __len < 1 ) {
  498. return npos;
  499. }
  500. const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
  501. const_reverse_iterator __rlast = const_reverse_iterator(__last);
  502. const_reverse_iterator __rresult =
  503. _STLP_PRIV __str_find_first_not_of(__rlast, rend(),
  504. __STATIC_CAST(const _CharType*, __s),
  505. __STATIC_CAST(const _CharType*, __s) + __n,
  506. __STATIC_CAST(_Traits*, 0));
  507. return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  508. }
  509. template <class _CharT, class _Traits, class _Alloc>
  510. __size_type__
  511. basic_string<_CharT, _Traits, _Alloc>::find_last_not_of(_CharT __c, size_type __pos) const
  512. {
  513. const size_type __len = size();
  514. if ( __len < 1 ) {
  515. return npos;
  516. }
  517. const_iterator __last = begin() + (min)(__len - 1, __pos) + 1;
  518. const_reverse_iterator __rlast = const_reverse_iterator(__last);
  519. const_reverse_iterator __rresult =
  520. _STLP_STD::find_if(__rlast, rend(),
  521. _STLP_PRIV _Neq_char_bound<_Traits>(__c));
  522. return __rresult != rend() ? (__rresult.base() - 1) - begin() : npos;
  523. }
  524. #if !defined (basic_string)
  525. _STLP_MOVE_TO_PRIV_NAMESPACE
  526. #endif
  527. template <class _CharT, class _Traits, class _Alloc>
  528. void _STLP_CALL _S_string_copy(const basic_string<_CharT,_Traits,_Alloc>& __s,
  529. _CharT* __buf, size_t __n) {
  530. if (__n > 0) {
  531. __n = (min) (__n - 1, __s.size());
  532. _STLP_STD::copy(__s.begin(), __s.begin() + __n, __buf);
  533. __buf[__n] = _CharT();
  534. }
  535. }
  536. _STLP_MOVE_TO_STD_NAMESPACE
  537. _STLP_END_NAMESPACE
  538. #include <stl/_range_errors.h>
  539. _STLP_BEGIN_NAMESPACE
  540. _STLP_MOVE_TO_PRIV_NAMESPACE
  541. // _String_base methods
  542. template <class _Tp, class _Alloc>
  543. void _String_base<_Tp,_Alloc>::_M_throw_length_error() const
  544. { __stl_throw_length_error("basic_string"); }
  545. template <class _Tp, class _Alloc>
  546. void _String_base<_Tp, _Alloc>::_M_throw_out_of_range() const
  547. { __stl_throw_out_of_range("basic_string"); }
  548. template <class _Tp, class _Alloc>
  549. void _String_base<_Tp, _Alloc>::_M_allocate_block(size_t __n) {
  550. if ((__n <= (max_size() + 1)) && (__n > 0)) {
  551. #if defined (_STLP_USE_SHORT_STRING_OPTIM)
  552. if (__n > _DEFAULT_SIZE) {
  553. this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);
  554. this->_M_finish = this->_M_start_of_storage._M_data;
  555. this->_M_buffers._M_end_of_storage = this->_M_start_of_storage._M_data + __n;
  556. }
  557. #else
  558. this->_M_start_of_storage._M_data = _M_start_of_storage.allocate(__n, __n);
  559. this->_M_finish = this->_M_start_of_storage._M_data;
  560. this->_M_end_of_storage = this->_M_start_of_storage._M_data + __n;
  561. #endif
  562. } else {
  563. this->_M_throw_length_error();
  564. }
  565. }
  566. #if !defined (basic_string)
  567. _STLP_MOVE_TO_STD_NAMESPACE
  568. #endif
  569. #if defined (_STLP_DONT_SUP_DFLT_PARAM)
  570. template <class _CharT, class _Traits, class _Alloc>
  571. basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s)
  572. : _STLP_PRIV _String_base<_CharT,_Alloc>(allocator_type()) {
  573. _STLP_FIX_LITERAL_BUG(__s)
  574. _M_range_initialize(__s, __s + traits_type::length(__s));
  575. }
  576. #endif
  577. template <class _CharT, class _Traits, class _Alloc>
  578. basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT* __s,
  579. const allocator_type& __a)
  580. : _STLP_PRIV _String_base<_CharT,_Alloc>(__a) {
  581. _STLP_FIX_LITERAL_BUG(__s)
  582. _M_range_initialize(__s, __s + traits_type::length(__s));
  583. }
  584. template <class _CharT, class _Traits, class _Alloc>
  585. basic_string<_CharT, _Traits, _Alloc>::basic_string(const basic_string<_CharT, _Traits, _Alloc> & __s)
  586. : _STLP_PRIV _String_base<_CharT,_Alloc>(__s.get_allocator())
  587. { _M_range_initialize(__s._M_Start(), __s._M_Finish()); }
  588. #if defined (basic_string)
  589. _STLP_MOVE_TO_STD_NAMESPACE
  590. # undef basic_string
  591. #endif
  592. #if !defined (_STLP_STATIC_CONST_INIT_BUG) && !defined (_STLP_NO_STATIC_CONST_DEFINITION)
  593. template <class _CharT, class _Traits, class _Alloc>
  594. const size_t basic_string<_CharT, _Traits, _Alloc>::npos;
  595. #endif
  596. _STLP_END_NAMESPACE
  597. #undef __size_type__
  598. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  599. # undef size_type
  600. # undef iterator
  601. #endif
  602. #endif /* _STLP_STRING_C */
  603. // Local Variables:
  604. // mode:C++
  605. // End: