string.h 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/string.h
  3. // Purpose: wxString class
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 29/01/98
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. /*
  11. Efficient string class [more or less] compatible with MFC CString,
  12. wxWidgets version 1 wxString and std::string and some handy functions
  13. missing from string.h.
  14. */
  15. #ifndef _WX_WXSTRING_H__
  16. #define _WX_WXSTRING_H__
  17. // ----------------------------------------------------------------------------
  18. // headers
  19. // ----------------------------------------------------------------------------
  20. #include "wx/defs.h" // everybody should include this
  21. #if defined(__WXMAC__) || defined(__VISAGECPP__)
  22. #include <ctype.h>
  23. #endif
  24. #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
  25. // problem in VACPP V4 with including stdlib.h multiple times
  26. // strconv includes it anyway
  27. # include <stdio.h>
  28. # include <string.h>
  29. # include <stdarg.h>
  30. # include <limits.h>
  31. #else
  32. # include <string.h>
  33. # include <stdio.h>
  34. # include <stdarg.h>
  35. # include <limits.h>
  36. # include <stdlib.h>
  37. #endif
  38. #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
  39. #include "wx/strvararg.h"
  40. #include "wx/buffer.h" // for wxCharBuffer
  41. #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
  42. #include "wx/stringimpl.h"
  43. #include "wx/stringops.h"
  44. #include "wx/unichar.h"
  45. // by default we cache the mapping of the positions in UTF-8 string to the byte
  46. // offset as this results in noticeable performance improvements for loops over
  47. // strings using indices; comment out this line to disable this
  48. //
  49. // notice that this optimization is well worth using even in debug builds as it
  50. // changes asymptotic complexity of algorithms using indices to iterate over
  51. // wxString back to expected linear from quadratic
  52. //
  53. // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
  54. // use in DLL build under pre-Vista Windows so we disable this code for now, if
  55. // anybody really needs to use UTF-8 build under Windows with this optimization
  56. // it would have to be re-tested and probably corrected
  57. // CS: under OSX release builds the string destructor/cache cleanup sometimes
  58. // crashes, disable until we find the true reason or a better workaround
  59. #if wxUSE_UNICODE_UTF8 && !defined(__WINDOWS__) && !defined(__WXOSX__)
  60. #define wxUSE_STRING_POS_CACHE 1
  61. #else
  62. #define wxUSE_STRING_POS_CACHE 0
  63. #endif
  64. #if wxUSE_STRING_POS_CACHE
  65. #include "wx/tls.h"
  66. // change this 0 to 1 to enable additional (very expensive) asserts
  67. // verifying that string caching logic works as expected
  68. #if 0
  69. #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
  70. #else
  71. #define wxSTRING_CACHE_ASSERT(cond)
  72. #endif
  73. #endif // wxUSE_STRING_POS_CACHE
  74. class WXDLLIMPEXP_FWD_BASE wxString;
  75. // unless this symbol is predefined to disable the compatibility functions, do
  76. // use them
  77. #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  78. #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
  79. #endif
  80. namespace wxPrivate
  81. {
  82. template <typename T> struct wxStringAsBufHelper;
  83. }
  84. // ---------------------------------------------------------------------------
  85. // macros
  86. // ---------------------------------------------------------------------------
  87. // casts [unfortunately!] needed to call some broken functions which require
  88. // "char *" instead of "const char *"
  89. #define WXSTRINGCAST (wxChar *)(const wxChar *)
  90. #define wxCSTRINGCAST (wxChar *)(const wxChar *)
  91. #define wxMBSTRINGCAST (char *)(const char *)
  92. #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
  93. // ----------------------------------------------------------------------------
  94. // constants
  95. // ----------------------------------------------------------------------------
  96. #if WXWIN_COMPATIBILITY_2_6
  97. // deprecated in favour of wxString::npos, don't use in new code
  98. //
  99. // maximum possible length for a string means "take all string" everywhere
  100. #define wxSTRING_MAXLEN wxString::npos
  101. #endif // WXWIN_COMPATIBILITY_2_6
  102. // ---------------------------------------------------------------------------
  103. // global functions complementing standard C string library replacements for
  104. // strlen() and portable strcasecmp()
  105. //---------------------------------------------------------------------------
  106. #if WXWIN_COMPATIBILITY_2_8
  107. // Use wxXXX() functions from wxcrt.h instead! These functions are for
  108. // backwards compatibility only.
  109. // checks whether the passed in pointer is NULL and if the string is empty
  110. wxDEPRECATED_MSG("use wxIsEmpty() instead")
  111. inline bool IsEmpty(const char *p) { return (!p || !*p); }
  112. // safe version of strlen() (returns 0 if passed NULL pointer)
  113. wxDEPRECATED_MSG("use wxStrlen() instead")
  114. inline size_t Strlen(const char *psz)
  115. { return psz ? strlen(psz) : 0; }
  116. // portable strcasecmp/_stricmp
  117. wxDEPRECATED_MSG("use wxStricmp() instead")
  118. inline int Stricmp(const char *psz1, const char *psz2)
  119. { return wxCRT_StricmpA(psz1, psz2); }
  120. #endif // WXWIN_COMPATIBILITY_2_8
  121. // ----------------------------------------------------------------------------
  122. // wxCStrData
  123. // ----------------------------------------------------------------------------
  124. // Lightweight object returned by wxString::c_str() and implicitly convertible
  125. // to either const char* or const wchar_t*.
  126. class wxCStrData
  127. {
  128. private:
  129. // Ctors; for internal use by wxString and wxCStrData only
  130. wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
  131. : m_str(str), m_offset(offset), m_owned(owned) {}
  132. public:
  133. // Ctor constructs the object from char literal; they are needed to make
  134. // operator?: compile and they intentionally take char*, not const char*
  135. inline wxCStrData(char *buf);
  136. inline wxCStrData(wchar_t *buf);
  137. inline wxCStrData(const wxCStrData& data);
  138. inline ~wxCStrData();
  139. // AsWChar() and AsChar() can't be defined here as they use wxString and so
  140. // must come after it and because of this won't be inlined when called from
  141. // wxString methods (without a lot of work to extract these wxString methods
  142. // from inside the class itself). But we still define them being inline
  143. // below to let compiler inline them from elsewhere. And because of this we
  144. // must declare them as inline here because otherwise some compilers give
  145. // warnings about them, e.g. mingw32 3.4.5 warns about "<symbol> defined
  146. // locally after being referenced with dllimport linkage" while IRIX
  147. // mipsPro 7.4 warns about "function declared inline after being called".
  148. inline const wchar_t* AsWChar() const;
  149. operator const wchar_t*() const { return AsWChar(); }
  150. inline const char* AsChar() const;
  151. const unsigned char* AsUnsignedChar() const
  152. { return (const unsigned char *) AsChar(); }
  153. operator const char*() const { return AsChar(); }
  154. operator const unsigned char*() const { return AsUnsignedChar(); }
  155. operator const void*() const { return AsChar(); }
  156. // returns buffers that are valid as long as the associated wxString exists
  157. const wxScopedCharBuffer AsCharBuf() const
  158. {
  159. return wxScopedCharBuffer::CreateNonOwned(AsChar());
  160. }
  161. const wxScopedWCharBuffer AsWCharBuf() const
  162. {
  163. return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
  164. }
  165. inline wxString AsString() const;
  166. // returns the value as C string in internal representation (equivalent
  167. // to AsString().wx_str(), but more efficient)
  168. const wxStringCharType *AsInternal() const;
  169. // allow expressions like "c_str()[0]":
  170. inline wxUniChar operator[](size_t n) const;
  171. wxUniChar operator[](int n) const { return operator[](size_t(n)); }
  172. wxUniChar operator[](long n) const { return operator[](size_t(n)); }
  173. #ifndef wxSIZE_T_IS_UINT
  174. wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
  175. #endif // size_t != unsigned int
  176. // These operators are needed to emulate the pointer semantics of c_str():
  177. // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
  178. // (we need both versions to resolve ambiguities). Note that this means
  179. // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
  180. // is *not* number of Unicode characters in wxString.
  181. wxCStrData operator+(int n) const
  182. { return wxCStrData(m_str, m_offset + n, m_owned); }
  183. wxCStrData operator+(long n) const
  184. { return wxCStrData(m_str, m_offset + n, m_owned); }
  185. wxCStrData operator+(size_t n) const
  186. { return wxCStrData(m_str, m_offset + n, m_owned); }
  187. // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
  188. // expression but it must be ptrdiff_t and not e.g. int to work in this
  189. // example):
  190. wxCStrData operator-(ptrdiff_t n) const
  191. {
  192. wxASSERT_MSG( n <= (ptrdiff_t)m_offset,
  193. wxT("attempt to construct address before the beginning of the string") );
  194. return wxCStrData(m_str, m_offset - n, m_owned);
  195. }
  196. // this operator is needed to make expressions like "*c_str()" or
  197. // "*(c_str() + 2)" work
  198. inline wxUniChar operator*() const;
  199. private:
  200. // the wxString this object was returned for
  201. const wxString *m_str;
  202. // Offset into c_str() return value. Note that this is *not* offset in
  203. // m_str in Unicode characters. Instead, it is index into the
  204. // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
  205. // on how is the wxCStrData instance used: if it is eventually cast to
  206. // const char*, m_offset will be in bytes form string's start; if it is
  207. // cast to const wchar_t*, it will be in wchar_t values.
  208. size_t m_offset;
  209. // should m_str be deleted, i.e. is it owned by us?
  210. bool m_owned;
  211. friend class WXDLLIMPEXP_FWD_BASE wxString;
  212. };
  213. // ----------------------------------------------------------------------------
  214. // wxStringPrintfMixin
  215. // ---------------------------------------------------------------------------
  216. // NB: VC6 has a bug that causes linker errors if you have template methods
  217. // in a class using __declspec(dllimport). The solution is to split such
  218. // class into two classes, one that contains the template methods and does
  219. // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
  220. // (with DLL linkage).
  221. //
  222. // We only do this for VC6 here, because the code is less efficient
  223. // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
  224. // cannot compile this code.
  225. #if defined(__VISUALC__) && __VISUALC__ < 1300
  226. #define wxNEEDS_WXSTRING_PRINTF_MIXIN
  227. #endif
  228. #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
  229. // this class contains implementation of wxString's vararg methods, it's
  230. // exported from wxBase DLL
  231. class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
  232. {
  233. protected:
  234. wxStringPrintfMixinBase() {}
  235. #if !wxUSE_UTF8_LOCALE_ONLY
  236. int DoPrintfWchar(const wxChar *format, ...);
  237. static wxString DoFormatWchar(const wxChar *format, ...);
  238. #endif
  239. #if wxUSE_UNICODE_UTF8
  240. int DoPrintfUtf8(const char *format, ...);
  241. static wxString DoFormatUtf8(const char *format, ...);
  242. #endif
  243. };
  244. // this class contains template wrappers for wxString's vararg methods, it's
  245. // intentionally *not* exported from the DLL in order to fix the VC6 bug
  246. // described above
  247. class wxStringPrintfMixin : public wxStringPrintfMixinBase
  248. {
  249. private:
  250. // to further complicate things, we can't return wxString from
  251. // wxStringPrintfMixin::Format() because wxString is not yet declared at
  252. // this point; the solution is to use this fake type trait template - this
  253. // way the compiler won't know the return type until Format() is used
  254. // (this doesn't compile with Watcom, but VC6 compiles it just fine):
  255. template<typename T> struct StringReturnType
  256. {
  257. typedef wxString type;
  258. };
  259. public:
  260. // these are duplicated wxString methods, they're also declared below
  261. // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
  262. // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
  263. WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
  264. Format, 1, (const wxFormatString&),
  265. DoFormatWchar, DoFormatUtf8)
  266. // We have to implement the version without template arguments manually
  267. // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
  268. // normally does it itself. It has to be a template so that we can use
  269. // the hack, even though there's no real template parameter. We can't move
  270. // it to wxStrig, because it would shadow these versions of Format() then.
  271. template<typename T>
  272. inline static typename StringReturnType<T>::type
  273. Format(const T& fmt)
  274. {
  275. // NB: this doesn't compile if T is not (some form of) a string;
  276. // this makes Format's prototype equivalent to
  277. // Format(const wxFormatString& fmt)
  278. return DoFormatWchar(wxFormatString(fmt));
  279. }
  280. // int Printf(const wxString& format, ...);
  281. WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
  282. DoPrintfWchar, DoPrintfUtf8)
  283. // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
  284. WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
  285. DoPrintfWchar, DoPrintfUtf8)
  286. protected:
  287. wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
  288. };
  289. #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
  290. // ----------------------------------------------------------------------------
  291. // wxString: string class trying to be compatible with std::string, MFC
  292. // CString and wxWindows 1.x wxString all at once
  293. // ---------------------------------------------------------------------------
  294. #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
  295. // "non dll-interface class 'wxStringPrintfMixin' used as base interface
  296. // for dll-interface class 'wxString'" -- this is OK in our case
  297. #pragma warning (push)
  298. #pragma warning (disable:4275)
  299. #endif
  300. #if wxUSE_UNICODE_UTF8
  301. // see the comment near wxString::iterator for why we need this
  302. class WXDLLIMPEXP_BASE wxStringIteratorNode
  303. {
  304. public:
  305. wxStringIteratorNode()
  306. : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
  307. wxStringIteratorNode(const wxString *str,
  308. wxStringImpl::const_iterator *citer)
  309. { DoSet(str, citer, NULL); }
  310. wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter)
  311. { DoSet(str, NULL, iter); }
  312. ~wxStringIteratorNode()
  313. { clear(); }
  314. inline void set(const wxString *str, wxStringImpl::const_iterator *citer)
  315. { clear(); DoSet(str, citer, NULL); }
  316. inline void set(const wxString *str, wxStringImpl::iterator *iter)
  317. { clear(); DoSet(str, NULL, iter); }
  318. const wxString *m_str;
  319. wxStringImpl::const_iterator *m_citer;
  320. wxStringImpl::iterator *m_iter;
  321. wxStringIteratorNode *m_prev, *m_next;
  322. private:
  323. inline void clear();
  324. inline void DoSet(const wxString *str,
  325. wxStringImpl::const_iterator *citer,
  326. wxStringImpl::iterator *iter);
  327. // the node belongs to a particular iterator instance, it's not copied
  328. // when a copy of the iterator is made
  329. wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode);
  330. };
  331. #endif // wxUSE_UNICODE_UTF8
  332. class WXDLLIMPEXP_BASE wxString
  333. #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
  334. : public wxStringPrintfMixin
  335. #endif
  336. {
  337. // NB: special care was taken in arranging the member functions in such order
  338. // that all inline functions can be effectively inlined, verify that all
  339. // performance critical functions are still inlined if you change order!
  340. public:
  341. // an 'invalid' value for string index, moved to this place due to a CW bug
  342. static const size_t npos;
  343. private:
  344. // if we hadn't made these operators private, it would be possible to
  345. // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
  346. // converted to char in C and we do have operator=(char)
  347. //
  348. // NB: we don't need other versions (short/long and unsigned) as attempt
  349. // to assign another numeric type to wxString will now result in
  350. // ambiguity between operator=(char) and operator=(int)
  351. wxString& operator=(int);
  352. // these methods are not implemented - there is _no_ conversion from int to
  353. // string, you're doing something wrong if the compiler wants to call it!
  354. //
  355. // try `s << i' or `s.Printf("%d", i)' instead
  356. wxString(int);
  357. // buffer for holding temporary substring when using any of the methods
  358. // that take (char*,size_t) or (wchar_t*,size_t) arguments:
  359. template<typename T>
  360. struct SubstrBufFromType
  361. {
  362. T data;
  363. size_t len;
  364. SubstrBufFromType(const T& data_, size_t len_)
  365. : data(data_), len(len_)
  366. {
  367. wxASSERT_MSG( len != npos, "must have real length" );
  368. }
  369. };
  370. #if wxUSE_UNICODE_UTF8
  371. // even char* -> char* needs conversion, from locale charset to UTF-8
  372. typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromWC;
  373. typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromMB;
  374. #elif wxUSE_UNICODE_WCHAR
  375. typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
  376. typedef SubstrBufFromType<wxScopedWCharBuffer> SubstrBufFromMB;
  377. #else
  378. typedef SubstrBufFromType<const char*> SubstrBufFromMB;
  379. typedef SubstrBufFromType<wxScopedCharBuffer> SubstrBufFromWC;
  380. #endif
  381. // Functions implementing primitive operations on string data; wxString
  382. // methods and iterators are implemented in terms of it. The differences
  383. // between UTF-8 and wchar_t* representations of the string are mostly
  384. // contained here.
  385. #if wxUSE_UNICODE_UTF8
  386. static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
  387. const wxMBConv& conv);
  388. static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
  389. const wxMBConv& conv);
  390. #elif wxUSE_UNICODE_WCHAR
  391. static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
  392. const wxMBConv& conv);
  393. #else
  394. static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
  395. const wxMBConv& conv);
  396. #endif
  397. #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
  398. // returns C string encoded as the implementation expects:
  399. #if wxUSE_UNICODE
  400. static const wchar_t* ImplStr(const wchar_t* str)
  401. { return str ? str : wxT(""); }
  402. static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
  403. { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
  404. static wxScopedWCharBuffer ImplStr(const char* str,
  405. const wxMBConv& conv = wxConvLibc)
  406. { return ConvertStr(str, npos, conv).data; }
  407. static SubstrBufFromMB ImplStr(const char* str, size_t n,
  408. const wxMBConv& conv = wxConvLibc)
  409. { return ConvertStr(str, n, conv); }
  410. #else
  411. static const char* ImplStr(const char* str,
  412. const wxMBConv& WXUNUSED(conv) = wxConvLibc)
  413. { return str ? str : ""; }
  414. static const SubstrBufFromMB ImplStr(const char* str, size_t n,
  415. const wxMBConv& WXUNUSED(conv) = wxConvLibc)
  416. { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
  417. static wxScopedCharBuffer ImplStr(const wchar_t* str)
  418. { return ConvertStr(str, npos, wxConvLibc).data; }
  419. static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
  420. { return ConvertStr(str, n, wxConvLibc); }
  421. #endif
  422. // translates position index in wxString to/from index in underlying
  423. // wxStringImpl:
  424. static size_t PosToImpl(size_t pos) { return pos; }
  425. static void PosLenToImpl(size_t pos, size_t len,
  426. size_t *implPos, size_t *implLen)
  427. { *implPos = pos; *implLen = len; }
  428. static size_t LenToImpl(size_t len) { return len; }
  429. static size_t PosFromImpl(size_t pos) { return pos; }
  430. // we don't want to define these as empty inline functions as it could
  431. // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
  432. // in debug build where the inline functions are not effectively inlined
  433. #define wxSTRING_INVALIDATE_CACHE()
  434. #define wxSTRING_INVALIDATE_CACHED_LENGTH()
  435. #define wxSTRING_UPDATE_CACHED_LENGTH(n)
  436. #define wxSTRING_SET_CACHED_LENGTH(n)
  437. #else // wxUSE_UNICODE_UTF8
  438. static wxScopedCharBuffer ImplStr(const char* str,
  439. const wxMBConv& conv = wxConvLibc)
  440. { return ConvertStr(str, npos, conv).data; }
  441. static SubstrBufFromMB ImplStr(const char* str, size_t n,
  442. const wxMBConv& conv = wxConvLibc)
  443. { return ConvertStr(str, n, conv); }
  444. static wxScopedCharBuffer ImplStr(const wchar_t* str)
  445. { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
  446. static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
  447. { return ConvertStr(str, n, wxMBConvUTF8()); }
  448. #if wxUSE_STRING_POS_CACHE
  449. // this is an extremely simple cache used by PosToImpl(): each cache element
  450. // contains the string it applies to and the index corresponding to the last
  451. // used position in this wxString in its m_impl string
  452. //
  453. // NB: notice that this struct (and nested Element one) must be a POD or we
  454. // wouldn't be able to use a thread-local variable of this type, in
  455. // particular it should have no ctor -- we rely on statics being
  456. // initialized to 0 instead
  457. struct Cache
  458. {
  459. enum { SIZE = 8 };
  460. struct Element
  461. {
  462. const wxString *str; // the string to which this element applies
  463. size_t pos, // the cached index in this string
  464. impl, // the corresponding position in its m_impl
  465. len; // cached length or npos if unknown
  466. // reset cached index to 0
  467. void ResetPos() { pos = impl = 0; }
  468. // reset position and length
  469. void Reset() { ResetPos(); len = npos; }
  470. };
  471. // cache the indices mapping for the last few string used
  472. Element cached[SIZE];
  473. // the last used index
  474. unsigned lastUsed;
  475. };
  476. #ifndef wxHAS_COMPILER_TLS
  477. // we must use an accessor function and not a static variable when the TLS
  478. // variables support is implemented in the library (and not by the compiler)
  479. // because the global s_cache variable could be not yet initialized when a
  480. // ctor of another global object is executed and if that ctor uses any
  481. // wxString methods, bad things happen
  482. //
  483. // however notice that this approach does not work when compiler TLS is used,
  484. // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
  485. // using this accessor incorrectly when optimizations are enabled (-O2 is
  486. // enough) -- luckily we don't need it then neither as static __thread
  487. // variables are initialized by 0 anyhow then and so we can use the variable
  488. // directly
  489. WXEXPORT static Cache& GetCache()
  490. {
  491. static wxTLS_TYPE(Cache) s_cache;
  492. return wxTLS_VALUE(s_cache);
  493. }
  494. // this helper struct is used to ensure that GetCache() is called during
  495. // static initialization time, i.e. before any threads creation, as otherwise
  496. // the static s_cache construction inside GetCache() wouldn't be MT-safe
  497. friend struct wxStrCacheInitializer;
  498. #else // wxHAS_COMPILER_TLS
  499. static wxTLS_TYPE(Cache) ms_cache;
  500. static Cache& GetCache() { return wxTLS_VALUE(ms_cache); }
  501. #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
  502. static Cache::Element *GetCacheBegin() { return GetCache().cached; }
  503. static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; }
  504. static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; }
  505. // this is used in debug builds only to provide a convenient function,
  506. // callable from a debugger, to show the cache contents
  507. friend struct wxStrCacheDumper;
  508. // uncomment this to have access to some profiling statistics on program
  509. // termination
  510. //#define wxPROFILE_STRING_CACHE
  511. #ifdef wxPROFILE_STRING_CACHE
  512. static struct PosToImplCacheStats
  513. {
  514. unsigned postot, // total non-trivial calls to PosToImpl
  515. poshits, // cache hits from PosToImpl()
  516. mishits, // cached position beyond the needed one
  517. sumpos, // sum of all positions, used to compute the
  518. // average position after dividing by postot
  519. sumofs, // sum of all offsets after using the cache, used to
  520. // compute the average after dividing by hits
  521. lentot, // number of total calls to length()
  522. lenhits; // number of cache hits in length()
  523. } ms_cacheStats;
  524. friend struct wxStrCacheStatsDumper;
  525. #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
  526. #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
  527. #else // !wxPROFILE_STRING_CACHE
  528. #define wxCACHE_PROFILE_FIELD_INC(field)
  529. #define wxCACHE_PROFILE_FIELD_ADD(field, val)
  530. #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
  531. // note: it could seem that the functions below shouldn't be inline because
  532. // they are big, contain loops and so the compiler shouldn't be able to
  533. // inline them anyhow, however moving them into string.cpp does decrease the
  534. // code performance by ~5%, at least when using g++ 4.1 so do keep them here
  535. // unless tests show that it's not advantageous any more
  536. // return the pointer to the cache element for this string or NULL if not
  537. // cached
  538. Cache::Element *FindCacheElement() const
  539. {
  540. // profiling seems to show a small but consistent gain if we use this
  541. // simple loop instead of starting from the last used element (there are
  542. // a lot of misses in this function...)
  543. Cache::Element * const cacheBegin = GetCacheBegin();
  544. #ifndef wxHAS_COMPILER_TLS
  545. // during destruction tls calls may return NULL, in this case return NULL
  546. // immediately without accessing anything else
  547. if ( cacheBegin == NULL )
  548. return NULL;
  549. #endif
  550. Cache::Element * const cacheEnd = GetCacheEnd();
  551. for ( Cache::Element *c = cacheBegin; c != cacheEnd; c++ )
  552. {
  553. if ( c->str == this )
  554. return c;
  555. }
  556. return NULL;
  557. }
  558. // unlike FindCacheElement(), this one always returns a valid pointer to the
  559. // cache element for this string, it may have valid last cached position and
  560. // its corresponding index in the byte string or not
  561. Cache::Element *GetCacheElement() const
  562. {
  563. Cache::Element * const cacheBegin = GetCacheBegin();
  564. Cache::Element * const cacheEnd = GetCacheEnd();
  565. Cache::Element * const cacheStart = cacheBegin + LastUsedCacheElement();
  566. // check the last used first, this does no (measurable) harm for a miss
  567. // but does help for simple loops addressing the same string all the time
  568. if ( cacheStart->str == this )
  569. return cacheStart;
  570. // notice that we're going to check cacheStart again inside this call but
  571. // profiling shows that it's still faster to use a simple loop like
  572. // inside FindCacheElement() than manually looping with wrapping starting
  573. // from the cache entry after the start one
  574. Cache::Element *c = FindCacheElement();
  575. if ( !c )
  576. {
  577. // claim the next cache entry for this string
  578. c = cacheStart;
  579. if ( ++c == cacheEnd )
  580. c = cacheBegin;
  581. c->str = this;
  582. c->Reset();
  583. // and remember the last used element
  584. LastUsedCacheElement() = c - cacheBegin;
  585. }
  586. return c;
  587. }
  588. size_t DoPosToImpl(size_t pos) const
  589. {
  590. wxCACHE_PROFILE_FIELD_INC(postot);
  591. // NB: although the case of pos == 1 (and offset from cached position
  592. // equal to 1) are common, nothing is gained by writing special code
  593. // for handling them, the compiler (at least g++ 4.1 used) seems to
  594. // optimize the code well enough on its own
  595. wxCACHE_PROFILE_FIELD_ADD(sumpos, pos);
  596. Cache::Element * const cache = GetCacheElement();
  597. // cached position can't be 0 so if it is, it means that this entry was
  598. // used for length caching only so far, i.e. it doesn't count as a hit
  599. // from our point of view
  600. if ( cache->pos )
  601. {
  602. wxCACHE_PROFILE_FIELD_INC(poshits);
  603. }
  604. if ( pos == cache->pos )
  605. return cache->impl;
  606. // this seems to happen only rarely so just reset the cache in this case
  607. // instead of complicating code even further by seeking backwards in this
  608. // case
  609. if ( cache->pos > pos )
  610. {
  611. wxCACHE_PROFILE_FIELD_INC(mishits);
  612. cache->ResetPos();
  613. }
  614. wxCACHE_PROFILE_FIELD_ADD(sumofs, pos - cache->pos);
  615. wxStringImpl::const_iterator i(m_impl.begin() + cache->impl);
  616. for ( size_t n = cache->pos; n < pos; n++ )
  617. wxStringOperations::IncIter(i);
  618. cache->pos = pos;
  619. cache->impl = i - m_impl.begin();
  620. wxSTRING_CACHE_ASSERT(
  621. (int)cache->impl == (begin() + pos).impl() - m_impl.begin() );
  622. return cache->impl;
  623. }
  624. void InvalidateCache()
  625. {
  626. Cache::Element * const cache = FindCacheElement();
  627. if ( cache )
  628. cache->Reset();
  629. }
  630. void InvalidateCachedLength()
  631. {
  632. Cache::Element * const cache = FindCacheElement();
  633. if ( cache )
  634. cache->len = npos;
  635. }
  636. void SetCachedLength(size_t len)
  637. {
  638. // we optimistically cache the length here even if the string wasn't
  639. // present in the cache before, this seems to do no harm and the
  640. // potential for avoiding length recomputation for long strings looks
  641. // interesting
  642. GetCacheElement()->len = len;
  643. }
  644. void UpdateCachedLength(ptrdiff_t delta)
  645. {
  646. Cache::Element * const cache = FindCacheElement();
  647. if ( cache && cache->len != npos )
  648. {
  649. wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache->len + delta >= 0 );
  650. cache->len += delta;
  651. }
  652. }
  653. #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
  654. #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
  655. #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
  656. #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
  657. #else // !wxUSE_STRING_POS_CACHE
  658. size_t DoPosToImpl(size_t pos) const
  659. {
  660. return (begin() + pos).impl() - m_impl.begin();
  661. }
  662. #define wxSTRING_INVALIDATE_CACHE()
  663. #define wxSTRING_INVALIDATE_CACHED_LENGTH()
  664. #define wxSTRING_UPDATE_CACHED_LENGTH(n)
  665. #define wxSTRING_SET_CACHED_LENGTH(n)
  666. #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
  667. size_t PosToImpl(size_t pos) const
  668. {
  669. return pos == 0 || pos == npos ? pos : DoPosToImpl(pos);
  670. }
  671. void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
  672. size_t LenToImpl(size_t len) const
  673. {
  674. size_t pos, len2;
  675. PosLenToImpl(0, len, &pos, &len2);
  676. return len2;
  677. }
  678. size_t PosFromImpl(size_t pos) const
  679. {
  680. if ( pos == 0 || pos == npos )
  681. return pos;
  682. else
  683. return const_iterator(this, m_impl.begin() + pos) - begin();
  684. }
  685. #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
  686. public:
  687. // standard types
  688. typedef wxUniChar value_type;
  689. typedef wxUniChar char_type;
  690. typedef wxUniCharRef reference;
  691. typedef wxChar* pointer;
  692. typedef const wxChar* const_pointer;
  693. typedef size_t size_type;
  694. typedef wxUniChar const_reference;
  695. #if wxUSE_STD_STRING
  696. #if wxUSE_UNICODE_UTF8
  697. // random access is not O(1), as required by Random Access Iterator
  698. #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
  699. #else
  700. #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
  701. #endif
  702. #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
  703. #else
  704. // not defining iterator_category at all in this case is better than defining
  705. // it as some dummy type -- at least it results in more intelligible error
  706. // messages
  707. #define WX_DEFINE_ITERATOR_CATEGORY(cat)
  708. #endif
  709. #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
  710. private: \
  711. typedef wxStringImpl::iterator_name underlying_iterator; \
  712. public: \
  713. WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
  714. typedef wxUniChar value_type; \
  715. typedef ptrdiff_t difference_type; \
  716. typedef reference_type reference; \
  717. typedef pointer_type pointer; \
  718. \
  719. reference operator[](size_t n) const { return *(*this + n); } \
  720. \
  721. iterator_name& operator++() \
  722. { wxStringOperations::IncIter(m_cur); return *this; } \
  723. iterator_name& operator--() \
  724. { wxStringOperations::DecIter(m_cur); return *this; } \
  725. iterator_name operator++(int) \
  726. { \
  727. iterator_name tmp = *this; \
  728. wxStringOperations::IncIter(m_cur); \
  729. return tmp; \
  730. } \
  731. iterator_name operator--(int) \
  732. { \
  733. iterator_name tmp = *this; \
  734. wxStringOperations::DecIter(m_cur); \
  735. return tmp; \
  736. } \
  737. \
  738. iterator_name& operator+=(ptrdiff_t n) \
  739. { \
  740. m_cur = wxStringOperations::AddToIter(m_cur, n); \
  741. return *this; \
  742. } \
  743. iterator_name& operator-=(ptrdiff_t n) \
  744. { \
  745. m_cur = wxStringOperations::AddToIter(m_cur, -n); \
  746. return *this; \
  747. } \
  748. \
  749. difference_type operator-(const iterator_name& i) const \
  750. { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
  751. \
  752. bool operator==(const iterator_name& i) const \
  753. { return m_cur == i.m_cur; } \
  754. bool operator!=(const iterator_name& i) const \
  755. { return m_cur != i.m_cur; } \
  756. \
  757. bool operator<(const iterator_name& i) const \
  758. { return m_cur < i.m_cur; } \
  759. bool operator>(const iterator_name& i) const \
  760. { return m_cur > i.m_cur; } \
  761. bool operator<=(const iterator_name& i) const \
  762. { return m_cur <= i.m_cur; } \
  763. bool operator>=(const iterator_name& i) const \
  764. { return m_cur >= i.m_cur; } \
  765. \
  766. private: \
  767. /* for internal wxString use only: */ \
  768. underlying_iterator impl() const { return m_cur; } \
  769. \
  770. friend class wxString; \
  771. friend class wxCStrData; \
  772. \
  773. private: \
  774. underlying_iterator m_cur
  775. class WXDLLIMPEXP_FWD_BASE const_iterator;
  776. #if wxUSE_UNICODE_UTF8
  777. // NB: In UTF-8 build, (non-const) iterator needs to keep reference
  778. // to the underlying wxStringImpl, because UTF-8 is variable-length
  779. // encoding and changing the value pointer to by an iterator (using
  780. // its operator*) requires calling wxStringImpl::replace() if the old
  781. // and new values differ in their encoding's length.
  782. //
  783. // Furthermore, the replace() call may invalid all iterators for the
  784. // string, so we have to keep track of outstanding iterators and update
  785. // them if replace() happens.
  786. //
  787. // This is implemented by maintaining linked list of iterators for every
  788. // string and traversing it in wxUniCharRef::operator=(). Head of the
  789. // list is stored in wxString. (FIXME-UTF8)
  790. class WXDLLIMPEXP_BASE iterator
  791. {
  792. WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
  793. public:
  794. iterator() {}
  795. iterator(const iterator& i)
  796. : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
  797. iterator& operator=(const iterator& i)
  798. {
  799. if (&i != this)
  800. {
  801. m_cur = i.m_cur;
  802. m_node.set(i.str(), &m_cur);
  803. }
  804. return *this;
  805. }
  806. reference operator*()
  807. { return wxUniCharRef::CreateForString(*str(), m_cur); }
  808. iterator operator+(ptrdiff_t n) const
  809. { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
  810. iterator operator-(ptrdiff_t n) const
  811. { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
  812. // Normal iterators need to be comparable with the const_iterators so
  813. // declare the comparison operators and implement them below after the
  814. // full const_iterator declaration.
  815. bool operator==(const const_iterator& i) const;
  816. bool operator!=(const const_iterator& i) const;
  817. bool operator<(const const_iterator& i) const;
  818. bool operator>(const const_iterator& i) const;
  819. bool operator<=(const const_iterator& i) const;
  820. bool operator>=(const const_iterator& i) const;
  821. private:
  822. iterator(wxString *wxstr, underlying_iterator ptr)
  823. : m_cur(ptr), m_node(wxstr, &m_cur) {}
  824. wxString* str() const { return const_cast<wxString*>(m_node.m_str); }
  825. wxStringIteratorNode m_node;
  826. friend class const_iterator;
  827. };
  828. class WXDLLIMPEXP_BASE const_iterator
  829. {
  830. // NB: reference_type is intentionally value, not reference, the character
  831. // may be encoded differently in wxString data:
  832. WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
  833. public:
  834. const_iterator() {}
  835. const_iterator(const const_iterator& i)
  836. : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
  837. const_iterator(const iterator& i)
  838. : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
  839. const_iterator& operator=(const const_iterator& i)
  840. {
  841. if (&i != this)
  842. {
  843. m_cur = i.m_cur;
  844. m_node.set(i.str(), &m_cur);
  845. }
  846. return *this;
  847. }
  848. const_iterator& operator=(const iterator& i)
  849. { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
  850. reference operator*() const
  851. { return wxStringOperations::DecodeChar(m_cur); }
  852. const_iterator operator+(ptrdiff_t n) const
  853. { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
  854. const_iterator operator-(ptrdiff_t n) const
  855. { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
  856. // Notice that comparison operators taking non-const iterator are not
  857. // needed here because of the implicit conversion from non-const iterator
  858. // to const ones ensure that the versions for const_iterator declared
  859. // inside WX_STR_ITERATOR_IMPL can be used.
  860. private:
  861. // for internal wxString use only:
  862. const_iterator(const wxString *wxstr, underlying_iterator ptr)
  863. : m_cur(ptr), m_node(wxstr, &m_cur) {}
  864. const wxString* str() const { return m_node.m_str; }
  865. wxStringIteratorNode m_node;
  866. };
  867. size_t IterToImplPos(wxString::iterator i) const
  868. { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); }
  869. iterator GetIterForNthChar(size_t n)
  870. { return iterator(this, m_impl.begin() + PosToImpl(n)); }
  871. const_iterator GetIterForNthChar(size_t n) const
  872. { return const_iterator(this, m_impl.begin() + PosToImpl(n)); }
  873. #else // !wxUSE_UNICODE_UTF8
  874. class WXDLLIMPEXP_BASE iterator
  875. {
  876. WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
  877. public:
  878. iterator() {}
  879. iterator(const iterator& i) : m_cur(i.m_cur) {}
  880. reference operator*()
  881. { return wxUniCharRef::CreateForString(m_cur); }
  882. iterator operator+(ptrdiff_t n) const
  883. { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
  884. iterator operator-(ptrdiff_t n) const
  885. { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
  886. // As in UTF-8 case above, define comparison operators taking
  887. // const_iterator too.
  888. bool operator==(const const_iterator& i) const;
  889. bool operator!=(const const_iterator& i) const;
  890. bool operator<(const const_iterator& i) const;
  891. bool operator>(const const_iterator& i) const;
  892. bool operator<=(const const_iterator& i) const;
  893. bool operator>=(const const_iterator& i) const;
  894. private:
  895. // for internal wxString use only:
  896. iterator(underlying_iterator ptr) : m_cur(ptr) {}
  897. iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
  898. friend class const_iterator;
  899. };
  900. class WXDLLIMPEXP_BASE const_iterator
  901. {
  902. // NB: reference_type is intentionally value, not reference, the character
  903. // may be encoded differently in wxString data:
  904. WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
  905. public:
  906. const_iterator() {}
  907. const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
  908. const_iterator(const iterator& i) : m_cur(i.m_cur) {}
  909. reference operator*() const
  910. { return wxStringOperations::DecodeChar(m_cur); }
  911. const_iterator operator+(ptrdiff_t n) const
  912. { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
  913. const_iterator operator-(ptrdiff_t n) const
  914. { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
  915. // As in UTF-8 case above, we don't need comparison operators taking
  916. // iterator because we have an implicit conversion from iterator to
  917. // const_iterator so the operators declared by WX_STR_ITERATOR_IMPL will
  918. // be used.
  919. private:
  920. // for internal wxString use only:
  921. const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
  922. const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr)
  923. : m_cur(ptr) {}
  924. };
  925. iterator GetIterForNthChar(size_t n) { return begin() + n; }
  926. const_iterator GetIterForNthChar(size_t n) const { return begin() + n; }
  927. #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
  928. #undef WX_STR_ITERATOR_TAG
  929. #undef WX_STR_ITERATOR_IMPL
  930. // This method is mostly used by wxWidgets itself and return the offset of
  931. // the given iterator in bytes relative to the start of the buffer
  932. // representing the current string contents in the current locale encoding.
  933. //
  934. // It is inefficient as it involves converting part of the string to this
  935. // encoding (and also unsafe as it simply returns 0 if the conversion fails)
  936. // and so should be avoided if possible, wx itself only uses it to implement
  937. // backwards-compatible API.
  938. ptrdiff_t IterOffsetInMBStr(const const_iterator& i) const
  939. {
  940. const wxString str(begin(), i);
  941. // This is logically equivalent to strlen(str.mb_str()) but avoids
  942. // actually converting the string to multibyte and just computes the
  943. // length that it would have after conversion.
  944. size_t ofs = wxConvLibc.FromWChar(NULL, 0, str.wc_str(), str.length());
  945. return ofs == wxCONV_FAILED ? 0 : static_cast<ptrdiff_t>(ofs);
  946. }
  947. friend class iterator;
  948. friend class const_iterator;
  949. template <typename T>
  950. class reverse_iterator_impl
  951. {
  952. public:
  953. typedef T iterator_type;
  954. WX_DEFINE_ITERATOR_CATEGORY(typename T::iterator_category)
  955. typedef typename T::value_type value_type;
  956. typedef typename T::difference_type difference_type;
  957. typedef typename T::reference reference;
  958. typedef typename T::pointer *pointer;
  959. reverse_iterator_impl() {}
  960. reverse_iterator_impl(iterator_type i) : m_cur(i) {}
  961. reverse_iterator_impl(const reverse_iterator_impl& ri)
  962. : m_cur(ri.m_cur) {}
  963. iterator_type base() const { return m_cur; }
  964. reference operator*() const { return *(m_cur-1); }
  965. reference operator[](size_t n) const { return *(*this + n); }
  966. reverse_iterator_impl& operator++()
  967. { --m_cur; return *this; }
  968. reverse_iterator_impl operator++(int)
  969. { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
  970. reverse_iterator_impl& operator--()
  971. { ++m_cur; return *this; }
  972. reverse_iterator_impl operator--(int)
  973. { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
  974. // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
  975. reverse_iterator_impl operator+(ptrdiff_t n) const
  976. { return reverse_iterator_impl<T>(m_cur - n); }
  977. reverse_iterator_impl operator-(ptrdiff_t n) const
  978. { return reverse_iterator_impl<T>(m_cur + n); }
  979. reverse_iterator_impl operator+=(ptrdiff_t n)
  980. { m_cur -= n; return *this; }
  981. reverse_iterator_impl operator-=(ptrdiff_t n)
  982. { m_cur += n; return *this; }
  983. unsigned operator-(const reverse_iterator_impl& i) const
  984. { return i.m_cur - m_cur; }
  985. bool operator==(const reverse_iterator_impl& ri) const
  986. { return m_cur == ri.m_cur; }
  987. bool operator!=(const reverse_iterator_impl& ri) const
  988. { return !(*this == ri); }
  989. bool operator<(const reverse_iterator_impl& i) const
  990. { return m_cur > i.m_cur; }
  991. bool operator>(const reverse_iterator_impl& i) const
  992. { return m_cur < i.m_cur; }
  993. bool operator<=(const reverse_iterator_impl& i) const
  994. { return m_cur >= i.m_cur; }
  995. bool operator>=(const reverse_iterator_impl& i) const
  996. { return m_cur <= i.m_cur; }
  997. private:
  998. iterator_type m_cur;
  999. };
  1000. typedef reverse_iterator_impl<iterator> reverse_iterator;
  1001. typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
  1002. private:
  1003. // used to transform an expression built using c_str() (and hence of type
  1004. // wxCStrData) to an iterator into the string
  1005. static const_iterator CreateConstIterator(const wxCStrData& data)
  1006. {
  1007. return const_iterator(data.m_str,
  1008. (data.m_str->begin() + data.m_offset).impl());
  1009. }
  1010. // in UTF-8 STL build, creation from std::string requires conversion under
  1011. // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
  1012. // instead we define dummy type that lets us have wxString ctor for creation
  1013. // from wxStringImpl that couldn't be used by user code (in all other builds,
  1014. // "standard" ctors can be used):
  1015. #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
  1016. struct CtorFromStringImplTag {};
  1017. wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
  1018. : m_impl(src) {}
  1019. static wxString FromImpl(const wxStringImpl& src)
  1020. { return wxString((CtorFromStringImplTag*)NULL, src); }
  1021. #else
  1022. #if !wxUSE_STL_BASED_WXSTRING
  1023. wxString(const wxStringImpl& src) : m_impl(src) { }
  1024. // else: already defined as wxString(wxStdString) below
  1025. #endif
  1026. static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
  1027. #endif
  1028. public:
  1029. // constructors and destructor
  1030. // ctor for an empty string
  1031. wxString() {}
  1032. // copy ctor
  1033. wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
  1034. // string containing nRepeat copies of ch
  1035. wxString(wxUniChar ch, size_t nRepeat = 1 )
  1036. { assign(nRepeat, ch); }
  1037. wxString(size_t nRepeat, wxUniChar ch)
  1038. { assign(nRepeat, ch); }
  1039. wxString(wxUniCharRef ch, size_t nRepeat = 1)
  1040. { assign(nRepeat, ch); }
  1041. wxString(size_t nRepeat, wxUniCharRef ch)
  1042. { assign(nRepeat, ch); }
  1043. wxString(char ch, size_t nRepeat = 1)
  1044. { assign(nRepeat, ch); }
  1045. wxString(size_t nRepeat, char ch)
  1046. { assign(nRepeat, ch); }
  1047. wxString(wchar_t ch, size_t nRepeat = 1)
  1048. { assign(nRepeat, ch); }
  1049. wxString(size_t nRepeat, wchar_t ch)
  1050. { assign(nRepeat, ch); }
  1051. // ctors from char* strings:
  1052. wxString(const char *psz)
  1053. : m_impl(ImplStr(psz)) {}
  1054. wxString(const char *psz, const wxMBConv& conv)
  1055. : m_impl(ImplStr(psz, conv)) {}
  1056. wxString(const char *psz, size_t nLength)
  1057. { assign(psz, nLength); }
  1058. wxString(const char *psz, const wxMBConv& conv, size_t nLength)
  1059. {
  1060. SubstrBufFromMB str(ImplStr(psz, nLength, conv));
  1061. m_impl.assign(str.data, str.len);
  1062. }
  1063. // and unsigned char*:
  1064. wxString(const unsigned char *psz)
  1065. : m_impl(ImplStr((const char*)psz)) {}
  1066. wxString(const unsigned char *psz, const wxMBConv& conv)
  1067. : m_impl(ImplStr((const char*)psz, conv)) {}
  1068. wxString(const unsigned char *psz, size_t nLength)
  1069. { assign((const char*)psz, nLength); }
  1070. wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
  1071. {
  1072. SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
  1073. m_impl.assign(str.data, str.len);
  1074. }
  1075. // ctors from wchar_t* strings:
  1076. wxString(const wchar_t *pwz)
  1077. : m_impl(ImplStr(pwz)) {}
  1078. wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
  1079. : m_impl(ImplStr(pwz)) {}
  1080. wxString(const wchar_t *pwz, size_t nLength)
  1081. { assign(pwz, nLength); }
  1082. wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
  1083. { assign(pwz, nLength); }
  1084. wxString(const wxScopedCharBuffer& buf)
  1085. { assign(buf.data(), buf.length()); }
  1086. wxString(const wxScopedWCharBuffer& buf)
  1087. { assign(buf.data(), buf.length()); }
  1088. // NB: this version uses m_impl.c_str() to force making a copy of the
  1089. // string, so that "wxString(str.c_str())" idiom for passing strings
  1090. // between threads works
  1091. wxString(const wxCStrData& cstr)
  1092. : m_impl(cstr.AsString().m_impl.c_str()) { }
  1093. // as we provide both ctors with this signature for both char and unsigned
  1094. // char string, we need to provide one for wxCStrData to resolve ambiguity
  1095. wxString(const wxCStrData& cstr, size_t nLength)
  1096. : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
  1097. // and because wxString is convertible to wxCStrData and const wxChar *
  1098. // we also need to provide this one
  1099. wxString(const wxString& str, size_t nLength)
  1100. { assign(str, nLength); }
  1101. #if wxUSE_STRING_POS_CACHE
  1102. ~wxString()
  1103. {
  1104. // we need to invalidate our cache entry as another string could be
  1105. // recreated at the same address (unlikely, but still possible, with the
  1106. // heap-allocated strings but perfectly common with stack-allocated ones)
  1107. InvalidateCache();
  1108. }
  1109. #endif // wxUSE_STRING_POS_CACHE
  1110. // even if we're not built with wxUSE_STD_STRING_CONV_IN_WXSTRING == 1 it is
  1111. // very convenient to allow implicit conversions from std::string to wxString
  1112. // and vice verse as this allows to use the same strings in non-GUI and GUI
  1113. // code, however we don't want to unconditionally add this ctor as it would
  1114. // make wx lib dependent on libstdc++ on some Linux versions which is bad, so
  1115. // instead we ask the client code to define this wxUSE_STD_STRING symbol if
  1116. // they need it
  1117. #if wxUSE_STD_STRING
  1118. #if wxUSE_UNICODE_WCHAR
  1119. wxString(const wxStdWideString& str) : m_impl(str) {}
  1120. #else // UTF-8 or ANSI
  1121. wxString(const wxStdWideString& str)
  1122. { assign(str.c_str(), str.length()); }
  1123. #endif
  1124. #if !wxUSE_UNICODE // ANSI build
  1125. // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
  1126. wxString(const std::string& str) : m_impl(str) {}
  1127. #else // Unicode
  1128. wxString(const std::string& str)
  1129. { assign(str.c_str(), str.length()); }
  1130. #endif
  1131. #endif // wxUSE_STD_STRING
  1132. // Also always provide explicit conversions to std::[w]string in any case,
  1133. // see below for the implicit ones.
  1134. #if wxUSE_STD_STRING
  1135. // We can avoid a copy if we already use this string type internally,
  1136. // otherwise we create a copy on the fly:
  1137. #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
  1138. #define wxStringToStdWstringRetType const wxStdWideString&
  1139. const wxStdWideString& ToStdWstring() const { return m_impl; }
  1140. #else
  1141. // wxStringImpl is either not std::string or needs conversion
  1142. #define wxStringToStdWstringRetType wxStdWideString
  1143. wxStdWideString ToStdWstring() const
  1144. {
  1145. #if wxUSE_UNICODE_WCHAR
  1146. wxScopedWCharBuffer buf =
  1147. wxScopedWCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length());
  1148. #else // !wxUSE_UNICODE_WCHAR
  1149. wxScopedWCharBuffer buf(wc_str());
  1150. #endif
  1151. return wxStdWideString(buf.data(), buf.length());
  1152. }
  1153. #endif
  1154. #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
  1155. // wxStringImpl is std::string in the encoding we want
  1156. #define wxStringToStdStringRetType const std::string&
  1157. const std::string& ToStdString() const { return m_impl; }
  1158. #else
  1159. // wxStringImpl is either not std::string or needs conversion
  1160. #define wxStringToStdStringRetType std::string
  1161. std::string ToStdString() const
  1162. {
  1163. wxScopedCharBuffer buf(mb_str());
  1164. return std::string(buf.data(), buf.length());
  1165. }
  1166. #endif
  1167. #if wxUSE_STD_STRING_CONV_IN_WXSTRING
  1168. // Implicit conversions to std::[w]string are not provided by default as
  1169. // they conflict with the implicit conversions to "const char/wchar_t *"
  1170. // which we use for backwards compatibility but do provide them if
  1171. // explicitly requested.
  1172. operator wxStringToStdStringRetType() const { return ToStdString(); }
  1173. operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
  1174. #endif // wxUSE_STD_STRING_CONV_IN_WXSTRING
  1175. #undef wxStringToStdStringRetType
  1176. #undef wxStringToStdWstringRetType
  1177. #endif // wxUSE_STD_STRING
  1178. wxString Clone() const
  1179. {
  1180. // make a deep copy of the string, i.e. the returned string will have
  1181. // ref count = 1 with refcounted implementation
  1182. return wxString::FromImpl(wxStringImpl(m_impl.c_str(), m_impl.length()));
  1183. }
  1184. // first valid index position
  1185. const_iterator begin() const { return const_iterator(this, m_impl.begin()); }
  1186. iterator begin() { return iterator(this, m_impl.begin()); }
  1187. // position one after the last valid one
  1188. const_iterator end() const { return const_iterator(this, m_impl.end()); }
  1189. iterator end() { return iterator(this, m_impl.end()); }
  1190. // first element of the reversed string
  1191. const_reverse_iterator rbegin() const
  1192. { return const_reverse_iterator(end()); }
  1193. reverse_iterator rbegin()
  1194. { return reverse_iterator(end()); }
  1195. // one beyond the end of the reversed string
  1196. const_reverse_iterator rend() const
  1197. { return const_reverse_iterator(begin()); }
  1198. reverse_iterator rend()
  1199. { return reverse_iterator(begin()); }
  1200. // std::string methods:
  1201. #if wxUSE_UNICODE_UTF8
  1202. size_t length() const
  1203. {
  1204. #if wxUSE_STRING_POS_CACHE
  1205. wxCACHE_PROFILE_FIELD_INC(lentot);
  1206. Cache::Element * const cache = GetCacheElement();
  1207. if ( cache->len == npos )
  1208. {
  1209. // it's probably not worth trying to be clever and using cache->pos
  1210. // here as it's probably 0 anyhow -- you usually call length() before
  1211. // starting to index the string
  1212. cache->len = end() - begin();
  1213. }
  1214. else
  1215. {
  1216. wxCACHE_PROFILE_FIELD_INC(lenhits);
  1217. wxSTRING_CACHE_ASSERT( (int)cache->len == end() - begin() );
  1218. }
  1219. return cache->len;
  1220. #else // !wxUSE_STRING_POS_CACHE
  1221. return end() - begin();
  1222. #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
  1223. }
  1224. #else
  1225. size_t length() const { return m_impl.length(); }
  1226. #endif
  1227. size_type size() const { return length(); }
  1228. size_type max_size() const { return npos; }
  1229. bool empty() const { return m_impl.empty(); }
  1230. // NB: these methods don't have a well-defined meaning in UTF-8 case
  1231. size_type capacity() const { return m_impl.capacity(); }
  1232. void reserve(size_t sz) { m_impl.reserve(sz); }
  1233. void resize(size_t nSize, wxUniChar ch = wxT('\0'))
  1234. {
  1235. const size_t len = length();
  1236. if ( nSize == len)
  1237. return;
  1238. #if wxUSE_UNICODE_UTF8
  1239. if ( nSize < len )
  1240. {
  1241. wxSTRING_INVALIDATE_CACHE();
  1242. // we can't use wxStringImpl::resize() for truncating the string as it
  1243. // counts in bytes, not characters
  1244. erase(nSize);
  1245. return;
  1246. }
  1247. // we also can't use (presumably more efficient) resize() if we have to
  1248. // append characters taking more than one byte
  1249. if ( !ch.IsAscii() )
  1250. {
  1251. append(nSize - len, ch);
  1252. }
  1253. else // can use (presumably faster) resize() version
  1254. #endif // wxUSE_UNICODE_UTF8
  1255. {
  1256. wxSTRING_INVALIDATE_CACHED_LENGTH();
  1257. m_impl.resize(nSize, (wxStringCharType)ch);
  1258. }
  1259. }
  1260. wxString substr(size_t nStart = 0, size_t nLen = npos) const
  1261. {
  1262. size_t pos, len;
  1263. PosLenToImpl(nStart, nLen, &pos, &len);
  1264. return FromImpl(m_impl.substr(pos, len));
  1265. }
  1266. // generic attributes & operations
  1267. // as standard strlen()
  1268. size_t Len() const { return length(); }
  1269. // string contains any characters?
  1270. bool IsEmpty() const { return empty(); }
  1271. // empty string is "false", so !str will return true
  1272. bool operator!() const { return empty(); }
  1273. // truncate the string to given length
  1274. wxString& Truncate(size_t uiLen);
  1275. // empty string contents
  1276. void Empty() { clear(); }
  1277. // empty the string and free memory
  1278. void Clear() { clear(); }
  1279. // contents test
  1280. // Is an ascii value
  1281. bool IsAscii() const;
  1282. // Is a number
  1283. bool IsNumber() const;
  1284. // Is a word
  1285. bool IsWord() const;
  1286. // data access (all indexes are 0 based)
  1287. // read access
  1288. wxUniChar at(size_t n) const
  1289. { return wxStringOperations::DecodeChar(m_impl.begin() + PosToImpl(n)); }
  1290. wxUniChar GetChar(size_t n) const
  1291. { return at(n); }
  1292. // read/write access
  1293. wxUniCharRef at(size_t n)
  1294. { return *GetIterForNthChar(n); }
  1295. wxUniCharRef GetWritableChar(size_t n)
  1296. { return at(n); }
  1297. // write access
  1298. void SetChar(size_t n, wxUniChar ch)
  1299. { at(n) = ch; }
  1300. // get last character
  1301. wxUniChar Last() const
  1302. {
  1303. wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
  1304. return *rbegin();
  1305. }
  1306. // get writable last character
  1307. wxUniCharRef Last()
  1308. {
  1309. wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
  1310. return *rbegin();
  1311. }
  1312. /*
  1313. Note that we we must define all of the overloads below to avoid
  1314. ambiguity when using str[0].
  1315. */
  1316. wxUniChar operator[](int n) const
  1317. { return at(n); }
  1318. wxUniChar operator[](long n) const
  1319. { return at(n); }
  1320. wxUniChar operator[](size_t n) const
  1321. { return at(n); }
  1322. #ifndef wxSIZE_T_IS_UINT
  1323. wxUniChar operator[](unsigned int n) const
  1324. { return at(n); }
  1325. #endif // size_t != unsigned int
  1326. // operator versions of GetWriteableChar()
  1327. wxUniCharRef operator[](int n)
  1328. { return at(n); }
  1329. wxUniCharRef operator[](long n)
  1330. { return at(n); }
  1331. wxUniCharRef operator[](size_t n)
  1332. { return at(n); }
  1333. #ifndef wxSIZE_T_IS_UINT
  1334. wxUniCharRef operator[](unsigned int n)
  1335. { return at(n); }
  1336. #endif // size_t != unsigned int
  1337. /*
  1338. Overview of wxString conversions, implicit and explicit:
  1339. - wxString has a std::[w]string-like c_str() method, however it does
  1340. not return a C-style string directly but instead returns wxCStrData
  1341. helper object which is convertible to either "char *" narrow string
  1342. or "wchar_t *" wide string. Usually the correct conversion will be
  1343. applied by the compiler automatically but if this doesn't happen you
  1344. need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
  1345. methods or another wxString conversion function.
  1346. - One of the places where the conversion does *NOT* happen correctly is
  1347. when c_str() is passed to a vararg function such as printf() so you
  1348. must *NOT* use c_str() with them. Either use wxPrintf() (all wx
  1349. functions do handle c_str() correctly, even if they appear to be
  1350. vararg (but they're not, really)) or add an explicit AsChar() or, if
  1351. compatibility with previous wxWidgets versions is important, add a
  1352. cast to "const char *".
  1353. - In non-STL mode only, wxString is also implicitly convertible to
  1354. wxCStrData. The same warning as above applies.
  1355. - c_str() is polymorphic as it can be converted to either narrow or
  1356. wide string. If you explicitly need one or the other, choose to use
  1357. mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
  1358. these functions can return either the pointer to string directly (if
  1359. this is what the string uses internally) or a temporary buffer
  1360. containing the string and convertible to it. Again, conversion will
  1361. usually be done automatically by the compiler but beware of the
  1362. vararg functions: you need an explicit cast when using them.
  1363. - There are also non-const versions of mb_str() and wc_str() called
  1364. char_str() and wchar_str(). They are only meant to be used with
  1365. non-const-correct functions and they always return buffers.
  1366. - Finally wx_str() returns whatever string representation is used by
  1367. wxString internally. It may be either a narrow or wide string
  1368. depending on wxWidgets build mode but it will always be a raw pointer
  1369. (and not a buffer).
  1370. */
  1371. // explicit conversion to wxCStrData
  1372. wxCStrData c_str() const { return wxCStrData(this); }
  1373. wxCStrData data() const { return c_str(); }
  1374. // implicit conversion to wxCStrData
  1375. operator wxCStrData() const { return c_str(); }
  1376. // the first two operators conflict with operators for conversion to
  1377. // std::string and they must be disabled if those conversions are enabled;
  1378. // the next one only makes sense if conversions to char* are also defined
  1379. // and not defining it in STL build also helps us to get more clear error
  1380. // messages for the code which relies on implicit conversion to char* in
  1381. // STL build
  1382. #if !wxUSE_STD_STRING_CONV_IN_WXSTRING
  1383. operator const char*() const { return c_str(); }
  1384. operator const wchar_t*() const { return c_str(); }
  1385. // implicit conversion to untyped pointer for compatibility with previous
  1386. // wxWidgets versions: this is the same as conversion to const char * so it
  1387. // may fail!
  1388. operator const void*() const { return c_str(); }
  1389. #endif // !wxUSE_STD_STRING_CONV_IN_WXSTRING
  1390. // identical to c_str(), for MFC compatibility
  1391. const wxCStrData GetData() const { return c_str(); }
  1392. // explicit conversion to C string in internal representation (char*,
  1393. // wchar_t*, UTF-8-encoded char*, depending on the build):
  1394. const wxStringCharType *wx_str() const { return m_impl.c_str(); }
  1395. // conversion to *non-const* multibyte or widestring buffer; modifying
  1396. // returned buffer won't affect the string, these methods are only useful
  1397. // for passing values to const-incorrect functions
  1398. wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
  1399. { return mb_str(conv); }
  1400. wxWritableWCharBuffer wchar_str() const { return wc_str(); }
  1401. // conversion to the buffer of the given type T (= char or wchar_t) and
  1402. // also optionally return the buffer length
  1403. //
  1404. // this is mostly/only useful for the template functions
  1405. //
  1406. // FIXME-VC6: the second argument only exists for VC6 which doesn't support
  1407. // explicit template function selection, do not use it unless
  1408. // you must support VC6!
  1409. template <typename T>
  1410. wxCharTypeBuffer<T> tchar_str(size_t *len = NULL,
  1411. T * WXUNUSED(dummy) = NULL) const
  1412. {
  1413. #if wxUSE_UNICODE
  1414. // we need a helper dispatcher depending on type
  1415. return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len);
  1416. #else // ANSI
  1417. // T can only be char in ANSI build
  1418. if ( len )
  1419. *len = length();
  1420. return wxCharTypeBuffer<T>::CreateNonOwned(wx_str(), length());
  1421. #endif // Unicode build kind
  1422. }
  1423. // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
  1424. // converting numbers or strings which are certain not to contain special
  1425. // chars (typically system functions, X atoms, environment variables etc.)
  1426. //
  1427. // the behaviour of these functions with the strings containing anything
  1428. // else than 7 bit ASCII characters is undefined, use at your own risk.
  1429. #if wxUSE_UNICODE
  1430. static wxString FromAscii(const char *ascii, size_t len);
  1431. static wxString FromAscii(const char *ascii);
  1432. static wxString FromAscii(char ascii);
  1433. const wxScopedCharBuffer ToAscii() const;
  1434. #else // ANSI
  1435. static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
  1436. static wxString FromAscii(const char *ascii, size_t len)
  1437. { return wxString( ascii, len ); }
  1438. static wxString FromAscii(char ascii) { return wxString( ascii ); }
  1439. const char *ToAscii() const { return c_str(); }
  1440. #endif // Unicode/!Unicode
  1441. // also provide unsigned char overloads as signed/unsigned doesn't matter
  1442. // for 7 bit ASCII characters
  1443. static wxString FromAscii(const unsigned char *ascii)
  1444. { return FromAscii((const char *)ascii); }
  1445. static wxString FromAscii(const unsigned char *ascii, size_t len)
  1446. { return FromAscii((const char *)ascii, len); }
  1447. // conversion to/from UTF-8:
  1448. #if wxUSE_UNICODE_UTF8
  1449. static wxString FromUTF8Unchecked(const char *utf8)
  1450. {
  1451. if ( !utf8 )
  1452. return wxEmptyString;
  1453. wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
  1454. return FromImpl(wxStringImpl(utf8));
  1455. }
  1456. static wxString FromUTF8Unchecked(const char *utf8, size_t len)
  1457. {
  1458. if ( !utf8 )
  1459. return wxEmptyString;
  1460. if ( len == npos )
  1461. return FromUTF8Unchecked(utf8);
  1462. wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
  1463. return FromImpl(wxStringImpl(utf8, len));
  1464. }
  1465. static wxString FromUTF8(const char *utf8)
  1466. {
  1467. if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) )
  1468. return "";
  1469. return FromImpl(wxStringImpl(utf8));
  1470. }
  1471. static wxString FromUTF8(const char *utf8, size_t len)
  1472. {
  1473. if ( len == npos )
  1474. return FromUTF8(utf8);
  1475. if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) )
  1476. return "";
  1477. return FromImpl(wxStringImpl(utf8, len));
  1478. }
  1479. const wxScopedCharBuffer utf8_str() const
  1480. { return wxCharBuffer::CreateNonOwned(m_impl.c_str(), m_impl.length()); }
  1481. // this function exists in UTF-8 build only and returns the length of the
  1482. // internal UTF-8 representation
  1483. size_t utf8_length() const { return m_impl.length(); }
  1484. #elif wxUSE_UNICODE_WCHAR
  1485. static wxString FromUTF8(const char *utf8, size_t len = npos)
  1486. { return wxString(utf8, wxMBConvUTF8(), len); }
  1487. static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
  1488. {
  1489. const wxString s(utf8, wxMBConvUTF8(), len);
  1490. wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
  1491. "string must be valid UTF-8" );
  1492. return s;
  1493. }
  1494. const wxScopedCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
  1495. #else // ANSI
  1496. static wxString FromUTF8(const char *utf8)
  1497. { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
  1498. static wxString FromUTF8(const char *utf8, size_t len)
  1499. {
  1500. size_t wlen;
  1501. wxScopedWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
  1502. return wxString(buf.data(), wlen);
  1503. }
  1504. static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
  1505. {
  1506. size_t wlen;
  1507. wxScopedWCharBuffer buf
  1508. (
  1509. wxMBConvUTF8().cMB2WC
  1510. (
  1511. utf8,
  1512. len == npos ? wxNO_LEN : len,
  1513. &wlen
  1514. )
  1515. );
  1516. wxASSERT_MSG( !utf8 || !*utf8 || wlen,
  1517. "string must be valid UTF-8" );
  1518. return wxString(buf.data(), wlen);
  1519. }
  1520. const wxScopedCharBuffer utf8_str() const
  1521. { return wxMBConvUTF8().cWC2MB(wc_str()); }
  1522. #endif
  1523. const wxScopedCharBuffer ToUTF8() const { return utf8_str(); }
  1524. // functions for storing binary data in wxString:
  1525. #if wxUSE_UNICODE
  1526. static wxString From8BitData(const char *data, size_t len)
  1527. { return wxString(data, wxConvISO8859_1, len); }
  1528. // version for NUL-terminated data:
  1529. static wxString From8BitData(const char *data)
  1530. { return wxString(data, wxConvISO8859_1); }
  1531. const wxScopedCharBuffer To8BitData() const
  1532. { return mb_str(wxConvISO8859_1); }
  1533. #else // ANSI
  1534. static wxString From8BitData(const char *data, size_t len)
  1535. { return wxString(data, len); }
  1536. // version for NUL-terminated data:
  1537. static wxString From8BitData(const char *data)
  1538. { return wxString(data); }
  1539. const wxScopedCharBuffer To8BitData() const
  1540. { return wxScopedCharBuffer::CreateNonOwned(wx_str(), length()); }
  1541. #endif // Unicode/ANSI
  1542. // conversions with (possible) format conversions: have to return a
  1543. // buffer with temporary data
  1544. //
  1545. // the functions defined (in either Unicode or ANSI) mode are mb_str() to
  1546. // return an ANSI (multibyte) string, wc_str() to return a wide string and
  1547. // fn_str() to return a string which should be used with the OS APIs
  1548. // accepting the file names. The return value is always the same, but the
  1549. // type differs because a function may either return pointer to the buffer
  1550. // directly or have to use intermediate buffer for translation.
  1551. #if wxUSE_UNICODE
  1552. // this is an optimization: even though using mb_str(wxConvLibc) does the
  1553. // same thing (i.e. returns pointer to internal representation as locale is
  1554. // always an UTF-8 one) in wxUSE_UTF8_LOCALE_ONLY case, we can avoid the
  1555. // extra checks and the temporary buffer construction by providing a
  1556. // separate mb_str() overload
  1557. #if wxUSE_UTF8_LOCALE_ONLY
  1558. const char* mb_str() const { return wx_str(); }
  1559. const wxScopedCharBuffer mb_str(const wxMBConv& conv) const
  1560. {
  1561. return AsCharBuf(conv);
  1562. }
  1563. #else // !wxUSE_UTF8_LOCALE_ONLY
  1564. const wxScopedCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const
  1565. {
  1566. return AsCharBuf(conv);
  1567. }
  1568. #endif // wxUSE_UTF8_LOCALE_ONLY/!wxUSE_UTF8_LOCALE_ONLY
  1569. const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
  1570. #if wxUSE_UNICODE_WCHAR
  1571. const wchar_t* wc_str() const { return wx_str(); }
  1572. #elif wxUSE_UNICODE_UTF8
  1573. const wxScopedWCharBuffer wc_str() const
  1574. { return AsWCharBuf(wxMBConvStrictUTF8()); }
  1575. #endif
  1576. // for compatibility with !wxUSE_UNICODE version
  1577. const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
  1578. { return wc_str(); }
  1579. #if wxMBFILES
  1580. const wxScopedCharBuffer fn_str() const { return mb_str(wxConvFile); }
  1581. #else // !wxMBFILES
  1582. const wxWX2WCbuf fn_str() const { return wc_str(); }
  1583. #endif // wxMBFILES/!wxMBFILES
  1584. #else // ANSI
  1585. const char* mb_str() const { return wx_str(); }
  1586. // for compatibility with wxUSE_UNICODE version
  1587. const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
  1588. const wxWX2MBbuf mbc_str() const { return mb_str(); }
  1589. const wxScopedWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const
  1590. { return AsWCharBuf(conv); }
  1591. const wxScopedCharBuffer fn_str() const
  1592. { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
  1593. #endif // Unicode/ANSI
  1594. #if wxUSE_UNICODE_UTF8
  1595. const wxScopedWCharBuffer t_str() const { return wc_str(); }
  1596. #elif wxUSE_UNICODE_WCHAR
  1597. const wchar_t* t_str() const { return wx_str(); }
  1598. #else
  1599. const char* t_str() const { return wx_str(); }
  1600. #endif
  1601. // overloaded assignment
  1602. // from another wxString
  1603. wxString& operator=(const wxString& stringSrc)
  1604. {
  1605. if ( this != &stringSrc )
  1606. {
  1607. wxSTRING_INVALIDATE_CACHE();
  1608. m_impl = stringSrc.m_impl;
  1609. }
  1610. return *this;
  1611. }
  1612. wxString& operator=(const wxCStrData& cstr)
  1613. { return *this = cstr.AsString(); }
  1614. // from a character
  1615. wxString& operator=(wxUniChar ch)
  1616. {
  1617. wxSTRING_INVALIDATE_CACHE();
  1618. #if wxUSE_UNICODE_UTF8
  1619. if ( !ch.IsAscii() )
  1620. m_impl = wxStringOperations::EncodeChar(ch);
  1621. else
  1622. #endif // wxUSE_UNICODE_UTF8
  1623. m_impl = (wxStringCharType)ch;
  1624. return *this;
  1625. }
  1626. wxString& operator=(wxUniCharRef ch)
  1627. { return operator=((wxUniChar)ch); }
  1628. wxString& operator=(char ch)
  1629. { return operator=(wxUniChar(ch)); }
  1630. wxString& operator=(unsigned char ch)
  1631. { return operator=(wxUniChar(ch)); }
  1632. wxString& operator=(wchar_t ch)
  1633. { return operator=(wxUniChar(ch)); }
  1634. // from a C string - STL probably will crash on NULL,
  1635. // so we need to compensate in that case
  1636. #if wxUSE_STL_BASED_WXSTRING
  1637. wxString& operator=(const char *psz)
  1638. {
  1639. wxSTRING_INVALIDATE_CACHE();
  1640. if ( psz )
  1641. m_impl = ImplStr(psz);
  1642. else
  1643. clear();
  1644. return *this;
  1645. }
  1646. wxString& operator=(const wchar_t *pwz)
  1647. {
  1648. wxSTRING_INVALIDATE_CACHE();
  1649. if ( pwz )
  1650. m_impl = ImplStr(pwz);
  1651. else
  1652. clear();
  1653. return *this;
  1654. }
  1655. #else // !wxUSE_STL_BASED_WXSTRING
  1656. wxString& operator=(const char *psz)
  1657. {
  1658. wxSTRING_INVALIDATE_CACHE();
  1659. m_impl = ImplStr(psz);
  1660. return *this;
  1661. }
  1662. wxString& operator=(const wchar_t *pwz)
  1663. {
  1664. wxSTRING_INVALIDATE_CACHE();
  1665. m_impl = ImplStr(pwz);
  1666. return *this;
  1667. }
  1668. #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
  1669. wxString& operator=(const unsigned char *psz)
  1670. { return operator=((const char*)psz); }
  1671. // from wxScopedWCharBuffer
  1672. wxString& operator=(const wxScopedWCharBuffer& s)
  1673. { return assign(s); }
  1674. // from wxScopedCharBuffer
  1675. wxString& operator=(const wxScopedCharBuffer& s)
  1676. { return assign(s); }
  1677. // string concatenation
  1678. // in place concatenation
  1679. /*
  1680. Concatenate and return the result. Note that the left to right
  1681. associativity of << allows to write things like "str << str1 << str2
  1682. << ..." (unlike with +=)
  1683. */
  1684. // string += string
  1685. wxString& operator<<(const wxString& s)
  1686. {
  1687. #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
  1688. wxASSERT_MSG( s.IsValid(),
  1689. wxT("did you forget to call UngetWriteBuf()?") );
  1690. #endif
  1691. append(s);
  1692. return *this;
  1693. }
  1694. // string += C string
  1695. wxString& operator<<(const char *psz)
  1696. { append(psz); return *this; }
  1697. wxString& operator<<(const wchar_t *pwz)
  1698. { append(pwz); return *this; }
  1699. wxString& operator<<(const wxCStrData& psz)
  1700. { append(psz.AsString()); return *this; }
  1701. // string += char
  1702. wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
  1703. wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
  1704. wxString& operator<<(char ch) { append(1, ch); return *this; }
  1705. wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
  1706. wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
  1707. // string += buffer (i.e. from wxGetString)
  1708. wxString& operator<<(const wxScopedWCharBuffer& s)
  1709. { return append(s); }
  1710. wxString& operator<<(const wxScopedCharBuffer& s)
  1711. { return append(s); }
  1712. // string += C string
  1713. wxString& Append(const wxString& s)
  1714. {
  1715. // test for empty() to share the string if possible
  1716. if ( empty() )
  1717. *this = s;
  1718. else
  1719. append(s);
  1720. return *this;
  1721. }
  1722. wxString& Append(const char* psz)
  1723. { append(psz); return *this; }
  1724. wxString& Append(const wchar_t* pwz)
  1725. { append(pwz); return *this; }
  1726. wxString& Append(const wxCStrData& psz)
  1727. { append(psz); return *this; }
  1728. wxString& Append(const wxScopedCharBuffer& psz)
  1729. { append(psz); return *this; }
  1730. wxString& Append(const wxScopedWCharBuffer& psz)
  1731. { append(psz); return *this; }
  1732. wxString& Append(const char* psz, size_t nLen)
  1733. { append(psz, nLen); return *this; }
  1734. wxString& Append(const wchar_t* pwz, size_t nLen)
  1735. { append(pwz, nLen); return *this; }
  1736. wxString& Append(const wxCStrData& psz, size_t nLen)
  1737. { append(psz, nLen); return *this; }
  1738. wxString& Append(const wxScopedCharBuffer& psz, size_t nLen)
  1739. { append(psz, nLen); return *this; }
  1740. wxString& Append(const wxScopedWCharBuffer& psz, size_t nLen)
  1741. { append(psz, nLen); return *this; }
  1742. // append count copies of given character
  1743. wxString& Append(wxUniChar ch, size_t count = 1u)
  1744. { append(count, ch); return *this; }
  1745. wxString& Append(wxUniCharRef ch, size_t count = 1u)
  1746. { append(count, ch); return *this; }
  1747. wxString& Append(char ch, size_t count = 1u)
  1748. { append(count, ch); return *this; }
  1749. wxString& Append(unsigned char ch, size_t count = 1u)
  1750. { append(count, ch); return *this; }
  1751. wxString& Append(wchar_t ch, size_t count = 1u)
  1752. { append(count, ch); return *this; }
  1753. // prepend a string, return the string itself
  1754. wxString& Prepend(const wxString& str)
  1755. { *this = str + *this; return *this; }
  1756. // non-destructive concatenation
  1757. // two strings
  1758. friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
  1759. const wxString& string2);
  1760. // string with a single char
  1761. friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
  1762. // char with a string
  1763. friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
  1764. // string with C string
  1765. friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
  1766. const char *psz);
  1767. friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
  1768. const wchar_t *pwz);
  1769. // C string with string
  1770. friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
  1771. const wxString& string);
  1772. friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
  1773. const wxString& string);
  1774. // stream-like functions
  1775. // insert an int into string
  1776. wxString& operator<<(int i)
  1777. { return (*this) << Format(wxT("%d"), i); }
  1778. // insert an unsigned int into string
  1779. wxString& operator<<(unsigned int ui)
  1780. { return (*this) << Format(wxT("%u"), ui); }
  1781. // insert a long into string
  1782. wxString& operator<<(long l)
  1783. { return (*this) << Format(wxT("%ld"), l); }
  1784. // insert an unsigned long into string
  1785. wxString& operator<<(unsigned long ul)
  1786. { return (*this) << Format(wxT("%lu"), ul); }
  1787. #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  1788. // insert a long long if they exist and aren't longs
  1789. wxString& operator<<(wxLongLong_t ll)
  1790. {
  1791. return (*this) << Format("%" wxLongLongFmtSpec "d", ll);
  1792. }
  1793. // insert an unsigned long long
  1794. wxString& operator<<(wxULongLong_t ull)
  1795. {
  1796. return (*this) << Format("%" wxLongLongFmtSpec "u" , ull);
  1797. }
  1798. #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
  1799. // insert a float into string
  1800. wxString& operator<<(float f)
  1801. { return (*this) << Format(wxT("%f"), f); }
  1802. // insert a double into string
  1803. wxString& operator<<(double d)
  1804. { return (*this) << Format(wxT("%g"), d); }
  1805. // string comparison
  1806. // case-sensitive comparison (returns a value < 0, = 0 or > 0)
  1807. int Cmp(const char *psz) const
  1808. { return compare(psz); }
  1809. int Cmp(const wchar_t *pwz) const
  1810. { return compare(pwz); }
  1811. int Cmp(const wxString& s) const
  1812. { return compare(s); }
  1813. int Cmp(const wxCStrData& s) const
  1814. { return compare(s); }
  1815. int Cmp(const wxScopedCharBuffer& s) const
  1816. { return compare(s); }
  1817. int Cmp(const wxScopedWCharBuffer& s) const
  1818. { return compare(s); }
  1819. // same as Cmp() but not case-sensitive
  1820. int CmpNoCase(const wxString& s) const;
  1821. // test for the string equality, either considering case or not
  1822. // (if compareWithCase then the case matters)
  1823. bool IsSameAs(const wxString& str, bool compareWithCase = true) const
  1824. {
  1825. #if !wxUSE_UNICODE_UTF8
  1826. // in UTF-8 build, length() is O(n) and doing this would be _slower_
  1827. if ( length() != str.length() )
  1828. return false;
  1829. #endif
  1830. return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
  1831. }
  1832. bool IsSameAs(const char *str, bool compareWithCase = true) const
  1833. { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
  1834. bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
  1835. { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
  1836. bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
  1837. { return IsSameAs(str.AsString(), compareWithCase); }
  1838. bool IsSameAs(const wxScopedCharBuffer& str, bool compareWithCase = true) const
  1839. { return IsSameAs(str.data(), compareWithCase); }
  1840. bool IsSameAs(const wxScopedWCharBuffer& str, bool compareWithCase = true) const
  1841. { return IsSameAs(str.data(), compareWithCase); }
  1842. // comparison with a single character: returns true if equal
  1843. bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
  1844. // FIXME-UTF8: remove these overloads
  1845. bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
  1846. { return IsSameAs(wxUniChar(c), compareWithCase); }
  1847. bool IsSameAs(char c, bool compareWithCase = true) const
  1848. { return IsSameAs(wxUniChar(c), compareWithCase); }
  1849. bool IsSameAs(unsigned char c, bool compareWithCase = true) const
  1850. { return IsSameAs(wxUniChar(c), compareWithCase); }
  1851. bool IsSameAs(wchar_t c, bool compareWithCase = true) const
  1852. { return IsSameAs(wxUniChar(c), compareWithCase); }
  1853. bool IsSameAs(int c, bool compareWithCase = true) const
  1854. { return IsSameAs(wxUniChar(c), compareWithCase); }
  1855. // simple sub-string extraction
  1856. // return substring starting at nFirst of length nCount (or till the end
  1857. // if nCount = default value)
  1858. wxString Mid(size_t nFirst, size_t nCount = npos) const;
  1859. // operator version of Mid()
  1860. wxString operator()(size_t start, size_t len) const
  1861. { return Mid(start, len); }
  1862. // check if the string starts with the given prefix and return the rest
  1863. // of the string in the provided pointer if it is not NULL; otherwise
  1864. // return false
  1865. bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
  1866. // check if the string ends with the given suffix and return the
  1867. // beginning of the string before the suffix in the provided pointer if
  1868. // it is not NULL; otherwise return false
  1869. bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
  1870. // get first nCount characters
  1871. wxString Left(size_t nCount) const;
  1872. // get last nCount characters
  1873. wxString Right(size_t nCount) const;
  1874. // get all characters before the first occurrence of ch
  1875. // (returns the whole string if ch not found) and also put everything
  1876. // following the first occurrence of ch into rest if it's non-NULL
  1877. wxString BeforeFirst(wxUniChar ch, wxString *rest = NULL) const;
  1878. // get all characters before the last occurrence of ch
  1879. // (returns empty string if ch not found) and also put everything
  1880. // following the last occurrence of ch into rest if it's non-NULL
  1881. wxString BeforeLast(wxUniChar ch, wxString *rest = NULL) const;
  1882. // get all characters after the first occurrence of ch
  1883. // (returns empty string if ch not found)
  1884. wxString AfterFirst(wxUniChar ch) const;
  1885. // get all characters after the last occurrence of ch
  1886. // (returns the whole string if ch not found)
  1887. wxString AfterLast(wxUniChar ch) const;
  1888. // for compatibility only, use more explicitly named functions above
  1889. wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
  1890. wxString After(wxUniChar ch) const { return AfterFirst(ch); }
  1891. // case conversion
  1892. // convert to upper case in place, return the string itself
  1893. wxString& MakeUpper();
  1894. // convert to upper case, return the copy of the string
  1895. wxString Upper() const { return wxString(*this).MakeUpper(); }
  1896. // convert to lower case in place, return the string itself
  1897. wxString& MakeLower();
  1898. // convert to lower case, return the copy of the string
  1899. wxString Lower() const { return wxString(*this).MakeLower(); }
  1900. // convert the first character to the upper case and the rest to the
  1901. // lower one, return the modified string itself
  1902. wxString& MakeCapitalized();
  1903. // convert the first character to the upper case and the rest to the
  1904. // lower one, return the copy of the string
  1905. wxString Capitalize() const { return wxString(*this).MakeCapitalized(); }
  1906. // trimming/padding whitespace (either side) and truncating
  1907. // remove spaces from left or from right (default) side
  1908. wxString& Trim(bool bFromRight = true);
  1909. // add nCount copies chPad in the beginning or at the end (default)
  1910. wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
  1911. // searching and replacing
  1912. // searching (return starting index, or -1 if not found)
  1913. int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
  1914. int Find(wxUniCharRef ch, bool bFromEnd = false) const
  1915. { return Find(wxUniChar(ch), bFromEnd); }
  1916. int Find(char ch, bool bFromEnd = false) const
  1917. { return Find(wxUniChar(ch), bFromEnd); }
  1918. int Find(unsigned char ch, bool bFromEnd = false) const
  1919. { return Find(wxUniChar(ch), bFromEnd); }
  1920. int Find(wchar_t ch, bool bFromEnd = false) const
  1921. { return Find(wxUniChar(ch), bFromEnd); }
  1922. // searching (return starting index, or -1 if not found)
  1923. int Find(const wxString& sub) const // like strstr
  1924. {
  1925. size_type idx = find(sub);
  1926. return (idx == npos) ? wxNOT_FOUND : (int)idx;
  1927. }
  1928. int Find(const char *sub) const // like strstr
  1929. {
  1930. size_type idx = find(sub);
  1931. return (idx == npos) ? wxNOT_FOUND : (int)idx;
  1932. }
  1933. int Find(const wchar_t *sub) const // like strstr
  1934. {
  1935. size_type idx = find(sub);
  1936. return (idx == npos) ? wxNOT_FOUND : (int)idx;
  1937. }
  1938. int Find(const wxCStrData& sub) const
  1939. { return Find(sub.AsString()); }
  1940. int Find(const wxScopedCharBuffer& sub) const
  1941. { return Find(sub.data()); }
  1942. int Find(const wxScopedWCharBuffer& sub) const
  1943. { return Find(sub.data()); }
  1944. // replace first (or all of bReplaceAll) occurrences of substring with
  1945. // another string, returns the number of replacements made
  1946. size_t Replace(const wxString& strOld,
  1947. const wxString& strNew,
  1948. bool bReplaceAll = true);
  1949. // check if the string contents matches a mask containing '*' and '?'
  1950. bool Matches(const wxString& mask) const;
  1951. // conversion to numbers: all functions return true only if the whole
  1952. // string is a number and put the value of this number into the pointer
  1953. // provided, the base is the numeric base in which the conversion should be
  1954. // done and must be comprised between 2 and 36 or be 0 in which case the
  1955. // standard C rules apply (leading '0' => octal, "0x" => hex)
  1956. // convert to a signed integer
  1957. bool ToLong(long *val, int base = 10) const;
  1958. // convert to an unsigned integer
  1959. bool ToULong(unsigned long *val, int base = 10) const;
  1960. // convert to wxLongLong
  1961. #if defined(wxLongLong_t)
  1962. bool ToLongLong(wxLongLong_t *val, int base = 10) const;
  1963. // convert to wxULongLong
  1964. bool ToULongLong(wxULongLong_t *val, int base = 10) const;
  1965. #endif // wxLongLong_t
  1966. // convert to a double
  1967. bool ToDouble(double *val) const;
  1968. // conversions to numbers using C locale
  1969. // convert to a signed integer
  1970. bool ToCLong(long *val, int base = 10) const;
  1971. // convert to an unsigned integer
  1972. bool ToCULong(unsigned long *val, int base = 10) const;
  1973. // convert to a double
  1974. bool ToCDouble(double *val) const;
  1975. // create a string representing the given floating point number with the
  1976. // default (like %g) or fixed (if precision >=0) precision
  1977. // in the current locale
  1978. static wxString FromDouble(double val, int precision = -1);
  1979. // in C locale
  1980. static wxString FromCDouble(double val, int precision = -1);
  1981. #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
  1982. // formatted input/output
  1983. // as sprintf(), returns the number of characters written or < 0 on error
  1984. // (take 'this' into account in attribute parameter count)
  1985. // int Printf(const wxString& format, ...);
  1986. WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
  1987. DoPrintfWchar, DoPrintfUtf8)
  1988. #ifdef __WATCOMC__
  1989. // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
  1990. WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
  1991. (wxFormatString(f1)));
  1992. WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
  1993. (wxFormatString(f1)));
  1994. WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
  1995. (wxFormatString(f1)));
  1996. WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
  1997. (wxFormatString(f1)));
  1998. #endif
  1999. #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
  2000. // as vprintf(), returns the number of characters written or < 0 on error
  2001. int PrintfV(const wxString& format, va_list argptr);
  2002. #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
  2003. // returns the string containing the result of Printf() to it
  2004. // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
  2005. WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
  2006. DoFormatWchar, DoFormatUtf8)
  2007. #ifdef __WATCOMC__
  2008. // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
  2009. WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
  2010. (wxFormatString(f1)));
  2011. WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
  2012. (wxFormatString(f1)));
  2013. WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
  2014. (wxFormatString(f1)));
  2015. WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
  2016. (wxFormatString(f1)));
  2017. #endif
  2018. #endif
  2019. // the same as above, but takes a va_list
  2020. static wxString FormatV(const wxString& format, va_list argptr);
  2021. // raw access to string memory
  2022. // ensure that string has space for at least nLen characters
  2023. // only works if the data of this string is not shared
  2024. bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; }
  2025. // minimize the string's memory
  2026. // only works if the data of this string is not shared
  2027. bool Shrink();
  2028. #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
  2029. // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
  2030. //
  2031. // get writable buffer of at least nLen bytes. Unget() *must* be called
  2032. // a.s.a.p. to put string back in a reasonable state!
  2033. wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
  2034. // call this immediately after GetWriteBuf() has been used
  2035. wxDEPRECATED( void UngetWriteBuf() );
  2036. wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
  2037. #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
  2038. // wxWidgets version 1 compatibility functions
  2039. // use Mid()
  2040. wxString SubString(size_t from, size_t to) const
  2041. { return Mid(from, (to - from + 1)); }
  2042. // values for second parameter of CompareTo function
  2043. enum caseCompare {exact, ignoreCase};
  2044. // values for first parameter of Strip function
  2045. enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
  2046. #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
  2047. // use Printf()
  2048. // (take 'this' into account in attribute parameter count)
  2049. // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
  2050. WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
  2051. DoPrintfWchar, DoPrintfUtf8)
  2052. #ifdef __WATCOMC__
  2053. // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
  2054. WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
  2055. (wxFormatString(f1)));
  2056. WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
  2057. (wxFormatString(f1)));
  2058. WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
  2059. (wxFormatString(f1)));
  2060. WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
  2061. (wxFormatString(f1)));
  2062. #endif
  2063. #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
  2064. // use Cmp()
  2065. int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
  2066. { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
  2067. // use length()
  2068. size_t Length() const { return length(); }
  2069. // Count the number of characters
  2070. int Freq(wxUniChar ch) const;
  2071. // use MakeLower
  2072. void LowerCase() { MakeLower(); }
  2073. // use MakeUpper
  2074. void UpperCase() { MakeUpper(); }
  2075. // use Trim except that it doesn't change this string
  2076. wxString Strip(stripType w = trailing) const;
  2077. // use Find (more general variants not yet supported)
  2078. size_t Index(const wxChar* psz) const { return Find(psz); }
  2079. size_t Index(wxUniChar ch) const { return Find(ch); }
  2080. // use Truncate
  2081. wxString& Remove(size_t pos) { return Truncate(pos); }
  2082. wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
  2083. wxString& Remove(size_t nStart, size_t nLen)
  2084. { return (wxString&)erase( nStart, nLen ); }
  2085. // use Find()
  2086. int First( wxUniChar ch ) const { return Find(ch); }
  2087. int First( wxUniCharRef ch ) const { return Find(ch); }
  2088. int First( char ch ) const { return Find(ch); }
  2089. int First( unsigned char ch ) const { return Find(ch); }
  2090. int First( wchar_t ch ) const { return Find(ch); }
  2091. int First( const wxString& str ) const { return Find(str); }
  2092. int Last( wxUniChar ch ) const { return Find(ch, true); }
  2093. bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
  2094. // use empty()
  2095. bool IsNull() const { return empty(); }
  2096. // std::string compatibility functions
  2097. // take nLen chars starting at nPos
  2098. wxString(const wxString& str, size_t nPos, size_t nLen)
  2099. { assign(str, nPos, nLen); }
  2100. // take all characters from first to last
  2101. wxString(const_iterator first, const_iterator last)
  2102. : m_impl(first.impl(), last.impl()) { }
  2103. #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2104. // the 2 overloads below are for compatibility with the existing code using
  2105. // pointers instead of iterators
  2106. wxString(const char *first, const char *last)
  2107. {
  2108. SubstrBufFromMB str(ImplStr(first, last - first));
  2109. m_impl.assign(str.data, str.len);
  2110. }
  2111. wxString(const wchar_t *first, const wchar_t *last)
  2112. {
  2113. SubstrBufFromWC str(ImplStr(first, last - first));
  2114. m_impl.assign(str.data, str.len);
  2115. }
  2116. // and this one is needed to compile code adding offsets to c_str() result
  2117. wxString(const wxCStrData& first, const wxCStrData& last)
  2118. : m_impl(CreateConstIterator(first).impl(),
  2119. CreateConstIterator(last).impl())
  2120. {
  2121. wxASSERT_MSG( first.m_str == last.m_str,
  2122. wxT("pointers must be into the same string") );
  2123. }
  2124. #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2125. // lib.string.modifiers
  2126. // append elements str[pos], ..., str[pos+n]
  2127. wxString& append(const wxString& str, size_t pos, size_t n)
  2128. {
  2129. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2130. size_t from, len;
  2131. str.PosLenToImpl(pos, n, &from, &len);
  2132. m_impl.append(str.m_impl, from, len);
  2133. return *this;
  2134. }
  2135. // append a string
  2136. wxString& append(const wxString& str)
  2137. {
  2138. wxSTRING_UPDATE_CACHED_LENGTH(str.length());
  2139. m_impl.append(str.m_impl);
  2140. return *this;
  2141. }
  2142. // append first n (or all if n == npos) characters of sz
  2143. wxString& append(const char *sz)
  2144. {
  2145. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2146. m_impl.append(ImplStr(sz));
  2147. return *this;
  2148. }
  2149. wxString& append(const wchar_t *sz)
  2150. {
  2151. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2152. m_impl.append(ImplStr(sz));
  2153. return *this;
  2154. }
  2155. wxString& append(const char *sz, size_t n)
  2156. {
  2157. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2158. SubstrBufFromMB str(ImplStr(sz, n));
  2159. m_impl.append(str.data, str.len);
  2160. return *this;
  2161. }
  2162. wxString& append(const wchar_t *sz, size_t n)
  2163. {
  2164. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2165. SubstrBufFromWC str(ImplStr(sz, n));
  2166. m_impl.append(str.data, str.len);
  2167. return *this;
  2168. }
  2169. wxString& append(const wxCStrData& str)
  2170. { return append(str.AsString()); }
  2171. wxString& append(const wxScopedCharBuffer& str)
  2172. { return append(str.data(), str.length()); }
  2173. wxString& append(const wxScopedWCharBuffer& str)
  2174. { return append(str.data(), str.length()); }
  2175. wxString& append(const wxCStrData& str, size_t n)
  2176. { return append(str.AsString(), 0, n); }
  2177. wxString& append(const wxScopedCharBuffer& str, size_t n)
  2178. { return append(str.data(), n); }
  2179. wxString& append(const wxScopedWCharBuffer& str, size_t n)
  2180. { return append(str.data(), n); }
  2181. // append n copies of ch
  2182. wxString& append(size_t n, wxUniChar ch)
  2183. {
  2184. #if wxUSE_UNICODE_UTF8
  2185. if ( !ch.IsAscii() )
  2186. {
  2187. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2188. m_impl.append(wxStringOperations::EncodeNChars(n, ch));
  2189. }
  2190. else // ASCII
  2191. #endif
  2192. {
  2193. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2194. m_impl.append(n, (wxStringCharType)ch);
  2195. }
  2196. return *this;
  2197. }
  2198. wxString& append(size_t n, wxUniCharRef ch)
  2199. { return append(n, wxUniChar(ch)); }
  2200. wxString& append(size_t n, char ch)
  2201. { return append(n, wxUniChar(ch)); }
  2202. wxString& append(size_t n, unsigned char ch)
  2203. { return append(n, wxUniChar(ch)); }
  2204. wxString& append(size_t n, wchar_t ch)
  2205. { return append(n, wxUniChar(ch)); }
  2206. // append from first to last
  2207. wxString& append(const_iterator first, const_iterator last)
  2208. {
  2209. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2210. m_impl.append(first.impl(), last.impl());
  2211. return *this;
  2212. }
  2213. #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2214. wxString& append(const char *first, const char *last)
  2215. { return append(first, last - first); }
  2216. wxString& append(const wchar_t *first, const wchar_t *last)
  2217. { return append(first, last - first); }
  2218. wxString& append(const wxCStrData& first, const wxCStrData& last)
  2219. { return append(CreateConstIterator(first), CreateConstIterator(last)); }
  2220. #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2221. // same as `this_string = str'
  2222. wxString& assign(const wxString& str)
  2223. {
  2224. wxSTRING_SET_CACHED_LENGTH(str.length());
  2225. m_impl = str.m_impl;
  2226. return *this;
  2227. }
  2228. // This is a non-standard-compliant overload taking the first "len"
  2229. // characters of the source string.
  2230. wxString& assign(const wxString& str, size_t len)
  2231. {
  2232. #if wxUSE_STRING_POS_CACHE
  2233. // It is legal to pass len > str.length() to wxStringImpl::assign() but
  2234. // by restricting it here we save some work for that function so it's not
  2235. // really less efficient and, at the same time, ensure that we don't
  2236. // cache invalid length.
  2237. const size_t lenSrc = str.length();
  2238. if ( len > lenSrc )
  2239. len = lenSrc;
  2240. wxSTRING_SET_CACHED_LENGTH(len);
  2241. #endif // wxUSE_STRING_POS_CACHE
  2242. m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
  2243. return *this;
  2244. }
  2245. // same as ` = str[pos..pos + n]
  2246. wxString& assign(const wxString& str, size_t pos, size_t n)
  2247. {
  2248. size_t from, len;
  2249. str.PosLenToImpl(pos, n, &from, &len);
  2250. m_impl.assign(str.m_impl, from, len);
  2251. // it's important to call this after PosLenToImpl() above in case str is
  2252. // the same string as this one
  2253. wxSTRING_SET_CACHED_LENGTH(n);
  2254. return *this;
  2255. }
  2256. // same as `= first n (or all if n == npos) characters of sz'
  2257. wxString& assign(const char *sz)
  2258. {
  2259. wxSTRING_INVALIDATE_CACHE();
  2260. m_impl.assign(ImplStr(sz));
  2261. return *this;
  2262. }
  2263. wxString& assign(const wchar_t *sz)
  2264. {
  2265. wxSTRING_INVALIDATE_CACHE();
  2266. m_impl.assign(ImplStr(sz));
  2267. return *this;
  2268. }
  2269. wxString& assign(const char *sz, size_t n)
  2270. {
  2271. wxSTRING_INVALIDATE_CACHE();
  2272. SubstrBufFromMB str(ImplStr(sz, n));
  2273. m_impl.assign(str.data, str.len);
  2274. return *this;
  2275. }
  2276. wxString& assign(const wchar_t *sz, size_t n)
  2277. {
  2278. wxSTRING_SET_CACHED_LENGTH(n);
  2279. SubstrBufFromWC str(ImplStr(sz, n));
  2280. m_impl.assign(str.data, str.len);
  2281. return *this;
  2282. }
  2283. wxString& assign(const wxCStrData& str)
  2284. { return assign(str.AsString()); }
  2285. wxString& assign(const wxScopedCharBuffer& str)
  2286. { return assign(str.data(), str.length()); }
  2287. wxString& assign(const wxScopedWCharBuffer& str)
  2288. { return assign(str.data(), str.length()); }
  2289. wxString& assign(const wxCStrData& str, size_t len)
  2290. { return assign(str.AsString(), len); }
  2291. wxString& assign(const wxScopedCharBuffer& str, size_t len)
  2292. { return assign(str.data(), len); }
  2293. wxString& assign(const wxScopedWCharBuffer& str, size_t len)
  2294. { return assign(str.data(), len); }
  2295. // same as `= n copies of ch'
  2296. wxString& assign(size_t n, wxUniChar ch)
  2297. {
  2298. wxSTRING_SET_CACHED_LENGTH(n);
  2299. #if wxUSE_UNICODE_UTF8
  2300. if ( !ch.IsAscii() )
  2301. m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
  2302. else
  2303. #endif
  2304. m_impl.assign(n, (wxStringCharType)ch);
  2305. return *this;
  2306. }
  2307. wxString& assign(size_t n, wxUniCharRef ch)
  2308. { return assign(n, wxUniChar(ch)); }
  2309. wxString& assign(size_t n, char ch)
  2310. { return assign(n, wxUniChar(ch)); }
  2311. wxString& assign(size_t n, unsigned char ch)
  2312. { return assign(n, wxUniChar(ch)); }
  2313. wxString& assign(size_t n, wchar_t ch)
  2314. { return assign(n, wxUniChar(ch)); }
  2315. // assign from first to last
  2316. wxString& assign(const_iterator first, const_iterator last)
  2317. {
  2318. wxSTRING_INVALIDATE_CACHE();
  2319. m_impl.assign(first.impl(), last.impl());
  2320. return *this;
  2321. }
  2322. #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2323. wxString& assign(const char *first, const char *last)
  2324. { return assign(first, last - first); }
  2325. wxString& assign(const wchar_t *first, const wchar_t *last)
  2326. { return assign(first, last - first); }
  2327. wxString& assign(const wxCStrData& first, const wxCStrData& last)
  2328. { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
  2329. #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2330. // string comparison
  2331. int compare(const wxString& str) const;
  2332. int compare(const char* sz) const;
  2333. int compare(const wchar_t* sz) const;
  2334. int compare(const wxCStrData& str) const
  2335. { return compare(str.AsString()); }
  2336. int compare(const wxScopedCharBuffer& str) const
  2337. { return compare(str.data()); }
  2338. int compare(const wxScopedWCharBuffer& str) const
  2339. { return compare(str.data()); }
  2340. // comparison with a substring
  2341. int compare(size_t nStart, size_t nLen, const wxString& str) const;
  2342. // comparison of 2 substrings
  2343. int compare(size_t nStart, size_t nLen,
  2344. const wxString& str, size_t nStart2, size_t nLen2) const;
  2345. // substring comparison with first nCount characters of sz
  2346. int compare(size_t nStart, size_t nLen,
  2347. const char* sz, size_t nCount = npos) const;
  2348. int compare(size_t nStart, size_t nLen,
  2349. const wchar_t* sz, size_t nCount = npos) const;
  2350. // insert another string
  2351. wxString& insert(size_t nPos, const wxString& str)
  2352. { insert(GetIterForNthChar(nPos), str.begin(), str.end()); return *this; }
  2353. // insert n chars of str starting at nStart (in str)
  2354. wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
  2355. {
  2356. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2357. size_t from, len;
  2358. str.PosLenToImpl(nStart, n, &from, &len);
  2359. m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
  2360. return *this;
  2361. }
  2362. // insert first n (or all if n == npos) characters of sz
  2363. wxString& insert(size_t nPos, const char *sz)
  2364. {
  2365. wxSTRING_INVALIDATE_CACHE();
  2366. m_impl.insert(PosToImpl(nPos), ImplStr(sz));
  2367. return *this;
  2368. }
  2369. wxString& insert(size_t nPos, const wchar_t *sz)
  2370. {
  2371. wxSTRING_INVALIDATE_CACHE();
  2372. m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this;
  2373. }
  2374. wxString& insert(size_t nPos, const char *sz, size_t n)
  2375. {
  2376. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2377. SubstrBufFromMB str(ImplStr(sz, n));
  2378. m_impl.insert(PosToImpl(nPos), str.data, str.len);
  2379. return *this;
  2380. }
  2381. wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
  2382. {
  2383. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2384. SubstrBufFromWC str(ImplStr(sz, n));
  2385. m_impl.insert(PosToImpl(nPos), str.data, str.len);
  2386. return *this;
  2387. }
  2388. // insert n copies of ch
  2389. wxString& insert(size_t nPos, size_t n, wxUniChar ch)
  2390. {
  2391. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2392. #if wxUSE_UNICODE_UTF8
  2393. if ( !ch.IsAscii() )
  2394. m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
  2395. else
  2396. #endif
  2397. m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
  2398. return *this;
  2399. }
  2400. iterator insert(iterator it, wxUniChar ch)
  2401. {
  2402. wxSTRING_UPDATE_CACHED_LENGTH(1);
  2403. #if wxUSE_UNICODE_UTF8
  2404. if ( !ch.IsAscii() )
  2405. {
  2406. size_t pos = IterToImplPos(it);
  2407. m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
  2408. return iterator(this, m_impl.begin() + pos);
  2409. }
  2410. else
  2411. #endif
  2412. return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
  2413. }
  2414. void insert(iterator it, const_iterator first, const_iterator last)
  2415. {
  2416. wxSTRING_INVALIDATE_CACHE();
  2417. m_impl.insert(it.impl(), first.impl(), last.impl());
  2418. }
  2419. #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2420. void insert(iterator it, const char *first, const char *last)
  2421. { insert(it - begin(), first, last - first); }
  2422. void insert(iterator it, const wchar_t *first, const wchar_t *last)
  2423. { insert(it - begin(), first, last - first); }
  2424. void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
  2425. { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
  2426. #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
  2427. void insert(iterator it, size_type n, wxUniChar ch)
  2428. {
  2429. wxSTRING_UPDATE_CACHED_LENGTH(n);
  2430. #if wxUSE_UNICODE_UTF8
  2431. if ( !ch.IsAscii() )
  2432. m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
  2433. else
  2434. #endif
  2435. m_impl.insert(it.impl(), n, (wxStringCharType)ch);
  2436. }
  2437. // delete characters from nStart to nStart + nLen
  2438. wxString& erase(size_type pos = 0, size_type n = npos)
  2439. {
  2440. wxSTRING_INVALIDATE_CACHE();
  2441. size_t from, len;
  2442. PosLenToImpl(pos, n, &from, &len);
  2443. m_impl.erase(from, len);
  2444. return *this;
  2445. }
  2446. // delete characters from first up to last
  2447. iterator erase(iterator first, iterator last)
  2448. {
  2449. wxSTRING_INVALIDATE_CACHE();
  2450. return iterator(this, m_impl.erase(first.impl(), last.impl()));
  2451. }
  2452. iterator erase(iterator first)
  2453. {
  2454. wxSTRING_UPDATE_CACHED_LENGTH(-1);
  2455. return iterator(this, m_impl.erase(first.impl()));
  2456. }
  2457. #ifdef wxSTRING_BASE_HASNT_CLEAR
  2458. void clear() { erase(); }
  2459. #else
  2460. void clear()
  2461. {
  2462. wxSTRING_SET_CACHED_LENGTH(0);
  2463. m_impl.clear();
  2464. }
  2465. #endif
  2466. // replaces the substring of length nLen starting at nStart
  2467. wxString& replace(size_t nStart, size_t nLen, const char* sz)
  2468. {
  2469. wxSTRING_INVALIDATE_CACHE();
  2470. size_t from, len;
  2471. PosLenToImpl(nStart, nLen, &from, &len);
  2472. m_impl.replace(from, len, ImplStr(sz));
  2473. return *this;
  2474. }
  2475. wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
  2476. {
  2477. wxSTRING_INVALIDATE_CACHE();
  2478. size_t from, len;
  2479. PosLenToImpl(nStart, nLen, &from, &len);
  2480. m_impl.replace(from, len, ImplStr(sz));
  2481. return *this;
  2482. }
  2483. // replaces the substring of length nLen starting at nStart
  2484. wxString& replace(size_t nStart, size_t nLen, const wxString& str)
  2485. {
  2486. wxSTRING_INVALIDATE_CACHE();
  2487. size_t from, len;
  2488. PosLenToImpl(nStart, nLen, &from, &len);
  2489. m_impl.replace(from, len, str.m_impl);
  2490. return *this;
  2491. }
  2492. // replaces the substring with nCount copies of ch
  2493. wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
  2494. {
  2495. wxSTRING_INVALIDATE_CACHE();
  2496. size_t from, len;
  2497. PosLenToImpl(nStart, nLen, &from, &len);
  2498. #if wxUSE_UNICODE_UTF8
  2499. if ( !ch.IsAscii() )
  2500. m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
  2501. else
  2502. #endif
  2503. m_impl.replace(from, len, nCount, (wxStringCharType)ch);
  2504. return *this;
  2505. }
  2506. // replaces a substring with another substring
  2507. wxString& replace(size_t nStart, size_t nLen,
  2508. const wxString& str, size_t nStart2, size_t nLen2)
  2509. {
  2510. wxSTRING_INVALIDATE_CACHE();
  2511. size_t from, len;
  2512. PosLenToImpl(nStart, nLen, &from, &len);
  2513. size_t from2, len2;
  2514. str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
  2515. m_impl.replace(from, len, str.m_impl, from2, len2);
  2516. return *this;
  2517. }
  2518. // replaces the substring with first nCount chars of sz
  2519. wxString& replace(size_t nStart, size_t nLen,
  2520. const char* sz, size_t nCount)
  2521. {
  2522. wxSTRING_INVALIDATE_CACHE();
  2523. size_t from, len;
  2524. PosLenToImpl(nStart, nLen, &from, &len);
  2525. SubstrBufFromMB str(ImplStr(sz, nCount));
  2526. m_impl.replace(from, len, str.data, str.len);
  2527. return *this;
  2528. }
  2529. wxString& replace(size_t nStart, size_t nLen,
  2530. const wchar_t* sz, size_t nCount)
  2531. {
  2532. wxSTRING_INVALIDATE_CACHE();
  2533. size_t from, len;
  2534. PosLenToImpl(nStart, nLen, &from, &len);
  2535. SubstrBufFromWC str(ImplStr(sz, nCount));
  2536. m_impl.replace(from, len, str.data, str.len);
  2537. return *this;
  2538. }
  2539. wxString& replace(size_t nStart, size_t nLen,
  2540. const wxString& s, size_t nCount)
  2541. {
  2542. wxSTRING_INVALIDATE_CACHE();
  2543. size_t from, len;
  2544. PosLenToImpl(nStart, nLen, &from, &len);
  2545. m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
  2546. return *this;
  2547. }
  2548. wxString& replace(iterator first, iterator last, const char* s)
  2549. {
  2550. wxSTRING_INVALIDATE_CACHE();
  2551. m_impl.replace(first.impl(), last.impl(), ImplStr(s));
  2552. return *this;
  2553. }
  2554. wxString& replace(iterator first, iterator last, const wchar_t* s)
  2555. {
  2556. wxSTRING_INVALIDATE_CACHE();
  2557. m_impl.replace(first.impl(), last.impl(), ImplStr(s));
  2558. return *this;
  2559. }
  2560. wxString& replace(iterator first, iterator last, const char* s, size_type n)
  2561. {
  2562. wxSTRING_INVALIDATE_CACHE();
  2563. SubstrBufFromMB str(ImplStr(s, n));
  2564. m_impl.replace(first.impl(), last.impl(), str.data, str.len);
  2565. return *this;
  2566. }
  2567. wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
  2568. {
  2569. wxSTRING_INVALIDATE_CACHE();
  2570. SubstrBufFromWC str(ImplStr(s, n));
  2571. m_impl.replace(first.impl(), last.impl(), str.data, str.len);
  2572. return *this;
  2573. }
  2574. wxString& replace(iterator first, iterator last, const wxString& s)
  2575. {
  2576. wxSTRING_INVALIDATE_CACHE();
  2577. m_impl.replace(first.impl(), last.impl(), s.m_impl);
  2578. return *this;
  2579. }
  2580. wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
  2581. {
  2582. wxSTRING_INVALIDATE_CACHE();
  2583. #if wxUSE_UNICODE_UTF8
  2584. if ( !ch.IsAscii() )
  2585. m_impl.replace(first.impl(), last.impl(),
  2586. wxStringOperations::EncodeNChars(n, ch));
  2587. else
  2588. #endif
  2589. m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
  2590. return *this;
  2591. }
  2592. wxString& replace(iterator first, iterator last,
  2593. const_iterator first1, const_iterator last1)
  2594. {
  2595. wxSTRING_INVALIDATE_CACHE();
  2596. m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
  2597. return *this;
  2598. }
  2599. wxString& replace(iterator first, iterator last,
  2600. const char *first1, const char *last1)
  2601. { replace(first, last, first1, last1 - first1); return *this; }
  2602. wxString& replace(iterator first, iterator last,
  2603. const wchar_t *first1, const wchar_t *last1)
  2604. { replace(first, last, first1, last1 - first1); return *this; }
  2605. // swap two strings
  2606. void swap(wxString& str)
  2607. {
  2608. #if wxUSE_STRING_POS_CACHE
  2609. // we modify not only this string but also the other one directly so we
  2610. // need to invalidate cache for both of them (we could also try to
  2611. // exchange their cache entries but it seems unlikely to be worth it)
  2612. InvalidateCache();
  2613. str.InvalidateCache();
  2614. #endif // wxUSE_STRING_POS_CACHE
  2615. m_impl.swap(str.m_impl);
  2616. }
  2617. // find a substring
  2618. size_t find(const wxString& str, size_t nStart = 0) const
  2619. { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
  2620. // find first n characters of sz
  2621. size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
  2622. {
  2623. SubstrBufFromMB str(ImplStr(sz, n));
  2624. return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
  2625. }
  2626. size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
  2627. {
  2628. SubstrBufFromWC str(ImplStr(sz, n));
  2629. return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
  2630. }
  2631. size_t find(const wxScopedCharBuffer& s, size_t nStart = 0, size_t n = npos) const
  2632. { return find(s.data(), nStart, n); }
  2633. size_t find(const wxScopedWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
  2634. { return find(s.data(), nStart, n); }
  2635. size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
  2636. { return find(s.AsWChar(), nStart, n); }
  2637. // find the first occurrence of character ch after nStart
  2638. size_t find(wxUniChar ch, size_t nStart = 0) const
  2639. {
  2640. #if wxUSE_UNICODE_UTF8
  2641. if ( !ch.IsAscii() )
  2642. return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
  2643. PosToImpl(nStart)));
  2644. else
  2645. #endif
  2646. return PosFromImpl(m_impl.find((wxStringCharType)ch,
  2647. PosToImpl(nStart)));
  2648. }
  2649. size_t find(wxUniCharRef ch, size_t nStart = 0) const
  2650. { return find(wxUniChar(ch), nStart); }
  2651. size_t find(char ch, size_t nStart = 0) const
  2652. { return find(wxUniChar(ch), nStart); }
  2653. size_t find(unsigned char ch, size_t nStart = 0) const
  2654. { return find(wxUniChar(ch), nStart); }
  2655. size_t find(wchar_t ch, size_t nStart = 0) const
  2656. { return find(wxUniChar(ch), nStart); }
  2657. // rfind() family is exactly like find() but works right to left
  2658. // as find, but from the end
  2659. size_t rfind(const wxString& str, size_t nStart = npos) const
  2660. { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
  2661. // as find, but from the end
  2662. size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
  2663. {
  2664. SubstrBufFromMB str(ImplStr(sz, n));
  2665. return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
  2666. }
  2667. size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
  2668. {
  2669. SubstrBufFromWC str(ImplStr(sz, n));
  2670. return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
  2671. }
  2672. size_t rfind(const wxScopedCharBuffer& s, size_t nStart = npos, size_t n = npos) const
  2673. { return rfind(s.data(), nStart, n); }
  2674. size_t rfind(const wxScopedWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
  2675. { return rfind(s.data(), nStart, n); }
  2676. size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
  2677. { return rfind(s.AsWChar(), nStart, n); }
  2678. // as find, but from the end
  2679. size_t rfind(wxUniChar ch, size_t nStart = npos) const
  2680. {
  2681. #if wxUSE_UNICODE_UTF8
  2682. if ( !ch.IsAscii() )
  2683. return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
  2684. PosToImpl(nStart)));
  2685. else
  2686. #endif
  2687. return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
  2688. PosToImpl(nStart)));
  2689. }
  2690. size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
  2691. { return rfind(wxUniChar(ch), nStart); }
  2692. size_t rfind(char ch, size_t nStart = npos) const
  2693. { return rfind(wxUniChar(ch), nStart); }
  2694. size_t rfind(unsigned char ch, size_t nStart = npos) const
  2695. { return rfind(wxUniChar(ch), nStart); }
  2696. size_t rfind(wchar_t ch, size_t nStart = npos) const
  2697. { return rfind(wxUniChar(ch), nStart); }
  2698. // find first/last occurrence of any character (not) in the set:
  2699. #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
  2700. // FIXME-UTF8: this is not entirely correct, because it doesn't work if
  2701. // sizeof(wchar_t)==2 and surrogates are present in the string;
  2702. // should we care? Probably not.
  2703. size_t find_first_of(const wxString& str, size_t nStart = 0) const
  2704. { return m_impl.find_first_of(str.m_impl, nStart); }
  2705. size_t find_first_of(const char* sz, size_t nStart = 0) const
  2706. { return m_impl.find_first_of(ImplStr(sz), nStart); }
  2707. size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
  2708. { return m_impl.find_first_of(ImplStr(sz), nStart); }
  2709. size_t find_first_of(const char* sz, size_t nStart, size_t n) const
  2710. { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
  2711. size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
  2712. { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
  2713. size_t find_first_of(wxUniChar c, size_t nStart = 0) const
  2714. { return m_impl.find_first_of((wxChar)c, nStart); }
  2715. size_t find_last_of(const wxString& str, size_t nStart = npos) const
  2716. { return m_impl.find_last_of(str.m_impl, nStart); }
  2717. size_t find_last_of(const char* sz, size_t nStart = npos) const
  2718. { return m_impl.find_last_of(ImplStr(sz), nStart); }
  2719. size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
  2720. { return m_impl.find_last_of(ImplStr(sz), nStart); }
  2721. size_t find_last_of(const char* sz, size_t nStart, size_t n) const
  2722. { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
  2723. size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
  2724. { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
  2725. size_t find_last_of(wxUniChar c, size_t nStart = npos) const
  2726. { return m_impl.find_last_of((wxChar)c, nStart); }
  2727. size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
  2728. { return m_impl.find_first_not_of(str.m_impl, nStart); }
  2729. size_t find_first_not_of(const char* sz, size_t nStart = 0) const
  2730. { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
  2731. size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
  2732. { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
  2733. size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
  2734. { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
  2735. size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
  2736. { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
  2737. size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
  2738. { return m_impl.find_first_not_of((wxChar)c, nStart); }
  2739. size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
  2740. { return m_impl.find_last_not_of(str.m_impl, nStart); }
  2741. size_t find_last_not_of(const char* sz, size_t nStart = npos) const
  2742. { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
  2743. size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
  2744. { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
  2745. size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
  2746. { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
  2747. size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
  2748. { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
  2749. size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
  2750. { return m_impl.find_last_not_of((wxChar)c, nStart); }
  2751. #else
  2752. // we can't use std::string implementation in UTF-8 build, because the
  2753. // character sets would be interpreted wrongly:
  2754. // as strpbrk() but starts at nStart, returns npos if not found
  2755. size_t find_first_of(const wxString& str, size_t nStart = 0) const
  2756. #if wxUSE_UNICODE // FIXME-UTF8: temporary
  2757. { return find_first_of(str.wc_str(), nStart); }
  2758. #else
  2759. { return find_first_of(str.mb_str(), nStart); }
  2760. #endif
  2761. // same as above
  2762. size_t find_first_of(const char* sz, size_t nStart = 0) const;
  2763. size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
  2764. size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
  2765. size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
  2766. // same as find(char, size_t)
  2767. size_t find_first_of(wxUniChar c, size_t nStart = 0) const
  2768. { return find(c, nStart); }
  2769. // find the last (starting from nStart) char from str in this string
  2770. size_t find_last_of (const wxString& str, size_t nStart = npos) const
  2771. #if wxUSE_UNICODE // FIXME-UTF8: temporary
  2772. { return find_last_of(str.wc_str(), nStart); }
  2773. #else
  2774. { return find_last_of(str.mb_str(), nStart); }
  2775. #endif
  2776. // same as above
  2777. size_t find_last_of (const char* sz, size_t nStart = npos) const;
  2778. size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
  2779. size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
  2780. size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
  2781. // same as above
  2782. size_t find_last_of(wxUniChar c, size_t nStart = npos) const
  2783. { return rfind(c, nStart); }
  2784. // find first/last occurrence of any character not in the set
  2785. // as strspn() (starting from nStart), returns npos on failure
  2786. size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
  2787. #if wxUSE_UNICODE // FIXME-UTF8: temporary
  2788. { return find_first_not_of(str.wc_str(), nStart); }
  2789. #else
  2790. { return find_first_not_of(str.mb_str(), nStart); }
  2791. #endif
  2792. // same as above
  2793. size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
  2794. size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
  2795. size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
  2796. size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
  2797. // same as above
  2798. size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
  2799. // as strcspn()
  2800. size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
  2801. #if wxUSE_UNICODE // FIXME-UTF8: temporary
  2802. { return find_last_not_of(str.wc_str(), nStart); }
  2803. #else
  2804. { return find_last_not_of(str.mb_str(), nStart); }
  2805. #endif
  2806. // same as above
  2807. size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
  2808. size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
  2809. size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
  2810. size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
  2811. // same as above
  2812. size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
  2813. #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
  2814. // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
  2815. // above to resolve ambiguities:
  2816. size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
  2817. { return find_first_of(wxUniChar(ch), nStart); }
  2818. size_t find_first_of(char ch, size_t nStart = 0) const
  2819. { return find_first_of(wxUniChar(ch), nStart); }
  2820. size_t find_first_of(unsigned char ch, size_t nStart = 0) const
  2821. { return find_first_of(wxUniChar(ch), nStart); }
  2822. size_t find_first_of(wchar_t ch, size_t nStart = 0) const
  2823. { return find_first_of(wxUniChar(ch), nStart); }
  2824. size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
  2825. { return find_last_of(wxUniChar(ch), nStart); }
  2826. size_t find_last_of(char ch, size_t nStart = npos) const
  2827. { return find_last_of(wxUniChar(ch), nStart); }
  2828. size_t find_last_of(unsigned char ch, size_t nStart = npos) const
  2829. { return find_last_of(wxUniChar(ch), nStart); }
  2830. size_t find_last_of(wchar_t ch, size_t nStart = npos) const
  2831. { return find_last_of(wxUniChar(ch), nStart); }
  2832. size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
  2833. { return find_first_not_of(wxUniChar(ch), nStart); }
  2834. size_t find_first_not_of(char ch, size_t nStart = 0) const
  2835. { return find_first_not_of(wxUniChar(ch), nStart); }
  2836. size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
  2837. { return find_first_not_of(wxUniChar(ch), nStart); }
  2838. size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
  2839. { return find_first_not_of(wxUniChar(ch), nStart); }
  2840. size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
  2841. { return find_last_not_of(wxUniChar(ch), nStart); }
  2842. size_t find_last_not_of(char ch, size_t nStart = npos) const
  2843. { return find_last_not_of(wxUniChar(ch), nStart); }
  2844. size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
  2845. { return find_last_not_of(wxUniChar(ch), nStart); }
  2846. size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
  2847. { return find_last_not_of(wxUniChar(ch), nStart); }
  2848. // and additional overloads for the versions taking strings:
  2849. size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
  2850. { return find_first_of(sz.AsString(), nStart); }
  2851. size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
  2852. { return find_first_of(sz.data(), nStart); }
  2853. size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
  2854. { return find_first_of(sz.data(), nStart); }
  2855. size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
  2856. { return find_first_of(sz.AsWChar(), nStart, n); }
  2857. size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
  2858. { return find_first_of(sz.data(), nStart, n); }
  2859. size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
  2860. { return find_first_of(sz.data(), nStart, n); }
  2861. size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
  2862. { return find_last_of(sz.AsString(), nStart); }
  2863. size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
  2864. { return find_last_of(sz.data(), nStart); }
  2865. size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
  2866. { return find_last_of(sz.data(), nStart); }
  2867. size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
  2868. { return find_last_of(sz.AsWChar(), nStart, n); }
  2869. size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
  2870. { return find_last_of(sz.data(), nStart, n); }
  2871. size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
  2872. { return find_last_of(sz.data(), nStart, n); }
  2873. size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
  2874. { return find_first_not_of(sz.AsString(), nStart); }
  2875. size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
  2876. { return find_first_not_of(sz.data(), nStart); }
  2877. size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
  2878. { return find_first_not_of(sz.data(), nStart); }
  2879. size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
  2880. { return find_first_not_of(sz.AsWChar(), nStart, n); }
  2881. size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
  2882. { return find_first_not_of(sz.data(), nStart, n); }
  2883. size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
  2884. { return find_first_not_of(sz.data(), nStart, n); }
  2885. size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
  2886. { return find_last_not_of(sz.AsString(), nStart); }
  2887. size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
  2888. { return find_last_not_of(sz.data(), nStart); }
  2889. size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
  2890. { return find_last_not_of(sz.data(), nStart); }
  2891. size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
  2892. { return find_last_not_of(sz.AsWChar(), nStart, n); }
  2893. size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
  2894. { return find_last_not_of(sz.data(), nStart, n); }
  2895. size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
  2896. { return find_last_not_of(sz.data(), nStart, n); }
  2897. // string += string
  2898. wxString& operator+=(const wxString& s)
  2899. {
  2900. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2901. m_impl += s.m_impl;
  2902. return *this;
  2903. }
  2904. // string += C string
  2905. wxString& operator+=(const char *psz)
  2906. {
  2907. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2908. m_impl += ImplStr(psz);
  2909. return *this;
  2910. }
  2911. wxString& operator+=(const wchar_t *pwz)
  2912. {
  2913. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2914. m_impl += ImplStr(pwz);
  2915. return *this;
  2916. }
  2917. wxString& operator+=(const wxCStrData& s)
  2918. {
  2919. wxSTRING_INVALIDATE_CACHED_LENGTH();
  2920. m_impl += s.AsString().m_impl;
  2921. return *this;
  2922. }
  2923. wxString& operator+=(const wxScopedCharBuffer& s)
  2924. { return append(s); }
  2925. wxString& operator+=(const wxScopedWCharBuffer& s)
  2926. { return append(s); }
  2927. // string += char
  2928. wxString& operator+=(wxUniChar ch)
  2929. {
  2930. wxSTRING_UPDATE_CACHED_LENGTH(1);
  2931. #if wxUSE_UNICODE_UTF8
  2932. if ( !ch.IsAscii() )
  2933. m_impl += wxStringOperations::EncodeChar(ch);
  2934. else
  2935. #endif
  2936. m_impl += (wxStringCharType)ch;
  2937. return *this;
  2938. }
  2939. wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
  2940. wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
  2941. wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
  2942. wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
  2943. wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
  2944. private:
  2945. #if !wxUSE_STL_BASED_WXSTRING
  2946. // helpers for wxStringBuffer and wxStringBufferLength
  2947. wxStringCharType *DoGetWriteBuf(size_t nLen)
  2948. {
  2949. return m_impl.DoGetWriteBuf(nLen);
  2950. }
  2951. void DoUngetWriteBuf()
  2952. {
  2953. wxSTRING_INVALIDATE_CACHE();
  2954. m_impl.DoUngetWriteBuf();
  2955. }
  2956. void DoUngetWriteBuf(size_t nLen)
  2957. {
  2958. wxSTRING_INVALIDATE_CACHE();
  2959. m_impl.DoUngetWriteBuf(nLen);
  2960. }
  2961. #endif // !wxUSE_STL_BASED_WXSTRING
  2962. #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
  2963. #if !wxUSE_UTF8_LOCALE_ONLY
  2964. int DoPrintfWchar(const wxChar *format, ...);
  2965. static wxString DoFormatWchar(const wxChar *format, ...);
  2966. #endif
  2967. #if wxUSE_UNICODE_UTF8
  2968. int DoPrintfUtf8(const char *format, ...);
  2969. static wxString DoFormatUtf8(const char *format, ...);
  2970. #endif
  2971. #endif
  2972. #if !wxUSE_STL_BASED_WXSTRING
  2973. // check string's data validity
  2974. bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
  2975. #endif
  2976. private:
  2977. wxStringImpl m_impl;
  2978. // buffers for compatibility conversion from (char*)c_str() and
  2979. // (wchar_t*)c_str(): the pointers returned by these functions should remain
  2980. // valid until the string itself is modified for compatibility with the
  2981. // existing code and consistency with std::string::c_str() so returning a
  2982. // temporary buffer won't do and we need to cache the conversion results
  2983. // TODO-UTF8: benchmark various approaches to keeping compatibility buffers
  2984. template<typename T>
  2985. struct ConvertedBuffer
  2986. {
  2987. // notice that there is no need to initialize m_len here as it's unused
  2988. // as long as m_str is NULL
  2989. ConvertedBuffer() : m_str(NULL) {}
  2990. ~ConvertedBuffer()
  2991. { free(m_str); }
  2992. bool Extend(size_t len)
  2993. {
  2994. // add extra 1 for the trailing NUL
  2995. void * const str = realloc(m_str, sizeof(T)*(len + 1));
  2996. if ( !str )
  2997. return false;
  2998. m_str = static_cast<T *>(str);
  2999. m_len = len;
  3000. return true;
  3001. }
  3002. const wxScopedCharTypeBuffer<T> AsScopedBuffer() const
  3003. {
  3004. return wxScopedCharTypeBuffer<T>::CreateNonOwned(m_str, m_len);
  3005. }
  3006. T *m_str; // pointer to the string data
  3007. size_t m_len; // length, not size, i.e. in chars and without last NUL
  3008. };
  3009. #if wxUSE_UNICODE
  3010. // common mb_str() and wxCStrData::AsChar() helper: performs the conversion
  3011. // and returns either m_convertedToChar.m_str (in which case its m_len is
  3012. // also updated) or NULL if it failed
  3013. //
  3014. // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a
  3015. // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion
  3016. // as optimization and so the caller needs to check for this before using
  3017. // m_convertedToChar
  3018. //
  3019. // NB: AsChar() returns char* in any build, unlike mb_str()
  3020. const char *AsChar(const wxMBConv& conv) const;
  3021. // mb_str() implementation helper
  3022. wxScopedCharBuffer AsCharBuf(const wxMBConv& conv) const
  3023. {
  3024. #if wxUSE_UNICODE_UTF8
  3025. // avoid conversion if we can
  3026. if ( conv.IsUTF8() )
  3027. {
  3028. return wxScopedCharBuffer::CreateNonOwned(m_impl.c_str(),
  3029. m_impl.length());
  3030. }
  3031. #endif // wxUSE_UNICODE_UTF8
  3032. // call this solely in order to fill in m_convertedToChar as AsChar()
  3033. // updates it as a side effect: this is a bit ugly but it's a completely
  3034. // internal function so the users of this class shouldn't care or know
  3035. // about it and doing it like this, i.e. having a separate AsChar(),
  3036. // allows us to avoid the creation and destruction of a temporary buffer
  3037. // when using wxCStrData without duplicating any code
  3038. if ( !AsChar(conv) )
  3039. {
  3040. // although it would be probably more correct to return NULL buffer
  3041. // from here if the conversion fails, a lot of existing code doesn't
  3042. // expect mb_str() (or wc_str()) to ever return NULL so return an
  3043. // empty string otherwise to avoid crashes in it
  3044. //
  3045. // also, some existing code does check for the conversion success and
  3046. // so asserting here would be bad too -- even if it does mean that
  3047. // silently losing data is possible for badly written code
  3048. return wxScopedCharBuffer::CreateNonOwned("", 0);
  3049. }
  3050. return m_convertedToChar.AsScopedBuffer();
  3051. }
  3052. ConvertedBuffer<char> m_convertedToChar;
  3053. #endif // !wxUSE_UNICODE
  3054. #if !wxUSE_UNICODE_WCHAR
  3055. // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI
  3056. // builds: converts the string contents into m_convertedToWChar and returns
  3057. // NULL if the conversion failed (this can only happen in ANSI build)
  3058. //
  3059. // NB: AsWChar() returns wchar_t* in any build, unlike wc_str()
  3060. const wchar_t *AsWChar(const wxMBConv& conv) const;
  3061. // wc_str() implementation helper
  3062. wxScopedWCharBuffer AsWCharBuf(const wxMBConv& conv) const
  3063. {
  3064. if ( !AsWChar(conv) )
  3065. return wxScopedWCharBuffer::CreateNonOwned(L"", 0);
  3066. return m_convertedToWChar.AsScopedBuffer();
  3067. }
  3068. ConvertedBuffer<wchar_t> m_convertedToWChar;
  3069. #endif // !wxUSE_UNICODE_WCHAR
  3070. #if wxUSE_UNICODE_UTF8
  3071. // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
  3072. // assigning to character pointer to by wxString::iterator may
  3073. // change the underlying wxStringImpl iterator, so we have to
  3074. // keep track of all iterators and update them as necessary:
  3075. struct wxStringIteratorNodeHead
  3076. {
  3077. wxStringIteratorNodeHead() : ptr(NULL) {}
  3078. wxStringIteratorNode *ptr;
  3079. // copying is disallowed as it would result in more than one pointer into
  3080. // the same linked list
  3081. wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead);
  3082. };
  3083. wxStringIteratorNodeHead m_iterators;
  3084. friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
  3085. friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
  3086. #endif // wxUSE_UNICODE_UTF8
  3087. friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
  3088. friend class wxStringInternalBuffer;
  3089. friend class wxStringInternalBufferLength;
  3090. };
  3091. #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
  3092. #pragma warning (pop)
  3093. #endif
  3094. // string iterator operators that satisfy STL Random Access Iterator
  3095. // requirements:
  3096. inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
  3097. { return i + n; }
  3098. inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
  3099. { return i + n; }
  3100. inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
  3101. { return i + n; }
  3102. inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
  3103. { return i + n; }
  3104. // notice that even though for many compilers the friend declarations above are
  3105. // enough, from the point of view of C++ standard we must have the declarations
  3106. // here as friend ones are not injected in the enclosing namespace and without
  3107. // them the code fails to compile with conforming compilers such as xlC or g++4
  3108. wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
  3109. wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
  3110. wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
  3111. wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
  3112. wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
  3113. wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
  3114. wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
  3115. inline wxString operator+(const wxString& string, wxUniCharRef ch)
  3116. { return string + (wxUniChar)ch; }
  3117. inline wxString operator+(const wxString& string, char ch)
  3118. { return string + wxUniChar(ch); }
  3119. inline wxString operator+(const wxString& string, wchar_t ch)
  3120. { return string + wxUniChar(ch); }
  3121. inline wxString operator+(wxUniCharRef ch, const wxString& string)
  3122. { return (wxUniChar)ch + string; }
  3123. inline wxString operator+(char ch, const wxString& string)
  3124. { return wxUniChar(ch) + string; }
  3125. inline wxString operator+(wchar_t ch, const wxString& string)
  3126. { return wxUniChar(ch) + string; }
  3127. #define wxGetEmptyString() wxString()
  3128. // ----------------------------------------------------------------------------
  3129. // helper functions which couldn't be defined inline
  3130. // ----------------------------------------------------------------------------
  3131. namespace wxPrivate
  3132. {
  3133. #if wxUSE_UNICODE_WCHAR
  3134. template <>
  3135. struct wxStringAsBufHelper<char>
  3136. {
  3137. static wxScopedCharBuffer Get(const wxString& s, size_t *len)
  3138. {
  3139. wxScopedCharBuffer buf(s.mb_str());
  3140. if ( len )
  3141. *len = buf ? strlen(buf) : 0;
  3142. return buf;
  3143. }
  3144. };
  3145. template <>
  3146. struct wxStringAsBufHelper<wchar_t>
  3147. {
  3148. static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
  3149. {
  3150. const size_t length = s.length();
  3151. if ( len )
  3152. *len = length;
  3153. return wxScopedWCharBuffer::CreateNonOwned(s.wx_str(), length);
  3154. }
  3155. };
  3156. #elif wxUSE_UNICODE_UTF8
  3157. template <>
  3158. struct wxStringAsBufHelper<char>
  3159. {
  3160. static wxScopedCharBuffer Get(const wxString& s, size_t *len)
  3161. {
  3162. const size_t length = s.utf8_length();
  3163. if ( len )
  3164. *len = length;
  3165. return wxScopedCharBuffer::CreateNonOwned(s.wx_str(), length);
  3166. }
  3167. };
  3168. template <>
  3169. struct wxStringAsBufHelper<wchar_t>
  3170. {
  3171. static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
  3172. {
  3173. wxScopedWCharBuffer wbuf(s.wc_str());
  3174. if ( len )
  3175. *len = wxWcslen(wbuf);
  3176. return wbuf;
  3177. }
  3178. };
  3179. #endif // Unicode build kind
  3180. } // namespace wxPrivate
  3181. // ----------------------------------------------------------------------------
  3182. // wxStringBuffer: a tiny class allowing to get a writable pointer into string
  3183. // ----------------------------------------------------------------------------
  3184. #if !wxUSE_STL_BASED_WXSTRING
  3185. // string buffer for direct access to string data in their native
  3186. // representation:
  3187. class wxStringInternalBuffer
  3188. {
  3189. public:
  3190. typedef wxStringCharType CharType;
  3191. wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
  3192. : m_str(str), m_buf(NULL)
  3193. { m_buf = m_str.DoGetWriteBuf(lenWanted); }
  3194. ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
  3195. operator wxStringCharType*() const { return m_buf; }
  3196. private:
  3197. wxString& m_str;
  3198. wxStringCharType *m_buf;
  3199. wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
  3200. };
  3201. class wxStringInternalBufferLength
  3202. {
  3203. public:
  3204. typedef wxStringCharType CharType;
  3205. wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
  3206. : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
  3207. {
  3208. m_buf = m_str.DoGetWriteBuf(lenWanted);
  3209. wxASSERT(m_buf != NULL);
  3210. }
  3211. ~wxStringInternalBufferLength()
  3212. {
  3213. wxASSERT(m_lenSet);
  3214. m_str.DoUngetWriteBuf(m_len);
  3215. }
  3216. operator wxStringCharType*() const { return m_buf; }
  3217. void SetLength(size_t length) { m_len = length; m_lenSet = true; }
  3218. private:
  3219. wxString& m_str;
  3220. wxStringCharType *m_buf;
  3221. size_t m_len;
  3222. bool m_lenSet;
  3223. wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
  3224. };
  3225. #endif // !wxUSE_STL_BASED_WXSTRING
  3226. template<typename T>
  3227. class wxStringTypeBufferBase
  3228. {
  3229. public:
  3230. typedef T CharType;
  3231. wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
  3232. : m_str(str), m_buf(lenWanted)
  3233. {
  3234. // for compatibility with old wxStringBuffer which provided direct
  3235. // access to wxString internal buffer, initialize ourselves with the
  3236. // string initial contents
  3237. // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
  3238. // tchar_str<CharType>
  3239. size_t len;
  3240. const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
  3241. if ( buf )
  3242. {
  3243. if ( len > lenWanted )
  3244. {
  3245. // in this case there is not enough space for terminating NUL,
  3246. // ensure that we still put it there
  3247. m_buf.data()[lenWanted] = 0;
  3248. len = lenWanted - 1;
  3249. }
  3250. memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
  3251. }
  3252. //else: conversion failed, this can happen when trying to get Unicode
  3253. // string contents into a char string
  3254. }
  3255. operator CharType*() { return m_buf.data(); }
  3256. protected:
  3257. wxString& m_str;
  3258. wxCharTypeBuffer<CharType> m_buf;
  3259. };
  3260. template<typename T>
  3261. class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T>
  3262. {
  3263. public:
  3264. wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
  3265. : wxStringTypeBufferBase<T>(str, lenWanted),
  3266. m_len(0),
  3267. m_lenSet(false)
  3268. { }
  3269. ~wxStringTypeBufferLengthBase()
  3270. {
  3271. wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
  3272. }
  3273. void SetLength(size_t length) { m_len = length; m_lenSet = true; }
  3274. protected:
  3275. size_t m_len;
  3276. bool m_lenSet;
  3277. };
  3278. template<typename T>
  3279. class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
  3280. {
  3281. public:
  3282. wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
  3283. : wxStringTypeBufferBase<T>(str, lenWanted)
  3284. { }
  3285. ~wxStringTypeBuffer()
  3286. {
  3287. this->m_str.assign(this->m_buf.data());
  3288. }
  3289. wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer);
  3290. };
  3291. template<typename T>
  3292. class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
  3293. {
  3294. public:
  3295. wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
  3296. : wxStringTypeBufferLengthBase<T>(str, lenWanted)
  3297. { }
  3298. ~wxStringTypeBufferLength()
  3299. {
  3300. this->m_str.assign(this->m_buf.data(), this->m_len);
  3301. }
  3302. wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength);
  3303. };
  3304. #if wxUSE_STL_BASED_WXSTRING
  3305. WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
  3306. class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
  3307. {
  3308. public:
  3309. wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
  3310. : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
  3311. ~wxStringInternalBuffer()
  3312. { m_str.m_impl.assign(m_buf.data()); }
  3313. wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
  3314. };
  3315. WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
  3316. wxStringTypeBufferLengthBase<wxStringCharType> )
  3317. class wxStringInternalBufferLength
  3318. : public wxStringTypeBufferLengthBase<wxStringCharType>
  3319. {
  3320. public:
  3321. wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
  3322. : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
  3323. ~wxStringInternalBufferLength()
  3324. {
  3325. m_str.m_impl.assign(m_buf.data(), m_len);
  3326. }
  3327. wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
  3328. };
  3329. #endif // wxUSE_STL_BASED_WXSTRING
  3330. #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
  3331. typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
  3332. typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
  3333. #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
  3334. typedef wxStringInternalBuffer wxStringBuffer;
  3335. typedef wxStringInternalBufferLength wxStringBufferLength;
  3336. #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
  3337. #if wxUSE_UNICODE_UTF8
  3338. typedef wxStringInternalBuffer wxUTF8StringBuffer;
  3339. typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
  3340. #elif wxUSE_UNICODE_WCHAR
  3341. WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
  3342. // Note about inlined dtors in the classes below: this is done not for
  3343. // performance reasons but just to avoid linking errors in the MSVC DLL build
  3344. // under Windows: if a class has non-inline methods it must be declared as
  3345. // being DLL-exported but, due to an extremely interesting feature of MSVC 7
  3346. // and later, any template class which is used as a base of a DLL-exported
  3347. // class is implicitly made DLL-exported too, as explained at the bottom of
  3348. // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
  3349. // _inheriting_ from a class can change whether it is being exported from DLL)
  3350. //
  3351. // But this results in link errors because the base template class is not DLL-
  3352. // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
  3353. // does have only inline functions. So the simplest fix is to just make all the
  3354. // functions of these classes inline too.
  3355. class wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
  3356. {
  3357. public:
  3358. wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
  3359. : wxStringTypeBufferBase<char>(str, lenWanted) {}
  3360. ~wxUTF8StringBuffer()
  3361. {
  3362. wxMBConvStrictUTF8 conv;
  3363. size_t wlen = conv.ToWChar(NULL, 0, m_buf);
  3364. wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
  3365. wxStringInternalBuffer wbuf(m_str, wlen);
  3366. conv.ToWChar(wbuf, wlen, m_buf);
  3367. }
  3368. wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer);
  3369. };
  3370. WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
  3371. class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char>
  3372. {
  3373. public:
  3374. wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
  3375. : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
  3376. ~wxUTF8StringBufferLength()
  3377. {
  3378. wxCHECK_RET(m_lenSet, "length not set");
  3379. wxMBConvStrictUTF8 conv;
  3380. size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
  3381. wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
  3382. wxStringInternalBufferLength wbuf(m_str, wlen);
  3383. conv.ToWChar(wbuf, wlen, m_buf, m_len);
  3384. wbuf.SetLength(wlen);
  3385. }
  3386. wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength);
  3387. };
  3388. #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
  3389. // ---------------------------------------------------------------------------
  3390. // wxString comparison functions: operator versions are always case sensitive
  3391. // ---------------------------------------------------------------------------
  3392. #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
  3393. wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
  3394. #undef wxCMP_WXCHAR_STRING
  3395. inline bool operator==(const wxString& s1, const wxString& s2)
  3396. { return s1.IsSameAs(s2); }
  3397. inline bool operator!=(const wxString& s1, const wxString& s2)
  3398. { return !s1.IsSameAs(s2); }
  3399. inline bool operator< (const wxString& s1, const wxString& s2)
  3400. { return s1.Cmp(s2) < 0; }
  3401. inline bool operator> (const wxString& s1, const wxString& s2)
  3402. { return s1.Cmp(s2) > 0; }
  3403. inline bool operator<=(const wxString& s1, const wxString& s2)
  3404. { return s1.Cmp(s2) <= 0; }
  3405. inline bool operator>=(const wxString& s1, const wxString& s2)
  3406. { return s1.Cmp(s2) >= 0; }
  3407. inline bool operator==(const wxString& s1, const wxCStrData& s2)
  3408. { return s1 == s2.AsString(); }
  3409. inline bool operator==(const wxCStrData& s1, const wxString& s2)
  3410. { return s1.AsString() == s2; }
  3411. inline bool operator!=(const wxString& s1, const wxCStrData& s2)
  3412. { return s1 != s2.AsString(); }
  3413. inline bool operator!=(const wxCStrData& s1, const wxString& s2)
  3414. { return s1.AsString() != s2; }
  3415. inline bool operator==(const wxString& s1, const wxScopedWCharBuffer& s2)
  3416. { return (s1.Cmp((const wchar_t *)s2) == 0); }
  3417. inline bool operator==(const wxScopedWCharBuffer& s1, const wxString& s2)
  3418. { return (s2.Cmp((const wchar_t *)s1) == 0); }
  3419. inline bool operator!=(const wxString& s1, const wxScopedWCharBuffer& s2)
  3420. { return (s1.Cmp((const wchar_t *)s2) != 0); }
  3421. inline bool operator!=(const wxScopedWCharBuffer& s1, const wxString& s2)
  3422. { return (s2.Cmp((const wchar_t *)s1) != 0); }
  3423. inline bool operator==(const wxString& s1, const wxScopedCharBuffer& s2)
  3424. { return (s1.Cmp((const char *)s2) == 0); }
  3425. inline bool operator==(const wxScopedCharBuffer& s1, const wxString& s2)
  3426. { return (s2.Cmp((const char *)s1) == 0); }
  3427. inline bool operator!=(const wxString& s1, const wxScopedCharBuffer& s2)
  3428. { return (s1.Cmp((const char *)s2) != 0); }
  3429. inline bool operator!=(const wxScopedCharBuffer& s1, const wxString& s2)
  3430. { return (s2.Cmp((const char *)s1) != 0); }
  3431. inline wxString operator+(const wxString& string, const wxScopedWCharBuffer& buf)
  3432. { return string + (const wchar_t *)buf; }
  3433. inline wxString operator+(const wxScopedWCharBuffer& buf, const wxString& string)
  3434. { return (const wchar_t *)buf + string; }
  3435. inline wxString operator+(const wxString& string, const wxScopedCharBuffer& buf)
  3436. { return string + (const char *)buf; }
  3437. inline wxString operator+(const wxScopedCharBuffer& buf, const wxString& string)
  3438. { return (const char *)buf + string; }
  3439. // comparison with char
  3440. inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
  3441. inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
  3442. inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
  3443. inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
  3444. inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
  3445. inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
  3446. inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
  3447. inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
  3448. inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
  3449. inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
  3450. inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
  3451. inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
  3452. inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
  3453. inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
  3454. inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
  3455. inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
  3456. inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
  3457. inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
  3458. // wxString iterators comparisons
  3459. inline bool wxString::iterator::operator==(const const_iterator& i) const
  3460. { return i == *this; }
  3461. inline bool wxString::iterator::operator!=(const const_iterator& i) const
  3462. { return i != *this; }
  3463. inline bool wxString::iterator::operator<(const const_iterator& i) const
  3464. { return i > *this; }
  3465. inline bool wxString::iterator::operator>(const const_iterator& i) const
  3466. { return i < *this; }
  3467. inline bool wxString::iterator::operator<=(const const_iterator& i) const
  3468. { return i >= *this; }
  3469. inline bool wxString::iterator::operator>=(const const_iterator& i) const
  3470. { return i <= *this; }
  3471. // comparison with C string in Unicode build
  3472. #if wxUSE_UNICODE
  3473. #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
  3474. wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
  3475. #undef wxCMP_CHAR_STRING
  3476. #endif // wxUSE_UNICODE
  3477. // we also need to provide the operators for comparison with wxCStrData to
  3478. // resolve ambiguity between operator(const wxChar *,const wxString &) and
  3479. // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
  3480. //
  3481. // notice that these are (shallow) pointer comparisons, not (deep) string ones
  3482. #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
  3483. #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
  3484. wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
  3485. wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
  3486. #undef wxCMP_CHAR_CSTRDATA
  3487. #undef wxCMP_WCHAR_CSTRDATA
  3488. // ---------------------------------------------------------------------------
  3489. // Implementation only from here until the end of file
  3490. // ---------------------------------------------------------------------------
  3491. #if wxUSE_STD_IOSTREAM
  3492. #include "wx/iosfwrap.h"
  3493. WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
  3494. WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
  3495. WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedCharBuffer&);
  3496. #ifndef __BORLANDC__
  3497. WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedWCharBuffer&);
  3498. #endif
  3499. #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
  3500. WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
  3501. WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
  3502. WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxScopedWCharBuffer&);
  3503. #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
  3504. #endif // wxUSE_STD_IOSTREAM
  3505. // ---------------------------------------------------------------------------
  3506. // wxCStrData implementation
  3507. // ---------------------------------------------------------------------------
  3508. inline wxCStrData::wxCStrData(char *buf)
  3509. : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
  3510. inline wxCStrData::wxCStrData(wchar_t *buf)
  3511. : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
  3512. inline wxCStrData::wxCStrData(const wxCStrData& data)
  3513. : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
  3514. m_offset(data.m_offset),
  3515. m_owned(data.m_owned)
  3516. {
  3517. }
  3518. inline wxCStrData::~wxCStrData()
  3519. {
  3520. if ( m_owned )
  3521. delete const_cast<wxString*>(m_str); // cast to silence warnings
  3522. }
  3523. // AsChar() and AsWChar() implementations simply forward to wxString methods
  3524. inline const wchar_t* wxCStrData::AsWChar() const
  3525. {
  3526. const wchar_t * const p =
  3527. #if wxUSE_UNICODE_WCHAR
  3528. m_str->wc_str();
  3529. #elif wxUSE_UNICODE_UTF8
  3530. m_str->AsWChar(wxMBConvStrictUTF8());
  3531. #else
  3532. m_str->AsWChar(wxConvLibc);
  3533. #endif
  3534. // in Unicode build the string always has a valid Unicode representation
  3535. // and even if a conversion is needed (as in UTF8 case) it can't fail
  3536. //
  3537. // but in ANSI build the string contents might be not convertible to
  3538. // Unicode using the current locale encoding so we do need to check for
  3539. // errors
  3540. #if !wxUSE_UNICODE
  3541. if ( !p )
  3542. {
  3543. // if conversion fails, return empty string and not NULL to avoid
  3544. // crashes in code written with either wxWidgets 2 wxString or
  3545. // std::string behaviour in mind: neither of them ever returns NULL
  3546. // from its c_str() and so we shouldn't neither
  3547. //
  3548. // notice that the same is done in AsChar() below and
  3549. // wxString::wc_str() and mb_str() for the same reasons
  3550. return L"";
  3551. }
  3552. #endif // !wxUSE_UNICODE
  3553. return p + m_offset;
  3554. }
  3555. inline const char* wxCStrData::AsChar() const
  3556. {
  3557. #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
  3558. const char * const p = m_str->AsChar(wxConvLibc);
  3559. if ( !p )
  3560. return "";
  3561. #else // !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
  3562. const char * const p = m_str->mb_str();
  3563. #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
  3564. return p + m_offset;
  3565. }
  3566. inline wxString wxCStrData::AsString() const
  3567. {
  3568. if ( m_offset == 0 )
  3569. return *m_str;
  3570. else
  3571. return m_str->Mid(m_offset);
  3572. }
  3573. inline const wxStringCharType *wxCStrData::AsInternal() const
  3574. {
  3575. #if wxUSE_UNICODE_UTF8
  3576. return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
  3577. #else
  3578. return m_str->wx_str() + m_offset;
  3579. #endif
  3580. }
  3581. inline wxUniChar wxCStrData::operator*() const
  3582. {
  3583. if ( m_str->empty() )
  3584. return wxUniChar(wxT('\0'));
  3585. else
  3586. return (*m_str)[m_offset];
  3587. }
  3588. inline wxUniChar wxCStrData::operator[](size_t n) const
  3589. {
  3590. // NB: we intentionally use operator[] and not at() here because the former
  3591. // works for the terminating NUL while the latter does not
  3592. return (*m_str)[m_offset + n];
  3593. }
  3594. // ----------------------------------------------------------------------------
  3595. // more wxCStrData operators
  3596. // ----------------------------------------------------------------------------
  3597. // we need to define those to allow "size_t pos = p - s.c_str()" where p is
  3598. // some pointer into the string
  3599. inline size_t operator-(const char *p, const wxCStrData& cs)
  3600. {
  3601. return p - cs.AsChar();
  3602. }
  3603. inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
  3604. {
  3605. return p - cs.AsWChar();
  3606. }
  3607. // ----------------------------------------------------------------------------
  3608. // implementation of wx[W]CharBuffer inline methods using wxCStrData
  3609. // ----------------------------------------------------------------------------
  3610. // FIXME-UTF8: move this to buffer.h
  3611. inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
  3612. : wxCharTypeBufferBase(cstr.AsCharBuf())
  3613. {
  3614. }
  3615. inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
  3616. : wxCharTypeBufferBase(cstr.AsWCharBuf())
  3617. {
  3618. }
  3619. #if wxUSE_UNICODE_UTF8
  3620. // ----------------------------------------------------------------------------
  3621. // implementation of wxStringIteratorNode inline methods
  3622. // ----------------------------------------------------------------------------
  3623. void wxStringIteratorNode::DoSet(const wxString *str,
  3624. wxStringImpl::const_iterator *citer,
  3625. wxStringImpl::iterator *iter)
  3626. {
  3627. m_prev = NULL;
  3628. m_iter = iter;
  3629. m_citer = citer;
  3630. m_str = str;
  3631. if ( str )
  3632. {
  3633. m_next = str->m_iterators.ptr;
  3634. const_cast<wxString*>(m_str)->m_iterators.ptr = this;
  3635. if ( m_next )
  3636. m_next->m_prev = this;
  3637. }
  3638. else
  3639. {
  3640. m_next = NULL;
  3641. }
  3642. }
  3643. void wxStringIteratorNode::clear()
  3644. {
  3645. if ( m_next )
  3646. m_next->m_prev = m_prev;
  3647. if ( m_prev )
  3648. m_prev->m_next = m_next;
  3649. else if ( m_str ) // first in the list
  3650. const_cast<wxString*>(m_str)->m_iterators.ptr = m_next;
  3651. m_next = m_prev = NULL;
  3652. m_citer = NULL;
  3653. m_iter = NULL;
  3654. m_str = NULL;
  3655. }
  3656. #endif // wxUSE_UNICODE_UTF8
  3657. #if WXWIN_COMPATIBILITY_2_8
  3658. // lot of code out there doesn't explicitly include wx/crt.h, but uses
  3659. // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
  3660. // so let's include this header now that wxString is defined and it's safe
  3661. // to do it:
  3662. #include "wx/crt.h"
  3663. #endif
  3664. // ----------------------------------------------------------------------------
  3665. // Checks on wxString characters
  3666. // ----------------------------------------------------------------------------
  3667. template<bool (T)(const wxUniChar& c)>
  3668. inline bool wxStringCheck(const wxString& val)
  3669. {
  3670. for ( wxString::const_iterator i = val.begin();
  3671. i != val.end();
  3672. ++i )
  3673. if (T(*i) == 0)
  3674. return false;
  3675. return true;
  3676. }
  3677. #endif // _WX_WXSTRING_H_