flex_string.hpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001 by Andrei Alexandrescu. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. // This code is taken from:
  9. // Andrei Alexandrescu, Generic<Programming>: A Policy-Based basic_string
  10. // Implementation. http://www.cuj.com/documents/s=7994/cujcexp1906alexandr/
  11. //
  12. // #HK030306:
  13. // - Moved into the namespace boost::wave::util
  14. // - Added a bunch of missing typename(s)
  15. // - Integrated with boost config
  16. // - Added a missing header include
  17. // - Added special constructors and operator= to allow CowString to be
  18. // a real COW-string (removed unnecessary data copying)
  19. // - Fixed a string terminating bug in append
  20. //
  21. // #HK040109:
  22. // - Incorporated the changes from Andrei's latest version of this class
  23. //
  24. // #HK070307:
  25. // - Once again incorporated the changes from Andrei's latest version of
  26. // this class
  27. //
  28. // #HK090523:
  29. // - Incorporated the changes from latest version of flex_string as
  30. // maintained in Loki
  31. //
  32. // #HK130910:
  33. // - Removed the getline implementation which was borrowed from the SGI
  34. // STL as the license for this code is not compatible with Boost.
  35. #ifndef FLEX_STRING_INC_
  36. #define FLEX_STRING_INC_
  37. /*
  38. ////////////////////////////////////////////////////////////////////////////////
  39. template <typename E, class A = @>
  40. class StoragePolicy
  41. {
  42. typedef E value_type;
  43. typedef @ iterator;
  44. typedef @ const_iterator;
  45. typedef A allocator_type;
  46. typedef @ size_type;
  47. StoragePolicy(const StoragePolicy& s);
  48. StoragePolicy(const A&);
  49. StoragePolicy(const E* s, size_type len, const A&);
  50. StoragePolicy(size_type len, E c, const A&);
  51. ~StoragePolicy();
  52. iterator begin();
  53. const_iterator begin() const;
  54. iterator end();
  55. const_iterator end() const;
  56. size_type size() const;
  57. size_type max_size() const;
  58. size_type capacity() const;
  59. void reserve(size_type res_arg);
  60. void append(const E* s, size_type sz);
  61. template <class InputIterator>
  62. void append(InputIterator b, InputIterator e);
  63. void resize(size_type newSize, E fill);
  64. void swap(StoragePolicy& rhs);
  65. const E* c_str() const;
  66. const E* data() const;
  67. A get_allocator() const;
  68. };
  69. ////////////////////////////////////////////////////////////////////////////////
  70. */
  71. #include <boost/config.hpp>
  72. #include <boost/assert.hpp>
  73. #include <boost/throw_exception.hpp>
  74. #include <boost/iterator/reverse_iterator.hpp>
  75. #include <boost/wave/wave_config.hpp>
  76. #if BOOST_WAVE_SERIALIZATION != 0
  77. #include <boost/serialization/serialization.hpp>
  78. #include <boost/serialization/split_free.hpp>
  79. #include <boost/serialization/collections_save_imp.hpp>
  80. #include <boost/serialization/collections_load_imp.hpp>
  81. #define BOOST_WAVE_FLEX_STRING_SERIALIZATION_HACK 1
  82. #endif
  83. #include <memory>
  84. #include <new>
  85. #include <string>
  86. #include <vector>
  87. #include <algorithm>
  88. #include <functional>
  89. #include <limits>
  90. #include <stdexcept>
  91. #include <ios>
  92. #include <cstddef>
  93. #include <cstring>
  94. #include <cstdlib>
  95. // this must occur after all of the includes and before any code appears
  96. #ifdef BOOST_HAS_ABI_HEADERS
  97. #include BOOST_ABI_PREFIX
  98. #endif
  99. ///////////////////////////////////////////////////////////////////////////////
  100. namespace boost {
  101. namespace wave {
  102. namespace util {
  103. namespace flex_string_details
  104. {
  105. template <class InIt, class OutIt>
  106. OutIt copy_n(InIt b,
  107. typename std::iterator_traits<InIt>::difference_type n, OutIt d)
  108. {
  109. for (/**/; n != 0; --n, ++b, ++d)
  110. {
  111. *d = *b;
  112. }
  113. return d;
  114. }
  115. template <class Pod, class T>
  116. inline void pod_fill(Pod* b, Pod* e, T c)
  117. {
  118. switch ((e - b) & 7)
  119. {
  120. case 0:
  121. while (b != e)
  122. {
  123. *b = c; ++b; BOOST_FALLTHROUGH;
  124. case 7: *b = c; ++b; BOOST_FALLTHROUGH;
  125. case 6: *b = c; ++b; BOOST_FALLTHROUGH;
  126. case 5: *b = c; ++b; BOOST_FALLTHROUGH;
  127. case 4: *b = c; ++b; BOOST_FALLTHROUGH;
  128. case 3: *b = c; ++b; BOOST_FALLTHROUGH;
  129. case 2: *b = c; ++b; BOOST_FALLTHROUGH;
  130. case 1: *b = c; ++b;
  131. }
  132. }
  133. }
  134. template <class Pod>
  135. inline void pod_move(const Pod* b, const Pod* e, Pod* d)
  136. {
  137. using namespace std;
  138. memmove(d, b, (e - b) * sizeof(*b));
  139. }
  140. template <class Pod>
  141. inline Pod* pod_copy(const Pod* b, const Pod* e, Pod* d)
  142. {
  143. const std::size_t s = e - b;
  144. using namespace std;
  145. memcpy(d, b, s * sizeof(*b));
  146. return d + s;
  147. }
  148. template <typename T> struct get_unsigned
  149. {
  150. typedef T result;
  151. };
  152. template <> struct get_unsigned<char>
  153. {
  154. typedef unsigned char result;
  155. };
  156. template <> struct get_unsigned<signed char>
  157. {
  158. typedef unsigned char result;
  159. };
  160. template <> struct get_unsigned<short int>
  161. {
  162. typedef unsigned short int result;
  163. };
  164. template <> struct get_unsigned<int>
  165. {
  166. typedef unsigned int result;
  167. };
  168. template <> struct get_unsigned<long int>
  169. {
  170. typedef unsigned long int result;
  171. };
  172. enum Shallow {};
  173. }
  174. template <class T> class mallocator
  175. {
  176. public:
  177. typedef T value_type;
  178. typedef value_type* pointer;
  179. typedef const value_type* const_pointer;
  180. typedef value_type& reference;
  181. typedef const value_type& const_reference;
  182. typedef std::size_t size_type;
  183. //typedef unsigned int size_type;
  184. //typedef std::ptrdiff_t difference_type;
  185. typedef int difference_type;
  186. template <class U>
  187. struct rebind { typedef mallocator<U> other; };
  188. mallocator() {}
  189. mallocator(const mallocator&) {}
  190. //template <class U>
  191. //mallocator(const mallocator<U>&) {}
  192. ~mallocator() {}
  193. pointer address(reference x) const { return &x; }
  194. const_pointer address(const_reference x) const
  195. {
  196. return x;
  197. }
  198. pointer allocate(size_type n, const_pointer = 0)
  199. {
  200. using namespace std;
  201. void* p = malloc(n * sizeof(T));
  202. if (!p) boost::throw_exception(std::bad_alloc());
  203. return static_cast<pointer>(p);
  204. }
  205. void deallocate(pointer p, size_type)
  206. {
  207. using namespace std;
  208. free(p);
  209. }
  210. size_type max_size() const
  211. {
  212. return static_cast<size_type>(-1) / sizeof(T);
  213. }
  214. void construct(pointer p, const value_type& x)
  215. {
  216. new(p) value_type(x);
  217. }
  218. void destroy(pointer p)
  219. {
  220. p->~value_type();
  221. }
  222. private:
  223. void operator=(const mallocator&);
  224. };
  225. template<> class mallocator<void>
  226. {
  227. typedef void value_type;
  228. typedef void* pointer;
  229. typedef const void* const_pointer;
  230. template <class U>
  231. struct rebind { typedef mallocator<U> other; };
  232. };
  233. template <class T>
  234. inline bool operator==(const mallocator<T>&,
  235. const mallocator<T>&) {
  236. return true;
  237. }
  238. template <class T>
  239. inline bool operator!=(const mallocator<T>&,
  240. const mallocator<T>&) {
  241. return false;
  242. }
  243. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  244. template<class A>
  245. struct allocator_traits {
  246. typedef typename A::value_type value_type;
  247. typedef typename A::pointer pointer;
  248. typedef typename A::const_pointer const_pointer;
  249. typedef typename A::size_type size_type;
  250. typedef typename A::reference reference;
  251. typedef typename A::const_reference const_reference;
  252. static pointer allocate( A& a, size_type n, const_pointer hint )
  253. {
  254. return a.allocate(n, hint);
  255. }
  256. static void deallocate( A& a, pointer p, size_type n )
  257. {
  258. a.deallocate(p, n);
  259. }
  260. static size_type max_size( A const& a ) { return a.max_size(); }
  261. };
  262. #else
  263. using std::allocator_traits;
  264. #endif
  265. ////////////////////////////////////////////////////////////////////////////////
  266. // class template SimpleStringStorage
  267. // Allocates memory with malloc
  268. ////////////////////////////////////////////////////////////////////////////////
  269. template <typename E, class A = std::allocator<E> >
  270. class SimpleStringStorage
  271. {
  272. // The "public" below exists because MSVC can't do template typedefs
  273. public:
  274. struct Data
  275. {
  276. Data() : pEnd_(buffer_), pEndOfMem_(buffer_) { buffer_[0] = E(0); }
  277. E* pEnd_;
  278. E* pEndOfMem_;
  279. E buffer_[1];
  280. };
  281. static const Data emptyString_;
  282. typedef typename allocator_traits<A>::size_type size_type;
  283. private:
  284. Data* pData_;
  285. void Init(size_type size, size_type capacity)
  286. {
  287. BOOST_ASSERT(size <= capacity);
  288. if (capacity == 0)
  289. {
  290. pData_ = const_cast<Data*>(&emptyString_);
  291. }
  292. else
  293. {
  294. // 11-17-2000: comment added:
  295. // No need to allocate (capacity + 1) to
  296. // accommodate the terminating 0, because Data already
  297. // has one character in there
  298. pData_ = static_cast<Data*>(
  299. malloc(sizeof(Data) + capacity * sizeof(E)));
  300. if (!pData_) boost::throw_exception(std::bad_alloc());
  301. pData_->pEnd_ = pData_->buffer_ + size;
  302. pData_->pEndOfMem_ = pData_->buffer_ + capacity;
  303. }
  304. }
  305. private:
  306. // Warning - this doesn't initialize pData_. Used in reserve()
  307. SimpleStringStorage()
  308. { }
  309. public:
  310. typedef E value_type;
  311. typedef E* iterator;
  312. typedef const E* const_iterator;
  313. typedef A allocator_type;
  314. SimpleStringStorage(const SimpleStringStorage& rhs)
  315. {
  316. const size_type sz = rhs.size();
  317. Init(sz, sz);
  318. if (sz) flex_string_details::pod_copy(rhs.begin(), rhs.end(), begin());
  319. }
  320. SimpleStringStorage(const SimpleStringStorage& s,
  321. flex_string_details::Shallow)
  322. : pData_(s.pData_)
  323. {
  324. }
  325. SimpleStringStorage(const A&)
  326. { pData_ = const_cast<Data*>(&emptyString_); }
  327. SimpleStringStorage(const E* s, size_type len, const A&)
  328. {
  329. Init(len, len);
  330. flex_string_details::pod_copy(s, s + len, begin());
  331. }
  332. SimpleStringStorage(size_type len, E c, const A&)
  333. {
  334. Init(len, len);
  335. flex_string_details::pod_fill(begin(), end(), c);
  336. }
  337. SimpleStringStorage& operator=(const SimpleStringStorage& rhs)
  338. {
  339. const size_type sz = rhs.size();
  340. reserve(sz);
  341. flex_string_details::pod_copy(&*rhs.begin(), &*rhs.end(), begin());
  342. pData_->pEnd_ = &*begin() + sz;
  343. return *this;
  344. }
  345. ~SimpleStringStorage()
  346. {
  347. BOOST_ASSERT(begin() <= end());
  348. if (pData_ != &emptyString_) free(pData_);
  349. }
  350. iterator begin()
  351. { return pData_->buffer_; }
  352. const_iterator begin() const
  353. { return pData_->buffer_; }
  354. iterator end()
  355. { return pData_->pEnd_; }
  356. const_iterator end() const
  357. { return pData_->pEnd_; }
  358. size_type size() const
  359. { return pData_->pEnd_ - pData_->buffer_; }
  360. size_type max_size() const
  361. { return std::size_t(-1) / sizeof(E) - sizeof(Data) - 1; }
  362. size_type capacity() const
  363. { return pData_->pEndOfMem_ - pData_->buffer_; }
  364. void reserve(size_type res_arg)
  365. {
  366. if (res_arg <= capacity())
  367. {
  368. // @@@ insert shrinkage here if you wish
  369. return;
  370. }
  371. if (pData_ == &emptyString_)
  372. {
  373. Init(0, res_arg);
  374. }
  375. else
  376. {
  377. const size_type sz = size();
  378. void* p = realloc(pData_,
  379. sizeof(Data) + res_arg * sizeof(E));
  380. if (!p) boost::throw_exception(std::bad_alloc());
  381. if (p != pData_)
  382. {
  383. pData_ = static_cast<Data*>(p);
  384. pData_->pEnd_ = pData_->buffer_ + sz;
  385. }
  386. pData_->pEndOfMem_ = pData_->buffer_ + res_arg;
  387. }
  388. }
  389. void append(const E* s, size_type sz)
  390. {
  391. const size_type neededCapacity = size() + sz;
  392. if (capacity() < neededCapacity)
  393. {
  394. const iterator b = begin();
  395. static std::less_equal<const E*> le;
  396. if (le(b, s) && le(s, end()))
  397. {
  398. // aliased
  399. const size_type offset = s - b;
  400. reserve(neededCapacity);
  401. s = begin() + offset;
  402. }
  403. else
  404. {
  405. reserve(neededCapacity);
  406. }
  407. }
  408. flex_string_details::pod_copy(s, s + sz, end());
  409. pData_->pEnd_ += sz;
  410. }
  411. template <class InputIterator>
  412. void append(InputIterator b, InputIterator e)
  413. {
  414. // @@@ todo: optimize this depending on iterator type
  415. for (; b != e; ++b)
  416. {
  417. *this += *b;
  418. }
  419. }
  420. void resize(size_type newSize, E fill)
  421. {
  422. const int delta = int(newSize - size());
  423. if (delta == 0) return;
  424. if (delta > 0)
  425. {
  426. if (newSize > capacity())
  427. {
  428. reserve(newSize);
  429. }
  430. E* e = &*end();
  431. flex_string_details::pod_fill(e, e + delta, fill);
  432. }
  433. pData_->pEnd_ = pData_->buffer_ + newSize;
  434. }
  435. void swap(SimpleStringStorage& rhs)
  436. {
  437. std::swap(pData_, rhs.pData_);
  438. }
  439. const E* c_str() const
  440. {
  441. if (pData_ != &emptyString_) *pData_->pEnd_ = E();
  442. return pData_->buffer_;
  443. }
  444. const E* data() const
  445. { return pData_->buffer_; }
  446. A get_allocator() const
  447. { return A(); }
  448. };
  449. template <typename E, class A>
  450. const typename SimpleStringStorage<E, A>::Data
  451. SimpleStringStorage<E, A>::emptyString_ =
  452. typename SimpleStringStorage<E, A>::Data();
  453. ////////////////////////////////////////////////////////////////////////////////
  454. // class template AllocatorStringStorage
  455. // Allocates with your allocator
  456. // Takes advantage of the Empty Base Optimization if available
  457. ////////////////////////////////////////////////////////////////////////////////
  458. template <typename E, class A = std::allocator<E> >
  459. class AllocatorStringStorage : public A
  460. {
  461. typedef typename allocator_traits<A>::size_type size_type;
  462. typedef typename SimpleStringStorage<E, A>::Data Data;
  463. void* Alloc(size_type sz, const void* p = 0)
  464. {
  465. return allocator_traits<A>::allocate(*this, 1 + (sz - 1) / sizeof(E),
  466. static_cast<const char*>(p));
  467. }
  468. void* Realloc(void* p, size_type oldSz, size_type newSz)
  469. {
  470. void* r = Alloc(newSz);
  471. flex_string_details::pod_copy(p, p + Min(oldSz, newSz), r);
  472. Free(p, oldSz);
  473. return r;
  474. }
  475. void Free(void* p, size_type sz)
  476. {
  477. allocator_traits<A>::deallocate(*this, static_cast<E*>(p), sz);
  478. }
  479. Data* pData_;
  480. void Init(size_type size, size_type cap)
  481. {
  482. BOOST_ASSERT(size <= cap);
  483. if (cap == 0)
  484. {
  485. pData_ = const_cast<Data*>(
  486. &SimpleStringStorage<E, A>::emptyString_);
  487. }
  488. else
  489. {
  490. pData_ = static_cast<Data*>(Alloc(
  491. cap * sizeof(E) + sizeof(Data)));
  492. pData_->pEnd_ = pData_->buffer_ + size;
  493. pData_->pEndOfMem_ = pData_->buffer_ + cap;
  494. }
  495. }
  496. public:
  497. typedef E value_type;
  498. typedef E* iterator;
  499. typedef const E* const_iterator;
  500. typedef A allocator_type;
  501. AllocatorStringStorage()
  502. : A(), pData_(0)
  503. {
  504. }
  505. AllocatorStringStorage(const AllocatorStringStorage& rhs)
  506. : A(rhs.get_allocator())
  507. {
  508. const size_type sz = rhs.size();
  509. Init(sz, sz);
  510. if (sz) flex_string_details::pod_copy(rhs.begin(), rhs.end(), begin());
  511. }
  512. AllocatorStringStorage(const AllocatorStringStorage& s,
  513. flex_string_details::Shallow)
  514. : A(s.get_allocator())
  515. {
  516. pData_ = s.pData_;
  517. }
  518. AllocatorStringStorage(const A& a) : A(a)
  519. {
  520. pData_ = const_cast<Data*>(
  521. &SimpleStringStorage<E, A>::emptyString_);
  522. }
  523. AllocatorStringStorage(const E* s, size_type len, const A& a)
  524. : A(a)
  525. {
  526. Init(len, len);
  527. flex_string_details::pod_copy(s, s + len, begin());
  528. }
  529. AllocatorStringStorage(size_type len, E c, const A& a)
  530. : A(a)
  531. {
  532. Init(len, len);
  533. flex_string_details::pod_fill(&*begin(), &*end(), c);
  534. }
  535. AllocatorStringStorage& operator=(const AllocatorStringStorage& rhs)
  536. {
  537. const size_type sz = rhs.size();
  538. reserve(sz);
  539. flex_string_details::pod_copy(&*rhs.begin(), &*rhs.end(), begin());
  540. pData_->pEnd_ = &*begin() + rhs.size();
  541. return *this;
  542. }
  543. ~AllocatorStringStorage()
  544. {
  545. if (capacity())
  546. {
  547. Free(pData_,
  548. sizeof(Data) + capacity() * sizeof(E));
  549. }
  550. }
  551. iterator begin()
  552. { return pData_->buffer_; }
  553. const_iterator begin() const
  554. { return pData_->buffer_; }
  555. iterator end()
  556. { return pData_->pEnd_; }
  557. const_iterator end() const
  558. { return pData_->pEnd_; }
  559. size_type size() const
  560. { return size_type(end() - begin()); }
  561. size_type max_size() const
  562. { return allocator_traits<A>::max_size(*this); }
  563. size_type capacity() const
  564. { return size_type(pData_->pEndOfMem_ - pData_->buffer_); }
  565. void resize(size_type n, E c)
  566. {
  567. reserve(n);
  568. iterator newEnd = begin() + n;
  569. iterator oldEnd = end();
  570. if (newEnd > oldEnd)
  571. {
  572. // Copy the characters
  573. flex_string_details::pod_fill(oldEnd, newEnd, c);
  574. }
  575. if (capacity()) pData_->pEnd_ = newEnd;
  576. }
  577. void reserve(size_type res_arg)
  578. {
  579. if (res_arg <= capacity())
  580. {
  581. // @@@ shrink to fit here
  582. return;
  583. }
  584. A& myAlloc = *this;
  585. AllocatorStringStorage newStr(myAlloc);
  586. newStr.Init(size(), res_arg);
  587. flex_string_details::pod_copy(begin(), end(), newStr.begin());
  588. swap(newStr);
  589. }
  590. template <class ForwardIterator>
  591. void append(ForwardIterator b, ForwardIterator e)
  592. {
  593. const size_type
  594. sz = std::distance(b, e),
  595. neededCapacity = size() + sz;
  596. if (capacity() < neededCapacity)
  597. {
  598. // typedef std::less_equal<const E*> le_type;
  599. // BOOST_ASSERT(!(le_type()(begin(), &*b) && le_type()(&*b, end())));
  600. reserve(neededCapacity);
  601. }
  602. std::copy(b, e, end());
  603. pData_->pEnd_ += sz;
  604. }
  605. void swap(AllocatorStringStorage& rhs)
  606. {
  607. // @@@ The following line is commented due to a bug in MSVC
  608. //std::swap(lhsAlloc, rhsAlloc);
  609. std::swap(pData_, rhs.pData_);
  610. }
  611. const E* c_str() const
  612. {
  613. if (pData_ != &SimpleStringStorage<E, A>::emptyString_)
  614. {
  615. *pData_->pEnd_ = E();
  616. }
  617. return &*begin();
  618. }
  619. const E* data() const
  620. { return &*begin(); }
  621. A get_allocator() const
  622. { return *this; }
  623. };
  624. ////////////////////////////////////////////////////////////////////////////////
  625. // class template VectorStringStorage
  626. // Uses std::vector
  627. // Takes advantage of the Empty Base Optimization if available
  628. ////////////////////////////////////////////////////////////////////////////////
  629. template <typename E, class A = std::allocator<E> >
  630. class VectorStringStorage : protected std::vector<E, A>
  631. {
  632. typedef std::vector<E, A> base;
  633. public: // protected:
  634. typedef E value_type;
  635. typedef typename base::iterator iterator;
  636. typedef typename base::const_iterator const_iterator;
  637. typedef A allocator_type;
  638. typedef typename allocator_traits<A>::size_type size_type;
  639. VectorStringStorage(const VectorStringStorage& s) : base(s)
  640. { }
  641. VectorStringStorage(const A& a) : base(1, E(), a)
  642. { }
  643. VectorStringStorage(const E* s, size_type len, const A& a)
  644. : base(a)
  645. {
  646. base::reserve(len + 1);
  647. base::insert(base::end(), s, s + len);
  648. // Terminating zero
  649. base::insert(base::end(), E());
  650. }
  651. VectorStringStorage(size_type len, E c, const A& a)
  652. : base(len + 1, c, a)
  653. {
  654. // Terminating zero
  655. base::back() = E();
  656. }
  657. VectorStringStorage& operator=(const VectorStringStorage& rhs)
  658. {
  659. base& v = *this;
  660. v = rhs;
  661. return *this;
  662. }
  663. iterator begin()
  664. { return base::begin(); }
  665. const_iterator begin() const
  666. { return base::begin(); }
  667. iterator end()
  668. { return base::end() - 1; }
  669. const_iterator end() const
  670. { return base::end() - 1; }
  671. size_type size() const
  672. { return base::size() - 1; }
  673. size_type max_size() const
  674. { return base::max_size() - 1; }
  675. size_type capacity() const
  676. { return base::capacity() - 1; }
  677. void reserve(size_type res_arg)
  678. {
  679. BOOST_ASSERT(res_arg < max_size());
  680. base::reserve(res_arg + 1);
  681. }
  682. void append(const E* s, size_type sz)
  683. {
  684. // Check for aliasing because std::vector doesn't do it.
  685. static std::less_equal<const E*> le;
  686. if (!base::empty())
  687. {
  688. const E* start = &base::front();
  689. if (le(start, s) && le(s, start + size()))
  690. {
  691. // aliased
  692. const size_type offset = s - start;
  693. reserve(size() + sz);
  694. s = &base::front() + offset;
  695. }
  696. }
  697. base::insert(end(), s, s + sz);
  698. }
  699. template <class InputIterator>
  700. void append(InputIterator b, InputIterator e)
  701. {
  702. base::insert(end(), b, e);
  703. }
  704. void resize(size_type n, E c)
  705. {
  706. base::reserve(n + 1);
  707. base::back() = c;
  708. base::resize(n + 1, c);
  709. base::back() = E();
  710. }
  711. void swap(VectorStringStorage& rhs)
  712. { base::swap(rhs); }
  713. const E* c_str() const
  714. { return &*begin(); }
  715. const E* data() const
  716. { return &*begin(); }
  717. A get_allocator() const
  718. { return base::get_allocator(); }
  719. };
  720. ////////////////////////////////////////////////////////////////////////////////
  721. // class template SmallStringOpt
  722. // Builds the small string optimization over any other storage
  723. ////////////////////////////////////////////////////////////////////////////////
  724. template <class Storage, unsigned int threshold,
  725. typename Align = typename Storage::value_type*>
  726. class SmallStringOpt
  727. {
  728. public:
  729. typedef typename Storage::value_type value_type;
  730. typedef value_type* iterator;
  731. typedef const value_type* const_iterator;
  732. typedef typename Storage::allocator_type allocator_type;
  733. typedef typename allocator_traits<allocator_type>::size_type size_type;
  734. private:
  735. enum { temp1 = threshold * sizeof(value_type) > sizeof(Storage)
  736. ? threshold * sizeof(value_type)
  737. : sizeof(Storage) };
  738. enum { temp2 = temp1 > sizeof(Align) ? temp1 : sizeof(Align) };
  739. public:
  740. enum { maxSmallString =
  741. (temp2 + sizeof(value_type) - 1) / sizeof(value_type) };
  742. private:
  743. enum { magic = maxSmallString + 1 };
  744. union
  745. {
  746. mutable value_type buf_[maxSmallString + 1];
  747. Align align_;
  748. };
  749. Storage& GetStorage()
  750. {
  751. BOOST_ASSERT(buf_[maxSmallString] == magic);
  752. Storage* p = reinterpret_cast<Storage*>(&buf_[0]);
  753. return *p;
  754. }
  755. const Storage& GetStorage() const
  756. {
  757. BOOST_ASSERT(buf_[maxSmallString] == magic);
  758. const Storage *p = reinterpret_cast<const Storage*>(&buf_[0]);
  759. return *p;
  760. }
  761. bool Small() const
  762. {
  763. return buf_[maxSmallString] != magic;
  764. }
  765. public:
  766. SmallStringOpt(const SmallStringOpt& s)
  767. {
  768. if (s.Small())
  769. {
  770. flex_string_details::pod_copy(
  771. s.buf_,
  772. s.buf_ + s.size(),
  773. buf_);
  774. }
  775. else
  776. {
  777. new(buf_) Storage(s.GetStorage());
  778. }
  779. buf_[maxSmallString] = s.buf_[maxSmallString];
  780. }
  781. SmallStringOpt(const allocator_type&)
  782. {
  783. buf_[maxSmallString] = maxSmallString;
  784. }
  785. SmallStringOpt(const value_type* s, size_type len, const allocator_type& a)
  786. {
  787. if (len <= maxSmallString)
  788. {
  789. flex_string_details::pod_copy(s, s + len, buf_);
  790. buf_[maxSmallString] = value_type(maxSmallString - len);
  791. }
  792. else
  793. {
  794. new(buf_) Storage(s, len, a);
  795. buf_[maxSmallString] = magic;
  796. }
  797. }
  798. SmallStringOpt(size_type len, value_type c, const allocator_type& a)
  799. {
  800. if (len <= maxSmallString)
  801. {
  802. flex_string_details::pod_fill(buf_, buf_ + len, c);
  803. buf_[maxSmallString] = value_type(maxSmallString - len);
  804. }
  805. else
  806. {
  807. new(buf_) Storage(len, c, a);
  808. buf_[maxSmallString] = magic;
  809. }
  810. }
  811. SmallStringOpt& operator=(const SmallStringOpt& rhs)
  812. {
  813. reserve(rhs.size());
  814. resize(0, 0);
  815. append(rhs.data(), rhs.size());
  816. return *this;
  817. }
  818. ~SmallStringOpt()
  819. {
  820. if (!Small()) GetStorage().~Storage();
  821. }
  822. iterator begin()
  823. {
  824. if (Small()) return buf_;
  825. return &*GetStorage().begin();
  826. }
  827. const_iterator begin() const
  828. {
  829. if (Small()) return buf_;
  830. return &*GetStorage().begin();
  831. }
  832. iterator end()
  833. {
  834. if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
  835. return &*GetStorage().end();
  836. }
  837. const_iterator end() const
  838. {
  839. if (Small()) return buf_ + maxSmallString - buf_[maxSmallString];
  840. return &*GetStorage().end();
  841. }
  842. size_type size() const
  843. {
  844. BOOST_ASSERT(!Small() || maxSmallString >= buf_[maxSmallString]);
  845. return Small()
  846. ? maxSmallString - buf_[maxSmallString]
  847. : GetStorage().size();
  848. }
  849. size_type max_size() const
  850. { return get_allocator().max_size(); }
  851. size_type capacity() const
  852. { return Small() ? maxSmallString : GetStorage().capacity(); }
  853. void reserve(size_type res_arg)
  854. {
  855. if (Small())
  856. {
  857. if (res_arg <= maxSmallString) return;
  858. SmallStringOpt temp(*this);
  859. this->~SmallStringOpt();
  860. new(buf_) Storage(temp.data(), temp.size(),
  861. temp.get_allocator());
  862. buf_[maxSmallString] = magic;
  863. GetStorage().reserve(res_arg);
  864. }
  865. else
  866. {
  867. GetStorage().reserve(res_arg);
  868. }
  869. BOOST_ASSERT(capacity() >= res_arg);
  870. }
  871. void append(const value_type* s, size_type sz)
  872. {
  873. if (!Small())
  874. {
  875. GetStorage().append(s, sz);
  876. }
  877. else
  878. {
  879. // append to a small string
  880. const size_type neededCapacity =
  881. maxSmallString - buf_[maxSmallString] + sz;
  882. if (maxSmallString < neededCapacity)
  883. {
  884. // need to change storage strategy
  885. allocator_type alloc;
  886. Storage temp(alloc);
  887. temp.reserve(neededCapacity);
  888. temp.append(buf_, maxSmallString - buf_[maxSmallString]);
  889. temp.append(s, sz);
  890. buf_[maxSmallString] = magic;
  891. new(buf_) Storage(temp.get_allocator());
  892. GetStorage().swap(temp);
  893. }
  894. else
  895. {
  896. flex_string_details::pod_move(s, s + sz,
  897. buf_ + maxSmallString - buf_[maxSmallString]);
  898. buf_[maxSmallString] -= value_type(sz);
  899. }
  900. }
  901. }
  902. template <class InputIterator>
  903. void append(InputIterator b, InputIterator e)
  904. {
  905. // @@@ todo: optimize this depending on iterator type
  906. for (; b != e; ++b)
  907. {
  908. *this += *b;
  909. }
  910. }
  911. void resize(size_type n, value_type c)
  912. {
  913. if (Small())
  914. {
  915. if (n > maxSmallString)
  916. {
  917. // Small string resized to big string
  918. SmallStringOpt temp(*this); // can't throw
  919. // 11-17-2001: correct exception safety bug
  920. Storage newString(temp.data(), temp.size(),
  921. temp.get_allocator());
  922. newString.resize(n, c);
  923. // We make the reasonable assumption that an empty Storage
  924. // constructor won't throw
  925. this->~SmallStringOpt();
  926. new(&buf_[0]) Storage(temp.get_allocator());
  927. buf_[maxSmallString] = value_type(magic);
  928. GetStorage().swap(newString);
  929. }
  930. else
  931. {
  932. // Small string resized to small string
  933. // 11-17-2001: bug fix: terminating zero not copied
  934. size_type toFill = n > size() ? n - size() : 0;
  935. flex_string_details::pod_fill(end(), end() + toFill, c);
  936. buf_[maxSmallString] = value_type(maxSmallString - n);
  937. }
  938. }
  939. else
  940. {
  941. if (n > maxSmallString)
  942. {
  943. // Big string resized to big string
  944. GetStorage().resize(n, c);
  945. }
  946. else
  947. {
  948. // Big string resized to small string
  949. // 11-17=2001: bug fix in the BOOST_ASSERTion below
  950. BOOST_ASSERT(capacity() > n);
  951. SmallStringOpt newObj(data(), n, get_allocator());
  952. newObj.swap(*this);
  953. }
  954. }
  955. }
  956. void swap(SmallStringOpt& rhs)
  957. {
  958. if (Small())
  959. {
  960. if (rhs.Small())
  961. {
  962. // Small swapped with small
  963. std::swap_ranges(buf_, buf_ + maxSmallString + 1,
  964. rhs.buf_);
  965. }
  966. else
  967. {
  968. // Small swapped with big
  969. // Make a copy of myself - can't throw
  970. SmallStringOpt temp(*this);
  971. // Nuke myself
  972. this->~SmallStringOpt();
  973. // Make an empty storage for myself (likely won't throw)
  974. new(buf_) Storage(0, value_type(), rhs.get_allocator());
  975. buf_[maxSmallString] = magic;
  976. // Recurse to this same function
  977. swap(rhs);
  978. // Nuke rhs
  979. rhs.~SmallStringOpt();
  980. // Build the new small string into rhs
  981. new(&rhs) SmallStringOpt(temp);
  982. }
  983. }
  984. else
  985. {
  986. if (rhs.Small())
  987. {
  988. // Big swapped with small
  989. // Already implemented, recurse with reversed args
  990. rhs.swap(*this);
  991. }
  992. else
  993. {
  994. // Big swapped with big
  995. GetStorage().swap(rhs.GetStorage());
  996. }
  997. }
  998. }
  999. const value_type* c_str() const
  1000. {
  1001. if (!Small()) return GetStorage().c_str();
  1002. buf_[maxSmallString - buf_[maxSmallString]] = value_type();
  1003. return buf_;
  1004. }
  1005. const value_type* data() const
  1006. { return Small() ? buf_ : GetStorage().data(); }
  1007. allocator_type get_allocator() const
  1008. { return allocator_type(); }
  1009. };
  1010. ////////////////////////////////////////////////////////////////////////////////
  1011. // class template CowString
  1012. // Implements Copy on Write over any storage
  1013. ////////////////////////////////////////////////////////////////////////////////
  1014. template <
  1015. typename Storage,
  1016. typename Align = BOOST_DEDUCED_TYPENAME Storage::value_type*
  1017. >
  1018. class CowString
  1019. {
  1020. typedef typename Storage::value_type E;
  1021. typedef typename flex_string_details::get_unsigned<E>::result RefCountType;
  1022. public:
  1023. typedef E value_type;
  1024. typedef typename Storage::iterator iterator;
  1025. typedef typename Storage::const_iterator const_iterator;
  1026. typedef typename Storage::allocator_type allocator_type;
  1027. typedef typename allocator_traits<allocator_type>::size_type size_type;
  1028. typedef typename Storage::value_type& reference;
  1029. private:
  1030. union
  1031. {
  1032. mutable char buf_[sizeof(Storage)];
  1033. Align align_;
  1034. };
  1035. Storage& Data() const
  1036. {
  1037. Storage* p = reinterpret_cast<Storage*>(&buf_[0]);
  1038. return *p;
  1039. }
  1040. RefCountType GetRefs() const
  1041. {
  1042. const Storage& d = Data();
  1043. BOOST_ASSERT(d.size() > 0);
  1044. BOOST_ASSERT(static_cast<RefCountType>(*d.begin()) != 0);
  1045. return *d.begin();
  1046. }
  1047. RefCountType& Refs()
  1048. {
  1049. Storage& d = Data();
  1050. BOOST_ASSERT(d.size() > 0);
  1051. return reinterpret_cast<RefCountType&>(*d.begin());
  1052. }
  1053. void MakeUnique() const
  1054. {
  1055. BOOST_ASSERT(GetRefs() >= 1);
  1056. if (GetRefs() == 1) return;
  1057. union
  1058. {
  1059. char buf_[sizeof(Storage)];
  1060. Align align_;
  1061. } temp;
  1062. --(*Data().begin()); // decrement the use count of the remaining object
  1063. Storage* p = reinterpret_cast<Storage*>(&temp.buf_[0]);
  1064. new(buf_) Storage(
  1065. *new(p) Storage(Data()),
  1066. flex_string_details::Shallow());
  1067. *Data().begin() = 1;
  1068. }
  1069. public:
  1070. CowString(const CowString& s)
  1071. {
  1072. if (s.GetRefs() == (std::numeric_limits<RefCountType>::max)())
  1073. {
  1074. // must make a brand new copy
  1075. new(buf_) Storage(s.Data()); // non shallow
  1076. Refs() = 1;
  1077. }
  1078. else
  1079. {
  1080. new(buf_) Storage(s.Data(), flex_string_details::Shallow());
  1081. ++Refs();
  1082. }
  1083. BOOST_ASSERT(Data().size() > 0);
  1084. }
  1085. CowString(const allocator_type& a)
  1086. {
  1087. new(buf_) Storage(1, 1, a);
  1088. }
  1089. CowString(const E* s, size_type len, const allocator_type& a)
  1090. {
  1091. // Warning - MSVC's debugger has trouble tracing through the code below.
  1092. // It seems to be a const-correctness issue
  1093. //
  1094. new(buf_) Storage(a);
  1095. Data().reserve(len + 1);
  1096. Data().resize(1, 1);
  1097. Data().append(s, s + len);
  1098. }
  1099. CowString(size_type len, E c, const allocator_type& a)
  1100. {
  1101. new(buf_) Storage(len + 1, c, a);
  1102. Refs() = 1;
  1103. }
  1104. CowString& operator=(const CowString& rhs)
  1105. {
  1106. // CowString(rhs).swap(*this);
  1107. if (--Refs() == 0)
  1108. Data().~Storage();
  1109. if (rhs.GetRefs() == (std::numeric_limits<RefCountType>::max)())
  1110. {
  1111. // must make a brand new copy
  1112. new(buf_) Storage(rhs.Data()); // non shallow
  1113. Refs() = 1;
  1114. }
  1115. else
  1116. {
  1117. new(buf_) Storage(rhs.Data(), flex_string_details::Shallow());
  1118. ++Refs();
  1119. }
  1120. BOOST_ASSERT(Data().size() > 0);
  1121. return *this;
  1122. }
  1123. ~CowString()
  1124. {
  1125. BOOST_ASSERT(Data().size() > 0);
  1126. if (--Refs() == 0)
  1127. Data().~Storage();
  1128. }
  1129. iterator begin()
  1130. {
  1131. BOOST_ASSERT(Data().size() > 0);
  1132. MakeUnique();
  1133. return Data().begin() + 1;
  1134. }
  1135. const_iterator begin() const
  1136. {
  1137. BOOST_ASSERT(Data().size() > 0);
  1138. return Data().begin() + 1;
  1139. }
  1140. iterator end()
  1141. {
  1142. MakeUnique();
  1143. return Data().end();
  1144. }
  1145. const_iterator end() const
  1146. {
  1147. return Data().end();
  1148. }
  1149. size_type size() const
  1150. {
  1151. BOOST_ASSERT(Data().size() > 0);
  1152. return Data().size() - 1;
  1153. }
  1154. size_type max_size() const
  1155. {
  1156. BOOST_ASSERT(Data().max_size() > 0);
  1157. return Data().max_size() - 1;
  1158. }
  1159. size_type capacity() const
  1160. {
  1161. BOOST_ASSERT(Data().capacity() > 0);
  1162. return Data().capacity() - 1;
  1163. }
  1164. void resize(size_type n, E c)
  1165. {
  1166. BOOST_ASSERT(Data().size() > 0);
  1167. MakeUnique();
  1168. Data().resize(n + 1, c);
  1169. }
  1170. template <class FwdIterator>
  1171. void append(FwdIterator b, FwdIterator e)
  1172. {
  1173. MakeUnique();
  1174. Data().append(b, e);
  1175. }
  1176. void reserve(size_type res_arg)
  1177. {
  1178. if (capacity() > res_arg) return;
  1179. MakeUnique();
  1180. Data().reserve(res_arg + 1);
  1181. }
  1182. void swap(CowString& rhs)
  1183. {
  1184. Data().swap(rhs.Data());
  1185. }
  1186. const E* c_str() const
  1187. {
  1188. BOOST_ASSERT(Data().size() > 0);
  1189. return Data().c_str() + 1;
  1190. }
  1191. const E* data() const
  1192. {
  1193. BOOST_ASSERT(Data().size() > 0);
  1194. return Data().data() + 1;
  1195. }
  1196. allocator_type get_allocator() const
  1197. {
  1198. return Data().get_allocator();
  1199. }
  1200. };
  1201. ////////////////////////////////////////////////////////////////////////////////
  1202. // class template flex_string
  1203. // a std::basic_string compatible implementation
  1204. // Uses a Storage policy
  1205. ////////////////////////////////////////////////////////////////////////////////
  1206. template <typename E,
  1207. class T = std::char_traits<E>,
  1208. class A = std::allocator<E>,
  1209. class Storage = AllocatorStringStorage<E, A> >
  1210. class flex_string : private Storage
  1211. {
  1212. #if defined(BOOST_WAVE_FLEXSTRING_THROW_ON_ENFORCE)
  1213. template <typename Exception>
  1214. static void Enforce(bool condition, Exception*, const char* msg)
  1215. { if (!condition) boost::throw_exception(Exception(msg)); }
  1216. #else
  1217. template <typename Exception>
  1218. static inline void Enforce(bool condition, Exception*, const char* msg)
  1219. { BOOST_ASSERT(condition && msg); }
  1220. #endif // defined(BOOST_WAVE_FLEXSTRING_THROW_ON_ENFORCE)
  1221. #ifndef NDEBUG
  1222. bool Sane() const
  1223. {
  1224. return
  1225. begin() <= end() &&
  1226. empty() == (size() == 0) &&
  1227. empty() == (begin() == end()) &&
  1228. size() <= max_size() &&
  1229. capacity() <= max_size() &&
  1230. size() <= capacity();
  1231. }
  1232. struct Invariant;
  1233. friend struct Invariant;
  1234. struct Invariant
  1235. {
  1236. Invariant(const flex_string& s) : s_(s)
  1237. {
  1238. BOOST_ASSERT(s_.Sane());
  1239. }
  1240. ~Invariant()
  1241. {
  1242. BOOST_ASSERT(s_.Sane());
  1243. }
  1244. private:
  1245. const flex_string& s_;
  1246. Invariant& operator=(const Invariant&);
  1247. };
  1248. #endif
  1249. public:
  1250. // types
  1251. typedef T traits_type;
  1252. typedef typename traits_type::char_type value_type;
  1253. typedef A allocator_type;
  1254. typedef typename allocator_traits<A>::value_type& reference;
  1255. typedef typename allocator_traits<A>::value_type const& const_reference;
  1256. typedef typename allocator_traits<A>::pointer pointer;
  1257. typedef typename allocator_traits<A>::const_pointer const_pointer;
  1258. typedef typename allocator_traits<A>::size_type size_type;
  1259. typedef typename Storage::iterator iterator;
  1260. typedef typename Storage::const_iterator const_iterator;
  1261. typedef boost::reverse_iterator<iterator> reverse_iterator;
  1262. typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
  1263. static const size_type npos; // = size_type(-1)
  1264. private:
  1265. static size_type Min(size_type lhs, size_type rhs)
  1266. { return lhs < rhs ? lhs : rhs; }
  1267. static void Procust(size_type& n, size_type nmax)
  1268. { if (n > nmax) n = nmax; }
  1269. public:
  1270. // 21.3.1 construct/copy/destroy
  1271. explicit flex_string(const A& a = A())
  1272. : Storage(a)
  1273. {}
  1274. flex_string(const flex_string& str)
  1275. : Storage(str)
  1276. {
  1277. }
  1278. flex_string(const flex_string& str, size_type pos,
  1279. size_type n = npos, const A& a = A())
  1280. : Storage(a)
  1281. {
  1282. Enforce(pos <= str.size(), (std::out_of_range*)0, "");
  1283. assign(str, pos, n);
  1284. }
  1285. flex_string(const value_type* s, const A& a = A())
  1286. : Storage(s, traits_type::length(s), a)
  1287. {}
  1288. flex_string(const value_type* s, size_type n, const A& a = A())
  1289. : Storage(s, n, a)
  1290. {}
  1291. flex_string(size_type n, value_type c, const A& a = A())
  1292. : Storage(n, c, a)
  1293. {}
  1294. template <class InputIterator>
  1295. flex_string(InputIterator begin, InputIterator end, const A& a = A())
  1296. : Storage(a)
  1297. {
  1298. assign(begin, end);
  1299. }
  1300. ~flex_string()
  1301. {}
  1302. flex_string& operator=(const flex_string& str)
  1303. {
  1304. if (this != &str) {
  1305. Storage& s = *this;
  1306. s = str;
  1307. }
  1308. return *this;
  1309. }
  1310. flex_string& operator=(const value_type* s)
  1311. {
  1312. assign(s);
  1313. return *this;
  1314. }
  1315. flex_string& operator=(value_type c)
  1316. {
  1317. assign(1, c);
  1318. return *this;
  1319. }
  1320. // 21.3.2 iterators:
  1321. iterator begin()
  1322. { return Storage::begin(); }
  1323. const_iterator begin() const
  1324. { return Storage::begin(); }
  1325. iterator end()
  1326. { return Storage::end(); }
  1327. const_iterator end() const
  1328. { return Storage::end(); }
  1329. reverse_iterator rbegin()
  1330. { return reverse_iterator(end()); }
  1331. const_reverse_iterator rbegin() const
  1332. { return const_reverse_iterator(end()); }
  1333. reverse_iterator rend()
  1334. { return reverse_iterator(begin()); }
  1335. const_reverse_iterator rend() const
  1336. { return const_reverse_iterator(begin()); }
  1337. #if BOOST_WAVE_FLEX_STRING_SERIALIZATION_HACK != 0
  1338. // temporary hack to make it easier to serialize flex_string's using
  1339. // the Boost.Serialization library
  1340. value_type & back() { return *(begin()+size()-1); }
  1341. value_type const& back() const { return *(begin()+size()-1); }
  1342. #endif
  1343. // 21.3.3 capacity:
  1344. size_type size() const
  1345. { return Storage::size(); }
  1346. size_type length() const
  1347. { return size(); }
  1348. size_type max_size() const
  1349. { return Storage::max_size(); }
  1350. void resize(size_type n, value_type c)
  1351. { Storage::resize(n, c); }
  1352. void resize(size_type n)
  1353. { resize(n, value_type()); }
  1354. size_type capacity() const
  1355. { return Storage::capacity(); }
  1356. void reserve(size_type res_arg = 0)
  1357. {
  1358. Enforce(res_arg <= max_size(), (std::length_error*)0, "");
  1359. Storage::reserve(res_arg);
  1360. }
  1361. void clear()
  1362. { resize(0); }
  1363. bool empty() const
  1364. { return size() == 0; }
  1365. // 21.3.4 element access:
  1366. const_reference operator[](size_type pos) const
  1367. { return *(begin() + pos); }
  1368. reference operator[](size_type pos)
  1369. { return *(begin() + pos); }
  1370. const_reference at(size_type n) const
  1371. {
  1372. Enforce(n < size(), (std::out_of_range*)0, "");
  1373. return (*this)[n];
  1374. }
  1375. reference at(size_type n)
  1376. {
  1377. Enforce(n < size(), (std::out_of_range*)0, "");
  1378. return (*this)[n];
  1379. }
  1380. // 21.3.5 modifiers:
  1381. flex_string& operator+=(const flex_string& str)
  1382. { return append(str); }
  1383. flex_string& operator+=(const value_type* s)
  1384. { return append(s); }
  1385. flex_string& operator+=(value_type c)
  1386. {
  1387. push_back(c);
  1388. return *this;
  1389. }
  1390. flex_string& append(const flex_string& str)
  1391. { return append(str, 0, npos); }
  1392. flex_string& append(const flex_string& str, const size_type pos,
  1393. size_type n)
  1394. {
  1395. const size_type sz = str.size();
  1396. Enforce(pos <= sz, (std::out_of_range*)0, "");
  1397. Procust(n, sz - pos);
  1398. return append(str.c_str() + pos, n);
  1399. }
  1400. flex_string& append(const value_type* s, const size_type n)
  1401. {
  1402. #ifndef NDEBUG
  1403. Invariant checker(*this);
  1404. #endif
  1405. if (IsAliasedRange(s, s + n))
  1406. {
  1407. const size_type offset = s - &*begin();
  1408. Storage::reserve(size() + n);
  1409. s = &*begin() + offset;
  1410. }
  1411. Storage::append(s, s+ n);
  1412. return *this;
  1413. }
  1414. flex_string& append(const value_type* s)
  1415. { return append(s, traits_type::length(s)); }
  1416. flex_string& append(size_type n, value_type c)
  1417. {
  1418. resize(size() + n, c);
  1419. return *this;
  1420. }
  1421. template<class InputIterator>
  1422. flex_string& append(InputIterator first, InputIterator last)
  1423. {
  1424. insert(end(), first, last);
  1425. return *this;
  1426. }
  1427. void push_back(value_type c)
  1428. {
  1429. const size_type cap = capacity();
  1430. if (size() == cap)
  1431. {
  1432. reserve(cap << 1u);
  1433. }
  1434. Storage::append(&c, &c + 1);
  1435. }
  1436. flex_string& assign(const flex_string& str)
  1437. {
  1438. if (&str == this) return *this;
  1439. return assign(str.data(), str.size());
  1440. }
  1441. flex_string& assign(const flex_string& str, size_type pos,
  1442. size_type n)
  1443. {
  1444. const size_type sz = str.size();
  1445. Enforce(pos <= str.size(), (std::out_of_range*)0, "");
  1446. Procust(n, sz - pos);
  1447. return assign(str.data() + pos, n);
  1448. }
  1449. flex_string& assign(const value_type* s, size_type n)
  1450. {
  1451. #ifndef NDEBUG
  1452. Invariant checker(*this);
  1453. #endif
  1454. if (size() >= n)
  1455. {
  1456. std::copy(s, s + n, begin());
  1457. resize(n);
  1458. }
  1459. else
  1460. {
  1461. const value_type *const s2 = s + size();
  1462. std::copy(s, s2, begin());
  1463. append(s2, n - size());
  1464. }
  1465. return *this;
  1466. }
  1467. flex_string& assign(const value_type* s)
  1468. { return assign(s, traits_type::length(s)); }
  1469. template <class ItOrLength, class ItOrChar>
  1470. flex_string& assign(ItOrLength first_or_n, ItOrChar last_or_c)
  1471. { return replace(begin(), end(), first_or_n, last_or_c); }
  1472. flex_string& insert(size_type pos1, const flex_string& str)
  1473. { return insert(pos1, str.data(), str.size()); }
  1474. flex_string& insert(size_type pos1, const flex_string& str,
  1475. size_type pos2, size_type n)
  1476. {
  1477. Enforce(pos2 <= str.length(), (std::out_of_range*)0, "");
  1478. Procust(n, str.length() - pos2);
  1479. return insert(pos1, str.data() + pos2, n);
  1480. }
  1481. flex_string& insert(size_type pos, const value_type* s, size_type n)
  1482. {
  1483. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1484. insert(begin() + pos, s, s + n);
  1485. return *this;
  1486. }
  1487. flex_string& insert(size_type pos, const value_type* s)
  1488. { return insert(pos, s, traits_type::length(s)); }
  1489. flex_string& insert(size_type pos, size_type n, value_type c)
  1490. {
  1491. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1492. insert(begin() + pos, n, c);
  1493. return *this;
  1494. }
  1495. iterator insert(iterator p, value_type c = value_type())
  1496. {
  1497. const size_type pos = p - begin();
  1498. insert(pos, &c, 1);
  1499. return begin() + pos;
  1500. }
  1501. private:
  1502. // Care must be taken when dereferencing some iterator types.
  1503. //
  1504. // Users can implement this function in their namespace if their storage
  1505. // uses a special iterator type, the function will be found through ADL.
  1506. template<class Iterator>
  1507. const typename std::iterator_traits<Iterator>::value_type*
  1508. DereferenceValidIterator(Iterator it) const
  1509. {
  1510. return &*it;
  1511. }
  1512. // Care must be taken when dereferencing a reverse iterators, hence this
  1513. // special case. This isn't in the std namespace so as not to pollute it or
  1514. // create name clashes.
  1515. template<typename Iterator>
  1516. const typename std::iterator_traits<Iterator>::value_type*
  1517. DereferenceValidIterator(std::reverse_iterator<Iterator> it) const
  1518. {
  1519. return &*--it;
  1520. }
  1521. // Determine if the range aliases the current string.
  1522. //
  1523. // This method cannot be const because calling begin/end on copy-on-write
  1524. // implementations must have side effects.
  1525. // A const version wouldn't make the string unique through this call.
  1526. template<class Iterator>
  1527. bool IsAliasedRange(Iterator beginIterator, Iterator endIterator)
  1528. {
  1529. if(!empty() && beginIterator != endIterator)
  1530. {
  1531. typedef const typename std::iterator_traits<Iterator>::value_type *
  1532. value_pointer;
  1533. value_pointer myBegin(&*begin());
  1534. value_pointer myEnd(&*begin() + size());
  1535. value_pointer rangeBegin(DereferenceValidIterator(beginIterator));
  1536. const std::less_equal<value_pointer> less_equal = std::less_equal<value_pointer>();
  1537. if(less_equal(myBegin, rangeBegin) && less_equal(rangeBegin, myEnd))
  1538. return true;
  1539. }
  1540. return false;
  1541. }
  1542. template <int i> class Selector {};
  1543. flex_string& InsertImplDiscr(iterator p,
  1544. size_type n, value_type c, Selector<1>)
  1545. {
  1546. #ifndef NDEBUG
  1547. Invariant checker(*this);
  1548. #endif
  1549. BOOST_ASSERT(begin() <= p && p <= end());
  1550. const size_type insertOffset(p - begin());
  1551. const size_type originalSize(size());
  1552. if(n < originalSize - insertOffset)
  1553. {
  1554. // The new characters fit within the original string.
  1555. // The characters that are pushed back need to be moved because
  1556. // they're aliased.
  1557. // The appended characters will all be overwritten by the move.
  1558. append(n, value_type(0));
  1559. value_type* begin(&*begin());
  1560. flex_string_details::pod_move(begin + insertOffset,
  1561. begin + originalSize, begin + insertOffset + n);
  1562. std::fill(begin + insertOffset, begin + insertOffset + n, c);
  1563. }
  1564. else
  1565. {
  1566. // The new characters exceed the original string.
  1567. // The characters that are pushed back can simply be copied since
  1568. // they aren't aliased.
  1569. // The appended characters will partly be overwritten by the copy.
  1570. append(n, c);
  1571. value_type* begin(&*begin());
  1572. flex_string_details::pod_copy(begin + insertOffset,
  1573. begin + originalSize, begin + insertOffset + n);
  1574. std::fill(begin + insertOffset, begin + originalSize, c);
  1575. }
  1576. return *this;
  1577. }
  1578. template<class InputIterator>
  1579. flex_string& InsertImplDiscr(iterator i,
  1580. InputIterator b, InputIterator e, Selector<0>)
  1581. {
  1582. InsertImpl(i, b, e,
  1583. typename std::iterator_traits<InputIterator>::iterator_category());
  1584. return *this;
  1585. }
  1586. template <class FwdIterator>
  1587. void InsertImpl(iterator i,
  1588. FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
  1589. {
  1590. if(s1 == s2)
  1591. {
  1592. // Insert an empty range.
  1593. return;
  1594. }
  1595. if(IsAliasedRange(s1, s2))
  1596. {
  1597. // The source range is contained in the current string, copy it
  1598. // and recurse.
  1599. const flex_string temporary(s1, s2);
  1600. InsertImpl(i, temporary.begin(), temporary.end(),
  1601. typename std::iterator_traits<FwdIterator>::iterator_category());
  1602. return;
  1603. }
  1604. #ifndef NDEBUG
  1605. Invariant checker(*this);
  1606. #endif
  1607. const size_type pos = i - begin();
  1608. const typename std::iterator_traits<FwdIterator>::difference_type n2 =
  1609. std::distance(s1, s2);
  1610. BOOST_ASSERT(n2 >= 0);
  1611. using namespace flex_string_details;
  1612. BOOST_ASSERT(pos <= size());
  1613. const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
  1614. capacity() - size();
  1615. if (maxn2 < n2)
  1616. {
  1617. // Reallocate the string.
  1618. BOOST_ASSERT(!IsAliasedRange(s1, s2));
  1619. reserve(size() + n2);
  1620. i = begin() + pos;
  1621. }
  1622. if (pos + n2 <= size())
  1623. {
  1624. const iterator tailBegin = end() - n2;
  1625. Storage::append(tailBegin, tailBegin + n2);
  1626. std::copy(reverse_iterator(tailBegin), reverse_iterator(i),
  1627. reverse_iterator(tailBegin + n2));
  1628. std::copy(s1, s2, i);
  1629. }
  1630. else
  1631. {
  1632. FwdIterator t = s1;
  1633. const size_type old_size = size();
  1634. std::advance(t, old_size - pos);
  1635. BOOST_ASSERT(std::distance(t, s2) >= 0);
  1636. Storage::append(t, s2);
  1637. Storage::append(data() + pos, data() + old_size);
  1638. std::copy(s1, t, i);
  1639. }
  1640. }
  1641. template <class InputIterator>
  1642. void InsertImpl(iterator insertPosition,
  1643. InputIterator inputBegin, InputIterator inputEnd,
  1644. std::input_iterator_tag)
  1645. {
  1646. flex_string temporary(begin(), insertPosition);
  1647. for (; inputBegin != inputEnd; ++inputBegin)
  1648. {
  1649. temporary.push_back(*inputBegin);
  1650. }
  1651. temporary.append(insertPosition, end());
  1652. swap(temporary);
  1653. }
  1654. public:
  1655. template <class ItOrLength, class ItOrChar>
  1656. void insert(iterator p, ItOrLength first_or_n, ItOrChar last_or_c)
  1657. {
  1658. Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
  1659. InsertImplDiscr(p, first_or_n, last_or_c, sel);
  1660. }
  1661. flex_string& erase(size_type pos = 0, size_type n = npos)
  1662. {
  1663. #ifndef NDEBUG
  1664. Invariant checker(*this);
  1665. #endif
  1666. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1667. Procust(n, length() - pos);
  1668. std::copy(begin() + pos + n, end(), begin() + pos);
  1669. resize(length() - n);
  1670. return *this;
  1671. }
  1672. iterator erase(iterator position)
  1673. {
  1674. const size_type pos(position - begin());
  1675. erase(pos, 1);
  1676. return begin() + pos;
  1677. }
  1678. iterator erase(iterator first, iterator last)
  1679. {
  1680. const size_type pos(first - begin());
  1681. erase(pos, last - first);
  1682. return begin() + pos;
  1683. }
  1684. // Replaces at most n1 chars of *this, starting with pos1 with the content of str
  1685. flex_string& replace(size_type pos1, size_type n1, const flex_string& str)
  1686. { return replace(pos1, n1, str, 0, npos); }
  1687. // Replaces at most n1 chars of *this, starting with pos1,
  1688. // with at most n2 chars of str starting with pos2
  1689. flex_string& replace(size_type pos1, size_type n1, const flex_string& str,
  1690. size_type pos2, size_type n2)
  1691. {
  1692. Enforce(pos2 <= str.length(), (std::out_of_range*)0, "");
  1693. return replace(pos1, n1, str.data() + pos2,
  1694. Min(n2, str.size() - pos2));
  1695. }
  1696. // Replaces at most n1 chars of *this, starting with pos, with chars from s
  1697. flex_string& replace(size_type pos, size_type n1, const value_type* s)
  1698. { return replace(pos, n1, s, traits_type::length(s)); }
  1699. // Replaces at most n1 chars of *this, starting with pos, with n2 occurrences of c
  1700. // consolidated with
  1701. // Replaces at most n1 chars of *this, starting with pos,
  1702. // with at most n2 chars of str.
  1703. // str must have at least n2 chars.
  1704. template <class StrOrLength, class NumOrChar>
  1705. flex_string& replace(size_type pos, size_type n1,
  1706. StrOrLength s_or_n2, NumOrChar n_or_c)
  1707. {
  1708. #ifndef NDEBUG
  1709. Invariant checker(*this);
  1710. #endif
  1711. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1712. Procust(n1, length() - pos);
  1713. const iterator b = begin() + pos;
  1714. return replace(b, b + n1, s_or_n2, n_or_c);
  1715. }
  1716. flex_string& replace(iterator i1, iterator i2, const flex_string& str)
  1717. { return replace(i1, i2, str.c_str(), str.length()); }
  1718. flex_string& replace(iterator i1, iterator i2, const value_type* s)
  1719. { return replace(i1, i2, s, traits_type::length(s)); }
  1720. private:
  1721. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1722. const value_type* s, size_type n, Selector<2>)
  1723. {
  1724. BOOST_ASSERT(i1 <= i2);
  1725. BOOST_ASSERT(begin() <= i1 && i1 <= end());
  1726. BOOST_ASSERT(begin() <= i2 && i2 <= end());
  1727. return replace(i1, i2, s, s + n);
  1728. }
  1729. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1730. size_type n2, value_type c, Selector<1>)
  1731. {
  1732. const size_type n1 = i2 - i1;
  1733. if (n1 > n2)
  1734. {
  1735. std::fill(i1, i1 + n2, c);
  1736. erase(i1 + n2, i2);
  1737. }
  1738. else
  1739. {
  1740. std::fill(i1, i2, c);
  1741. insert(i2, n2 - n1, c);
  1742. }
  1743. return *this;
  1744. }
  1745. template <class InputIterator>
  1746. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1747. InputIterator b, InputIterator e, Selector<0>)
  1748. {
  1749. ReplaceImpl(i1, i2, b, e,
  1750. typename std::iterator_traits<InputIterator>::iterator_category());
  1751. return *this;
  1752. }
  1753. template <class FwdIterator>
  1754. void ReplaceImpl(iterator i1, iterator i2,
  1755. FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
  1756. {
  1757. #ifndef NDEBUG
  1758. Invariant checker(*this);
  1759. #endif
  1760. const typename std::iterator_traits<iterator>::difference_type n1 =
  1761. i2 - i1;
  1762. BOOST_ASSERT(n1 >= 0);
  1763. const typename std::iterator_traits<FwdIterator>::difference_type n2 =
  1764. std::distance(s1, s2);
  1765. BOOST_ASSERT(n2 >= 0);
  1766. if (IsAliasedRange(s1, s2))
  1767. {
  1768. // Aliased replace, copy to new string.
  1769. flex_string temporary;
  1770. temporary.reserve(size() - n1 + n2);
  1771. temporary.append(begin(), i1).append(s1, s2).append(i2, end());
  1772. swap(temporary);
  1773. return;
  1774. }
  1775. if (n1 > n2)
  1776. {
  1777. // Shrinks
  1778. std::copy(s1, s2, i1);
  1779. erase(i1 + n2, i2);
  1780. }
  1781. else
  1782. {
  1783. // Grows
  1784. flex_string_details::copy_n(s1, n1, i1);
  1785. std::advance(s1, n1);
  1786. insert(i2, s1, s2);
  1787. }
  1788. }
  1789. template <class InputIterator>
  1790. void ReplaceImpl(iterator i1, iterator i2,
  1791. InputIterator b, InputIterator e, std::input_iterator_tag)
  1792. {
  1793. flex_string temp(begin(), i1);
  1794. temp.append(b, e).append(i2, end());
  1795. swap(temp);
  1796. }
  1797. public:
  1798. template <class T1, class T2>
  1799. flex_string& replace(iterator i1, iterator i2,
  1800. T1 first_or_n_or_s, T2 last_or_c_or_n)
  1801. {
  1802. const bool
  1803. num1 = std::numeric_limits<T1>::is_specialized,
  1804. num2 = std::numeric_limits<T2>::is_specialized;
  1805. return ReplaceImplDiscr(i1, i2, first_or_n_or_s, last_or_c_or_n,
  1806. Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
  1807. }
  1808. size_type copy(value_type* s, size_type n, size_type pos = 0) const
  1809. {
  1810. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1811. n = Min(n, size() - pos);
  1812. flex_string_details::pod_copy(
  1813. &*begin() + pos,
  1814. &*begin() + pos + n,
  1815. s);
  1816. return n;
  1817. }
  1818. void swap(flex_string& rhs)
  1819. {
  1820. Storage& srhs = rhs;
  1821. this->Storage::swap(srhs);
  1822. }
  1823. // 21.3.6 string operations:
  1824. const value_type* c_str() const
  1825. { return Storage::c_str(); }
  1826. const value_type* data() const
  1827. { return Storage::data(); }
  1828. allocator_type get_allocator() const
  1829. { return Storage::get_allocator(); }
  1830. size_type find(const flex_string& str, size_type pos = 0) const
  1831. { return find(str.data(), pos, str.length()); }
  1832. size_type find (const value_type* s, size_type pos, size_type n) const
  1833. {
  1834. const size_type size_(size());
  1835. if (n + pos > size_)
  1836. return npos;
  1837. for (; pos < size_; ++pos)
  1838. {
  1839. if (traits_type::compare(&*begin() + pos, s, n) == 0)
  1840. {
  1841. return pos;
  1842. }
  1843. }
  1844. return npos;
  1845. }
  1846. size_type find (const value_type* s, size_type pos = 0) const
  1847. { return find(s, pos, traits_type::length(s)); }
  1848. size_type find (value_type c, size_type pos = 0) const
  1849. { return find(&c, pos, 1); }
  1850. size_type rfind(const flex_string& str, size_type pos = npos) const
  1851. { return rfind(str.c_str(), pos, str.length()); }
  1852. size_type rfind(const value_type* s, size_type pos, size_type n) const
  1853. {
  1854. if (n > length()) return npos;
  1855. pos = Min(pos, length() - n);
  1856. if (n == 0) return pos;
  1857. const_iterator i(begin() + pos);
  1858. for (; ; --i)
  1859. {
  1860. if (traits_type::eq(*i, *s)
  1861. && traits_type::compare(&*i, s, n) == 0)
  1862. {
  1863. return i - begin();
  1864. }
  1865. if (i == begin()) break;
  1866. }
  1867. return npos;
  1868. }
  1869. size_type rfind(const value_type* s, size_type pos = npos) const
  1870. { return rfind(s, pos, traits_type::length(s)); }
  1871. size_type rfind(value_type c, size_type pos = npos) const
  1872. { return rfind(&c, pos, 1); }
  1873. size_type find_first_of(const flex_string& str, size_type pos = 0) const
  1874. { return find_first_of(str.c_str(), pos, str.length()); }
  1875. size_type find_first_of(const value_type* s,
  1876. size_type pos, size_type n) const
  1877. {
  1878. if (pos > length() || n == 0) return npos;
  1879. const_iterator i(begin() + pos),
  1880. finish(end());
  1881. for (; i != finish; ++i)
  1882. {
  1883. if (traits_type::find(s, n, *i) != 0)
  1884. {
  1885. return i - begin();
  1886. }
  1887. }
  1888. return npos;
  1889. }
  1890. size_type find_first_of(const value_type* s, size_type pos = 0) const
  1891. { return find_first_of(s, pos, traits_type::length(s)); }
  1892. size_type find_first_of(value_type c, size_type pos = 0) const
  1893. { return find_first_of(&c, pos, 1); }
  1894. size_type find_last_of (const flex_string& str,
  1895. size_type pos = npos) const
  1896. { return find_last_of(str.c_str(), pos, str.length()); }
  1897. size_type find_last_of (const value_type* s, size_type pos,
  1898. size_type n) const
  1899. {
  1900. if (!empty() && n > 0)
  1901. {
  1902. pos = Min(pos, length() - 1);
  1903. const_iterator i(begin() + pos);
  1904. for (;; --i)
  1905. {
  1906. if (traits_type::find(s, n, *i) != 0)
  1907. {
  1908. return i - begin();
  1909. }
  1910. if (i == begin()) break;
  1911. }
  1912. }
  1913. return npos;
  1914. }
  1915. size_type find_last_of (const value_type* s,
  1916. size_type pos = npos) const
  1917. { return find_last_of(s, pos, traits_type::length(s)); }
  1918. size_type find_last_of (value_type c, size_type pos = npos) const
  1919. { return find_last_of(&c, pos, 1); }
  1920. size_type find_first_not_of(const flex_string& str,
  1921. size_type pos = 0) const
  1922. { return find_first_not_of(str.data(), pos, str.size()); }
  1923. size_type find_first_not_of(const value_type* s, size_type pos,
  1924. size_type n) const
  1925. {
  1926. if (pos < length())
  1927. {
  1928. const_iterator
  1929. i(begin() + pos),
  1930. finish(end());
  1931. for (; i != finish; ++i)
  1932. {
  1933. if (traits_type::find(s, n, *i) == 0)
  1934. {
  1935. return i - begin();
  1936. }
  1937. }
  1938. }
  1939. return npos;
  1940. }
  1941. size_type find_first_not_of(const value_type* s,
  1942. size_type pos = 0) const
  1943. { return find_first_not_of(s, pos, traits_type::length(s)); }
  1944. size_type find_first_not_of(value_type c, size_type pos = 0) const
  1945. { return find_first_not_of(&c, pos, 1); }
  1946. size_type find_last_not_of(const flex_string& str,
  1947. size_type pos = npos) const
  1948. { return find_last_not_of(str.c_str(), pos, str.length()); }
  1949. size_type find_last_not_of(const value_type* s, size_type pos,
  1950. size_type n) const
  1951. {
  1952. if (!empty())
  1953. {
  1954. pos = Min(pos, size() - 1);
  1955. const_iterator i(begin() + pos);
  1956. for (;; --i)
  1957. {
  1958. if (traits_type::find(s, n, *i) == 0)
  1959. {
  1960. return i - begin();
  1961. }
  1962. if (i == begin()) break;
  1963. }
  1964. }
  1965. return npos;
  1966. }
  1967. size_type find_last_not_of(const value_type* s,
  1968. size_type pos = npos) const
  1969. { return find_last_not_of(s, pos, traits_type::length(s)); }
  1970. size_type find_last_not_of (value_type c, size_type pos = npos) const
  1971. { return find_last_not_of(&c, pos, 1); }
  1972. flex_string substr(size_type pos = 0, size_type n = npos) const
  1973. {
  1974. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1975. return flex_string(data() + pos, Min(n, size() - pos));
  1976. }
  1977. std::ptrdiff_t compare(const flex_string& str) const
  1978. {
  1979. // FIX due to Goncalo N M de Carvalho July 18, 2005
  1980. return compare(0, size(), str);
  1981. }
  1982. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1983. const flex_string& str) const
  1984. { return compare(pos1, n1, str.data(), str.size()); }
  1985. // FIX to compare: added the TC
  1986. // (http://www.comeaucomputing.com/iso/lwg-defects.html number 5)
  1987. // Thanks to Caleb Epstein for the fix
  1988. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1989. const value_type* s) const
  1990. {
  1991. return compare(pos1, n1, s, traits_type::length(s));
  1992. }
  1993. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1994. const value_type* s, size_type n2) const
  1995. {
  1996. Enforce(pos1 <= size(), (std::out_of_range*)0, "");
  1997. Procust(n1, size() - pos1);
  1998. const int r = traits_type::compare(pos1 + data(), s, Min(n1, n2));
  1999. return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
  2000. }
  2001. std::ptrdiff_t compare(size_type pos1, size_type n1,
  2002. const flex_string& str,
  2003. size_type pos2, size_type n2) const
  2004. {
  2005. Enforce(pos2 <= str.size(), (std::out_of_range*)0, "");
  2006. return compare(pos1, n1, str.data() + pos2, Min(n2, str.size() - pos2));
  2007. }
  2008. std::ptrdiff_t compare(const value_type* s) const
  2009. {
  2010. // Could forward to compare(0, size(), s, traits_type::length(s))
  2011. // but that does two extra checks
  2012. const size_type n1(size()), n2(traits_type::length(s));
  2013. const int r = traits_type::compare(data(), s, Min(n1, n2));
  2014. return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
  2015. }
  2016. };
  2017. // non-member functions
  2018. template <typename E, class T, class A, class S>
  2019. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2020. const flex_string<E, T, A, S>& rhs)
  2021. {
  2022. flex_string<E, T, A, S> result;
  2023. result.reserve(lhs.size() + rhs.size());
  2024. result.append(lhs).append(rhs);
  2025. return result;
  2026. }
  2027. template <typename E, class T, class A, class S>
  2028. flex_string<E, T, A, S> operator+(const typename flex_string<E, T, A, S>::value_type* lhs,
  2029. const flex_string<E, T, A, S>& rhs)
  2030. {
  2031. flex_string<E, T, A, S> result;
  2032. const typename flex_string<E, T, A, S>::size_type len =
  2033. flex_string<E, T, A, S>::traits_type::length(lhs);
  2034. result.reserve(len + rhs.size());
  2035. result.append(lhs, len).append(rhs);
  2036. return result;
  2037. }
  2038. template <typename E, class T, class A, class S>
  2039. flex_string<E, T, A, S> operator+(
  2040. typename flex_string<E, T, A, S>::value_type lhs,
  2041. const flex_string<E, T, A, S>& rhs)
  2042. {
  2043. flex_string<E, T, A, S> result;
  2044. result.reserve(1 + rhs.size());
  2045. result.push_back(lhs);
  2046. result.append(rhs);
  2047. return result;
  2048. }
  2049. template <typename E, class T, class A, class S>
  2050. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2051. const typename flex_string<E, T, A, S>::value_type* rhs)
  2052. {
  2053. typedef typename flex_string<E, T, A, S>::size_type size_type;
  2054. typedef typename flex_string<E, T, A, S>::traits_type traits_type;
  2055. flex_string<E, T, A, S> result;
  2056. const size_type len = traits_type::length(rhs);
  2057. result.reserve(lhs.size() + len);
  2058. result.append(lhs).append(rhs, len);
  2059. return result;
  2060. }
  2061. template <typename E, class T, class A, class S>
  2062. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2063. typename flex_string<E, T, A, S>::value_type rhs)
  2064. {
  2065. flex_string<E, T, A, S> result;
  2066. result.reserve(lhs.size() + 1);
  2067. result.append(lhs);
  2068. result.push_back(rhs);
  2069. return result;
  2070. }
  2071. template <typename E, class T, class A, class S>
  2072. inline bool operator==(const flex_string<E, T, A, S>& lhs,
  2073. const flex_string<E, T, A, S>& rhs)
  2074. { return lhs.compare(rhs) == 0; }
  2075. template <typename E, class T, class A, class S>
  2076. inline bool operator==(const typename flex_string<E, T, A, S>::value_type* lhs,
  2077. const flex_string<E, T, A, S>& rhs)
  2078. { return rhs == lhs; }
  2079. template <typename E, class T, class A, class S>
  2080. inline bool operator==(const flex_string<E, T, A, S>& lhs,
  2081. const typename flex_string<E, T, A, S>::value_type* rhs)
  2082. { return lhs.compare(rhs) == 0; }
  2083. template <typename E, class T, class A, class S>
  2084. inline bool operator!=(const flex_string<E, T, A, S>& lhs,
  2085. const flex_string<E, T, A, S>& rhs)
  2086. { return !(lhs == rhs); }
  2087. template <typename E, class T, class A, class S>
  2088. inline bool operator!=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2089. const flex_string<E, T, A, S>& rhs)
  2090. { return !(lhs == rhs); }
  2091. template <typename E, class T, class A, class S>
  2092. inline bool operator!=(const flex_string<E, T, A, S>& lhs,
  2093. const typename flex_string<E, T, A, S>::value_type* rhs)
  2094. { return !(lhs == rhs); }
  2095. template <typename E, class T, class A, class S>
  2096. inline bool operator<(const flex_string<E, T, A, S>& lhs,
  2097. const flex_string<E, T, A, S>& rhs)
  2098. { return lhs.compare(rhs) < 0; }
  2099. template <typename E, class T, class A, class S>
  2100. inline bool operator<(const flex_string<E, T, A, S>& lhs,
  2101. const typename flex_string<E, T, A, S>::value_type* rhs)
  2102. { return lhs.compare(rhs) < 0; }
  2103. template <typename E, class T, class A, class S>
  2104. inline bool operator<(const typename flex_string<E, T, A, S>::value_type* lhs,
  2105. const flex_string<E, T, A, S>& rhs)
  2106. { return rhs.compare(lhs) > 0; }
  2107. template <typename E, class T, class A, class S>
  2108. inline bool operator>(const flex_string<E, T, A, S>& lhs,
  2109. const flex_string<E, T, A, S>& rhs)
  2110. { return rhs < lhs; }
  2111. template <typename E, class T, class A, class S>
  2112. inline bool operator>(const flex_string<E, T, A, S>& lhs,
  2113. const typename flex_string<E, T, A, S>::value_type* rhs)
  2114. { return rhs < lhs; }
  2115. template <typename E, class T, class A, class S>
  2116. bool operator>(const typename flex_string<E, T, A, S>::value_type* lhs,
  2117. const flex_string<E, T, A, S>& rhs)
  2118. { return rhs < lhs; }
  2119. template <typename E, class T, class A, class S>
  2120. inline bool operator<=(const flex_string<E, T, A, S>& lhs,
  2121. const flex_string<E, T, A, S>& rhs)
  2122. { return !(rhs < lhs); }
  2123. template <typename E, class T, class A, class S>
  2124. inline bool operator<=(const flex_string<E, T, A, S>& lhs,
  2125. const typename flex_string<E, T, A, S>::value_type* rhs)
  2126. { return !(rhs < lhs); }
  2127. template <typename E, class T, class A, class S>
  2128. bool operator<=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2129. const flex_string<E, T, A, S>& rhs)
  2130. { return !(rhs < lhs); }
  2131. template <typename E, class T, class A, class S>
  2132. bool operator>=(const flex_string<E, T, A, S>& lhs,
  2133. const flex_string<E, T, A, S>& rhs)
  2134. { return !(lhs < rhs); }
  2135. template <typename E, class T, class A, class S>
  2136. bool operator>=(const flex_string<E, T, A, S>& lhs,
  2137. const typename flex_string<E, T, A, S>::value_type* rhs)
  2138. { return !(lhs < rhs); }
  2139. template <typename E, class T, class A, class S>
  2140. inline bool operator>=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2141. const flex_string<E, T, A, S>& rhs)
  2142. { return !(lhs < rhs); }
  2143. template <typename E, class T, class A, class S>
  2144. void swap(flex_string<E, T, A, S>& lhs, flex_string<E, T, A, S>& rhs)
  2145. {
  2146. // subclause 21.3.7.8:
  2147. lhs.swap(rhs);
  2148. }
  2149. template <typename E, class T, class A, class S>
  2150. inline std::basic_istream<typename flex_string<E, T, A, S>::value_type,
  2151. typename flex_string<E, T, A, S>::traits_type>&
  2152. operator>>(
  2153. std::basic_istream<typename flex_string<E, T, A, S>::value_type,
  2154. typename flex_string<E, T, A, S>::traits_type>& is,
  2155. flex_string<E, T, A, S>& str);
  2156. template <typename E, class T, class A, class S>
  2157. std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
  2158. typename flex_string<E, T, A, S>::traits_type>&
  2159. operator<<(
  2160. std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
  2161. typename flex_string<E, T, A, S>::traits_type>& os,
  2162. const flex_string<E, T, A, S>& str)
  2163. { return os << str.c_str(); }
  2164. template <typename E1, class T, class A, class S>
  2165. const typename flex_string<E1, T, A, S>::size_type
  2166. flex_string<E1, T, A, S>::npos = (typename flex_string<E1, T, A, S>::size_type)(-1);
  2167. ///////////////////////////////////////////////////////////////////////////////
  2168. } // namespace util
  2169. } // namespace wave
  2170. } // namespace boost
  2171. #if BOOST_WAVE_SERIALIZATION != 0
  2172. ///////////////////////////////////////////////////////////////////////////////
  2173. namespace boost { namespace serialization {
  2174. #if !defined(BOOST_WAVE_FLEX_STRING_SERIALIZATION_HACK)
  2175. // FIXME: This doesn't work because of the missing flex_string::operator>>()
  2176. template <typename E, class T, class A, class S>
  2177. struct implementation_level<boost::wave::util::flex_string<E, T, A, S> >
  2178. {
  2179. typedef mpl::integral_c_tag tag;
  2180. typedef mpl::int_<boost::serialization::primitive_type> type;
  2181. BOOST_STATIC_CONSTANT(
  2182. int,
  2183. value = implementation_level::type::value
  2184. );
  2185. };
  2186. #else
  2187. // We serialize flex_strings as vectors of char's for now
  2188. template<class Archive, typename E, class T, class A, class S>
  2189. inline void save(Archive & ar,
  2190. boost::wave::util::flex_string<E, T, A, S> const &t,
  2191. const unsigned int file_version)
  2192. {
  2193. boost::serialization::stl::save_collection<
  2194. Archive, wave::util::flex_string<E, T, A, S> >(ar, t);
  2195. }
  2196. template<class Archive, typename E, class T, class A, class S>
  2197. inline void load(Archive & ar, boost::wave::util::flex_string<E, T, A, S> &t,
  2198. const unsigned int file_version)
  2199. {
  2200. boost::serialization::stl::load_collection<
  2201. Archive, boost::wave::util::flex_string<E, T, A, S>,
  2202. boost::serialization::stl::archive_input_seq<
  2203. Archive, boost::wave::util::flex_string<E, T, A, S> >,
  2204. boost::serialization::stl::reserve_imp<
  2205. boost::wave::util::flex_string<E, T, A, S> >
  2206. >(ar, t);
  2207. }
  2208. // split non-intrusive serialization function member into separate
  2209. // non intrusive save/load member functions
  2210. template<class Archive, typename E, class T, class A, class S>
  2211. inline void serialize(Archive & ar, boost::wave::util::flex_string<E, T, A, S> &t,
  2212. const unsigned int file_version)
  2213. {
  2214. boost::serialization::split_free(ar, t, file_version);
  2215. }
  2216. #endif
  2217. ///////////////////////////////////////////////////////////////////////////////
  2218. }} // boost::serialization
  2219. #endif
  2220. // the suffix header occurs after all of the code
  2221. #ifdef BOOST_HAS_ABI_HEADERS
  2222. #include BOOST_ABI_SUFFIX
  2223. #endif
  2224. #endif // FLEX_STRING_INC_