flex_string.hpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  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. template <class Allocator>
  244. typename Allocator::pointer Reallocate(
  245. Allocator& alloc,
  246. typename Allocator::pointer p,
  247. typename Allocator::size_type oldObjCount,
  248. typename Allocator::size_type newObjCount,
  249. void*)
  250. {
  251. // @@@ not implemented
  252. return NULL;
  253. }
  254. template <class Allocator>
  255. typename Allocator::pointer Reallocate(
  256. Allocator& alloc,
  257. typename Allocator::pointer p,
  258. typename Allocator::size_type oldObjCount,
  259. typename Allocator::size_type newObjCount,
  260. mallocator<void>*)
  261. {
  262. // @@@ not implemented
  263. return NULL;
  264. }
  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 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 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 A::allocate(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. A::deallocate(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 A::max_size(); }
  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 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_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_type::size_type size_type;
  1028. typedef typename Storage::reference 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 A::size_type size_type;
  1255. typedef typename A::difference_type difference_type;
  1256. typedef typename A::reference reference;
  1257. typedef typename A::const_reference const_reference;
  1258. typedef typename A::pointer pointer;
  1259. typedef typename A::const_pointer const_pointer;
  1260. typedef typename Storage::iterator iterator;
  1261. typedef typename Storage::const_iterator const_iterator;
  1262. typedef boost::reverse_iterator<iterator> reverse_iterator;
  1263. typedef boost::reverse_iterator<const_iterator> const_reverse_iterator;
  1264. static const size_type npos; // = size_type(-1)
  1265. private:
  1266. static size_type Min(size_type lhs, size_type rhs)
  1267. { return lhs < rhs ? lhs : rhs; }
  1268. static void Procust(size_type& n, size_type nmax)
  1269. { if (n > nmax) n = nmax; }
  1270. public:
  1271. // 21.3.1 construct/copy/destroy
  1272. explicit flex_string(const A& a = A())
  1273. : Storage(a)
  1274. {}
  1275. flex_string(const flex_string& str)
  1276. : Storage(str)
  1277. {
  1278. }
  1279. flex_string(const flex_string& str, size_type pos,
  1280. size_type n = npos, const A& a = A())
  1281. : Storage(a)
  1282. {
  1283. Enforce(pos <= str.size(), (std::out_of_range*)0, "");
  1284. assign(str, pos, n);
  1285. }
  1286. flex_string(const value_type* s, const A& a = A())
  1287. : Storage(s, traits_type::length(s), a)
  1288. {}
  1289. flex_string(const value_type* s, size_type n, const A& a = A())
  1290. : Storage(s, n, a)
  1291. {}
  1292. flex_string(size_type n, value_type c, const A& a = A())
  1293. : Storage(n, c, a)
  1294. {}
  1295. template <class InputIterator>
  1296. flex_string(InputIterator begin, InputIterator end, const A& a = A())
  1297. : Storage(a)
  1298. {
  1299. assign(begin, end);
  1300. }
  1301. ~flex_string()
  1302. {}
  1303. flex_string& operator=(const flex_string& str)
  1304. {
  1305. if (this != &str) {
  1306. Storage& s = *this;
  1307. s = str;
  1308. }
  1309. return *this;
  1310. }
  1311. flex_string& operator=(const value_type* s)
  1312. {
  1313. assign(s);
  1314. return *this;
  1315. }
  1316. flex_string& operator=(value_type c)
  1317. {
  1318. assign(1, c);
  1319. return *this;
  1320. }
  1321. // 21.3.2 iterators:
  1322. iterator begin()
  1323. { return Storage::begin(); }
  1324. const_iterator begin() const
  1325. { return Storage::begin(); }
  1326. iterator end()
  1327. { return Storage::end(); }
  1328. const_iterator end() const
  1329. { return Storage::end(); }
  1330. reverse_iterator rbegin()
  1331. { return reverse_iterator(end()); }
  1332. const_reverse_iterator rbegin() const
  1333. { return const_reverse_iterator(end()); }
  1334. reverse_iterator rend()
  1335. { return reverse_iterator(begin()); }
  1336. const_reverse_iterator rend() const
  1337. { return const_reverse_iterator(begin()); }
  1338. #if BOOST_WAVE_FLEX_STRING_SERIALIZATION_HACK != 0
  1339. // temporary hack to make it easier to serialize flex_string's using
  1340. // the Boost.Serialization library
  1341. value_type & back() { return *(begin()+size()-1); }
  1342. value_type const& back() const { return *(begin()+size()-1); }
  1343. #endif
  1344. // 21.3.3 capacity:
  1345. size_type size() const
  1346. { return Storage::size(); }
  1347. size_type length() const
  1348. { return size(); }
  1349. size_type max_size() const
  1350. { return Storage::max_size(); }
  1351. void resize(size_type n, value_type c)
  1352. { Storage::resize(n, c); }
  1353. void resize(size_type n)
  1354. { resize(n, value_type()); }
  1355. size_type capacity() const
  1356. { return Storage::capacity(); }
  1357. void reserve(size_type res_arg = 0)
  1358. {
  1359. Enforce(res_arg <= max_size(), (std::length_error*)0, "");
  1360. Storage::reserve(res_arg);
  1361. }
  1362. void clear()
  1363. { resize(0); }
  1364. bool empty() const
  1365. { return size() == 0; }
  1366. // 21.3.4 element access:
  1367. const_reference operator[](size_type pos) const
  1368. { return *(begin() + pos); }
  1369. reference operator[](size_type pos)
  1370. { return *(begin() + pos); }
  1371. const_reference at(size_type n) const
  1372. {
  1373. Enforce(n < size(), (std::out_of_range*)0, "");
  1374. return (*this)[n];
  1375. }
  1376. reference at(size_type n)
  1377. {
  1378. Enforce(n < size(), (std::out_of_range*)0, "");
  1379. return (*this)[n];
  1380. }
  1381. // 21.3.5 modifiers:
  1382. flex_string& operator+=(const flex_string& str)
  1383. { return append(str); }
  1384. flex_string& operator+=(const value_type* s)
  1385. { return append(s); }
  1386. flex_string& operator+=(value_type c)
  1387. {
  1388. push_back(c);
  1389. return *this;
  1390. }
  1391. flex_string& append(const flex_string& str)
  1392. { return append(str, 0, npos); }
  1393. flex_string& append(const flex_string& str, const size_type pos,
  1394. size_type n)
  1395. {
  1396. const size_type sz = str.size();
  1397. Enforce(pos <= sz, (std::out_of_range*)0, "");
  1398. Procust(n, sz - pos);
  1399. return append(str.c_str() + pos, n);
  1400. }
  1401. flex_string& append(const value_type* s, const size_type n)
  1402. {
  1403. #ifndef NDEBUG
  1404. Invariant checker(*this);
  1405. #endif
  1406. if (IsAliasedRange(s, s + n))
  1407. {
  1408. const size_type offset = s - &*begin();
  1409. Storage::reserve(size() + n);
  1410. s = &*begin() + offset;
  1411. }
  1412. Storage::append(s, s+ n);
  1413. return *this;
  1414. }
  1415. flex_string& append(const value_type* s)
  1416. { return append(s, traits_type::length(s)); }
  1417. flex_string& append(size_type n, value_type c)
  1418. {
  1419. resize(size() + n, c);
  1420. return *this;
  1421. }
  1422. template<class InputIterator>
  1423. flex_string& append(InputIterator first, InputIterator last)
  1424. {
  1425. insert(end(), first, last);
  1426. return *this;
  1427. }
  1428. void push_back(value_type c)
  1429. {
  1430. const size_type cap = capacity();
  1431. if (size() == cap)
  1432. {
  1433. reserve(cap << 1u);
  1434. }
  1435. Storage::append(&c, &c + 1);
  1436. }
  1437. flex_string& assign(const flex_string& str)
  1438. {
  1439. if (&str == this) return *this;
  1440. return assign(str.data(), str.size());
  1441. }
  1442. flex_string& assign(const flex_string& str, size_type pos,
  1443. size_type n)
  1444. {
  1445. const size_type sz = str.size();
  1446. Enforce(pos <= str.size(), (std::out_of_range*)0, "");
  1447. Procust(n, sz - pos);
  1448. return assign(str.data() + pos, n);
  1449. }
  1450. flex_string& assign(const value_type* s, size_type n)
  1451. {
  1452. #ifndef NDEBUG
  1453. Invariant checker(*this);
  1454. #endif
  1455. if (size() >= n)
  1456. {
  1457. std::copy(s, s + n, begin());
  1458. resize(n);
  1459. }
  1460. else
  1461. {
  1462. const value_type *const s2 = s + size();
  1463. std::copy(s, s2, begin());
  1464. append(s2, n - size());
  1465. }
  1466. return *this;
  1467. }
  1468. flex_string& assign(const value_type* s)
  1469. { return assign(s, traits_type::length(s)); }
  1470. template <class ItOrLength, class ItOrChar>
  1471. flex_string& assign(ItOrLength first_or_n, ItOrChar last_or_c)
  1472. { return replace(begin(), end(), first_or_n, last_or_c); }
  1473. flex_string& insert(size_type pos1, const flex_string& str)
  1474. { return insert(pos1, str.data(), str.size()); }
  1475. flex_string& insert(size_type pos1, const flex_string& str,
  1476. size_type pos2, size_type n)
  1477. {
  1478. Enforce(pos2 <= str.length(), (std::out_of_range*)0, "");
  1479. Procust(n, str.length() - pos2);
  1480. return insert(pos1, str.data() + pos2, n);
  1481. }
  1482. flex_string& insert(size_type pos, const value_type* s, size_type n)
  1483. {
  1484. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1485. insert(begin() + pos, s, s + n);
  1486. return *this;
  1487. }
  1488. flex_string& insert(size_type pos, const value_type* s)
  1489. { return insert(pos, s, traits_type::length(s)); }
  1490. flex_string& insert(size_type pos, size_type n, value_type c)
  1491. {
  1492. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1493. insert(begin() + pos, n, c);
  1494. return *this;
  1495. }
  1496. iterator insert(iterator p, value_type c = value_type())
  1497. {
  1498. const size_type pos = p - begin();
  1499. insert(pos, &c, 1);
  1500. return begin() + pos;
  1501. }
  1502. private:
  1503. // Care must be taken when dereferencing some iterator types.
  1504. //
  1505. // Users can implement this function in their namespace if their storage
  1506. // uses a special iterator type, the function will be found through ADL.
  1507. template<class Iterator>
  1508. const typename std::iterator_traits<Iterator>::value_type*
  1509. DereferenceValidIterator(Iterator it) const
  1510. {
  1511. return &*it;
  1512. }
  1513. // Care must be taken when dereferencing a reverse iterators, hence this
  1514. // special case. This isn't in the std namespace so as not to pollute it or
  1515. // create name clashes.
  1516. template<typename Iterator>
  1517. const typename std::iterator_traits<Iterator>::value_type*
  1518. DereferenceValidIterator(std::reverse_iterator<Iterator> it) const
  1519. {
  1520. return &*--it;
  1521. }
  1522. // Determine if the range aliases the current string.
  1523. //
  1524. // This method cannot be const because calling begin/end on copy-on-write
  1525. // implementations must have side effects.
  1526. // A const version wouldn't make the string unique through this call.
  1527. template<class Iterator>
  1528. bool IsAliasedRange(Iterator beginIterator, Iterator endIterator)
  1529. {
  1530. if(!empty() && beginIterator != endIterator)
  1531. {
  1532. typedef const typename std::iterator_traits<Iterator>::value_type *
  1533. pointer;
  1534. pointer myBegin(&*begin());
  1535. pointer myEnd(&*begin() + size());
  1536. pointer rangeBegin(DereferenceValidIterator(beginIterator));
  1537. const std::less_equal<pointer> less_equal = std::less_equal<pointer>();
  1538. if(less_equal(myBegin, rangeBegin) && less_equal(rangeBegin, myEnd))
  1539. return true;
  1540. }
  1541. return false;
  1542. }
  1543. template <int i> class Selector {};
  1544. flex_string& InsertImplDiscr(iterator p,
  1545. size_type n, value_type c, Selector<1>)
  1546. {
  1547. #ifndef NDEBUG
  1548. Invariant checker(*this);
  1549. #endif
  1550. BOOST_ASSERT(begin() <= p && p <= end());
  1551. const size_type insertOffset(p - begin());
  1552. const size_type originalSize(size());
  1553. if(n < originalSize - insertOffset)
  1554. {
  1555. // The new characters fit within the original string.
  1556. // The characters that are pushed back need to be moved because
  1557. // they're aliased.
  1558. // The appended characters will all be overwritten by the move.
  1559. append(n, value_type(0));
  1560. value_type* begin(&*begin());
  1561. flex_string_details::pod_move(begin + insertOffset,
  1562. begin + originalSize, begin + insertOffset + n);
  1563. std::fill(begin + insertOffset, begin + insertOffset + n, c);
  1564. }
  1565. else
  1566. {
  1567. // The new characters exceed the original string.
  1568. // The characters that are pushed back can simply be copied since
  1569. // they aren't aliased.
  1570. // The appended characters will partly be overwritten by the copy.
  1571. append(n, c);
  1572. value_type* begin(&*begin());
  1573. flex_string_details::pod_copy(begin + insertOffset,
  1574. begin + originalSize, begin + insertOffset + n);
  1575. std::fill(begin + insertOffset, begin + originalSize, c);
  1576. }
  1577. return *this;
  1578. }
  1579. template<class InputIterator>
  1580. flex_string& InsertImplDiscr(iterator i,
  1581. InputIterator b, InputIterator e, Selector<0>)
  1582. {
  1583. InsertImpl(i, b, e,
  1584. typename std::iterator_traits<InputIterator>::iterator_category());
  1585. return *this;
  1586. }
  1587. template <class FwdIterator>
  1588. void InsertImpl(iterator i,
  1589. FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
  1590. {
  1591. if(s1 == s2)
  1592. {
  1593. // Insert an empty range.
  1594. return;
  1595. }
  1596. if(IsAliasedRange(s1, s2))
  1597. {
  1598. // The source range is contained in the current string, copy it
  1599. // and recurse.
  1600. const flex_string temporary(s1, s2);
  1601. InsertImpl(i, temporary.begin(), temporary.end(),
  1602. typename std::iterator_traits<FwdIterator>::iterator_category());
  1603. return;
  1604. }
  1605. #ifndef NDEBUG
  1606. Invariant checker(*this);
  1607. #endif
  1608. const size_type pos = i - begin();
  1609. const typename std::iterator_traits<FwdIterator>::difference_type n2 =
  1610. std::distance(s1, s2);
  1611. BOOST_ASSERT(n2 >= 0);
  1612. using namespace flex_string_details;
  1613. BOOST_ASSERT(pos <= size());
  1614. const typename std::iterator_traits<FwdIterator>::difference_type maxn2 =
  1615. capacity() - size();
  1616. if (maxn2 < n2)
  1617. {
  1618. // Reallocate the string.
  1619. BOOST_ASSERT(!IsAliasedRange(s1, s2));
  1620. reserve(size() + n2);
  1621. i = begin() + pos;
  1622. }
  1623. if (pos + n2 <= size())
  1624. {
  1625. const iterator tailBegin = end() - n2;
  1626. Storage::append(tailBegin, tailBegin + n2);
  1627. std::copy(reverse_iterator(tailBegin), reverse_iterator(i),
  1628. reverse_iterator(tailBegin + n2));
  1629. std::copy(s1, s2, i);
  1630. }
  1631. else
  1632. {
  1633. FwdIterator t = s1;
  1634. const size_type old_size = size();
  1635. std::advance(t, old_size - pos);
  1636. BOOST_ASSERT(std::distance(t, s2) >= 0);
  1637. Storage::append(t, s2);
  1638. Storage::append(data() + pos, data() + old_size);
  1639. std::copy(s1, t, i);
  1640. }
  1641. }
  1642. template <class InputIterator>
  1643. void InsertImpl(iterator insertPosition,
  1644. InputIterator inputBegin, InputIterator inputEnd,
  1645. std::input_iterator_tag)
  1646. {
  1647. flex_string temporary(begin(), insertPosition);
  1648. for (; inputBegin != inputEnd; ++inputBegin)
  1649. {
  1650. temporary.push_back(*inputBegin);
  1651. }
  1652. temporary.append(insertPosition, end());
  1653. swap(temporary);
  1654. }
  1655. public:
  1656. template <class ItOrLength, class ItOrChar>
  1657. void insert(iterator p, ItOrLength first_or_n, ItOrChar last_or_c)
  1658. {
  1659. Selector<std::numeric_limits<ItOrLength>::is_specialized> sel;
  1660. InsertImplDiscr(p, first_or_n, last_or_c, sel);
  1661. }
  1662. flex_string& erase(size_type pos = 0, size_type n = npos)
  1663. {
  1664. #ifndef NDEBUG
  1665. Invariant checker(*this);
  1666. #endif
  1667. Enforce(pos <= length(), (std::out_of_range*)0, "");
  1668. Procust(n, length() - pos);
  1669. std::copy(begin() + pos + n, end(), begin() + pos);
  1670. resize(length() - n);
  1671. return *this;
  1672. }
  1673. iterator erase(iterator position)
  1674. {
  1675. const size_type pos(position - begin());
  1676. erase(pos, 1);
  1677. return begin() + pos;
  1678. }
  1679. iterator erase(iterator first, iterator last)
  1680. {
  1681. const size_type pos(first - begin());
  1682. erase(pos, last - first);
  1683. return begin() + pos;
  1684. }
  1685. // Replaces at most n1 chars of *this, starting with pos1 with the content of str
  1686. flex_string& replace(size_type pos1, size_type n1, const flex_string& str)
  1687. { return replace(pos1, n1, str, 0, npos); }
  1688. // Replaces at most n1 chars of *this, starting with pos1,
  1689. // with at most n2 chars of str starting with pos2
  1690. flex_string& replace(size_type pos1, size_type n1, const flex_string& str,
  1691. size_type pos2, size_type n2)
  1692. {
  1693. Enforce(pos2 <= str.length(), (std::out_of_range*)0, "");
  1694. return replace(pos1, n1, str.data() + pos2,
  1695. Min(n2, str.size() - pos2));
  1696. }
  1697. // Replaces at most n1 chars of *this, starting with pos, with chars from s
  1698. flex_string& replace(size_type pos, size_type n1, const value_type* s)
  1699. { return replace(pos, n1, s, traits_type::length(s)); }
  1700. // Replaces at most n1 chars of *this, starting with pos, with n2 occurrences of c
  1701. // consolidated with
  1702. // Replaces at most n1 chars of *this, starting with pos,
  1703. // with at most n2 chars of str.
  1704. // str must have at least n2 chars.
  1705. template <class StrOrLength, class NumOrChar>
  1706. flex_string& replace(size_type pos, size_type n1,
  1707. StrOrLength s_or_n2, NumOrChar n_or_c)
  1708. {
  1709. #ifndef NDEBUG
  1710. Invariant checker(*this);
  1711. #endif
  1712. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1713. Procust(n1, length() - pos);
  1714. const iterator b = begin() + pos;
  1715. return replace(b, b + n1, s_or_n2, n_or_c);
  1716. }
  1717. flex_string& replace(iterator i1, iterator i2, const flex_string& str)
  1718. { return replace(i1, i2, str.c_str(), str.length()); }
  1719. flex_string& replace(iterator i1, iterator i2, const value_type* s)
  1720. { return replace(i1, i2, s, traits_type::length(s)); }
  1721. private:
  1722. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1723. const value_type* s, size_type n, Selector<2>)
  1724. {
  1725. BOOST_ASSERT(i1 <= i2);
  1726. BOOST_ASSERT(begin() <= i1 && i1 <= end());
  1727. BOOST_ASSERT(begin() <= i2 && i2 <= end());
  1728. return replace(i1, i2, s, s + n);
  1729. }
  1730. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1731. size_type n2, value_type c, Selector<1>)
  1732. {
  1733. const size_type n1 = i2 - i1;
  1734. if (n1 > n2)
  1735. {
  1736. std::fill(i1, i1 + n2, c);
  1737. erase(i1 + n2, i2);
  1738. }
  1739. else
  1740. {
  1741. std::fill(i1, i2, c);
  1742. insert(i2, n2 - n1, c);
  1743. }
  1744. return *this;
  1745. }
  1746. template <class InputIterator>
  1747. flex_string& ReplaceImplDiscr(iterator i1, iterator i2,
  1748. InputIterator b, InputIterator e, Selector<0>)
  1749. {
  1750. ReplaceImpl(i1, i2, b, e,
  1751. typename std::iterator_traits<InputIterator>::iterator_category());
  1752. return *this;
  1753. }
  1754. template <class FwdIterator>
  1755. void ReplaceImpl(iterator i1, iterator i2,
  1756. FwdIterator s1, FwdIterator s2, std::forward_iterator_tag)
  1757. {
  1758. #ifndef NDEBUG
  1759. Invariant checker(*this);
  1760. #endif
  1761. const typename std::iterator_traits<iterator>::difference_type n1 =
  1762. i2 - i1;
  1763. BOOST_ASSERT(n1 >= 0);
  1764. const typename std::iterator_traits<FwdIterator>::difference_type n2 =
  1765. std::distance(s1, s2);
  1766. BOOST_ASSERT(n2 >= 0);
  1767. if (IsAliasedRange(s1, s2))
  1768. {
  1769. // Aliased replace, copy to new string.
  1770. flex_string temporary;
  1771. temporary.reserve(size() - n1 + n2);
  1772. temporary.append(begin(), i1).append(s1, s2).append(i2, end());
  1773. swap(temporary);
  1774. return;
  1775. }
  1776. if (n1 > n2)
  1777. {
  1778. // Shrinks
  1779. std::copy(s1, s2, i1);
  1780. erase(i1 + n2, i2);
  1781. }
  1782. else
  1783. {
  1784. // Grows
  1785. flex_string_details::copy_n(s1, n1, i1);
  1786. std::advance(s1, n1);
  1787. insert(i2, s1, s2);
  1788. }
  1789. }
  1790. template <class InputIterator>
  1791. void ReplaceImpl(iterator i1, iterator i2,
  1792. InputIterator b, InputIterator e, std::input_iterator_tag)
  1793. {
  1794. flex_string temp(begin(), i1);
  1795. temp.append(b, e).append(i2, end());
  1796. swap(temp);
  1797. }
  1798. public:
  1799. template <class T1, class T2>
  1800. flex_string& replace(iterator i1, iterator i2,
  1801. T1 first_or_n_or_s, T2 last_or_c_or_n)
  1802. {
  1803. const bool
  1804. num1 = std::numeric_limits<T1>::is_specialized,
  1805. num2 = std::numeric_limits<T2>::is_specialized;
  1806. return ReplaceImplDiscr(i1, i2, first_or_n_or_s, last_or_c_or_n,
  1807. Selector<num1 ? (num2 ? 1 : -1) : (num2 ? 2 : 0)>());
  1808. }
  1809. size_type copy(value_type* s, size_type n, size_type pos = 0) const
  1810. {
  1811. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1812. n = Min(n, size() - pos);
  1813. flex_string_details::pod_copy(
  1814. &*begin() + pos,
  1815. &*begin() + pos + n,
  1816. s);
  1817. return n;
  1818. }
  1819. void swap(flex_string& rhs)
  1820. {
  1821. Storage& srhs = rhs;
  1822. this->Storage::swap(srhs);
  1823. }
  1824. // 21.3.6 string operations:
  1825. const value_type* c_str() const
  1826. { return Storage::c_str(); }
  1827. const value_type* data() const
  1828. { return Storage::data(); }
  1829. allocator_type get_allocator() const
  1830. { return Storage::get_allocator(); }
  1831. size_type find(const flex_string& str, size_type pos = 0) const
  1832. { return find(str.data(), pos, str.length()); }
  1833. size_type find (const value_type* s, size_type pos, size_type n) const
  1834. {
  1835. const size_type size_(size());
  1836. if (n + pos > size_)
  1837. return npos;
  1838. for (; pos < size_; ++pos)
  1839. {
  1840. if (traits_type::compare(&*begin() + pos, s, n) == 0)
  1841. {
  1842. return pos;
  1843. }
  1844. }
  1845. return npos;
  1846. }
  1847. size_type find (const value_type* s, size_type pos = 0) const
  1848. { return find(s, pos, traits_type::length(s)); }
  1849. size_type find (value_type c, size_type pos = 0) const
  1850. { return find(&c, pos, 1); }
  1851. size_type rfind(const flex_string& str, size_type pos = npos) const
  1852. { return rfind(str.c_str(), pos, str.length()); }
  1853. size_type rfind(const value_type* s, size_type pos, size_type n) const
  1854. {
  1855. if (n > length()) return npos;
  1856. pos = Min(pos, length() - n);
  1857. if (n == 0) return pos;
  1858. const_iterator i(begin() + pos);
  1859. for (; ; --i)
  1860. {
  1861. if (traits_type::eq(*i, *s)
  1862. && traits_type::compare(&*i, s, n) == 0)
  1863. {
  1864. return i - begin();
  1865. }
  1866. if (i == begin()) break;
  1867. }
  1868. return npos;
  1869. }
  1870. size_type rfind(const value_type* s, size_type pos = npos) const
  1871. { return rfind(s, pos, traits_type::length(s)); }
  1872. size_type rfind(value_type c, size_type pos = npos) const
  1873. { return rfind(&c, pos, 1); }
  1874. size_type find_first_of(const flex_string& str, size_type pos = 0) const
  1875. { return find_first_of(str.c_str(), pos, str.length()); }
  1876. size_type find_first_of(const value_type* s,
  1877. size_type pos, size_type n) const
  1878. {
  1879. if (pos > length() || n == 0) return npos;
  1880. const_iterator i(begin() + pos),
  1881. finish(end());
  1882. for (; i != finish; ++i)
  1883. {
  1884. if (traits_type::find(s, n, *i) != 0)
  1885. {
  1886. return i - begin();
  1887. }
  1888. }
  1889. return npos;
  1890. }
  1891. size_type find_first_of(const value_type* s, size_type pos = 0) const
  1892. { return find_first_of(s, pos, traits_type::length(s)); }
  1893. size_type find_first_of(value_type c, size_type pos = 0) const
  1894. { return find_first_of(&c, pos, 1); }
  1895. size_type find_last_of (const flex_string& str,
  1896. size_type pos = npos) const
  1897. { return find_last_of(str.c_str(), pos, str.length()); }
  1898. size_type find_last_of (const value_type* s, size_type pos,
  1899. size_type n) const
  1900. {
  1901. if (!empty() && n > 0)
  1902. {
  1903. pos = Min(pos, length() - 1);
  1904. const_iterator i(begin() + pos);
  1905. for (;; --i)
  1906. {
  1907. if (traits_type::find(s, n, *i) != 0)
  1908. {
  1909. return i - begin();
  1910. }
  1911. if (i == begin()) break;
  1912. }
  1913. }
  1914. return npos;
  1915. }
  1916. size_type find_last_of (const value_type* s,
  1917. size_type pos = npos) const
  1918. { return find_last_of(s, pos, traits_type::length(s)); }
  1919. size_type find_last_of (value_type c, size_type pos = npos) const
  1920. { return find_last_of(&c, pos, 1); }
  1921. size_type find_first_not_of(const flex_string& str,
  1922. size_type pos = 0) const
  1923. { return find_first_not_of(str.data(), pos, str.size()); }
  1924. size_type find_first_not_of(const value_type* s, size_type pos,
  1925. size_type n) const
  1926. {
  1927. if (pos < length())
  1928. {
  1929. const_iterator
  1930. i(begin() + pos),
  1931. finish(end());
  1932. for (; i != finish; ++i)
  1933. {
  1934. if (traits_type::find(s, n, *i) == 0)
  1935. {
  1936. return i - begin();
  1937. }
  1938. }
  1939. }
  1940. return npos;
  1941. }
  1942. size_type find_first_not_of(const value_type* s,
  1943. size_type pos = 0) const
  1944. { return find_first_not_of(s, pos, traits_type::length(s)); }
  1945. size_type find_first_not_of(value_type c, size_type pos = 0) const
  1946. { return find_first_not_of(&c, pos, 1); }
  1947. size_type find_last_not_of(const flex_string& str,
  1948. size_type pos = npos) const
  1949. { return find_last_not_of(str.c_str(), pos, str.length()); }
  1950. size_type find_last_not_of(const value_type* s, size_type pos,
  1951. size_type n) const
  1952. {
  1953. if (!empty())
  1954. {
  1955. pos = Min(pos, size() - 1);
  1956. const_iterator i(begin() + pos);
  1957. for (;; --i)
  1958. {
  1959. if (traits_type::find(s, n, *i) == 0)
  1960. {
  1961. return i - begin();
  1962. }
  1963. if (i == begin()) break;
  1964. }
  1965. }
  1966. return npos;
  1967. }
  1968. size_type find_last_not_of(const value_type* s,
  1969. size_type pos = npos) const
  1970. { return find_last_not_of(s, pos, traits_type::length(s)); }
  1971. size_type find_last_not_of (value_type c, size_type pos = npos) const
  1972. { return find_last_not_of(&c, pos, 1); }
  1973. flex_string substr(size_type pos = 0, size_type n = npos) const
  1974. {
  1975. Enforce(pos <= size(), (std::out_of_range*)0, "");
  1976. return flex_string(data() + pos, Min(n, size() - pos));
  1977. }
  1978. std::ptrdiff_t compare(const flex_string& str) const
  1979. {
  1980. // FIX due to Goncalo N M de Carvalho July 18, 2005
  1981. return compare(0, size(), str);
  1982. }
  1983. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1984. const flex_string& str) const
  1985. { return compare(pos1, n1, str.data(), str.size()); }
  1986. // FIX to compare: added the TC
  1987. // (http://www.comeaucomputing.com/iso/lwg-defects.html number 5)
  1988. // Thanks to Caleb Epstein for the fix
  1989. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1990. const value_type* s) const
  1991. {
  1992. return compare(pos1, n1, s, traits_type::length(s));
  1993. }
  1994. std::ptrdiff_t compare(size_type pos1, size_type n1,
  1995. const value_type* s, size_type n2) const
  1996. {
  1997. Enforce(pos1 <= size(), (std::out_of_range*)0, "");
  1998. Procust(n1, size() - pos1);
  1999. const int r = traits_type::compare(pos1 + data(), s, Min(n1, n2));
  2000. return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
  2001. }
  2002. std::ptrdiff_t compare(size_type pos1, size_type n1,
  2003. const flex_string& str,
  2004. size_type pos2, size_type n2) const
  2005. {
  2006. Enforce(pos2 <= str.size(), (std::out_of_range*)0, "");
  2007. return compare(pos1, n1, str.data() + pos2, Min(n2, str.size() - pos2));
  2008. }
  2009. std::ptrdiff_t compare(const value_type* s) const
  2010. {
  2011. // Could forward to compare(0, size(), s, traits_type::length(s))
  2012. // but that does two extra checks
  2013. const size_type n1(size()), n2(traits_type::length(s));
  2014. const int r = traits_type::compare(data(), s, Min(n1, n2));
  2015. return r != 0 ? r : n1 > n2 ? 1 : n1 < n2 ? -1 : 0;
  2016. }
  2017. };
  2018. // non-member functions
  2019. template <typename E, class T, class A, class S>
  2020. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2021. const flex_string<E, T, A, S>& rhs)
  2022. {
  2023. flex_string<E, T, A, S> result;
  2024. result.reserve(lhs.size() + rhs.size());
  2025. result.append(lhs).append(rhs);
  2026. return result;
  2027. }
  2028. template <typename E, class T, class A, class S>
  2029. flex_string<E, T, A, S> operator+(const typename flex_string<E, T, A, S>::value_type* lhs,
  2030. const flex_string<E, T, A, S>& rhs)
  2031. {
  2032. flex_string<E, T, A, S> result;
  2033. const typename flex_string<E, T, A, S>::size_type len =
  2034. flex_string<E, T, A, S>::traits_type::length(lhs);
  2035. result.reserve(len + rhs.size());
  2036. result.append(lhs, len).append(rhs);
  2037. return result;
  2038. }
  2039. template <typename E, class T, class A, class S>
  2040. flex_string<E, T, A, S> operator+(
  2041. typename flex_string<E, T, A, S>::value_type lhs,
  2042. const flex_string<E, T, A, S>& rhs)
  2043. {
  2044. flex_string<E, T, A, S> result;
  2045. result.reserve(1 + rhs.size());
  2046. result.push_back(lhs);
  2047. result.append(rhs);
  2048. return result;
  2049. }
  2050. template <typename E, class T, class A, class S>
  2051. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2052. const typename flex_string<E, T, A, S>::value_type* rhs)
  2053. {
  2054. typedef typename flex_string<E, T, A, S>::size_type size_type;
  2055. typedef typename flex_string<E, T, A, S>::traits_type traits_type;
  2056. flex_string<E, T, A, S> result;
  2057. const size_type len = traits_type::length(rhs);
  2058. result.reserve(lhs.size() + len);
  2059. result.append(lhs).append(rhs, len);
  2060. return result;
  2061. }
  2062. template <typename E, class T, class A, class S>
  2063. flex_string<E, T, A, S> operator+(const flex_string<E, T, A, S>& lhs,
  2064. typename flex_string<E, T, A, S>::value_type rhs)
  2065. {
  2066. flex_string<E, T, A, S> result;
  2067. result.reserve(lhs.size() + 1);
  2068. result.append(lhs);
  2069. result.push_back(rhs);
  2070. return result;
  2071. }
  2072. template <typename E, class T, class A, class S>
  2073. inline bool operator==(const flex_string<E, T, A, S>& lhs,
  2074. const flex_string<E, T, A, S>& rhs)
  2075. { return lhs.compare(rhs) == 0; }
  2076. template <typename E, class T, class A, class S>
  2077. inline bool operator==(const typename flex_string<E, T, A, S>::value_type* lhs,
  2078. const flex_string<E, T, A, S>& rhs)
  2079. { return rhs == lhs; }
  2080. template <typename E, class T, class A, class S>
  2081. inline bool operator==(const flex_string<E, T, A, S>& lhs,
  2082. const typename flex_string<E, T, A, S>::value_type* rhs)
  2083. { return lhs.compare(rhs) == 0; }
  2084. template <typename E, class T, class A, class S>
  2085. inline bool operator!=(const flex_string<E, T, A, S>& lhs,
  2086. const flex_string<E, T, A, S>& rhs)
  2087. { return !(lhs == rhs); }
  2088. template <typename E, class T, class A, class S>
  2089. inline bool operator!=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2090. const flex_string<E, T, A, S>& rhs)
  2091. { return !(lhs == rhs); }
  2092. template <typename E, class T, class A, class S>
  2093. inline bool operator!=(const flex_string<E, T, A, S>& lhs,
  2094. const typename flex_string<E, T, A, S>::value_type* rhs)
  2095. { return !(lhs == rhs); }
  2096. template <typename E, class T, class A, class S>
  2097. inline bool operator<(const flex_string<E, T, A, S>& lhs,
  2098. const flex_string<E, T, A, S>& rhs)
  2099. { return lhs.compare(rhs) < 0; }
  2100. template <typename E, class T, class A, class S>
  2101. inline bool operator<(const flex_string<E, T, A, S>& lhs,
  2102. const typename flex_string<E, T, A, S>::value_type* rhs)
  2103. { return lhs.compare(rhs) < 0; }
  2104. template <typename E, class T, class A, class S>
  2105. inline bool operator<(const typename flex_string<E, T, A, S>::value_type* lhs,
  2106. const flex_string<E, T, A, S>& rhs)
  2107. { return rhs.compare(lhs) > 0; }
  2108. template <typename E, class T, class A, class S>
  2109. inline bool operator>(const flex_string<E, T, A, S>& lhs,
  2110. const flex_string<E, T, A, S>& rhs)
  2111. { return rhs < lhs; }
  2112. template <typename E, class T, class A, class S>
  2113. inline bool operator>(const flex_string<E, T, A, S>& lhs,
  2114. const typename flex_string<E, T, A, S>::value_type* rhs)
  2115. { return rhs < lhs; }
  2116. template <typename E, class T, class A, class S>
  2117. bool operator>(const typename flex_string<E, T, A, S>::value_type* lhs,
  2118. const flex_string<E, T, A, S>& rhs)
  2119. { return rhs < lhs; }
  2120. template <typename E, class T, class A, class S>
  2121. inline bool operator<=(const flex_string<E, T, A, S>& lhs,
  2122. const flex_string<E, T, A, S>& rhs)
  2123. { return !(rhs < lhs); }
  2124. template <typename E, class T, class A, class S>
  2125. inline bool operator<=(const flex_string<E, T, A, S>& lhs,
  2126. const typename flex_string<E, T, A, S>::value_type* rhs)
  2127. { return !(rhs < lhs); }
  2128. template <typename E, class T, class A, class S>
  2129. bool operator<=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2130. const flex_string<E, T, A, S>& rhs)
  2131. { return !(rhs < lhs); }
  2132. template <typename E, class T, class A, class S>
  2133. bool operator>=(const flex_string<E, T, A, S>& lhs,
  2134. const flex_string<E, T, A, S>& rhs)
  2135. { return !(lhs < rhs); }
  2136. template <typename E, class T, class A, class S>
  2137. bool operator>=(const flex_string<E, T, A, S>& lhs,
  2138. const typename flex_string<E, T, A, S>::value_type* rhs)
  2139. { return !(lhs < rhs); }
  2140. template <typename E, class T, class A, class S>
  2141. inline bool operator>=(const typename flex_string<E, T, A, S>::value_type* lhs,
  2142. const flex_string<E, T, A, S>& rhs)
  2143. { return !(lhs < rhs); }
  2144. template <typename E, class T, class A, class S>
  2145. void swap(flex_string<E, T, A, S>& lhs, flex_string<E, T, A, S>& rhs)
  2146. {
  2147. // subclause 21.3.7.8:
  2148. lhs.swap(rhs);
  2149. }
  2150. template <typename E, class T, class A, class S>
  2151. inline std::basic_istream<typename flex_string<E, T, A, S>::value_type,
  2152. typename flex_string<E, T, A, S>::traits_type>&
  2153. operator>>(
  2154. std::basic_istream<typename flex_string<E, T, A, S>::value_type,
  2155. typename flex_string<E, T, A, S>::traits_type>& is,
  2156. flex_string<E, T, A, S>& str);
  2157. template <typename E, class T, class A, class S>
  2158. std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
  2159. typename flex_string<E, T, A, S>::traits_type>&
  2160. operator<<(
  2161. std::basic_ostream<typename flex_string<E, T, A, S>::value_type,
  2162. typename flex_string<E, T, A, S>::traits_type>& os,
  2163. const flex_string<E, T, A, S>& str)
  2164. { return os << str.c_str(); }
  2165. template <typename E1, class T, class A, class S>
  2166. const typename flex_string<E1, T, A, S>::size_type
  2167. flex_string<E1, T, A, S>::npos = (typename flex_string<E1, T, A, S>::size_type)(-1);
  2168. ///////////////////////////////////////////////////////////////////////////////
  2169. } // namespace util
  2170. } // namespace wave
  2171. } // namespace boost
  2172. #if BOOST_WAVE_SERIALIZATION != 0
  2173. ///////////////////////////////////////////////////////////////////////////////
  2174. namespace boost { namespace serialization {
  2175. #if !defined(BOOST_WAVE_FLEX_STRING_SERIALIZATION_HACK)
  2176. // FIXME: This doesn't work because of the missing flex_string::operator>>()
  2177. template <typename E, class T, class A, class S>
  2178. struct implementation_level<boost::wave::util::flex_string<E, T, A, S> >
  2179. {
  2180. typedef mpl::integral_c_tag tag;
  2181. typedef mpl::int_<boost::serialization::primitive_type> type;
  2182. BOOST_STATIC_CONSTANT(
  2183. int,
  2184. value = implementation_level::type::value
  2185. );
  2186. };
  2187. #else
  2188. // We serialize flex_strings as vectors of char's for now
  2189. template<class Archive, typename E, class T, class A, class S>
  2190. inline void save(Archive & ar,
  2191. boost::wave::util::flex_string<E, T, A, S> const &t,
  2192. const unsigned int file_version)
  2193. {
  2194. boost::serialization::stl::save_collection<
  2195. Archive, wave::util::flex_string<E, T, A, S> >(ar, t);
  2196. }
  2197. template<class Archive, typename E, class T, class A, class S>
  2198. inline void load(Archive & ar, boost::wave::util::flex_string<E, T, A, S> &t,
  2199. const unsigned int file_version)
  2200. {
  2201. boost::serialization::stl::load_collection<
  2202. Archive, boost::wave::util::flex_string<E, T, A, S>,
  2203. boost::serialization::stl::archive_input_seq<
  2204. Archive, boost::wave::util::flex_string<E, T, A, S> >,
  2205. boost::serialization::stl::reserve_imp<
  2206. boost::wave::util::flex_string<E, T, A, S> >
  2207. >(ar, t);
  2208. }
  2209. // split non-intrusive serialization function member into separate
  2210. // non intrusive save/load member functions
  2211. template<class Archive, typename E, class T, class A, class S>
  2212. inline void serialize(Archive & ar, boost::wave::util::flex_string<E, T, A, S> &t,
  2213. const unsigned int file_version)
  2214. {
  2215. boost::serialization::split_free(ar, t, file_version);
  2216. }
  2217. #endif
  2218. ///////////////////////////////////////////////////////////////////////////////
  2219. }} // boost::serialization
  2220. #endif
  2221. // the suffix header occurs after all of the code
  2222. #ifdef BOOST_HAS_ABI_HEADERS
  2223. #include BOOST_ABI_SUFFIX
  2224. #endif
  2225. #endif // FLEX_STRING_INC_