unistr.h 171 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 1998-2016, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. *
  9. * File unistr.h
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 09/25/98 stephen Creation.
  15. * 11/11/98 stephen Changed per 11/9 code review.
  16. * 04/20/99 stephen Overhauled per 4/16 code review.
  17. * 11/18/99 aliu Made to inherit from Replaceable. Added method
  18. * handleReplaceBetween(); other methods unchanged.
  19. * 06/25/01 grhoten Remove dependency on iostream.
  20. ******************************************************************************
  21. */
  22. #ifndef UNISTR_H
  23. #define UNISTR_H
  24. /**
  25. * \file
  26. * \brief C++ API: Unicode String
  27. */
  28. #include "unicode/utypes.h"
  29. #if U_SHOW_CPLUSPLUS_API
  30. #include <cstddef>
  31. #include "unicode/char16ptr.h"
  32. #include "unicode/rep.h"
  33. #include "unicode/std_string.h"
  34. #include "unicode/stringpiece.h"
  35. #include "unicode/bytestream.h"
  36. struct UConverter; // unicode/ucnv.h
  37. #ifndef USTRING_H
  38. /**
  39. * \ingroup ustring_ustrlen
  40. * @param s Pointer to sequence of UChars.
  41. * @return Length of sequence.
  42. */
  43. U_CAPI int32_t U_EXPORT2 u_strlen(const UChar *s);
  44. #endif
  45. U_NAMESPACE_BEGIN
  46. #if !UCONFIG_NO_BREAK_ITERATION
  47. class BreakIterator; // unicode/brkiter.h
  48. #endif
  49. class Edits;
  50. U_NAMESPACE_END
  51. // Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper.
  52. /**
  53. * Internal string case mapping function type.
  54. * All error checking must be done.
  55. * src and dest must not overlap.
  56. * @internal
  57. */
  58. typedef int32_t U_CALLCONV
  59. UStringCaseMapper(int32_t caseLocale, uint32_t options,
  60. #if !UCONFIG_NO_BREAK_ITERATION
  61. icu::BreakIterator *iter,
  62. #endif
  63. char16_t *dest, int32_t destCapacity,
  64. const char16_t *src, int32_t srcLength,
  65. icu::Edits *edits,
  66. UErrorCode &errorCode);
  67. U_NAMESPACE_BEGIN
  68. class Locale; // unicode/locid.h
  69. class StringCharacterIterator;
  70. class UnicodeStringAppendable; // unicode/appendable.h
  71. /* The <iostream> include has been moved to unicode/ustream.h */
  72. /**
  73. * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor
  74. * which constructs a Unicode string from an invariant-character char * string.
  75. * About invariant characters see utypes.h.
  76. * This constructor has no runtime dependency on conversion code and is
  77. * therefore recommended over ones taking a charset name string
  78. * (where the empty string "" indicates invariant-character conversion).
  79. *
  80. * @stable ICU 3.2
  81. */
  82. #define US_INV icu::UnicodeString::kInvariant
  83. /**
  84. * Unicode String literals in C++.
  85. *
  86. * Note: these macros are not recommended for new code.
  87. * Prior to the availability of C++11 and u"unicode string literals",
  88. * these macros were provided for portability and efficiency when
  89. * initializing UnicodeStrings from literals.
  90. *
  91. * They work only for strings that contain "invariant characters", i.e.,
  92. * only latin letters, digits, and some punctuation.
  93. * See utypes.h for details.
  94. *
  95. * The string parameter must be a C string literal.
  96. * The length of the string, not including the terminating
  97. * `NUL`, must be specified as a constant.
  98. * @stable ICU 2.0
  99. */
  100. #if !U_CHAR16_IS_TYPEDEF
  101. # define UNICODE_STRING(cs, _length) icu::UnicodeString(true, u ## cs, _length)
  102. #else
  103. # define UNICODE_STRING(cs, _length) icu::UnicodeString(true, (const char16_t*)u ## cs, _length)
  104. #endif
  105. /**
  106. * Unicode String literals in C++.
  107. * Dependent on the platform properties, different UnicodeString
  108. * constructors should be used to create a UnicodeString object from
  109. * a string literal.
  110. * The macros are defined for improved performance.
  111. * They work only for strings that contain "invariant characters", i.e.,
  112. * only latin letters, digits, and some punctuation.
  113. * See utypes.h for details.
  114. *
  115. * The string parameter must be a C string literal.
  116. * @stable ICU 2.0
  117. */
  118. #define UNICODE_STRING_SIMPLE(cs) UNICODE_STRING(cs, -1)
  119. /**
  120. * \def UNISTR_FROM_CHAR_EXPLICIT
  121. * This can be defined to be empty or "explicit".
  122. * If explicit, then the UnicodeString(char16_t) and UnicodeString(UChar32)
  123. * constructors are marked as explicit, preventing their inadvertent use.
  124. * @stable ICU 49
  125. */
  126. #ifndef UNISTR_FROM_CHAR_EXPLICIT
  127. # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
  128. // Auto-"explicit" in ICU library code.
  129. # define UNISTR_FROM_CHAR_EXPLICIT explicit
  130. # else
  131. // Empty by default for source code compatibility.
  132. # define UNISTR_FROM_CHAR_EXPLICIT
  133. # endif
  134. #endif
  135. /**
  136. * \def UNISTR_FROM_STRING_EXPLICIT
  137. * This can be defined to be empty or "explicit".
  138. * If explicit, then the UnicodeString(const char *) and UnicodeString(const char16_t *)
  139. * constructors are marked as explicit, preventing their inadvertent use.
  140. *
  141. * In particular, this helps prevent accidentally depending on ICU conversion code
  142. * by passing a string literal into an API with a const UnicodeString & parameter.
  143. * @stable ICU 49
  144. */
  145. #ifndef UNISTR_FROM_STRING_EXPLICIT
  146. # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
  147. // Auto-"explicit" in ICU library code.
  148. # define UNISTR_FROM_STRING_EXPLICIT explicit
  149. # else
  150. // Empty by default for source code compatibility.
  151. # define UNISTR_FROM_STRING_EXPLICIT
  152. # endif
  153. #endif
  154. /**
  155. * \def UNISTR_OBJECT_SIZE
  156. * Desired sizeof(UnicodeString) in bytes.
  157. * It should be a multiple of sizeof(pointer) to avoid unusable space for padding.
  158. * The object size may want to be a multiple of 16 bytes,
  159. * which is a common granularity for heap allocation.
  160. *
  161. * Any space inside the object beyond sizeof(vtable pointer) + 2
  162. * is available for storing short strings inside the object.
  163. * The bigger the object, the longer a string that can be stored inside the object,
  164. * without additional heap allocation.
  165. *
  166. * Depending on a platform's pointer size, pointer alignment requirements,
  167. * and struct padding, the compiler will usually round up sizeof(UnicodeString)
  168. * to 4 * sizeof(pointer) (or 3 * sizeof(pointer) for P128 data models),
  169. * to hold the fields for heap-allocated strings.
  170. * Such a minimum size also ensures that the object is easily large enough
  171. * to hold at least 2 char16_ts, for one supplementary code point (U16_MAX_LENGTH).
  172. *
  173. * sizeof(UnicodeString) >= 48 should work for all known platforms.
  174. *
  175. * For example, on a 64-bit machine where sizeof(vtable pointer) is 8,
  176. * sizeof(UnicodeString) = 64 would leave space for
  177. * (64 - sizeof(vtable pointer) - 2) / U_SIZEOF_UCHAR = (64 - 8 - 2) / 2 = 27
  178. * char16_ts stored inside the object.
  179. *
  180. * The minimum object size on a 64-bit machine would be
  181. * 4 * sizeof(pointer) = 4 * 8 = 32 bytes,
  182. * and the internal buffer would hold up to 11 char16_ts in that case.
  183. *
  184. * @see U16_MAX_LENGTH
  185. * @stable ICU 56
  186. */
  187. #ifndef UNISTR_OBJECT_SIZE
  188. # define UNISTR_OBJECT_SIZE 64
  189. #endif
  190. /**
  191. * UnicodeString is a string class that stores Unicode characters directly and provides
  192. * similar functionality as the Java String and StringBuffer/StringBuilder classes.
  193. * It is a concrete implementation of the abstract class Replaceable (for transliteration).
  194. *
  195. * The UnicodeString equivalent of std::string’s clear() is remove().
  196. *
  197. * A UnicodeString may "alias" an external array of characters
  198. * (that is, point to it, rather than own the array)
  199. * whose lifetime must then at least match the lifetime of the aliasing object.
  200. * This aliasing may be preserved when returning a UnicodeString by value,
  201. * depending on the compiler and the function implementation,
  202. * via Return Value Optimization (RVO) or the move assignment operator.
  203. * (However, the copy assignment operator does not preserve aliasing.)
  204. * For details see the description of storage models at the end of the class API docs
  205. * and in the User Guide chapter linked from there.
  206. *
  207. * The UnicodeString class is not suitable for subclassing.
  208. *
  209. * For an overview of Unicode strings in C and C++ see the
  210. * [User Guide Strings chapter](https://unicode-org.github.io/icu/userguide/strings#strings-in-cc).
  211. *
  212. * In ICU, a Unicode string consists of 16-bit Unicode *code units*.
  213. * A Unicode character may be stored with either one code unit
  214. * (the most common case) or with a matched pair of special code units
  215. * ("surrogates"). The data type for code units is char16_t.
  216. * For single-character handling, a Unicode character code *point* is a value
  217. * in the range 0..0x10ffff. ICU uses the UChar32 type for code points.
  218. *
  219. * Indexes and offsets into and lengths of strings always count code units, not code points.
  220. * This is the same as with multi-byte char* strings in traditional string handling.
  221. * Operations on partial strings typically do not test for code point boundaries.
  222. * If necessary, the user needs to take care of such boundaries by testing for the code unit
  223. * values or by using functions like
  224. * UnicodeString::getChar32Start() and UnicodeString::getChar32Limit()
  225. * (or, in C, the equivalent macros U16_SET_CP_START() and U16_SET_CP_LIMIT(), see utf.h).
  226. *
  227. * UnicodeString methods are more lenient with regard to input parameter values
  228. * than other ICU APIs. In particular:
  229. * - If indexes are out of bounds for a UnicodeString object
  230. * (< 0 or > length()) then they are "pinned" to the nearest boundary.
  231. * - If the buffer passed to an insert/append/replace operation is owned by the
  232. * target object, e.g., calling str.append(str), an extra copy may take place
  233. * to ensure safety.
  234. * - If primitive string pointer values (e.g., const char16_t * or char *)
  235. * for input strings are nullptr, then those input string parameters are treated
  236. * as if they pointed to an empty string.
  237. * However, this is *not* the case for char * parameters for charset names
  238. * or other IDs.
  239. * - Most UnicodeString methods do not take a UErrorCode parameter because
  240. * there are usually very few opportunities for failure other than a shortage
  241. * of memory, error codes in low-level C++ string methods would be inconvenient,
  242. * and the error code as the last parameter (ICU convention) would prevent
  243. * the use of default parameter values.
  244. * Instead, such methods set the UnicodeString into a "bogus" state
  245. * (see isBogus()) if an error occurs.
  246. *
  247. * In string comparisons, two UnicodeString objects that are both "bogus"
  248. * compare equal (to be transitive and prevent endless loops in sorting),
  249. * and a "bogus" string compares less than any non-"bogus" one.
  250. *
  251. * Const UnicodeString methods are thread-safe. Multiple threads can use
  252. * const methods on the same UnicodeString object simultaneously,
  253. * but non-const methods must not be called concurrently (in multiple threads)
  254. * with any other (const or non-const) methods.
  255. *
  256. * Similarly, const UnicodeString & parameters are thread-safe.
  257. * One object may be passed in as such a parameter concurrently in multiple threads.
  258. * This includes the const UnicodeString & parameters for
  259. * copy construction, assignment, and cloning.
  260. *
  261. * UnicodeString uses several storage methods.
  262. * String contents can be stored inside the UnicodeString object itself,
  263. * in an allocated and shared buffer, or in an outside buffer that is "aliased".
  264. * Most of this is done transparently, but careful aliasing in particular provides
  265. * significant performance improvements.
  266. * Also, the internal buffer is accessible via special functions.
  267. * For details see the
  268. * [User Guide Strings chapter](https://unicode-org.github.io/icu/userguide/strings#maximizing-performance-with-the-unicodestring-storage-model).
  269. *
  270. * @see utf.h
  271. * @see CharacterIterator
  272. * @stable ICU 2.0
  273. */
  274. class U_COMMON_API UnicodeString : public Replaceable
  275. {
  276. public:
  277. /**
  278. * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor
  279. * which constructs a Unicode string from an invariant-character char * string.
  280. * Use the macro US_INV instead of the full qualification for this value.
  281. *
  282. * @see US_INV
  283. * @stable ICU 3.2
  284. */
  285. enum EInvariant {
  286. /**
  287. * @see EInvariant
  288. * @stable ICU 3.2
  289. */
  290. kInvariant
  291. };
  292. //========================================
  293. // Read-only operations
  294. //========================================
  295. /* Comparison - bitwise only - for international comparison use collation */
  296. /**
  297. * Equality operator. Performs only bitwise comparison.
  298. * @param text The UnicodeString to compare to this one.
  299. * @return true if `text` contains the same characters as this one,
  300. * false otherwise.
  301. * @stable ICU 2.0
  302. */
  303. inline bool operator== (const UnicodeString& text) const;
  304. /**
  305. * Inequality operator. Performs only bitwise comparison.
  306. * @param text The UnicodeString to compare to this one.
  307. * @return false if `text` contains the same characters as this one,
  308. * true otherwise.
  309. * @stable ICU 2.0
  310. */
  311. inline bool operator!= (const UnicodeString& text) const;
  312. /**
  313. * Greater than operator. Performs only bitwise comparison.
  314. * @param text The UnicodeString to compare to this one.
  315. * @return true if the characters in this are bitwise
  316. * greater than the characters in `text`, false otherwise
  317. * @stable ICU 2.0
  318. */
  319. inline UBool operator> (const UnicodeString& text) const;
  320. /**
  321. * Less than operator. Performs only bitwise comparison.
  322. * @param text The UnicodeString to compare to this one.
  323. * @return true if the characters in this are bitwise
  324. * less than the characters in `text`, false otherwise
  325. * @stable ICU 2.0
  326. */
  327. inline UBool operator< (const UnicodeString& text) const;
  328. /**
  329. * Greater than or equal operator. Performs only bitwise comparison.
  330. * @param text The UnicodeString to compare to this one.
  331. * @return true if the characters in this are bitwise
  332. * greater than or equal to the characters in `text`, false otherwise
  333. * @stable ICU 2.0
  334. */
  335. inline UBool operator>= (const UnicodeString& text) const;
  336. /**
  337. * Less than or equal operator. Performs only bitwise comparison.
  338. * @param text The UnicodeString to compare to this one.
  339. * @return true if the characters in this are bitwise
  340. * less than or equal to the characters in `text`, false otherwise
  341. * @stable ICU 2.0
  342. */
  343. inline UBool operator<= (const UnicodeString& text) const;
  344. /**
  345. * Compare the characters bitwise in this UnicodeString to
  346. * the characters in `text`.
  347. * @param text The UnicodeString to compare to this one.
  348. * @return The result of bitwise character comparison: 0 if this
  349. * contains the same characters as `text`, -1 if the characters in
  350. * this are bitwise less than the characters in `text`, +1 if the
  351. * characters in this are bitwise greater than the characters
  352. * in `text`.
  353. * @stable ICU 2.0
  354. */
  355. inline int8_t compare(const UnicodeString& text) const;
  356. /**
  357. * Compare the characters bitwise in the range
  358. * [`start`, `start + length`) with the characters
  359. * in the **entire string** `text`.
  360. * (The parameters "start" and "length" are not applied to the other text "text".)
  361. * @param start the offset at which the compare operation begins
  362. * @param length the number of characters of text to compare.
  363. * @param text the other text to be compared against this string.
  364. * @return The result of bitwise character comparison: 0 if this
  365. * contains the same characters as `text`, -1 if the characters in
  366. * this are bitwise less than the characters in `text`, +1 if the
  367. * characters in this are bitwise greater than the characters
  368. * in `text`.
  369. * @stable ICU 2.0
  370. */
  371. inline int8_t compare(int32_t start,
  372. int32_t length,
  373. const UnicodeString& text) const;
  374. /**
  375. * Compare the characters bitwise in the range
  376. * [`start`, `start + length`) with the characters
  377. * in `srcText` in the range
  378. * [`srcStart`, `srcStart + srcLength`).
  379. * @param start the offset at which the compare operation begins
  380. * @param length the number of characters in this to compare.
  381. * @param srcText the text to be compared
  382. * @param srcStart the offset into `srcText` to start comparison
  383. * @param srcLength the number of characters in `src` to compare
  384. * @return The result of bitwise character comparison: 0 if this
  385. * contains the same characters as `srcText`, -1 if the characters in
  386. * this are bitwise less than the characters in `srcText`, +1 if the
  387. * characters in this are bitwise greater than the characters
  388. * in `srcText`.
  389. * @stable ICU 2.0
  390. */
  391. inline int8_t compare(int32_t start,
  392. int32_t length,
  393. const UnicodeString& srcText,
  394. int32_t srcStart,
  395. int32_t srcLength) const;
  396. /**
  397. * Compare the characters bitwise in this UnicodeString with the first
  398. * `srcLength` characters in `srcChars`.
  399. * @param srcChars The characters to compare to this UnicodeString.
  400. * @param srcLength the number of characters in `srcChars` to compare
  401. * @return The result of bitwise character comparison: 0 if this
  402. * contains the same characters as `srcChars`, -1 if the characters in
  403. * this are bitwise less than the characters in `srcChars`, +1 if the
  404. * characters in this are bitwise greater than the characters
  405. * in `srcChars`.
  406. * @stable ICU 2.0
  407. */
  408. inline int8_t compare(ConstChar16Ptr srcChars,
  409. int32_t srcLength) const;
  410. /**
  411. * Compare the characters bitwise in the range
  412. * [`start`, `start + length`) with the first
  413. * `length` characters in `srcChars`
  414. * @param start the offset at which the compare operation begins
  415. * @param length the number of characters to compare.
  416. * @param srcChars the characters to be compared
  417. * @return The result of bitwise character comparison: 0 if this
  418. * contains the same characters as `srcChars`, -1 if the characters in
  419. * this are bitwise less than the characters in `srcChars`, +1 if the
  420. * characters in this are bitwise greater than the characters
  421. * in `srcChars`.
  422. * @stable ICU 2.0
  423. */
  424. inline int8_t compare(int32_t start,
  425. int32_t length,
  426. const char16_t *srcChars) const;
  427. /**
  428. * Compare the characters bitwise in the range
  429. * [`start`, `start + length`) with the characters
  430. * in `srcChars` in the range
  431. * [`srcStart`, `srcStart + srcLength`).
  432. * @param start the offset at which the compare operation begins
  433. * @param length the number of characters in this to compare
  434. * @param srcChars the characters to be compared
  435. * @param srcStart the offset into `srcChars` to start comparison
  436. * @param srcLength the number of characters in `srcChars` to compare
  437. * @return The result of bitwise character comparison: 0 if this
  438. * contains the same characters as `srcChars`, -1 if the characters in
  439. * this are bitwise less than the characters in `srcChars`, +1 if the
  440. * characters in this are bitwise greater than the characters
  441. * in `srcChars`.
  442. * @stable ICU 2.0
  443. */
  444. inline int8_t compare(int32_t start,
  445. int32_t length,
  446. const char16_t *srcChars,
  447. int32_t srcStart,
  448. int32_t srcLength) const;
  449. /**
  450. * Compare the characters bitwise in the range
  451. * [`start`, `limit`) with the characters
  452. * in `srcText` in the range
  453. * [`srcStart`, `srcLimit`).
  454. * @param start the offset at which the compare operation begins
  455. * @param limit the offset immediately following the compare operation
  456. * @param srcText the text to be compared
  457. * @param srcStart the offset into `srcText` to start comparison
  458. * @param srcLimit the offset into `srcText` to limit comparison
  459. * @return The result of bitwise character comparison: 0 if this
  460. * contains the same characters as `srcText`, -1 if the characters in
  461. * this are bitwise less than the characters in `srcText`, +1 if the
  462. * characters in this are bitwise greater than the characters
  463. * in `srcText`.
  464. * @stable ICU 2.0
  465. */
  466. inline int8_t compareBetween(int32_t start,
  467. int32_t limit,
  468. const UnicodeString& srcText,
  469. int32_t srcStart,
  470. int32_t srcLimit) const;
  471. /**
  472. * Compare two Unicode strings in code point order.
  473. * The result may be different from the results of compare(), operator<, etc.
  474. * if supplementary characters are present:
  475. *
  476. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  477. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  478. * which means that they compare as less than some other BMP characters like U+feff.
  479. * This function compares Unicode strings in code point order.
  480. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  481. *
  482. * @param text Another string to compare this one to.
  483. * @return a negative/zero/positive integer corresponding to whether
  484. * this string is less than/equal to/greater than the second one
  485. * in code point order
  486. * @stable ICU 2.0
  487. */
  488. inline int8_t compareCodePointOrder(const UnicodeString& text) const;
  489. /**
  490. * Compare two Unicode strings in code point order.
  491. * The result may be different from the results of compare(), operator<, etc.
  492. * if supplementary characters are present:
  493. *
  494. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  495. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  496. * which means that they compare as less than some other BMP characters like U+feff.
  497. * This function compares Unicode strings in code point order.
  498. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  499. *
  500. * @param start The start offset in this string at which the compare operation begins.
  501. * @param length The number of code units from this string to compare.
  502. * @param srcText Another string to compare this one to.
  503. * @return a negative/zero/positive integer corresponding to whether
  504. * this string is less than/equal to/greater than the second one
  505. * in code point order
  506. * @stable ICU 2.0
  507. */
  508. inline int8_t compareCodePointOrder(int32_t start,
  509. int32_t length,
  510. const UnicodeString& srcText) const;
  511. /**
  512. * Compare two Unicode strings in code point order.
  513. * The result may be different from the results of compare(), operator<, etc.
  514. * if supplementary characters are present:
  515. *
  516. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  517. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  518. * which means that they compare as less than some other BMP characters like U+feff.
  519. * This function compares Unicode strings in code point order.
  520. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  521. *
  522. * @param start The start offset in this string at which the compare operation begins.
  523. * @param length The number of code units from this string to compare.
  524. * @param srcText Another string to compare this one to.
  525. * @param srcStart The start offset in that string at which the compare operation begins.
  526. * @param srcLength The number of code units from that string to compare.
  527. * @return a negative/zero/positive integer corresponding to whether
  528. * this string is less than/equal to/greater than the second one
  529. * in code point order
  530. * @stable ICU 2.0
  531. */
  532. inline int8_t compareCodePointOrder(int32_t start,
  533. int32_t length,
  534. const UnicodeString& srcText,
  535. int32_t srcStart,
  536. int32_t srcLength) const;
  537. /**
  538. * Compare two Unicode strings in code point order.
  539. * The result may be different from the results of compare(), operator<, etc.
  540. * if supplementary characters are present:
  541. *
  542. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  543. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  544. * which means that they compare as less than some other BMP characters like U+feff.
  545. * This function compares Unicode strings in code point order.
  546. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  547. *
  548. * @param srcChars A pointer to another string to compare this one to.
  549. * @param srcLength The number of code units from that string to compare.
  550. * @return a negative/zero/positive integer corresponding to whether
  551. * this string is less than/equal to/greater than the second one
  552. * in code point order
  553. * @stable ICU 2.0
  554. */
  555. inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars,
  556. int32_t srcLength) const;
  557. /**
  558. * Compare two Unicode strings in code point order.
  559. * The result may be different from the results of compare(), operator<, etc.
  560. * if supplementary characters are present:
  561. *
  562. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  563. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  564. * which means that they compare as less than some other BMP characters like U+feff.
  565. * This function compares Unicode strings in code point order.
  566. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  567. *
  568. * @param start The start offset in this string at which the compare operation begins.
  569. * @param length The number of code units from this string to compare.
  570. * @param srcChars A pointer to another string to compare this one to.
  571. * @return a negative/zero/positive integer corresponding to whether
  572. * this string is less than/equal to/greater than the second one
  573. * in code point order
  574. * @stable ICU 2.0
  575. */
  576. inline int8_t compareCodePointOrder(int32_t start,
  577. int32_t length,
  578. const char16_t *srcChars) const;
  579. /**
  580. * Compare two Unicode strings in code point order.
  581. * The result may be different from the results of compare(), operator<, etc.
  582. * if supplementary characters are present:
  583. *
  584. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  585. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  586. * which means that they compare as less than some other BMP characters like U+feff.
  587. * This function compares Unicode strings in code point order.
  588. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  589. *
  590. * @param start The start offset in this string at which the compare operation begins.
  591. * @param length The number of code units from this string to compare.
  592. * @param srcChars A pointer to another string to compare this one to.
  593. * @param srcStart The start offset in that string at which the compare operation begins.
  594. * @param srcLength The number of code units from that string to compare.
  595. * @return a negative/zero/positive integer corresponding to whether
  596. * this string is less than/equal to/greater than the second one
  597. * in code point order
  598. * @stable ICU 2.0
  599. */
  600. inline int8_t compareCodePointOrder(int32_t start,
  601. int32_t length,
  602. const char16_t *srcChars,
  603. int32_t srcStart,
  604. int32_t srcLength) const;
  605. /**
  606. * Compare two Unicode strings in code point order.
  607. * The result may be different from the results of compare(), operator<, etc.
  608. * if supplementary characters are present:
  609. *
  610. * In UTF-16, supplementary characters (with code points U+10000 and above) are
  611. * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
  612. * which means that they compare as less than some other BMP characters like U+feff.
  613. * This function compares Unicode strings in code point order.
  614. * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
  615. *
  616. * @param start The start offset in this string at which the compare operation begins.
  617. * @param limit The offset after the last code unit from this string to compare.
  618. * @param srcText Another string to compare this one to.
  619. * @param srcStart The start offset in that string at which the compare operation begins.
  620. * @param srcLimit The offset after the last code unit from that string to compare.
  621. * @return a negative/zero/positive integer corresponding to whether
  622. * this string is less than/equal to/greater than the second one
  623. * in code point order
  624. * @stable ICU 2.0
  625. */
  626. inline int8_t compareCodePointOrderBetween(int32_t start,
  627. int32_t limit,
  628. const UnicodeString& srcText,
  629. int32_t srcStart,
  630. int32_t srcLimit) const;
  631. /**
  632. * Compare two strings case-insensitively using full case folding.
  633. * This is equivalent to this->foldCase(options).compare(text.foldCase(options)).
  634. *
  635. * @param text Another string to compare this one to.
  636. * @param options A bit set of options:
  637. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  638. * Comparison in code unit order with default case folding.
  639. *
  640. * - U_COMPARE_CODE_POINT_ORDER
  641. * Set to choose code point order instead of code unit order
  642. * (see u_strCompare for details).
  643. *
  644. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  645. *
  646. * @return A negative, zero, or positive integer indicating the comparison result.
  647. * @stable ICU 2.0
  648. */
  649. inline int8_t caseCompare(const UnicodeString& text, uint32_t options) const;
  650. /**
  651. * Compare two strings case-insensitively using full case folding.
  652. * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
  653. *
  654. * @param start The start offset in this string at which the compare operation begins.
  655. * @param length The number of code units from this string to compare.
  656. * @param srcText Another string to compare this one to.
  657. * @param options A bit set of options:
  658. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  659. * Comparison in code unit order with default case folding.
  660. *
  661. * - U_COMPARE_CODE_POINT_ORDER
  662. * Set to choose code point order instead of code unit order
  663. * (see u_strCompare for details).
  664. *
  665. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  666. *
  667. * @return A negative, zero, or positive integer indicating the comparison result.
  668. * @stable ICU 2.0
  669. */
  670. inline int8_t caseCompare(int32_t start,
  671. int32_t length,
  672. const UnicodeString& srcText,
  673. uint32_t options) const;
  674. /**
  675. * Compare two strings case-insensitively using full case folding.
  676. * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
  677. *
  678. * @param start The start offset in this string at which the compare operation begins.
  679. * @param length The number of code units from this string to compare.
  680. * @param srcText Another string to compare this one to.
  681. * @param srcStart The start offset in that string at which the compare operation begins.
  682. * @param srcLength The number of code units from that string to compare.
  683. * @param options A bit set of options:
  684. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  685. * Comparison in code unit order with default case folding.
  686. *
  687. * - U_COMPARE_CODE_POINT_ORDER
  688. * Set to choose code point order instead of code unit order
  689. * (see u_strCompare for details).
  690. *
  691. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  692. *
  693. * @return A negative, zero, or positive integer indicating the comparison result.
  694. * @stable ICU 2.0
  695. */
  696. inline int8_t caseCompare(int32_t start,
  697. int32_t length,
  698. const UnicodeString& srcText,
  699. int32_t srcStart,
  700. int32_t srcLength,
  701. uint32_t options) const;
  702. /**
  703. * Compare two strings case-insensitively using full case folding.
  704. * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
  705. *
  706. * @param srcChars A pointer to another string to compare this one to.
  707. * @param srcLength The number of code units from that string to compare.
  708. * @param options A bit set of options:
  709. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  710. * Comparison in code unit order with default case folding.
  711. *
  712. * - U_COMPARE_CODE_POINT_ORDER
  713. * Set to choose code point order instead of code unit order
  714. * (see u_strCompare for details).
  715. *
  716. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  717. *
  718. * @return A negative, zero, or positive integer indicating the comparison result.
  719. * @stable ICU 2.0
  720. */
  721. inline int8_t caseCompare(ConstChar16Ptr srcChars,
  722. int32_t srcLength,
  723. uint32_t options) const;
  724. /**
  725. * Compare two strings case-insensitively using full case folding.
  726. * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
  727. *
  728. * @param start The start offset in this string at which the compare operation begins.
  729. * @param length The number of code units from this string to compare.
  730. * @param srcChars A pointer to another string to compare this one to.
  731. * @param options A bit set of options:
  732. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  733. * Comparison in code unit order with default case folding.
  734. *
  735. * - U_COMPARE_CODE_POINT_ORDER
  736. * Set to choose code point order instead of code unit order
  737. * (see u_strCompare for details).
  738. *
  739. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  740. *
  741. * @return A negative, zero, or positive integer indicating the comparison result.
  742. * @stable ICU 2.0
  743. */
  744. inline int8_t caseCompare(int32_t start,
  745. int32_t length,
  746. const char16_t *srcChars,
  747. uint32_t options) const;
  748. /**
  749. * Compare two strings case-insensitively using full case folding.
  750. * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
  751. *
  752. * @param start The start offset in this string at which the compare operation begins.
  753. * @param length The number of code units from this string to compare.
  754. * @param srcChars A pointer to another string to compare this one to.
  755. * @param srcStart The start offset in that string at which the compare operation begins.
  756. * @param srcLength The number of code units from that string to compare.
  757. * @param options A bit set of options:
  758. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  759. * Comparison in code unit order with default case folding.
  760. *
  761. * - U_COMPARE_CODE_POINT_ORDER
  762. * Set to choose code point order instead of code unit order
  763. * (see u_strCompare for details).
  764. *
  765. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  766. *
  767. * @return A negative, zero, or positive integer indicating the comparison result.
  768. * @stable ICU 2.0
  769. */
  770. inline int8_t caseCompare(int32_t start,
  771. int32_t length,
  772. const char16_t *srcChars,
  773. int32_t srcStart,
  774. int32_t srcLength,
  775. uint32_t options) const;
  776. /**
  777. * Compare two strings case-insensitively using full case folding.
  778. * This is equivalent to this->foldCase(options).compareBetween(text.foldCase(options)).
  779. *
  780. * @param start The start offset in this string at which the compare operation begins.
  781. * @param limit The offset after the last code unit from this string to compare.
  782. * @param srcText Another string to compare this one to.
  783. * @param srcStart The start offset in that string at which the compare operation begins.
  784. * @param srcLimit The offset after the last code unit from that string to compare.
  785. * @param options A bit set of options:
  786. * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
  787. * Comparison in code unit order with default case folding.
  788. *
  789. * - U_COMPARE_CODE_POINT_ORDER
  790. * Set to choose code point order instead of code unit order
  791. * (see u_strCompare for details).
  792. *
  793. * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
  794. *
  795. * @return A negative, zero, or positive integer indicating the comparison result.
  796. * @stable ICU 2.0
  797. */
  798. inline int8_t caseCompareBetween(int32_t start,
  799. int32_t limit,
  800. const UnicodeString& srcText,
  801. int32_t srcStart,
  802. int32_t srcLimit,
  803. uint32_t options) const;
  804. /**
  805. * Determine if this starts with the characters in `text`
  806. * @param text The text to match.
  807. * @return true if this starts with the characters in `text`,
  808. * false otherwise
  809. * @stable ICU 2.0
  810. */
  811. inline UBool startsWith(const UnicodeString& text) const;
  812. /**
  813. * Determine if this starts with the characters in `srcText`
  814. * in the range [`srcStart`, `srcStart + srcLength`).
  815. * @param srcText The text to match.
  816. * @param srcStart the offset into `srcText` to start matching
  817. * @param srcLength the number of characters in `srcText` to match
  818. * @return true if this starts with the characters in `text`,
  819. * false otherwise
  820. * @stable ICU 2.0
  821. */
  822. inline UBool startsWith(const UnicodeString& srcText,
  823. int32_t srcStart,
  824. int32_t srcLength) const;
  825. /**
  826. * Determine if this starts with the characters in `srcChars`
  827. * @param srcChars The characters to match.
  828. * @param srcLength the number of characters in `srcChars`
  829. * @return true if this starts with the characters in `srcChars`,
  830. * false otherwise
  831. * @stable ICU 2.0
  832. */
  833. inline UBool startsWith(ConstChar16Ptr srcChars,
  834. int32_t srcLength) const;
  835. /**
  836. * Determine if this ends with the characters in `srcChars`
  837. * in the range [`srcStart`, `srcStart + srcLength`).
  838. * @param srcChars The characters to match.
  839. * @param srcStart the offset into `srcText` to start matching
  840. * @param srcLength the number of characters in `srcChars` to match
  841. * @return true if this ends with the characters in `srcChars`, false otherwise
  842. * @stable ICU 2.0
  843. */
  844. inline UBool startsWith(const char16_t *srcChars,
  845. int32_t srcStart,
  846. int32_t srcLength) const;
  847. /**
  848. * Determine if this ends with the characters in `text`
  849. * @param text The text to match.
  850. * @return true if this ends with the characters in `text`,
  851. * false otherwise
  852. * @stable ICU 2.0
  853. */
  854. inline UBool endsWith(const UnicodeString& text) const;
  855. /**
  856. * Determine if this ends with the characters in `srcText`
  857. * in the range [`srcStart`, `srcStart + srcLength`).
  858. * @param srcText The text to match.
  859. * @param srcStart the offset into `srcText` to start matching
  860. * @param srcLength the number of characters in `srcText` to match
  861. * @return true if this ends with the characters in `text`,
  862. * false otherwise
  863. * @stable ICU 2.0
  864. */
  865. inline UBool endsWith(const UnicodeString& srcText,
  866. int32_t srcStart,
  867. int32_t srcLength) const;
  868. /**
  869. * Determine if this ends with the characters in `srcChars`
  870. * @param srcChars The characters to match.
  871. * @param srcLength the number of characters in `srcChars`
  872. * @return true if this ends with the characters in `srcChars`,
  873. * false otherwise
  874. * @stable ICU 2.0
  875. */
  876. inline UBool endsWith(ConstChar16Ptr srcChars,
  877. int32_t srcLength) const;
  878. /**
  879. * Determine if this ends with the characters in `srcChars`
  880. * in the range [`srcStart`, `srcStart + srcLength`).
  881. * @param srcChars The characters to match.
  882. * @param srcStart the offset into `srcText` to start matching
  883. * @param srcLength the number of characters in `srcChars` to match
  884. * @return true if this ends with the characters in `srcChars`,
  885. * false otherwise
  886. * @stable ICU 2.0
  887. */
  888. inline UBool endsWith(const char16_t *srcChars,
  889. int32_t srcStart,
  890. int32_t srcLength) const;
  891. /* Searching - bitwise only */
  892. /**
  893. * Locate in this the first occurrence of the characters in `text`,
  894. * using bitwise comparison.
  895. * @param text The text to search for.
  896. * @return The offset into this of the start of `text`,
  897. * or -1 if not found.
  898. * @stable ICU 2.0
  899. */
  900. inline int32_t indexOf(const UnicodeString& text) const;
  901. /**
  902. * Locate in this the first occurrence of the characters in `text`
  903. * starting at offset `start`, using bitwise comparison.
  904. * @param text The text to search for.
  905. * @param start The offset at which searching will start.
  906. * @return The offset into this of the start of `text`,
  907. * or -1 if not found.
  908. * @stable ICU 2.0
  909. */
  910. inline int32_t indexOf(const UnicodeString& text,
  911. int32_t start) const;
  912. /**
  913. * Locate in this the first occurrence in the range
  914. * [`start`, `start + length`) of the characters
  915. * in `text`, using bitwise comparison.
  916. * @param text The text to search for.
  917. * @param start The offset at which searching will start.
  918. * @param length The number of characters to search
  919. * @return The offset into this of the start of `text`,
  920. * or -1 if not found.
  921. * @stable ICU 2.0
  922. */
  923. inline int32_t indexOf(const UnicodeString& text,
  924. int32_t start,
  925. int32_t length) const;
  926. /**
  927. * Locate in this the first occurrence in the range
  928. * [`start`, `start + length`) of the characters
  929. * in `srcText` in the range
  930. * [`srcStart`, `srcStart + srcLength`),
  931. * using bitwise comparison.
  932. * @param srcText The text to search for.
  933. * @param srcStart the offset into `srcText` at which
  934. * to start matching
  935. * @param srcLength the number of characters in `srcText` to match
  936. * @param start the offset into this at which to start matching
  937. * @param length the number of characters in this to search
  938. * @return The offset into this of the start of `text`,
  939. * or -1 if not found.
  940. * @stable ICU 2.0
  941. */
  942. inline int32_t indexOf(const UnicodeString& srcText,
  943. int32_t srcStart,
  944. int32_t srcLength,
  945. int32_t start,
  946. int32_t length) const;
  947. /**
  948. * Locate in this the first occurrence of the characters in
  949. * `srcChars`
  950. * starting at offset `start`, using bitwise comparison.
  951. * @param srcChars The text to search for.
  952. * @param srcLength the number of characters in `srcChars` to match
  953. * @param start the offset into this at which to start matching
  954. * @return The offset into this of the start of `text`,
  955. * or -1 if not found.
  956. * @stable ICU 2.0
  957. */
  958. inline int32_t indexOf(const char16_t *srcChars,
  959. int32_t srcLength,
  960. int32_t start) const;
  961. /**
  962. * Locate in this the first occurrence in the range
  963. * [`start`, `start + length`) of the characters
  964. * in `srcChars`, using bitwise comparison.
  965. * @param srcChars The text to search for.
  966. * @param srcLength the number of characters in `srcChars`
  967. * @param start The offset at which searching will start.
  968. * @param length The number of characters to search
  969. * @return The offset into this of the start of `srcChars`,
  970. * or -1 if not found.
  971. * @stable ICU 2.0
  972. */
  973. inline int32_t indexOf(ConstChar16Ptr srcChars,
  974. int32_t srcLength,
  975. int32_t start,
  976. int32_t length) const;
  977. /**
  978. * Locate in this the first occurrence in the range
  979. * [`start`, `start + length`) of the characters
  980. * in `srcChars` in the range
  981. * [`srcStart`, `srcStart + srcLength`),
  982. * using bitwise comparison.
  983. * @param srcChars The text to search for.
  984. * @param srcStart the offset into `srcChars` at which
  985. * to start matching
  986. * @param srcLength the number of characters in `srcChars` to match
  987. * @param start the offset into this at which to start matching
  988. * @param length the number of characters in this to search
  989. * @return The offset into this of the start of `text`,
  990. * or -1 if not found.
  991. * @stable ICU 2.0
  992. */
  993. int32_t indexOf(const char16_t *srcChars,
  994. int32_t srcStart,
  995. int32_t srcLength,
  996. int32_t start,
  997. int32_t length) const;
  998. /**
  999. * Locate in this the first occurrence of the BMP code point `c`,
  1000. * using bitwise comparison.
  1001. * @param c The code unit to search for.
  1002. * @return The offset into this of `c`, or -1 if not found.
  1003. * @stable ICU 2.0
  1004. */
  1005. inline int32_t indexOf(char16_t c) const;
  1006. /**
  1007. * Locate in this the first occurrence of the code point `c`,
  1008. * using bitwise comparison.
  1009. *
  1010. * @param c The code point to search for.
  1011. * @return The offset into this of `c`, or -1 if not found.
  1012. * @stable ICU 2.0
  1013. */
  1014. inline int32_t indexOf(UChar32 c) const;
  1015. /**
  1016. * Locate in this the first occurrence of the BMP code point `c`,
  1017. * starting at offset `start`, using bitwise comparison.
  1018. * @param c The code unit to search for.
  1019. * @param start The offset at which searching will start.
  1020. * @return The offset into this of `c`, or -1 if not found.
  1021. * @stable ICU 2.0
  1022. */
  1023. inline int32_t indexOf(char16_t c,
  1024. int32_t start) const;
  1025. /**
  1026. * Locate in this the first occurrence of the code point `c`
  1027. * starting at offset `start`, using bitwise comparison.
  1028. *
  1029. * @param c The code point to search for.
  1030. * @param start The offset at which searching will start.
  1031. * @return The offset into this of `c`, or -1 if not found.
  1032. * @stable ICU 2.0
  1033. */
  1034. inline int32_t indexOf(UChar32 c,
  1035. int32_t start) const;
  1036. /**
  1037. * Locate in this the first occurrence of the BMP code point `c`
  1038. * in the range [`start`, `start + length`),
  1039. * using bitwise comparison.
  1040. * @param c The code unit to search for.
  1041. * @param start the offset into this at which to start matching
  1042. * @param length the number of characters in this to search
  1043. * @return The offset into this of `c`, or -1 if not found.
  1044. * @stable ICU 2.0
  1045. */
  1046. inline int32_t indexOf(char16_t c,
  1047. int32_t start,
  1048. int32_t length) const;
  1049. /**
  1050. * Locate in this the first occurrence of the code point `c`
  1051. * in the range [`start`, `start + length`),
  1052. * using bitwise comparison.
  1053. *
  1054. * @param c The code point to search for.
  1055. * @param start the offset into this at which to start matching
  1056. * @param length the number of characters in this to search
  1057. * @return The offset into this of `c`, or -1 if not found.
  1058. * @stable ICU 2.0
  1059. */
  1060. inline int32_t indexOf(UChar32 c,
  1061. int32_t start,
  1062. int32_t length) const;
  1063. /**
  1064. * Locate in this the last occurrence of the characters in `text`,
  1065. * using bitwise comparison.
  1066. * @param text The text to search for.
  1067. * @return The offset into this of the start of `text`,
  1068. * or -1 if not found.
  1069. * @stable ICU 2.0
  1070. */
  1071. inline int32_t lastIndexOf(const UnicodeString& text) const;
  1072. /**
  1073. * Locate in this the last occurrence of the characters in `text`
  1074. * starting at offset `start`, using bitwise comparison.
  1075. * @param text The text to search for.
  1076. * @param start The offset at which searching will start.
  1077. * @return The offset into this of the start of `text`,
  1078. * or -1 if not found.
  1079. * @stable ICU 2.0
  1080. */
  1081. inline int32_t lastIndexOf(const UnicodeString& text,
  1082. int32_t start) const;
  1083. /**
  1084. * Locate in this the last occurrence in the range
  1085. * [`start`, `start + length`) of the characters
  1086. * in `text`, using bitwise comparison.
  1087. * @param text The text to search for.
  1088. * @param start The offset at which searching will start.
  1089. * @param length The number of characters to search
  1090. * @return The offset into this of the start of `text`,
  1091. * or -1 if not found.
  1092. * @stable ICU 2.0
  1093. */
  1094. inline int32_t lastIndexOf(const UnicodeString& text,
  1095. int32_t start,
  1096. int32_t length) const;
  1097. /**
  1098. * Locate in this the last occurrence in the range
  1099. * [`start`, `start + length`) of the characters
  1100. * in `srcText` in the range
  1101. * [`srcStart`, `srcStart + srcLength`),
  1102. * using bitwise comparison.
  1103. * @param srcText The text to search for.
  1104. * @param srcStart the offset into `srcText` at which
  1105. * to start matching
  1106. * @param srcLength the number of characters in `srcText` to match
  1107. * @param start the offset into this at which to start matching
  1108. * @param length the number of characters in this to search
  1109. * @return The offset into this of the start of `text`,
  1110. * or -1 if not found.
  1111. * @stable ICU 2.0
  1112. */
  1113. inline int32_t lastIndexOf(const UnicodeString& srcText,
  1114. int32_t srcStart,
  1115. int32_t srcLength,
  1116. int32_t start,
  1117. int32_t length) const;
  1118. /**
  1119. * Locate in this the last occurrence of the characters in `srcChars`
  1120. * starting at offset `start`, using bitwise comparison.
  1121. * @param srcChars The text to search for.
  1122. * @param srcLength the number of characters in `srcChars` to match
  1123. * @param start the offset into this at which to start matching
  1124. * @return The offset into this of the start of `text`,
  1125. * or -1 if not found.
  1126. * @stable ICU 2.0
  1127. */
  1128. inline int32_t lastIndexOf(const char16_t *srcChars,
  1129. int32_t srcLength,
  1130. int32_t start) const;
  1131. /**
  1132. * Locate in this the last occurrence in the range
  1133. * [`start`, `start + length`) of the characters
  1134. * in `srcChars`, using bitwise comparison.
  1135. * @param srcChars The text to search for.
  1136. * @param srcLength the number of characters in `srcChars`
  1137. * @param start The offset at which searching will start.
  1138. * @param length The number of characters to search
  1139. * @return The offset into this of the start of `srcChars`,
  1140. * or -1 if not found.
  1141. * @stable ICU 2.0
  1142. */
  1143. inline int32_t lastIndexOf(ConstChar16Ptr srcChars,
  1144. int32_t srcLength,
  1145. int32_t start,
  1146. int32_t length) const;
  1147. /**
  1148. * Locate in this the last occurrence in the range
  1149. * [`start`, `start + length`) of the characters
  1150. * in `srcChars` in the range
  1151. * [`srcStart`, `srcStart + srcLength`),
  1152. * using bitwise comparison.
  1153. * @param srcChars The text to search for.
  1154. * @param srcStart the offset into `srcChars` at which
  1155. * to start matching
  1156. * @param srcLength the number of characters in `srcChars` to match
  1157. * @param start the offset into this at which to start matching
  1158. * @param length the number of characters in this to search
  1159. * @return The offset into this of the start of `text`,
  1160. * or -1 if not found.
  1161. * @stable ICU 2.0
  1162. */
  1163. int32_t lastIndexOf(const char16_t *srcChars,
  1164. int32_t srcStart,
  1165. int32_t srcLength,
  1166. int32_t start,
  1167. int32_t length) const;
  1168. /**
  1169. * Locate in this the last occurrence of the BMP code point `c`,
  1170. * using bitwise comparison.
  1171. * @param c The code unit to search for.
  1172. * @return The offset into this of `c`, or -1 if not found.
  1173. * @stable ICU 2.0
  1174. */
  1175. inline int32_t lastIndexOf(char16_t c) const;
  1176. /**
  1177. * Locate in this the last occurrence of the code point `c`,
  1178. * using bitwise comparison.
  1179. *
  1180. * @param c The code point to search for.
  1181. * @return The offset into this of `c`, or -1 if not found.
  1182. * @stable ICU 2.0
  1183. */
  1184. inline int32_t lastIndexOf(UChar32 c) const;
  1185. /**
  1186. * Locate in this the last occurrence of the BMP code point `c`
  1187. * starting at offset `start`, using bitwise comparison.
  1188. * @param c The code unit to search for.
  1189. * @param start The offset at which searching will start.
  1190. * @return The offset into this of `c`, or -1 if not found.
  1191. * @stable ICU 2.0
  1192. */
  1193. inline int32_t lastIndexOf(char16_t c,
  1194. int32_t start) const;
  1195. /**
  1196. * Locate in this the last occurrence of the code point `c`
  1197. * starting at offset `start`, using bitwise comparison.
  1198. *
  1199. * @param c The code point to search for.
  1200. * @param start The offset at which searching will start.
  1201. * @return The offset into this of `c`, or -1 if not found.
  1202. * @stable ICU 2.0
  1203. */
  1204. inline int32_t lastIndexOf(UChar32 c,
  1205. int32_t start) const;
  1206. /**
  1207. * Locate in this the last occurrence of the BMP code point `c`
  1208. * in the range [`start`, `start + length`),
  1209. * using bitwise comparison.
  1210. * @param c The code unit to search for.
  1211. * @param start the offset into this at which to start matching
  1212. * @param length the number of characters in this to search
  1213. * @return The offset into this of `c`, or -1 if not found.
  1214. * @stable ICU 2.0
  1215. */
  1216. inline int32_t lastIndexOf(char16_t c,
  1217. int32_t start,
  1218. int32_t length) const;
  1219. /**
  1220. * Locate in this the last occurrence of the code point `c`
  1221. * in the range [`start`, `start + length`),
  1222. * using bitwise comparison.
  1223. *
  1224. * @param c The code point to search for.
  1225. * @param start the offset into this at which to start matching
  1226. * @param length the number of characters in this to search
  1227. * @return The offset into this of `c`, or -1 if not found.
  1228. * @stable ICU 2.0
  1229. */
  1230. inline int32_t lastIndexOf(UChar32 c,
  1231. int32_t start,
  1232. int32_t length) const;
  1233. /* Character access */
  1234. /**
  1235. * Return the code unit at offset `offset`.
  1236. * If the offset is not valid (0..length()-1) then U+ffff is returned.
  1237. * @param offset a valid offset into the text
  1238. * @return the code unit at offset `offset`
  1239. * or 0xffff if the offset is not valid for this string
  1240. * @stable ICU 2.0
  1241. */
  1242. inline char16_t charAt(int32_t offset) const;
  1243. /**
  1244. * Return the code unit at offset `offset`.
  1245. * If the offset is not valid (0..length()-1) then U+ffff is returned.
  1246. * @param offset a valid offset into the text
  1247. * @return the code unit at offset `offset`
  1248. * @stable ICU 2.0
  1249. */
  1250. inline char16_t operator[] (int32_t offset) const;
  1251. /**
  1252. * Return the code point that contains the code unit
  1253. * at offset `offset`.
  1254. * If the offset is not valid (0..length()-1) then U+ffff is returned.
  1255. * @param offset a valid offset into the text
  1256. * that indicates the text offset of any of the code units
  1257. * that will be assembled into a code point (21-bit value) and returned
  1258. * @return the code point of text at `offset`
  1259. * or 0xffff if the offset is not valid for this string
  1260. * @stable ICU 2.0
  1261. */
  1262. UChar32 char32At(int32_t offset) const;
  1263. /**
  1264. * Adjust a random-access offset so that
  1265. * it points to the beginning of a Unicode character.
  1266. * The offset that is passed in points to
  1267. * any code unit of a code point,
  1268. * while the returned offset will point to the first code unit
  1269. * of the same code point.
  1270. * In UTF-16, if the input offset points to a second surrogate
  1271. * of a surrogate pair, then the returned offset will point
  1272. * to the first surrogate.
  1273. * @param offset a valid offset into one code point of the text
  1274. * @return offset of the first code unit of the same code point
  1275. * @see U16_SET_CP_START
  1276. * @stable ICU 2.0
  1277. */
  1278. int32_t getChar32Start(int32_t offset) const;
  1279. /**
  1280. * Adjust a random-access offset so that
  1281. * it points behind a Unicode character.
  1282. * The offset that is passed in points behind
  1283. * any code unit of a code point,
  1284. * while the returned offset will point behind the last code unit
  1285. * of the same code point.
  1286. * In UTF-16, if the input offset points behind the first surrogate
  1287. * (i.e., to the second surrogate)
  1288. * of a surrogate pair, then the returned offset will point
  1289. * behind the second surrogate (i.e., to the first surrogate).
  1290. * @param offset a valid offset after any code unit of a code point of the text
  1291. * @return offset of the first code unit after the same code point
  1292. * @see U16_SET_CP_LIMIT
  1293. * @stable ICU 2.0
  1294. */
  1295. int32_t getChar32Limit(int32_t offset) const;
  1296. /**
  1297. * Move the code unit index along the string by delta code points.
  1298. * Interpret the input index as a code unit-based offset into the string,
  1299. * move the index forward or backward by delta code points, and
  1300. * return the resulting index.
  1301. * The input index should point to the first code unit of a code point,
  1302. * if there is more than one.
  1303. *
  1304. * Both input and output indexes are code unit-based as for all
  1305. * string indexes/offsets in ICU (and other libraries, like MBCS char*).
  1306. * If delta<0 then the index is moved backward (toward the start of the string).
  1307. * If delta>0 then the index is moved forward (toward the end of the string).
  1308. *
  1309. * This behaves like CharacterIterator::move32(delta, kCurrent).
  1310. *
  1311. * Behavior for out-of-bounds indexes:
  1312. * `moveIndex32` pins the input index to 0..length(), i.e.,
  1313. * if the input index<0 then it is pinned to 0;
  1314. * if it is index>length() then it is pinned to length().
  1315. * Afterwards, the index is moved by `delta` code points
  1316. * forward or backward,
  1317. * but no further backward than to 0 and no further forward than to length().
  1318. * The resulting index return value will be in between 0 and length(), inclusively.
  1319. *
  1320. * Examples:
  1321. * \code
  1322. * // s has code points 'a' U+10000 'b' U+10ffff U+2029
  1323. * UnicodeString s(u"a\U00010000b\U0010ffff\u2029");
  1324. *
  1325. * // initial index: position of U+10000
  1326. * int32_t index=1;
  1327. *
  1328. * // the following examples will all result in index==4, position of U+10ffff
  1329. *
  1330. * // skip 2 code points from some position in the string
  1331. * index=s.moveIndex32(index, 2); // skips U+10000 and 'b'
  1332. *
  1333. * // go to the 3rd code point from the start of s (0-based)
  1334. * index=s.moveIndex32(0, 3); // skips 'a', U+10000, and 'b'
  1335. *
  1336. * // go to the next-to-last code point of s
  1337. * index=s.moveIndex32(s.length(), -2); // backward-skips U+2029 and U+10ffff
  1338. * \endcode
  1339. *
  1340. * @param index input code unit index
  1341. * @param delta (signed) code point count to move the index forward or backward
  1342. * in the string
  1343. * @return the resulting code unit index
  1344. * @stable ICU 2.0
  1345. */
  1346. int32_t moveIndex32(int32_t index, int32_t delta) const;
  1347. /* Substring extraction */
  1348. /**
  1349. * Copy the characters in the range
  1350. * [`start`, `start + length`) into the array `dst`,
  1351. * beginning at `dstStart`.
  1352. * If the string aliases to `dst` itself as an external buffer,
  1353. * then extract() will not copy the contents.
  1354. *
  1355. * @param start offset of first character which will be copied into the array
  1356. * @param length the number of characters to extract
  1357. * @param dst array in which to copy characters. The length of `dst`
  1358. * must be at least (`dstStart + length`).
  1359. * @param dstStart the offset in `dst` where the first character
  1360. * will be extracted
  1361. * @stable ICU 2.0
  1362. */
  1363. inline void extract(int32_t start,
  1364. int32_t length,
  1365. Char16Ptr dst,
  1366. int32_t dstStart = 0) const;
  1367. /**
  1368. * Copy the contents of the string into dest.
  1369. * This is a convenience function that
  1370. * checks if there is enough space in dest,
  1371. * extracts the entire string if possible,
  1372. * and NUL-terminates dest if possible.
  1373. *
  1374. * If the string fits into dest but cannot be NUL-terminated
  1375. * (length()==destCapacity) then the error code is set to U_STRING_NOT_TERMINATED_WARNING.
  1376. * If the string itself does not fit into dest
  1377. * (length()>destCapacity) then the error code is set to U_BUFFER_OVERFLOW_ERROR.
  1378. *
  1379. * If the string aliases to `dest` itself as an external buffer,
  1380. * then extract() will not copy the contents.
  1381. *
  1382. * @param dest Destination string buffer.
  1383. * @param destCapacity Number of char16_ts available at dest.
  1384. * @param errorCode ICU error code.
  1385. * @return length()
  1386. * @stable ICU 2.0
  1387. */
  1388. int32_t
  1389. extract(Char16Ptr dest, int32_t destCapacity,
  1390. UErrorCode &errorCode) const;
  1391. /**
  1392. * Copy the characters in the range
  1393. * [`start`, `start + length`) into the UnicodeString
  1394. * `target`.
  1395. * @param start offset of first character which will be copied
  1396. * @param length the number of characters to extract
  1397. * @param target UnicodeString into which to copy characters.
  1398. * @stable ICU 2.0
  1399. */
  1400. inline void extract(int32_t start,
  1401. int32_t length,
  1402. UnicodeString& target) const;
  1403. /**
  1404. * Copy the characters in the range [`start`, `limit`)
  1405. * into the array `dst`, beginning at `dstStart`.
  1406. * @param start offset of first character which will be copied into the array
  1407. * @param limit offset immediately following the last character to be copied
  1408. * @param dst array in which to copy characters. The length of `dst`
  1409. * must be at least (`dstStart + (limit - start)`).
  1410. * @param dstStart the offset in `dst` where the first character
  1411. * will be extracted
  1412. * @stable ICU 2.0
  1413. */
  1414. inline void extractBetween(int32_t start,
  1415. int32_t limit,
  1416. char16_t *dst,
  1417. int32_t dstStart = 0) const;
  1418. /**
  1419. * Copy the characters in the range [`start`, `limit`)
  1420. * into the UnicodeString `target`. Replaceable API.
  1421. * @param start offset of first character which will be copied
  1422. * @param limit offset immediately following the last character to be copied
  1423. * @param target UnicodeString into which to copy characters.
  1424. * @stable ICU 2.0
  1425. */
  1426. virtual void extractBetween(int32_t start,
  1427. int32_t limit,
  1428. UnicodeString& target) const override;
  1429. /**
  1430. * Copy the characters in the range
  1431. * [`start`, `start + startLength`) into an array of characters.
  1432. * All characters must be invariant (see utypes.h).
  1433. * Use US_INV as the last, signature-distinguishing parameter.
  1434. *
  1435. * This function does not write any more than `targetCapacity`
  1436. * characters but returns the length of the entire output string
  1437. * so that one can allocate a larger buffer and call the function again
  1438. * if necessary.
  1439. * The output string is NUL-terminated if possible.
  1440. *
  1441. * @param start offset of first character which will be copied
  1442. * @param startLength the number of characters to extract
  1443. * @param target the target buffer for extraction, can be nullptr
  1444. * if targetLength is 0
  1445. * @param targetCapacity the length of the target buffer
  1446. * @param inv Signature-distinguishing parameter, use US_INV.
  1447. * @return the output string length, not including the terminating NUL
  1448. * @stable ICU 3.2
  1449. */
  1450. int32_t extract(int32_t start,
  1451. int32_t startLength,
  1452. char *target,
  1453. int32_t targetCapacity,
  1454. enum EInvariant inv) const;
  1455. #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
  1456. /**
  1457. * Copy the characters in the range
  1458. * [`start`, `start + length`) into an array of characters
  1459. * in the platform's default codepage.
  1460. * This function does not write any more than `targetLength`
  1461. * characters but returns the length of the entire output string
  1462. * so that one can allocate a larger buffer and call the function again
  1463. * if necessary.
  1464. * The output string is NUL-terminated if possible.
  1465. *
  1466. * @param start offset of first character which will be copied
  1467. * @param startLength the number of characters to extract
  1468. * @param target the target buffer for extraction
  1469. * @param targetLength the length of the target buffer
  1470. * If `target` is nullptr, then the number of bytes required for
  1471. * `target` is returned.
  1472. * @return the output string length, not including the terminating NUL
  1473. * @stable ICU 2.0
  1474. */
  1475. int32_t extract(int32_t start,
  1476. int32_t startLength,
  1477. char *target,
  1478. uint32_t targetLength) const;
  1479. #endif
  1480. #if !UCONFIG_NO_CONVERSION
  1481. /**
  1482. * Copy the characters in the range
  1483. * [`start`, `start + length`) into an array of characters
  1484. * in a specified codepage.
  1485. * The output string is NUL-terminated.
  1486. *
  1487. * Recommendation: For invariant-character strings use
  1488. * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const
  1489. * because it avoids object code dependencies of UnicodeString on
  1490. * the conversion code.
  1491. *
  1492. * @param start offset of first character which will be copied
  1493. * @param startLength the number of characters to extract
  1494. * @param target the target buffer for extraction
  1495. * @param codepage the desired codepage for the characters. 0 has
  1496. * the special meaning of the default codepage
  1497. * If `codepage` is an empty string (`""`),
  1498. * then a simple conversion is performed on the codepage-invariant
  1499. * subset ("invariant characters") of the platform encoding. See utypes.h.
  1500. * If `target` is nullptr, then the number of bytes required for
  1501. * `target` is returned. It is assumed that the target is big enough
  1502. * to fit all of the characters.
  1503. * @return the output string length, not including the terminating NUL
  1504. * @stable ICU 2.0
  1505. */
  1506. inline int32_t extract(int32_t start,
  1507. int32_t startLength,
  1508. char* target,
  1509. const char* codepage = nullptr) const;
  1510. /**
  1511. * Copy the characters in the range
  1512. * [`start`, `start + length`) into an array of characters
  1513. * in a specified codepage.
  1514. * This function does not write any more than `targetLength`
  1515. * characters but returns the length of the entire output string
  1516. * so that one can allocate a larger buffer and call the function again
  1517. * if necessary.
  1518. * The output string is NUL-terminated if possible.
  1519. *
  1520. * Recommendation: For invariant-character strings use
  1521. * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const
  1522. * because it avoids object code dependencies of UnicodeString on
  1523. * the conversion code.
  1524. *
  1525. * @param start offset of first character which will be copied
  1526. * @param startLength the number of characters to extract
  1527. * @param target the target buffer for extraction
  1528. * @param targetLength the length of the target buffer
  1529. * @param codepage the desired codepage for the characters. 0 has
  1530. * the special meaning of the default codepage
  1531. * If `codepage` is an empty string (`""`),
  1532. * then a simple conversion is performed on the codepage-invariant
  1533. * subset ("invariant characters") of the platform encoding. See utypes.h.
  1534. * If `target` is nullptr, then the number of bytes required for
  1535. * `target` is returned.
  1536. * @return the output string length, not including the terminating NUL
  1537. * @stable ICU 2.0
  1538. */
  1539. int32_t extract(int32_t start,
  1540. int32_t startLength,
  1541. char *target,
  1542. uint32_t targetLength,
  1543. const char *codepage) const;
  1544. /**
  1545. * Convert the UnicodeString into a codepage string using an existing UConverter.
  1546. * The output string is NUL-terminated if possible.
  1547. *
  1548. * This function avoids the overhead of opening and closing a converter if
  1549. * multiple strings are extracted.
  1550. *
  1551. * @param dest destination string buffer, can be nullptr if destCapacity==0
  1552. * @param destCapacity the number of chars available at dest
  1553. * @param cnv the converter object to be used (ucnv_resetFromUnicode() will be called),
  1554. * or nullptr for the default converter
  1555. * @param errorCode normal ICU error code
  1556. * @return the length of the output string, not counting the terminating NUL;
  1557. * if the length is greater than destCapacity, then the string will not fit
  1558. * and a buffer of the indicated length would need to be passed in
  1559. * @stable ICU 2.0
  1560. */
  1561. int32_t extract(char *dest, int32_t destCapacity,
  1562. UConverter *cnv,
  1563. UErrorCode &errorCode) const;
  1564. #endif
  1565. /**
  1566. * Create a temporary substring for the specified range.
  1567. * Unlike the substring constructor and setTo() functions,
  1568. * the object returned here will be a read-only alias (using getBuffer())
  1569. * rather than copying the text.
  1570. * As a result, this substring operation is much faster but requires
  1571. * that the original string not be modified or deleted during the lifetime
  1572. * of the returned substring object.
  1573. * @param start offset of the first character visible in the substring
  1574. * @param length length of the substring
  1575. * @return a read-only alias UnicodeString object for the substring
  1576. * @stable ICU 4.4
  1577. */
  1578. UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const;
  1579. /**
  1580. * Create a temporary substring for the specified range.
  1581. * Same as tempSubString(start, length) except that the substring range
  1582. * is specified as a (start, limit) pair (with an exclusive limit index)
  1583. * rather than a (start, length) pair.
  1584. * @param start offset of the first character visible in the substring
  1585. * @param limit offset immediately following the last character visible in the substring
  1586. * @return a read-only alias UnicodeString object for the substring
  1587. * @stable ICU 4.4
  1588. */
  1589. inline UnicodeString tempSubStringBetween(int32_t start, int32_t limit=INT32_MAX) const;
  1590. /**
  1591. * Convert the UnicodeString to UTF-8 and write the result
  1592. * to a ByteSink. This is called by toUTF8String().
  1593. * Unpaired surrogates are replaced with U+FFFD.
  1594. * Calls u_strToUTF8WithSub().
  1595. *
  1596. * @param sink A ByteSink to which the UTF-8 version of the string is written.
  1597. * sink.Flush() is called at the end.
  1598. * @stable ICU 4.2
  1599. * @see toUTF8String
  1600. */
  1601. void toUTF8(ByteSink &sink) const;
  1602. /**
  1603. * Convert the UnicodeString to UTF-8 and append the result
  1604. * to a standard string.
  1605. * Unpaired surrogates are replaced with U+FFFD.
  1606. * Calls toUTF8().
  1607. *
  1608. * @param result A standard string (or a compatible object)
  1609. * to which the UTF-8 version of the string is appended.
  1610. * @return The string object.
  1611. * @stable ICU 4.2
  1612. * @see toUTF8
  1613. */
  1614. template<typename StringClass>
  1615. StringClass &toUTF8String(StringClass &result) const {
  1616. StringByteSink<StringClass> sbs(&result, length());
  1617. toUTF8(sbs);
  1618. return result;
  1619. }
  1620. /**
  1621. * Convert the UnicodeString to UTF-32.
  1622. * Unpaired surrogates are replaced with U+FFFD.
  1623. * Calls u_strToUTF32WithSub().
  1624. *
  1625. * @param utf32 destination string buffer, can be nullptr if capacity==0
  1626. * @param capacity the number of UChar32s available at utf32
  1627. * @param errorCode Standard ICU error code. Its input value must
  1628. * pass the U_SUCCESS() test, or else the function returns
  1629. * immediately. Check for U_FAILURE() on output or use with
  1630. * function chaining. (See User Guide for details.)
  1631. * @return The length of the UTF-32 string.
  1632. * @see fromUTF32
  1633. * @stable ICU 4.2
  1634. */
  1635. int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const;
  1636. /* Length operations */
  1637. /**
  1638. * Return the length of the UnicodeString object.
  1639. * The length is the number of char16_t code units are in the UnicodeString.
  1640. * If you want the number of code points, please use countChar32().
  1641. * @return the length of the UnicodeString object
  1642. * @see countChar32
  1643. * @stable ICU 2.0
  1644. */
  1645. inline int32_t length() const;
  1646. /**
  1647. * Count Unicode code points in the length char16_t code units of the string.
  1648. * A code point may occupy either one or two char16_t code units.
  1649. * Counting code points involves reading all code units.
  1650. *
  1651. * This functions is basically the inverse of moveIndex32().
  1652. *
  1653. * @param start the index of the first code unit to check
  1654. * @param length the number of char16_t code units to check
  1655. * @return the number of code points in the specified code units
  1656. * @see length
  1657. * @stable ICU 2.0
  1658. */
  1659. int32_t
  1660. countChar32(int32_t start=0, int32_t length=INT32_MAX) const;
  1661. /**
  1662. * Check if the length char16_t code units of the string
  1663. * contain more Unicode code points than a certain number.
  1664. * This is more efficient than counting all code points in this part of the string
  1665. * and comparing that number with a threshold.
  1666. * This function may not need to scan the string at all if the length
  1667. * falls within a certain range, and
  1668. * never needs to count more than 'number+1' code points.
  1669. * Logically equivalent to (countChar32(start, length)>number).
  1670. * A Unicode code point may occupy either one or two char16_t code units.
  1671. *
  1672. * @param start the index of the first code unit to check (0 for the entire string)
  1673. * @param length the number of char16_t code units to check
  1674. * (use INT32_MAX for the entire string; remember that start/length
  1675. * values are pinned)
  1676. * @param number The number of code points in the (sub)string is compared against
  1677. * the 'number' parameter.
  1678. * @return Boolean value for whether the string contains more Unicode code points
  1679. * than 'number'. Same as (u_countChar32(s, length)>number).
  1680. * @see countChar32
  1681. * @see u_strHasMoreChar32Than
  1682. * @stable ICU 2.4
  1683. */
  1684. UBool
  1685. hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const;
  1686. /**
  1687. * Determine if this string is empty.
  1688. * @return true if this string contains 0 characters, false otherwise.
  1689. * @stable ICU 2.0
  1690. */
  1691. inline UBool isEmpty() const;
  1692. /**
  1693. * Return the capacity of the internal buffer of the UnicodeString object.
  1694. * This is useful together with the getBuffer functions.
  1695. * See there for details.
  1696. *
  1697. * @return the number of char16_ts available in the internal buffer
  1698. * @see getBuffer
  1699. * @stable ICU 2.0
  1700. */
  1701. inline int32_t getCapacity() const;
  1702. /* Other operations */
  1703. /**
  1704. * Generate a hash code for this object.
  1705. * @return The hash code of this UnicodeString.
  1706. * @stable ICU 2.0
  1707. */
  1708. inline int32_t hashCode() const;
  1709. /**
  1710. * Determine if this object contains a valid string.
  1711. * A bogus string has no value. It is different from an empty string,
  1712. * although in both cases isEmpty() returns true and length() returns 0.
  1713. * setToBogus() and isBogus() can be used to indicate that no string value is available.
  1714. * For a bogus string, getBuffer() and getTerminatedBuffer() return nullptr, and
  1715. * length() returns 0.
  1716. *
  1717. * @return true if the string is bogus/invalid, false otherwise
  1718. * @see setToBogus()
  1719. * @stable ICU 2.0
  1720. */
  1721. inline UBool isBogus() const;
  1722. //========================================
  1723. // Write operations
  1724. //========================================
  1725. /* Assignment operations */
  1726. /**
  1727. * Assignment operator. Replace the characters in this UnicodeString
  1728. * with the characters from `srcText`.
  1729. *
  1730. * Starting with ICU 2.4, the assignment operator and the copy constructor
  1731. * allocate a new buffer and copy the buffer contents even for readonly aliases.
  1732. * By contrast, the fastCopyFrom() function implements the old,
  1733. * more efficient but less safe behavior
  1734. * of making this string also a readonly alias to the same buffer.
  1735. *
  1736. * If the source object has an "open" buffer from getBuffer(minCapacity),
  1737. * then the copy is an empty string.
  1738. *
  1739. * @param srcText The text containing the characters to replace
  1740. * @return a reference to this
  1741. * @stable ICU 2.0
  1742. * @see fastCopyFrom
  1743. */
  1744. UnicodeString &operator=(const UnicodeString &srcText);
  1745. /**
  1746. * Almost the same as the assignment operator.
  1747. * Replace the characters in this UnicodeString
  1748. * with the characters from `srcText`.
  1749. *
  1750. * This function works the same as the assignment operator
  1751. * for all strings except for ones that are readonly aliases.
  1752. *
  1753. * Starting with ICU 2.4, the assignment operator and the copy constructor
  1754. * allocate a new buffer and copy the buffer contents even for readonly aliases.
  1755. * This function implements the old, more efficient but less safe behavior
  1756. * of making this string also a readonly alias to the same buffer.
  1757. *
  1758. * The fastCopyFrom function must be used only if it is known that the lifetime of
  1759. * this UnicodeString does not exceed the lifetime of the aliased buffer
  1760. * including its contents, for example for strings from resource bundles
  1761. * or aliases to string constants.
  1762. *
  1763. * If the source object has an "open" buffer from getBuffer(minCapacity),
  1764. * then the copy is an empty string.
  1765. *
  1766. * @param src The text containing the characters to replace.
  1767. * @return a reference to this
  1768. * @stable ICU 2.4
  1769. */
  1770. UnicodeString &fastCopyFrom(const UnicodeString &src);
  1771. /**
  1772. * Move assignment operator; might leave src in bogus state.
  1773. * This string will have the same contents and state that the source string had.
  1774. * The behavior is undefined if *this and src are the same object.
  1775. * @param src source string
  1776. * @return *this
  1777. * @stable ICU 56
  1778. */
  1779. UnicodeString &operator=(UnicodeString &&src) noexcept;
  1780. /**
  1781. * Swap strings.
  1782. * @param other other string
  1783. * @stable ICU 56
  1784. */
  1785. void swap(UnicodeString &other) noexcept;
  1786. /**
  1787. * Non-member UnicodeString swap function.
  1788. * @param s1 will get s2's contents and state
  1789. * @param s2 will get s1's contents and state
  1790. * @stable ICU 56
  1791. */
  1792. friend inline void U_EXPORT2
  1793. swap(UnicodeString &s1, UnicodeString &s2) noexcept {
  1794. s1.swap(s2);
  1795. }
  1796. /**
  1797. * Assignment operator. Replace the characters in this UnicodeString
  1798. * with the code unit `ch`.
  1799. * @param ch the code unit to replace
  1800. * @return a reference to this
  1801. * @stable ICU 2.0
  1802. */
  1803. inline UnicodeString& operator= (char16_t ch);
  1804. /**
  1805. * Assignment operator. Replace the characters in this UnicodeString
  1806. * with the code point `ch`.
  1807. * @param ch the code point to replace
  1808. * @return a reference to this
  1809. * @stable ICU 2.0
  1810. */
  1811. inline UnicodeString& operator= (UChar32 ch);
  1812. /**
  1813. * Set the text in the UnicodeString object to the characters
  1814. * in `srcText` in the range
  1815. * [`srcStart`, `srcText.length()`).
  1816. * `srcText` is not modified.
  1817. * @param srcText the source for the new characters
  1818. * @param srcStart the offset into `srcText` where new characters
  1819. * will be obtained
  1820. * @return a reference to this
  1821. * @stable ICU 2.2
  1822. */
  1823. inline UnicodeString& setTo(const UnicodeString& srcText,
  1824. int32_t srcStart);
  1825. /**
  1826. * Set the text in the UnicodeString object to the characters
  1827. * in `srcText` in the range
  1828. * [`srcStart`, `srcStart + srcLength`).
  1829. * `srcText` is not modified.
  1830. * @param srcText the source for the new characters
  1831. * @param srcStart the offset into `srcText` where new characters
  1832. * will be obtained
  1833. * @param srcLength the number of characters in `srcText` in the
  1834. * replace string.
  1835. * @return a reference to this
  1836. * @stable ICU 2.0
  1837. */
  1838. inline UnicodeString& setTo(const UnicodeString& srcText,
  1839. int32_t srcStart,
  1840. int32_t srcLength);
  1841. /**
  1842. * Set the text in the UnicodeString object to the characters in
  1843. * `srcText`.
  1844. * `srcText` is not modified.
  1845. * @param srcText the source for the new characters
  1846. * @return a reference to this
  1847. * @stable ICU 2.0
  1848. */
  1849. inline UnicodeString& setTo(const UnicodeString& srcText);
  1850. /**
  1851. * Set the characters in the UnicodeString object to the characters
  1852. * in `srcChars`. `srcChars` is not modified.
  1853. * @param srcChars the source for the new characters
  1854. * @param srcLength the number of Unicode characters in srcChars.
  1855. * @return a reference to this
  1856. * @stable ICU 2.0
  1857. */
  1858. inline UnicodeString& setTo(const char16_t *srcChars,
  1859. int32_t srcLength);
  1860. /**
  1861. * Set the characters in the UnicodeString object to the code unit
  1862. * `srcChar`.
  1863. * @param srcChar the code unit which becomes the UnicodeString's character
  1864. * content
  1865. * @return a reference to this
  1866. * @stable ICU 2.0
  1867. */
  1868. inline UnicodeString& setTo(char16_t srcChar);
  1869. /**
  1870. * Set the characters in the UnicodeString object to the code point
  1871. * `srcChar`.
  1872. * @param srcChar the code point which becomes the UnicodeString's character
  1873. * content
  1874. * @return a reference to this
  1875. * @stable ICU 2.0
  1876. */
  1877. inline UnicodeString& setTo(UChar32 srcChar);
  1878. /**
  1879. * Aliasing setTo() function, analogous to the readonly-aliasing char16_t* constructor.
  1880. * The text will be used for the UnicodeString object, but
  1881. * it will not be released when the UnicodeString is destroyed.
  1882. * This has copy-on-write semantics:
  1883. * When the string is modified, then the buffer is first copied into
  1884. * newly allocated memory.
  1885. * The aliased buffer is never modified.
  1886. *
  1887. * In an assignment to another UnicodeString, when using the copy constructor
  1888. * or the assignment operator, the text will be copied.
  1889. * When using fastCopyFrom(), the text will be aliased again,
  1890. * so that both strings then alias the same readonly-text.
  1891. *
  1892. * @param isTerminated specifies if `text` is `NUL`-terminated.
  1893. * This must be true if `textLength==-1`.
  1894. * @param text The characters to alias for the UnicodeString.
  1895. * @param textLength The number of Unicode characters in `text` to alias.
  1896. * If -1, then this constructor will determine the length
  1897. * by calling `u_strlen()`.
  1898. * @return a reference to this
  1899. * @stable ICU 2.0
  1900. */
  1901. UnicodeString &setTo(UBool isTerminated,
  1902. ConstChar16Ptr text,
  1903. int32_t textLength);
  1904. /**
  1905. * Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor.
  1906. * The text will be used for the UnicodeString object, but
  1907. * it will not be released when the UnicodeString is destroyed.
  1908. * This has write-through semantics:
  1909. * For as long as the capacity of the buffer is sufficient, write operations
  1910. * will directly affect the buffer. When more capacity is necessary, then
  1911. * a new buffer will be allocated and the contents copied as with regularly
  1912. * constructed strings.
  1913. * In an assignment to another UnicodeString, the buffer will be copied.
  1914. * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
  1915. * as the string buffer itself and will in this case not copy the contents.
  1916. *
  1917. * @param buffer The characters to alias for the UnicodeString.
  1918. * @param buffLength The number of Unicode characters in `buffer` to alias.
  1919. * @param buffCapacity The size of `buffer` in char16_ts.
  1920. * @return a reference to this
  1921. * @stable ICU 2.0
  1922. */
  1923. UnicodeString &setTo(char16_t *buffer,
  1924. int32_t buffLength,
  1925. int32_t buffCapacity);
  1926. /**
  1927. * Make this UnicodeString object invalid.
  1928. * The string will test true with isBogus().
  1929. *
  1930. * A bogus string has no value. It is different from an empty string.
  1931. * It can be used to indicate that no string value is available.
  1932. * getBuffer() and getTerminatedBuffer() return nullptr, and
  1933. * length() returns 0.
  1934. *
  1935. * This utility function is used throughout the UnicodeString
  1936. * implementation to indicate that a UnicodeString operation failed,
  1937. * and may be used in other functions,
  1938. * especially but not exclusively when such functions do not
  1939. * take a UErrorCode for simplicity.
  1940. *
  1941. * The following methods, and no others, will clear a string object's bogus flag:
  1942. * - remove()
  1943. * - remove(0, INT32_MAX)
  1944. * - truncate(0)
  1945. * - operator=() (assignment operator)
  1946. * - setTo(...)
  1947. *
  1948. * The simplest ways to turn a bogus string into an empty one
  1949. * is to use the remove() function.
  1950. * Examples for other functions that are equivalent to "set to empty string":
  1951. * \code
  1952. * if(s.isBogus()) {
  1953. * s.remove(); // set to an empty string (remove all), or
  1954. * s.remove(0, INT32_MAX); // set to an empty string (remove all), or
  1955. * s.truncate(0); // set to an empty string (complete truncation), or
  1956. * s=UnicodeString(); // assign an empty string, or
  1957. * s.setTo((UChar32)-1); // set to a pseudo code point that is out of range, or
  1958. * s.setTo(u"", 0); // set to an empty C Unicode string
  1959. * }
  1960. * \endcode
  1961. *
  1962. * @see isBogus()
  1963. * @stable ICU 2.0
  1964. */
  1965. void setToBogus();
  1966. /**
  1967. * Set the character at the specified offset to the specified character.
  1968. * @param offset A valid offset into the text of the character to set
  1969. * @param ch The new character
  1970. * @return A reference to this
  1971. * @stable ICU 2.0
  1972. */
  1973. UnicodeString& setCharAt(int32_t offset,
  1974. char16_t ch);
  1975. /* Append operations */
  1976. /**
  1977. * Append operator. Append the code unit `ch` to the UnicodeString
  1978. * object.
  1979. * @param ch the code unit to be appended
  1980. * @return a reference to this
  1981. * @stable ICU 2.0
  1982. */
  1983. inline UnicodeString& operator+= (char16_t ch);
  1984. /**
  1985. * Append operator. Append the code point `ch` to the UnicodeString
  1986. * object.
  1987. * @param ch the code point to be appended
  1988. * @return a reference to this
  1989. * @stable ICU 2.0
  1990. */
  1991. inline UnicodeString& operator+= (UChar32 ch);
  1992. /**
  1993. * Append operator. Append the characters in `srcText` to the
  1994. * UnicodeString object. `srcText` is not modified.
  1995. * @param srcText the source for the new characters
  1996. * @return a reference to this
  1997. * @stable ICU 2.0
  1998. */
  1999. inline UnicodeString& operator+= (const UnicodeString& srcText);
  2000. /**
  2001. * Append the characters
  2002. * in `srcText` in the range
  2003. * [`srcStart`, `srcStart + srcLength`) to the
  2004. * UnicodeString object at offset `start`. `srcText`
  2005. * is not modified.
  2006. * @param srcText the source for the new characters
  2007. * @param srcStart the offset into `srcText` where new characters
  2008. * will be obtained
  2009. * @param srcLength the number of characters in `srcText` in
  2010. * the append string
  2011. * @return a reference to this
  2012. * @stable ICU 2.0
  2013. */
  2014. inline UnicodeString& append(const UnicodeString& srcText,
  2015. int32_t srcStart,
  2016. int32_t srcLength);
  2017. /**
  2018. * Append the characters in `srcText` to the UnicodeString object.
  2019. * `srcText` is not modified.
  2020. * @param srcText the source for the new characters
  2021. * @return a reference to this
  2022. * @stable ICU 2.0
  2023. */
  2024. inline UnicodeString& append(const UnicodeString& srcText);
  2025. /**
  2026. * Append the characters in `srcChars` in the range
  2027. * [`srcStart`, `srcStart + srcLength`) to the UnicodeString
  2028. * object at offset
  2029. * `start`. `srcChars` is not modified.
  2030. * @param srcChars the source for the new characters
  2031. * @param srcStart the offset into `srcChars` where new characters
  2032. * will be obtained
  2033. * @param srcLength the number of characters in `srcChars` in
  2034. * the append string; can be -1 if `srcChars` is NUL-terminated
  2035. * @return a reference to this
  2036. * @stable ICU 2.0
  2037. */
  2038. inline UnicodeString& append(const char16_t *srcChars,
  2039. int32_t srcStart,
  2040. int32_t srcLength);
  2041. /**
  2042. * Append the characters in `srcChars` to the UnicodeString object
  2043. * at offset `start`. `srcChars` is not modified.
  2044. * @param srcChars the source for the new characters
  2045. * @param srcLength the number of Unicode characters in `srcChars`;
  2046. * can be -1 if `srcChars` is NUL-terminated
  2047. * @return a reference to this
  2048. * @stable ICU 2.0
  2049. */
  2050. inline UnicodeString& append(ConstChar16Ptr srcChars,
  2051. int32_t srcLength);
  2052. /**
  2053. * Append the code unit `srcChar` to the UnicodeString object.
  2054. * @param srcChar the code unit to append
  2055. * @return a reference to this
  2056. * @stable ICU 2.0
  2057. */
  2058. inline UnicodeString& append(char16_t srcChar);
  2059. /**
  2060. * Append the code point `srcChar` to the UnicodeString object.
  2061. * @param srcChar the code point to append
  2062. * @return a reference to this
  2063. * @stable ICU 2.0
  2064. */
  2065. UnicodeString& append(UChar32 srcChar);
  2066. /* Insert operations */
  2067. /**
  2068. * Insert the characters in `srcText` in the range
  2069. * [`srcStart`, `srcStart + srcLength`) into the UnicodeString
  2070. * object at offset `start`. `srcText` is not modified.
  2071. * @param start the offset where the insertion begins
  2072. * @param srcText the source for the new characters
  2073. * @param srcStart the offset into `srcText` where new characters
  2074. * will be obtained
  2075. * @param srcLength the number of characters in `srcText` in
  2076. * the insert string
  2077. * @return a reference to this
  2078. * @stable ICU 2.0
  2079. */
  2080. inline UnicodeString& insert(int32_t start,
  2081. const UnicodeString& srcText,
  2082. int32_t srcStart,
  2083. int32_t srcLength);
  2084. /**
  2085. * Insert the characters in `srcText` into the UnicodeString object
  2086. * at offset `start`. `srcText` is not modified.
  2087. * @param start the offset where the insertion begins
  2088. * @param srcText the source for the new characters
  2089. * @return a reference to this
  2090. * @stable ICU 2.0
  2091. */
  2092. inline UnicodeString& insert(int32_t start,
  2093. const UnicodeString& srcText);
  2094. /**
  2095. * Insert the characters in `srcChars` in the range
  2096. * [`srcStart`, `srcStart + srcLength`) into the UnicodeString
  2097. * object at offset `start`. `srcChars` is not modified.
  2098. * @param start the offset at which the insertion begins
  2099. * @param srcChars the source for the new characters
  2100. * @param srcStart the offset into `srcChars` where new characters
  2101. * will be obtained
  2102. * @param srcLength the number of characters in `srcChars`
  2103. * in the insert string
  2104. * @return a reference to this
  2105. * @stable ICU 2.0
  2106. */
  2107. inline UnicodeString& insert(int32_t start,
  2108. const char16_t *srcChars,
  2109. int32_t srcStart,
  2110. int32_t srcLength);
  2111. /**
  2112. * Insert the characters in `srcChars` into the UnicodeString object
  2113. * at offset `start`. `srcChars` is not modified.
  2114. * @param start the offset where the insertion begins
  2115. * @param srcChars the source for the new characters
  2116. * @param srcLength the number of Unicode characters in srcChars.
  2117. * @return a reference to this
  2118. * @stable ICU 2.0
  2119. */
  2120. inline UnicodeString& insert(int32_t start,
  2121. ConstChar16Ptr srcChars,
  2122. int32_t srcLength);
  2123. /**
  2124. * Insert the code unit `srcChar` into the UnicodeString object at
  2125. * offset `start`.
  2126. * @param start the offset at which the insertion occurs
  2127. * @param srcChar the code unit to insert
  2128. * @return a reference to this
  2129. * @stable ICU 2.0
  2130. */
  2131. inline UnicodeString& insert(int32_t start,
  2132. char16_t srcChar);
  2133. /**
  2134. * Insert the code point `srcChar` into the UnicodeString object at
  2135. * offset `start`.
  2136. * @param start the offset at which the insertion occurs
  2137. * @param srcChar the code point to insert
  2138. * @return a reference to this
  2139. * @stable ICU 2.0
  2140. */
  2141. inline UnicodeString& insert(int32_t start,
  2142. UChar32 srcChar);
  2143. /* Replace operations */
  2144. /**
  2145. * Replace the characters in the range
  2146. * [`start`, `start + length`) with the characters in
  2147. * `srcText` in the range
  2148. * [`srcStart`, `srcStart + srcLength`).
  2149. * `srcText` is not modified.
  2150. * @param start the offset at which the replace operation begins
  2151. * @param length the number of characters to replace. The character at
  2152. * `start + length` is not modified.
  2153. * @param srcText the source for the new characters
  2154. * @param srcStart the offset into `srcText` where new characters
  2155. * will be obtained
  2156. * @param srcLength the number of characters in `srcText` in
  2157. * the replace string
  2158. * @return a reference to this
  2159. * @stable ICU 2.0
  2160. */
  2161. inline UnicodeString& replace(int32_t start,
  2162. int32_t length,
  2163. const UnicodeString& srcText,
  2164. int32_t srcStart,
  2165. int32_t srcLength);
  2166. /**
  2167. * Replace the characters in the range
  2168. * [`start`, `start + length`)
  2169. * with the characters in `srcText`. `srcText` is
  2170. * not modified.
  2171. * @param start the offset at which the replace operation begins
  2172. * @param length the number of characters to replace. The character at
  2173. * `start + length` is not modified.
  2174. * @param srcText the source for the new characters
  2175. * @return a reference to this
  2176. * @stable ICU 2.0
  2177. */
  2178. inline UnicodeString& replace(int32_t start,
  2179. int32_t length,
  2180. const UnicodeString& srcText);
  2181. /**
  2182. * Replace the characters in the range
  2183. * [`start`, `start + length`) with the characters in
  2184. * `srcChars` in the range
  2185. * [`srcStart`, `srcStart + srcLength`). `srcChars`
  2186. * is not modified.
  2187. * @param start the offset at which the replace operation begins
  2188. * @param length the number of characters to replace. The character at
  2189. * `start + length` is not modified.
  2190. * @param srcChars the source for the new characters
  2191. * @param srcStart the offset into `srcChars` where new characters
  2192. * will be obtained
  2193. * @param srcLength the number of characters in `srcChars`
  2194. * in the replace string
  2195. * @return a reference to this
  2196. * @stable ICU 2.0
  2197. */
  2198. inline UnicodeString& replace(int32_t start,
  2199. int32_t length,
  2200. const char16_t *srcChars,
  2201. int32_t srcStart,
  2202. int32_t srcLength);
  2203. /**
  2204. * Replace the characters in the range
  2205. * [`start`, `start + length`) with the characters in
  2206. * `srcChars`. `srcChars` is not modified.
  2207. * @param start the offset at which the replace operation begins
  2208. * @param length number of characters to replace. The character at
  2209. * `start + length` is not modified.
  2210. * @param srcChars the source for the new characters
  2211. * @param srcLength the number of Unicode characters in srcChars
  2212. * @return a reference to this
  2213. * @stable ICU 2.0
  2214. */
  2215. inline UnicodeString& replace(int32_t start,
  2216. int32_t length,
  2217. ConstChar16Ptr srcChars,
  2218. int32_t srcLength);
  2219. /**
  2220. * Replace the characters in the range
  2221. * [`start`, `start + length`) with the code unit
  2222. * `srcChar`.
  2223. * @param start the offset at which the replace operation begins
  2224. * @param length the number of characters to replace. The character at
  2225. * `start + length` is not modified.
  2226. * @param srcChar the new code unit
  2227. * @return a reference to this
  2228. * @stable ICU 2.0
  2229. */
  2230. inline UnicodeString& replace(int32_t start,
  2231. int32_t length,
  2232. char16_t srcChar);
  2233. /**
  2234. * Replace the characters in the range
  2235. * [`start`, `start + length`) with the code point
  2236. * `srcChar`.
  2237. * @param start the offset at which the replace operation begins
  2238. * @param length the number of characters to replace. The character at
  2239. * `start + length` is not modified.
  2240. * @param srcChar the new code point
  2241. * @return a reference to this
  2242. * @stable ICU 2.0
  2243. */
  2244. UnicodeString& replace(int32_t start, int32_t length, UChar32 srcChar);
  2245. /**
  2246. * Replace the characters in the range [`start`, `limit`)
  2247. * with the characters in `srcText`. `srcText` is not modified.
  2248. * @param start the offset at which the replace operation begins
  2249. * @param limit the offset immediately following the replace range
  2250. * @param srcText the source for the new characters
  2251. * @return a reference to this
  2252. * @stable ICU 2.0
  2253. */
  2254. inline UnicodeString& replaceBetween(int32_t start,
  2255. int32_t limit,
  2256. const UnicodeString& srcText);
  2257. /**
  2258. * Replace the characters in the range [`start`, `limit`)
  2259. * with the characters in `srcText` in the range
  2260. * [`srcStart`, `srcLimit`). `srcText` is not modified.
  2261. * @param start the offset at which the replace operation begins
  2262. * @param limit the offset immediately following the replace range
  2263. * @param srcText the source for the new characters
  2264. * @param srcStart the offset into `srcChars` where new characters
  2265. * will be obtained
  2266. * @param srcLimit the offset immediately following the range to copy
  2267. * in `srcText`
  2268. * @return a reference to this
  2269. * @stable ICU 2.0
  2270. */
  2271. inline UnicodeString& replaceBetween(int32_t start,
  2272. int32_t limit,
  2273. const UnicodeString& srcText,
  2274. int32_t srcStart,
  2275. int32_t srcLimit);
  2276. /**
  2277. * Replace a substring of this object with the given text.
  2278. * @param start the beginning index, inclusive; `0 <= start <= limit`.
  2279. * @param limit the ending index, exclusive; `start <= limit <= length()`.
  2280. * @param text the text to replace characters `start` to `limit - 1`
  2281. * @stable ICU 2.0
  2282. */
  2283. virtual void handleReplaceBetween(int32_t start,
  2284. int32_t limit,
  2285. const UnicodeString& text) override;
  2286. /**
  2287. * Replaceable API
  2288. * @return true if it has MetaData
  2289. * @stable ICU 2.4
  2290. */
  2291. virtual UBool hasMetaData() const override;
  2292. /**
  2293. * Copy a substring of this object, retaining attribute (out-of-band)
  2294. * information. This method is used to duplicate or reorder substrings.
  2295. * The destination index must not overlap the source range.
  2296. *
  2297. * @param start the beginning index, inclusive; `0 <= start <= limit`.
  2298. * @param limit the ending index, exclusive; `start <= limit <= length()`.
  2299. * @param dest the destination index. The characters from
  2300. * `start..limit-1` will be copied to `dest`.
  2301. * Implementations of this method may assume that `dest <= start ||
  2302. * dest >= limit`.
  2303. * @stable ICU 2.0
  2304. */
  2305. virtual void copy(int32_t start, int32_t limit, int32_t dest) override;
  2306. /* Search and replace operations */
  2307. /**
  2308. * Replace all occurrences of characters in oldText with the characters
  2309. * in newText
  2310. * @param oldText the text containing the search text
  2311. * @param newText the text containing the replacement text
  2312. * @return a reference to this
  2313. * @stable ICU 2.0
  2314. */
  2315. inline UnicodeString& findAndReplace(const UnicodeString& oldText,
  2316. const UnicodeString& newText);
  2317. /**
  2318. * Replace all occurrences of characters in oldText with characters
  2319. * in newText
  2320. * in the range [`start`, `start + length`).
  2321. * @param start the start of the range in which replace will performed
  2322. * @param length the length of the range in which replace will be performed
  2323. * @param oldText the text containing the search text
  2324. * @param newText the text containing the replacement text
  2325. * @return a reference to this
  2326. * @stable ICU 2.0
  2327. */
  2328. inline UnicodeString& findAndReplace(int32_t start,
  2329. int32_t length,
  2330. const UnicodeString& oldText,
  2331. const UnicodeString& newText);
  2332. /**
  2333. * Replace all occurrences of characters in oldText in the range
  2334. * [`oldStart`, `oldStart + oldLength`) with the characters
  2335. * in newText in the range
  2336. * [`newStart`, `newStart + newLength`)
  2337. * in the range [`start`, `start + length`).
  2338. * @param start the start of the range in which replace will performed
  2339. * @param length the length of the range in which replace will be performed
  2340. * @param oldText the text containing the search text
  2341. * @param oldStart the start of the search range in `oldText`
  2342. * @param oldLength the length of the search range in `oldText`
  2343. * @param newText the text containing the replacement text
  2344. * @param newStart the start of the replacement range in `newText`
  2345. * @param newLength the length of the replacement range in `newText`
  2346. * @return a reference to this
  2347. * @stable ICU 2.0
  2348. */
  2349. UnicodeString& findAndReplace(int32_t start,
  2350. int32_t length,
  2351. const UnicodeString& oldText,
  2352. int32_t oldStart,
  2353. int32_t oldLength,
  2354. const UnicodeString& newText,
  2355. int32_t newStart,
  2356. int32_t newLength);
  2357. /* Remove operations */
  2358. /**
  2359. * Removes all characters from the UnicodeString object and clears the bogus flag.
  2360. * This is the UnicodeString equivalent of std::string’s clear().
  2361. *
  2362. * @return a reference to this
  2363. * @see setToBogus
  2364. * @stable ICU 2.0
  2365. */
  2366. inline UnicodeString& remove();
  2367. /**
  2368. * Remove the characters in the range
  2369. * [`start`, `start + length`) from the UnicodeString object.
  2370. * @param start the offset of the first character to remove
  2371. * @param length the number of characters to remove
  2372. * @return a reference to this
  2373. * @stable ICU 2.0
  2374. */
  2375. inline UnicodeString& remove(int32_t start,
  2376. int32_t length = (int32_t)INT32_MAX);
  2377. /**
  2378. * Remove the characters in the range
  2379. * [`start`, `limit`) from the UnicodeString object.
  2380. * @param start the offset of the first character to remove
  2381. * @param limit the offset immediately following the range to remove
  2382. * @return a reference to this
  2383. * @stable ICU 2.0
  2384. */
  2385. inline UnicodeString& removeBetween(int32_t start,
  2386. int32_t limit = (int32_t)INT32_MAX);
  2387. /**
  2388. * Retain only the characters in the range
  2389. * [`start`, `limit`) from the UnicodeString object.
  2390. * Removes characters before `start` and at and after `limit`.
  2391. * @param start the offset of the first character to retain
  2392. * @param limit the offset immediately following the range to retain
  2393. * @return a reference to this
  2394. * @stable ICU 4.4
  2395. */
  2396. inline UnicodeString &retainBetween(int32_t start, int32_t limit = INT32_MAX);
  2397. /* Length operations */
  2398. /**
  2399. * Pad the start of this UnicodeString with the character `padChar`.
  2400. * If the length of this UnicodeString is less than targetLength,
  2401. * length() - targetLength copies of padChar will be added to the
  2402. * beginning of this UnicodeString.
  2403. * @param targetLength the desired length of the string
  2404. * @param padChar the character to use for padding. Defaults to
  2405. * space (U+0020)
  2406. * @return true if the text was padded, false otherwise.
  2407. * @stable ICU 2.0
  2408. */
  2409. UBool padLeading(int32_t targetLength,
  2410. char16_t padChar = 0x0020);
  2411. /**
  2412. * Pad the end of this UnicodeString with the character `padChar`.
  2413. * If the length of this UnicodeString is less than targetLength,
  2414. * length() - targetLength copies of padChar will be added to the
  2415. * end of this UnicodeString.
  2416. * @param targetLength the desired length of the string
  2417. * @param padChar the character to use for padding. Defaults to
  2418. * space (U+0020)
  2419. * @return true if the text was padded, false otherwise.
  2420. * @stable ICU 2.0
  2421. */
  2422. UBool padTrailing(int32_t targetLength,
  2423. char16_t padChar = 0x0020);
  2424. /**
  2425. * Truncate this UnicodeString to the `targetLength`.
  2426. * @param targetLength the desired length of this UnicodeString.
  2427. * @return true if the text was truncated, false otherwise
  2428. * @stable ICU 2.0
  2429. */
  2430. inline UBool truncate(int32_t targetLength);
  2431. /**
  2432. * Trims leading and trailing whitespace from this UnicodeString.
  2433. * @return a reference to this
  2434. * @stable ICU 2.0
  2435. */
  2436. UnicodeString& trim();
  2437. /* Miscellaneous operations */
  2438. /**
  2439. * Reverse this UnicodeString in place.
  2440. * @return a reference to this
  2441. * @stable ICU 2.0
  2442. */
  2443. inline UnicodeString& reverse();
  2444. /**
  2445. * Reverse the range [`start`, `start + length`) in
  2446. * this UnicodeString.
  2447. * @param start the start of the range to reverse
  2448. * @param length the number of characters to to reverse
  2449. * @return a reference to this
  2450. * @stable ICU 2.0
  2451. */
  2452. inline UnicodeString& reverse(int32_t start,
  2453. int32_t length);
  2454. /**
  2455. * Convert the characters in this to UPPER CASE following the conventions of
  2456. * the default locale.
  2457. * @return A reference to this.
  2458. * @stable ICU 2.0
  2459. */
  2460. UnicodeString& toUpper();
  2461. /**
  2462. * Convert the characters in this to UPPER CASE following the conventions of
  2463. * a specific locale.
  2464. * @param locale The locale containing the conventions to use.
  2465. * @return A reference to this.
  2466. * @stable ICU 2.0
  2467. */
  2468. UnicodeString& toUpper(const Locale& locale);
  2469. /**
  2470. * Convert the characters in this to lower case following the conventions of
  2471. * the default locale.
  2472. * @return A reference to this.
  2473. * @stable ICU 2.0
  2474. */
  2475. UnicodeString& toLower();
  2476. /**
  2477. * Convert the characters in this to lower case following the conventions of
  2478. * a specific locale.
  2479. * @param locale The locale containing the conventions to use.
  2480. * @return A reference to this.
  2481. * @stable ICU 2.0
  2482. */
  2483. UnicodeString& toLower(const Locale& locale);
  2484. #if !UCONFIG_NO_BREAK_ITERATION
  2485. /**
  2486. * Titlecase this string, convenience function using the default locale.
  2487. *
  2488. * Casing is locale-dependent and context-sensitive.
  2489. * Titlecasing uses a break iterator to find the first characters of words
  2490. * that are to be titlecased. It titlecases those characters and lowercases
  2491. * all others.
  2492. *
  2493. * The titlecase break iterator can be provided to customize for arbitrary
  2494. * styles, using rules and dictionaries beyond the standard iterators.
  2495. * It may be more efficient to always provide an iterator to avoid
  2496. * opening and closing one for each string.
  2497. * The standard titlecase iterator for the root locale implements the
  2498. * algorithm of Unicode TR 21.
  2499. *
  2500. * This function uses only the setText(), first() and next() methods of the
  2501. * provided break iterator.
  2502. *
  2503. * @param titleIter A break iterator to find the first characters of words
  2504. * that are to be titlecased.
  2505. * If none is provided (0), then a standard titlecase
  2506. * break iterator is opened.
  2507. * Otherwise the provided iterator is set to the string's text.
  2508. * @return A reference to this.
  2509. * @stable ICU 2.1
  2510. */
  2511. UnicodeString &toTitle(BreakIterator *titleIter);
  2512. /**
  2513. * Titlecase this string.
  2514. *
  2515. * Casing is locale-dependent and context-sensitive.
  2516. * Titlecasing uses a break iterator to find the first characters of words
  2517. * that are to be titlecased. It titlecases those characters and lowercases
  2518. * all others.
  2519. *
  2520. * The titlecase break iterator can be provided to customize for arbitrary
  2521. * styles, using rules and dictionaries beyond the standard iterators.
  2522. * It may be more efficient to always provide an iterator to avoid
  2523. * opening and closing one for each string.
  2524. * The standard titlecase iterator for the root locale implements the
  2525. * algorithm of Unicode TR 21.
  2526. *
  2527. * This function uses only the setText(), first() and next() methods of the
  2528. * provided break iterator.
  2529. *
  2530. * @param titleIter A break iterator to find the first characters of words
  2531. * that are to be titlecased.
  2532. * If none is provided (0), then a standard titlecase
  2533. * break iterator is opened.
  2534. * Otherwise the provided iterator is set to the string's text.
  2535. * @param locale The locale to consider.
  2536. * @return A reference to this.
  2537. * @stable ICU 2.1
  2538. */
  2539. UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale);
  2540. /**
  2541. * Titlecase this string, with options.
  2542. *
  2543. * Casing is locale-dependent and context-sensitive.
  2544. * Titlecasing uses a break iterator to find the first characters of words
  2545. * that are to be titlecased. It titlecases those characters and lowercases
  2546. * all others. (This can be modified with options.)
  2547. *
  2548. * The titlecase break iterator can be provided to customize for arbitrary
  2549. * styles, using rules and dictionaries beyond the standard iterators.
  2550. * It may be more efficient to always provide an iterator to avoid
  2551. * opening and closing one for each string.
  2552. * The standard titlecase iterator for the root locale implements the
  2553. * algorithm of Unicode TR 21.
  2554. *
  2555. * This function uses only the setText(), first() and next() methods of the
  2556. * provided break iterator.
  2557. *
  2558. * @param titleIter A break iterator to find the first characters of words
  2559. * that are to be titlecased.
  2560. * If none is provided (0), then a standard titlecase
  2561. * break iterator is opened.
  2562. * Otherwise the provided iterator is set to the string's text.
  2563. * @param locale The locale to consider.
  2564. * @param options Options bit set, usually 0. See U_TITLECASE_NO_LOWERCASE,
  2565. * U_TITLECASE_NO_BREAK_ADJUSTMENT, U_TITLECASE_ADJUST_TO_CASED,
  2566. * U_TITLECASE_WHOLE_STRING, U_TITLECASE_SENTENCES.
  2567. * @return A reference to this.
  2568. * @stable ICU 3.8
  2569. */
  2570. UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options);
  2571. #endif
  2572. /**
  2573. * Case-folds the characters in this string.
  2574. *
  2575. * Case-folding is locale-independent and not context-sensitive,
  2576. * but there is an option for whether to include or exclude mappings for dotted I
  2577. * and dotless i that are marked with 'T' in CaseFolding.txt.
  2578. *
  2579. * The result may be longer or shorter than the original.
  2580. *
  2581. * @param options Either U_FOLD_CASE_DEFAULT or U_FOLD_CASE_EXCLUDE_SPECIAL_I
  2582. * @return A reference to this.
  2583. * @stable ICU 2.0
  2584. */
  2585. UnicodeString &foldCase(uint32_t options=0 /*U_FOLD_CASE_DEFAULT*/);
  2586. //========================================
  2587. // Access to the internal buffer
  2588. //========================================
  2589. /**
  2590. * Get a read/write pointer to the internal buffer.
  2591. * The buffer is guaranteed to be large enough for at least minCapacity char16_ts,
  2592. * writable, and is still owned by the UnicodeString object.
  2593. * Calls to getBuffer(minCapacity) must not be nested, and
  2594. * must be matched with calls to releaseBuffer(newLength).
  2595. * If the string buffer was read-only or shared,
  2596. * then it will be reallocated and copied.
  2597. *
  2598. * An attempted nested call will return 0, and will not further modify the
  2599. * state of the UnicodeString object.
  2600. * It also returns 0 if the string is bogus.
  2601. *
  2602. * The actual capacity of the string buffer may be larger than minCapacity.
  2603. * getCapacity() returns the actual capacity.
  2604. * For many operations, the full capacity should be used to avoid reallocations.
  2605. *
  2606. * While the buffer is "open" between getBuffer(minCapacity)
  2607. * and releaseBuffer(newLength), the following applies:
  2608. * - The string length is set to 0.
  2609. * - Any read API call on the UnicodeString object will behave like on a 0-length string.
  2610. * - Any write API call on the UnicodeString object is disallowed and will have no effect.
  2611. * - You can read from and write to the returned buffer.
  2612. * - The previous string contents will still be in the buffer;
  2613. * if you want to use it, then you need to call length() before getBuffer(minCapacity).
  2614. * If the length() was greater than minCapacity, then any contents after minCapacity
  2615. * may be lost.
  2616. * The buffer contents is not NUL-terminated by getBuffer().
  2617. * If length() < getCapacity() then you can terminate it by writing a NUL
  2618. * at index length().
  2619. * - You must call releaseBuffer(newLength) before and in order to
  2620. * return to normal UnicodeString operation.
  2621. *
  2622. * @param minCapacity the minimum number of char16_ts that are to be available
  2623. * in the buffer, starting at the returned pointer;
  2624. * default to the current string capacity if minCapacity==-1
  2625. * @return a writable pointer to the internal string buffer,
  2626. * or nullptr if an error occurs (nested calls, out of memory)
  2627. *
  2628. * @see releaseBuffer
  2629. * @see getTerminatedBuffer()
  2630. * @stable ICU 2.0
  2631. */
  2632. char16_t *getBuffer(int32_t minCapacity);
  2633. /**
  2634. * Release a read/write buffer on a UnicodeString object with an
  2635. * "open" getBuffer(minCapacity).
  2636. * This function must be called in a matched pair with getBuffer(minCapacity).
  2637. * releaseBuffer(newLength) must be called if and only if a getBuffer(minCapacity) is "open".
  2638. *
  2639. * It will set the string length to newLength, at most to the current capacity.
  2640. * If newLength==-1 then it will set the length according to the
  2641. * first NUL in the buffer, or to the capacity if there is no NUL.
  2642. *
  2643. * After calling releaseBuffer(newLength) the UnicodeString is back to normal operation.
  2644. *
  2645. * @param newLength the new length of the UnicodeString object;
  2646. * defaults to the current capacity if newLength is greater than that;
  2647. * if newLength==-1, it defaults to u_strlen(buffer) but not more than
  2648. * the current capacity of the string
  2649. *
  2650. * @see getBuffer(int32_t minCapacity)
  2651. * @stable ICU 2.0
  2652. */
  2653. void releaseBuffer(int32_t newLength=-1);
  2654. /**
  2655. * Get a read-only pointer to the internal buffer.
  2656. * This can be called at any time on a valid UnicodeString.
  2657. *
  2658. * It returns 0 if the string is bogus, or
  2659. * during an "open" getBuffer(minCapacity).
  2660. *
  2661. * It can be called as many times as desired.
  2662. * The pointer that it returns will remain valid until the UnicodeString object is modified,
  2663. * at which time the pointer is semantically invalidated and must not be used any more.
  2664. *
  2665. * The capacity of the buffer can be determined with getCapacity().
  2666. * The part after length() may or may not be initialized and valid,
  2667. * depending on the history of the UnicodeString object.
  2668. *
  2669. * The buffer contents is (probably) not NUL-terminated.
  2670. * You can check if it is with
  2671. * `(s.length() < s.getCapacity() && buffer[s.length()]==0)`.
  2672. * (See getTerminatedBuffer().)
  2673. *
  2674. * The buffer may reside in read-only memory. Its contents must not
  2675. * be modified.
  2676. *
  2677. * @return a read-only pointer to the internal string buffer,
  2678. * or nullptr if the string is empty or bogus
  2679. *
  2680. * @see getBuffer(int32_t minCapacity)
  2681. * @see getTerminatedBuffer()
  2682. * @stable ICU 2.0
  2683. */
  2684. inline const char16_t *getBuffer() const;
  2685. /**
  2686. * Get a read-only pointer to the internal buffer,
  2687. * making sure that it is NUL-terminated.
  2688. * This can be called at any time on a valid UnicodeString.
  2689. *
  2690. * It returns 0 if the string is bogus, or
  2691. * during an "open" getBuffer(minCapacity), or if the buffer cannot
  2692. * be NUL-terminated (because memory allocation failed).
  2693. *
  2694. * It can be called as many times as desired.
  2695. * The pointer that it returns will remain valid until the UnicodeString object is modified,
  2696. * at which time the pointer is semantically invalidated and must not be used any more.
  2697. *
  2698. * The capacity of the buffer can be determined with getCapacity().
  2699. * The part after length()+1 may or may not be initialized and valid,
  2700. * depending on the history of the UnicodeString object.
  2701. *
  2702. * The buffer contents is guaranteed to be NUL-terminated.
  2703. * getTerminatedBuffer() may reallocate the buffer if a terminating NUL
  2704. * is written.
  2705. * For this reason, this function is not const, unlike getBuffer().
  2706. * Note that a UnicodeString may also contain NUL characters as part of its contents.
  2707. *
  2708. * The buffer may reside in read-only memory. Its contents must not
  2709. * be modified.
  2710. *
  2711. * @return a read-only pointer to the internal string buffer,
  2712. * or 0 if the string is empty or bogus
  2713. *
  2714. * @see getBuffer(int32_t minCapacity)
  2715. * @see getBuffer()
  2716. * @stable ICU 2.2
  2717. */
  2718. const char16_t *getTerminatedBuffer();
  2719. //========================================
  2720. // Constructors
  2721. //========================================
  2722. /** Construct an empty UnicodeString.
  2723. * @stable ICU 2.0
  2724. */
  2725. inline UnicodeString();
  2726. /**
  2727. * Construct a UnicodeString with capacity to hold `capacity` char16_ts
  2728. * @param capacity the number of char16_ts this UnicodeString should hold
  2729. * before a resize is necessary; if count is greater than 0 and count
  2730. * code points c take up more space than capacity, then capacity is adjusted
  2731. * accordingly.
  2732. * @param c is used to initially fill the string
  2733. * @param count specifies how many code points c are to be written in the
  2734. * string
  2735. * @stable ICU 2.0
  2736. */
  2737. UnicodeString(int32_t capacity, UChar32 c, int32_t count);
  2738. /**
  2739. * Single char16_t (code unit) constructor.
  2740. *
  2741. * It is recommended to mark this constructor "explicit" by
  2742. * `-DUNISTR_FROM_CHAR_EXPLICIT=explicit`
  2743. * on the compiler command line or similar.
  2744. * @param ch the character to place in the UnicodeString
  2745. * @stable ICU 2.0
  2746. */
  2747. UNISTR_FROM_CHAR_EXPLICIT UnicodeString(char16_t ch);
  2748. /**
  2749. * Single UChar32 (code point) constructor.
  2750. *
  2751. * It is recommended to mark this constructor "explicit" by
  2752. * `-DUNISTR_FROM_CHAR_EXPLICIT=explicit`
  2753. * on the compiler command line or similar.
  2754. * @param ch the character to place in the UnicodeString
  2755. * @stable ICU 2.0
  2756. */
  2757. UNISTR_FROM_CHAR_EXPLICIT UnicodeString(UChar32 ch);
  2758. /**
  2759. * char16_t* constructor.
  2760. *
  2761. * It is recommended to mark this constructor "explicit" by
  2762. * `-DUNISTR_FROM_STRING_EXPLICIT=explicit`
  2763. * on the compiler command line or similar.
  2764. * @param text The characters to place in the UnicodeString. `text`
  2765. * must be NUL (U+0000) terminated.
  2766. * @stable ICU 2.0
  2767. */
  2768. UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char16_t *text);
  2769. #if !U_CHAR16_IS_TYPEDEF
  2770. /**
  2771. * uint16_t * constructor.
  2772. * Delegates to UnicodeString(const char16_t *).
  2773. *
  2774. * It is recommended to mark this constructor "explicit" by
  2775. * `-DUNISTR_FROM_STRING_EXPLICIT=explicit`
  2776. * on the compiler command line or similar.
  2777. * @param text NUL-terminated UTF-16 string
  2778. * @stable ICU 59
  2779. */
  2780. UNISTR_FROM_STRING_EXPLICIT UnicodeString(const uint16_t *text) :
  2781. UnicodeString(ConstChar16Ptr(text)) {}
  2782. #endif
  2783. #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
  2784. /**
  2785. * wchar_t * constructor.
  2786. * (Only defined if U_SIZEOF_WCHAR_T==2.)
  2787. * Delegates to UnicodeString(const char16_t *).
  2788. *
  2789. * It is recommended to mark this constructor "explicit" by
  2790. * `-DUNISTR_FROM_STRING_EXPLICIT=explicit`
  2791. * on the compiler command line or similar.
  2792. * @param text NUL-terminated UTF-16 string
  2793. * @stable ICU 59
  2794. */
  2795. UNISTR_FROM_STRING_EXPLICIT UnicodeString(const wchar_t *text) :
  2796. UnicodeString(ConstChar16Ptr(text)) {}
  2797. #endif
  2798. /**
  2799. * nullptr_t constructor.
  2800. * Effectively the same as the default constructor, makes an empty string object.
  2801. *
  2802. * It is recommended to mark this constructor "explicit" by
  2803. * `-DUNISTR_FROM_STRING_EXPLICIT=explicit`
  2804. * on the compiler command line or similar.
  2805. * @param text nullptr
  2806. * @stable ICU 59
  2807. */
  2808. UNISTR_FROM_STRING_EXPLICIT inline UnicodeString(const std::nullptr_t text);
  2809. /**
  2810. * char16_t* constructor.
  2811. * @param text The characters to place in the UnicodeString.
  2812. * @param textLength The number of Unicode characters in `text`
  2813. * to copy.
  2814. * @stable ICU 2.0
  2815. */
  2816. UnicodeString(const char16_t *text,
  2817. int32_t textLength);
  2818. #if !U_CHAR16_IS_TYPEDEF
  2819. /**
  2820. * uint16_t * constructor.
  2821. * Delegates to UnicodeString(const char16_t *, int32_t).
  2822. * @param text UTF-16 string
  2823. * @param textLength string length
  2824. * @stable ICU 59
  2825. */
  2826. UnicodeString(const uint16_t *text, int32_t textLength) :
  2827. UnicodeString(ConstChar16Ptr(text), textLength) {}
  2828. #endif
  2829. #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
  2830. /**
  2831. * wchar_t * constructor.
  2832. * (Only defined if U_SIZEOF_WCHAR_T==2.)
  2833. * Delegates to UnicodeString(const char16_t *, int32_t).
  2834. * @param text NUL-terminated UTF-16 string
  2835. * @param textLength string length
  2836. * @stable ICU 59
  2837. */
  2838. UnicodeString(const wchar_t *text, int32_t textLength) :
  2839. UnicodeString(ConstChar16Ptr(text), textLength) {}
  2840. #endif
  2841. /**
  2842. * nullptr_t constructor.
  2843. * Effectively the same as the default constructor, makes an empty string object.
  2844. * @param text nullptr
  2845. * @param textLength ignored
  2846. * @stable ICU 59
  2847. */
  2848. inline UnicodeString(const std::nullptr_t text, int32_t textLength);
  2849. /**
  2850. * Readonly-aliasing char16_t* constructor.
  2851. * The text will be used for the UnicodeString object, but
  2852. * it will not be released when the UnicodeString is destroyed.
  2853. * This has copy-on-write semantics:
  2854. * When the string is modified, then the buffer is first copied into
  2855. * newly allocated memory.
  2856. * The aliased buffer is never modified.
  2857. *
  2858. * In an assignment to another UnicodeString, when using the copy constructor
  2859. * or the assignment operator, the text will be copied.
  2860. * When using fastCopyFrom(), the text will be aliased again,
  2861. * so that both strings then alias the same readonly-text.
  2862. *
  2863. * @param isTerminated specifies if `text` is `NUL`-terminated.
  2864. * This must be true if `textLength==-1`.
  2865. * @param text The characters to alias for the UnicodeString.
  2866. * @param textLength The number of Unicode characters in `text` to alias.
  2867. * If -1, then this constructor will determine the length
  2868. * by calling `u_strlen()`.
  2869. * @stable ICU 2.0
  2870. */
  2871. UnicodeString(UBool isTerminated,
  2872. ConstChar16Ptr text,
  2873. int32_t textLength);
  2874. /**
  2875. * Writable-aliasing char16_t* constructor.
  2876. * The text will be used for the UnicodeString object, but
  2877. * it will not be released when the UnicodeString is destroyed.
  2878. * This has write-through semantics:
  2879. * For as long as the capacity of the buffer is sufficient, write operations
  2880. * will directly affect the buffer. When more capacity is necessary, then
  2881. * a new buffer will be allocated and the contents copied as with regularly
  2882. * constructed strings.
  2883. * In an assignment to another UnicodeString, the buffer will be copied.
  2884. * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
  2885. * as the string buffer itself and will in this case not copy the contents.
  2886. *
  2887. * @param buffer The characters to alias for the UnicodeString.
  2888. * @param buffLength The number of Unicode characters in `buffer` to alias.
  2889. * @param buffCapacity The size of `buffer` in char16_ts.
  2890. * @stable ICU 2.0
  2891. */
  2892. UnicodeString(char16_t *buffer, int32_t buffLength, int32_t buffCapacity);
  2893. #if !U_CHAR16_IS_TYPEDEF
  2894. /**
  2895. * Writable-aliasing uint16_t * constructor.
  2896. * Delegates to UnicodeString(const char16_t *, int32_t, int32_t).
  2897. * @param buffer writable buffer of/for UTF-16 text
  2898. * @param buffLength length of the current buffer contents
  2899. * @param buffCapacity buffer capacity
  2900. * @stable ICU 59
  2901. */
  2902. UnicodeString(uint16_t *buffer, int32_t buffLength, int32_t buffCapacity) :
  2903. UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
  2904. #endif
  2905. #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
  2906. /**
  2907. * Writable-aliasing wchar_t * constructor.
  2908. * (Only defined if U_SIZEOF_WCHAR_T==2.)
  2909. * Delegates to UnicodeString(const char16_t *, int32_t, int32_t).
  2910. * @param buffer writable buffer of/for UTF-16 text
  2911. * @param buffLength length of the current buffer contents
  2912. * @param buffCapacity buffer capacity
  2913. * @stable ICU 59
  2914. */
  2915. UnicodeString(wchar_t *buffer, int32_t buffLength, int32_t buffCapacity) :
  2916. UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
  2917. #endif
  2918. /**
  2919. * Writable-aliasing nullptr_t constructor.
  2920. * Effectively the same as the default constructor, makes an empty string object.
  2921. * @param buffer nullptr
  2922. * @param buffLength ignored
  2923. * @param buffCapacity ignored
  2924. * @stable ICU 59
  2925. */
  2926. inline UnicodeString(std::nullptr_t buffer, int32_t buffLength, int32_t buffCapacity);
  2927. #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
  2928. /**
  2929. * char* constructor.
  2930. * Uses the default converter (and thus depends on the ICU conversion code)
  2931. * unless U_CHARSET_IS_UTF8 is set to 1.
  2932. *
  2933. * For ASCII (really "invariant character") strings it is more efficient to use
  2934. * the constructor that takes a US_INV (for its enum EInvariant).
  2935. * For ASCII (invariant-character) string literals, see UNICODE_STRING and
  2936. * UNICODE_STRING_SIMPLE.
  2937. *
  2938. * It is recommended to mark this constructor "explicit" by
  2939. * `-DUNISTR_FROM_STRING_EXPLICIT=explicit`
  2940. * on the compiler command line or similar.
  2941. * @param codepageData an array of bytes, null-terminated,
  2942. * in the platform's default codepage.
  2943. * @stable ICU 2.0
  2944. * @see UNICODE_STRING
  2945. * @see UNICODE_STRING_SIMPLE
  2946. */
  2947. UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char *codepageData);
  2948. /**
  2949. * char* constructor.
  2950. * Uses the default converter (and thus depends on the ICU conversion code)
  2951. * unless U_CHARSET_IS_UTF8 is set to 1.
  2952. * @param codepageData an array of bytes in the platform's default codepage.
  2953. * @param dataLength The number of bytes in `codepageData`.
  2954. * @stable ICU 2.0
  2955. */
  2956. UnicodeString(const char *codepageData, int32_t dataLength);
  2957. #endif
  2958. #if !UCONFIG_NO_CONVERSION
  2959. /**
  2960. * char* constructor.
  2961. * @param codepageData an array of bytes, null-terminated
  2962. * @param codepage the encoding of `codepageData`. The special
  2963. * value 0 for `codepage` indicates that the text is in the
  2964. * platform's default codepage.
  2965. *
  2966. * If `codepage` is an empty string (`""`),
  2967. * then a simple conversion is performed on the codepage-invariant
  2968. * subset ("invariant characters") of the platform encoding. See utypes.h.
  2969. * Recommendation: For invariant-character strings use the constructor
  2970. * UnicodeString(const char *src, int32_t length, enum EInvariant inv)
  2971. * because it avoids object code dependencies of UnicodeString on
  2972. * the conversion code.
  2973. *
  2974. * @stable ICU 2.0
  2975. */
  2976. UnicodeString(const char *codepageData, const char *codepage);
  2977. /**
  2978. * char* constructor.
  2979. * @param codepageData an array of bytes.
  2980. * @param dataLength The number of bytes in `codepageData`.
  2981. * @param codepage the encoding of `codepageData`. The special
  2982. * value 0 for `codepage` indicates that the text is in the
  2983. * platform's default codepage.
  2984. * If `codepage` is an empty string (`""`),
  2985. * then a simple conversion is performed on the codepage-invariant
  2986. * subset ("invariant characters") of the platform encoding. See utypes.h.
  2987. * Recommendation: For invariant-character strings use the constructor
  2988. * UnicodeString(const char *src, int32_t length, enum EInvariant inv)
  2989. * because it avoids object code dependencies of UnicodeString on
  2990. * the conversion code.
  2991. *
  2992. * @stable ICU 2.0
  2993. */
  2994. UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage);
  2995. /**
  2996. * char * / UConverter constructor.
  2997. * This constructor uses an existing UConverter object to
  2998. * convert the codepage string to Unicode and construct a UnicodeString
  2999. * from that.
  3000. *
  3001. * The converter is reset at first.
  3002. * If the error code indicates a failure before this constructor is called,
  3003. * or if an error occurs during conversion or construction,
  3004. * then the string will be bogus.
  3005. *
  3006. * This function avoids the overhead of opening and closing a converter if
  3007. * multiple strings are constructed.
  3008. *
  3009. * @param src input codepage string
  3010. * @param srcLength length of the input string, can be -1 for NUL-terminated strings
  3011. * @param cnv converter object (ucnv_resetToUnicode() will be called),
  3012. * can be nullptr for the default converter
  3013. * @param errorCode normal ICU error code
  3014. * @stable ICU 2.0
  3015. */
  3016. UnicodeString(
  3017. const char *src, int32_t srcLength,
  3018. UConverter *cnv,
  3019. UErrorCode &errorCode);
  3020. #endif
  3021. /**
  3022. * Constructs a Unicode string from an invariant-character char * string.
  3023. * About invariant characters see utypes.h.
  3024. * This constructor has no runtime dependency on conversion code and is
  3025. * therefore recommended over ones taking a charset name string
  3026. * (where the empty string "" indicates invariant-character conversion).
  3027. *
  3028. * Use the macro US_INV as the third, signature-distinguishing parameter.
  3029. *
  3030. * For example:
  3031. * \code
  3032. * void fn(const char *s) {
  3033. * UnicodeString ustr(s, -1, US_INV);
  3034. * // use ustr ...
  3035. * }
  3036. * \endcode
  3037. * @param src String using only invariant characters.
  3038. * @param textLength Length of src, or -1 if NUL-terminated.
  3039. * @param inv Signature-distinguishing parameter, use US_INV.
  3040. *
  3041. * @see US_INV
  3042. * @stable ICU 3.2
  3043. */
  3044. UnicodeString(const char *src, int32_t textLength, enum EInvariant inv);
  3045. /**
  3046. * Copy constructor.
  3047. *
  3048. * Starting with ICU 2.4, the assignment operator and the copy constructor
  3049. * allocate a new buffer and copy the buffer contents even for readonly aliases.
  3050. * By contrast, the fastCopyFrom() function implements the old,
  3051. * more efficient but less safe behavior
  3052. * of making this string also a readonly alias to the same buffer.
  3053. *
  3054. * If the source object has an "open" buffer from getBuffer(minCapacity),
  3055. * then the copy is an empty string.
  3056. *
  3057. * @param that The UnicodeString object to copy.
  3058. * @stable ICU 2.0
  3059. * @see fastCopyFrom
  3060. */
  3061. UnicodeString(const UnicodeString& that);
  3062. /**
  3063. * Move constructor; might leave src in bogus state.
  3064. * This string will have the same contents and state that the source string had.
  3065. * @param src source string
  3066. * @stable ICU 56
  3067. */
  3068. UnicodeString(UnicodeString &&src) noexcept;
  3069. /**
  3070. * 'Substring' constructor from tail of source string.
  3071. * @param src The UnicodeString object to copy.
  3072. * @param srcStart The offset into `src` at which to start copying.
  3073. * @stable ICU 2.2
  3074. */
  3075. UnicodeString(const UnicodeString& src, int32_t srcStart);
  3076. /**
  3077. * 'Substring' constructor from subrange of source string.
  3078. * @param src The UnicodeString object to copy.
  3079. * @param srcStart The offset into `src` at which to start copying.
  3080. * @param srcLength The number of characters from `src` to copy.
  3081. * @stable ICU 2.2
  3082. */
  3083. UnicodeString(const UnicodeString& src, int32_t srcStart, int32_t srcLength);
  3084. /**
  3085. * Clone this object, an instance of a subclass of Replaceable.
  3086. * Clones can be used concurrently in multiple threads.
  3087. * If a subclass does not implement clone(), or if an error occurs,
  3088. * then nullptr is returned.
  3089. * The caller must delete the clone.
  3090. *
  3091. * @return a clone of this object
  3092. *
  3093. * @see Replaceable::clone
  3094. * @see getDynamicClassID
  3095. * @stable ICU 2.6
  3096. */
  3097. virtual UnicodeString *clone() const override;
  3098. /** Destructor.
  3099. * @stable ICU 2.0
  3100. */
  3101. virtual ~UnicodeString();
  3102. /**
  3103. * Create a UnicodeString from a UTF-8 string.
  3104. * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string.
  3105. * Calls u_strFromUTF8WithSub().
  3106. *
  3107. * @param utf8 UTF-8 input string.
  3108. * Note that a StringPiece can be implicitly constructed
  3109. * from a std::string or a NUL-terminated const char * string.
  3110. * @return A UnicodeString with equivalent UTF-16 contents.
  3111. * @see toUTF8
  3112. * @see toUTF8String
  3113. * @stable ICU 4.2
  3114. */
  3115. static UnicodeString fromUTF8(StringPiece utf8);
  3116. /**
  3117. * Create a UnicodeString from a UTF-32 string.
  3118. * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string.
  3119. * Calls u_strFromUTF32WithSub().
  3120. *
  3121. * @param utf32 UTF-32 input string. Must not be nullptr.
  3122. * @param length Length of the input string, or -1 if NUL-terminated.
  3123. * @return A UnicodeString with equivalent UTF-16 contents.
  3124. * @see toUTF32
  3125. * @stable ICU 4.2
  3126. */
  3127. static UnicodeString fromUTF32(const UChar32 *utf32, int32_t length);
  3128. /* Miscellaneous operations */
  3129. /**
  3130. * Unescape a string of characters and return a string containing
  3131. * the result. The following escape sequences are recognized:
  3132. *
  3133. * \\uhhhh 4 hex digits; h in [0-9A-Fa-f]
  3134. * \\Uhhhhhhhh 8 hex digits
  3135. * \\xhh 1-2 hex digits
  3136. * \\ooo 1-3 octal digits; o in [0-7]
  3137. * \\cX control-X; X is masked with 0x1F
  3138. *
  3139. * as well as the standard ANSI C escapes:
  3140. *
  3141. * \\a => U+0007, \\b => U+0008, \\t => U+0009, \\n => U+000A,
  3142. * \\v => U+000B, \\f => U+000C, \\r => U+000D, \\e => U+001B,
  3143. * \\" => U+0022, \\' => U+0027, \\? => U+003F, \\\\ => U+005C
  3144. *
  3145. * Anything else following a backslash is generically escaped. For
  3146. * example, "[a\\-z]" returns "[a-z]".
  3147. *
  3148. * If an escape sequence is ill-formed, this method returns an empty
  3149. * string. An example of an ill-formed sequence is "\\u" followed by
  3150. * fewer than 4 hex digits.
  3151. *
  3152. * This function is similar to u_unescape() but not identical to it.
  3153. * The latter takes a source char*, so it does escape recognition
  3154. * and also invariant conversion.
  3155. *
  3156. * @return a string with backslash escapes interpreted, or an
  3157. * empty string on error.
  3158. * @see UnicodeString#unescapeAt()
  3159. * @see u_unescape()
  3160. * @see u_unescapeAt()
  3161. * @stable ICU 2.0
  3162. */
  3163. UnicodeString unescape() const;
  3164. /**
  3165. * Unescape a single escape sequence and return the represented
  3166. * character. See unescape() for a listing of the recognized escape
  3167. * sequences. The character at offset-1 is assumed (without
  3168. * checking) to be a backslash. If the escape sequence is
  3169. * ill-formed, or the offset is out of range, U_SENTINEL=-1 is
  3170. * returned.
  3171. *
  3172. * @param offset an input output parameter. On input, it is the
  3173. * offset into this string where the escape sequence is located,
  3174. * after the initial backslash. On output, it is advanced after the
  3175. * last character parsed. On error, it is not advanced at all.
  3176. * @return the character represented by the escape sequence at
  3177. * offset, or U_SENTINEL=-1 on error.
  3178. * @see UnicodeString#unescape()
  3179. * @see u_unescape()
  3180. * @see u_unescapeAt()
  3181. * @stable ICU 2.0
  3182. */
  3183. UChar32 unescapeAt(int32_t &offset) const;
  3184. /**
  3185. * ICU "poor man's RTTI", returns a UClassID for this class.
  3186. *
  3187. * @stable ICU 2.2
  3188. */
  3189. static UClassID U_EXPORT2 getStaticClassID();
  3190. /**
  3191. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  3192. *
  3193. * @stable ICU 2.2
  3194. */
  3195. virtual UClassID getDynamicClassID() const override;
  3196. //========================================
  3197. // Implementation methods
  3198. //========================================
  3199. protected:
  3200. /**
  3201. * Implement Replaceable::getLength() (see jitterbug 1027).
  3202. * @stable ICU 2.4
  3203. */
  3204. virtual int32_t getLength() const override;
  3205. /**
  3206. * The change in Replaceable to use virtual getCharAt() allows
  3207. * UnicodeString::charAt() to be inline again (see jitterbug 709).
  3208. * @stable ICU 2.4
  3209. */
  3210. virtual char16_t getCharAt(int32_t offset) const override;
  3211. /**
  3212. * The change in Replaceable to use virtual getChar32At() allows
  3213. * UnicodeString::char32At() to be inline again (see jitterbug 709).
  3214. * @stable ICU 2.4
  3215. */
  3216. virtual UChar32 getChar32At(int32_t offset) const override;
  3217. private:
  3218. // For char* constructors. Could be made public.
  3219. UnicodeString &setToUTF8(StringPiece utf8);
  3220. // For extract(char*).
  3221. // We could make a toUTF8(target, capacity, errorCode) public but not
  3222. // this version: New API will be cleaner if we make callers create substrings
  3223. // rather than having start+length on every method,
  3224. // and it should take a UErrorCode&.
  3225. int32_t
  3226. toUTF8(int32_t start, int32_t len,
  3227. char *target, int32_t capacity) const;
  3228. /**
  3229. * Internal string contents comparison, called by operator==.
  3230. * Requires: this & text not bogus and have same lengths.
  3231. */
  3232. UBool doEquals(const UnicodeString &text, int32_t len) const;
  3233. inline UBool
  3234. doEqualsSubstring(int32_t start,
  3235. int32_t length,
  3236. const UnicodeString& srcText,
  3237. int32_t srcStart,
  3238. int32_t srcLength) const;
  3239. UBool doEqualsSubstring(int32_t start,
  3240. int32_t length,
  3241. const char16_t *srcChars,
  3242. int32_t srcStart,
  3243. int32_t srcLength) const;
  3244. inline int8_t
  3245. doCompare(int32_t start,
  3246. int32_t length,
  3247. const UnicodeString& srcText,
  3248. int32_t srcStart,
  3249. int32_t srcLength) const;
  3250. int8_t doCompare(int32_t start,
  3251. int32_t length,
  3252. const char16_t *srcChars,
  3253. int32_t srcStart,
  3254. int32_t srcLength) const;
  3255. inline int8_t
  3256. doCompareCodePointOrder(int32_t start,
  3257. int32_t length,
  3258. const UnicodeString& srcText,
  3259. int32_t srcStart,
  3260. int32_t srcLength) const;
  3261. int8_t doCompareCodePointOrder(int32_t start,
  3262. int32_t length,
  3263. const char16_t *srcChars,
  3264. int32_t srcStart,
  3265. int32_t srcLength) const;
  3266. inline int8_t
  3267. doCaseCompare(int32_t start,
  3268. int32_t length,
  3269. const UnicodeString &srcText,
  3270. int32_t srcStart,
  3271. int32_t srcLength,
  3272. uint32_t options) const;
  3273. int8_t
  3274. doCaseCompare(int32_t start,
  3275. int32_t length,
  3276. const char16_t *srcChars,
  3277. int32_t srcStart,
  3278. int32_t srcLength,
  3279. uint32_t options) const;
  3280. int32_t doIndexOf(char16_t c,
  3281. int32_t start,
  3282. int32_t length) const;
  3283. int32_t doIndexOf(UChar32 c,
  3284. int32_t start,
  3285. int32_t length) const;
  3286. int32_t doLastIndexOf(char16_t c,
  3287. int32_t start,
  3288. int32_t length) const;
  3289. int32_t doLastIndexOf(UChar32 c,
  3290. int32_t start,
  3291. int32_t length) const;
  3292. void doExtract(int32_t start,
  3293. int32_t length,
  3294. char16_t *dst,
  3295. int32_t dstStart) const;
  3296. inline void doExtract(int32_t start,
  3297. int32_t length,
  3298. UnicodeString& target) const;
  3299. inline char16_t doCharAt(int32_t offset) const;
  3300. UnicodeString& doReplace(int32_t start,
  3301. int32_t length,
  3302. const UnicodeString& srcText,
  3303. int32_t srcStart,
  3304. int32_t srcLength);
  3305. UnicodeString& doReplace(int32_t start,
  3306. int32_t length,
  3307. const char16_t *srcChars,
  3308. int32_t srcStart,
  3309. int32_t srcLength);
  3310. UnicodeString& doAppend(const UnicodeString& src, int32_t srcStart, int32_t srcLength);
  3311. UnicodeString& doAppend(const char16_t *srcChars, int32_t srcStart, int32_t srcLength);
  3312. UnicodeString& doReverse(int32_t start,
  3313. int32_t length);
  3314. // calculate hash code
  3315. int32_t doHashCode() const;
  3316. // get pointer to start of array
  3317. // these do not check for kOpenGetBuffer, unlike the public getBuffer() function
  3318. inline char16_t* getArrayStart();
  3319. inline const char16_t* getArrayStart() const;
  3320. inline UBool hasShortLength() const;
  3321. inline int32_t getShortLength() const;
  3322. // A UnicodeString object (not necessarily its current buffer)
  3323. // is writable unless it isBogus() or it has an "open" getBuffer(minCapacity).
  3324. inline UBool isWritable() const;
  3325. // Is the current buffer writable?
  3326. inline UBool isBufferWritable() const;
  3327. // None of the following does releaseArray().
  3328. inline void setZeroLength();
  3329. inline void setShortLength(int32_t len);
  3330. inline void setLength(int32_t len);
  3331. inline void setToEmpty();
  3332. inline void setArray(char16_t *array, int32_t len, int32_t capacity); // sets length but not flags
  3333. // allocate the array; result may be the stack buffer
  3334. // sets refCount to 1 if appropriate
  3335. // sets fArray, fCapacity, and flags
  3336. // sets length to 0
  3337. // returns boolean for success or failure
  3338. UBool allocate(int32_t capacity);
  3339. // release the array if owned
  3340. void releaseArray();
  3341. // turn a bogus string into an empty one
  3342. void unBogus();
  3343. // implements assignment operator, copy constructor, and fastCopyFrom()
  3344. UnicodeString &copyFrom(const UnicodeString &src, UBool fastCopy=false);
  3345. // Copies just the fields without memory management.
  3346. void copyFieldsFrom(UnicodeString &src, UBool setSrcToBogus) noexcept;
  3347. // Pin start and limit to acceptable values.
  3348. inline void pinIndex(int32_t& start) const;
  3349. inline void pinIndices(int32_t& start,
  3350. int32_t& length) const;
  3351. #if !UCONFIG_NO_CONVERSION
  3352. /* Internal extract() using UConverter. */
  3353. int32_t doExtract(int32_t start, int32_t length,
  3354. char *dest, int32_t destCapacity,
  3355. UConverter *cnv,
  3356. UErrorCode &errorCode) const;
  3357. /*
  3358. * Real constructor for converting from codepage data.
  3359. * It assumes that it is called with !fRefCounted.
  3360. *
  3361. * If `codepage==0`, then the default converter
  3362. * is used for the platform encoding.
  3363. * If `codepage` is an empty string (`""`),
  3364. * then a simple conversion is performed on the codepage-invariant
  3365. * subset ("invariant characters") of the platform encoding. See utypes.h.
  3366. */
  3367. void doCodepageCreate(const char *codepageData,
  3368. int32_t dataLength,
  3369. const char *codepage);
  3370. /*
  3371. * Worker function for creating a UnicodeString from
  3372. * a codepage string using a UConverter.
  3373. */
  3374. void
  3375. doCodepageCreate(const char *codepageData,
  3376. int32_t dataLength,
  3377. UConverter *converter,
  3378. UErrorCode &status);
  3379. #endif
  3380. /*
  3381. * This function is called when write access to the array
  3382. * is necessary.
  3383. *
  3384. * We need to make a copy of the array if
  3385. * the buffer is read-only, or
  3386. * the buffer is refCounted (shared), and refCount>1, or
  3387. * the buffer is too small.
  3388. *
  3389. * Return false if memory could not be allocated.
  3390. */
  3391. UBool cloneArrayIfNeeded(int32_t newCapacity = -1,
  3392. int32_t growCapacity = -1,
  3393. UBool doCopyArray = true,
  3394. int32_t** pBufferToDelete = nullptr,
  3395. UBool forceClone = false);
  3396. /**
  3397. * Common function for UnicodeString case mappings.
  3398. * The stringCaseMapper has the same type UStringCaseMapper
  3399. * as in ustr_imp.h for ustrcase_map().
  3400. */
  3401. UnicodeString &
  3402. caseMap(int32_t caseLocale, uint32_t options,
  3403. #if !UCONFIG_NO_BREAK_ITERATION
  3404. BreakIterator *iter,
  3405. #endif
  3406. UStringCaseMapper *stringCaseMapper);
  3407. // ref counting
  3408. void addRef();
  3409. int32_t removeRef();
  3410. int32_t refCount() const;
  3411. // constants
  3412. enum {
  3413. /**
  3414. * Size of stack buffer for short strings.
  3415. * Must be at least U16_MAX_LENGTH for the single-code point constructor to work.
  3416. * @see UNISTR_OBJECT_SIZE
  3417. */
  3418. US_STACKBUF_SIZE=(int32_t)(UNISTR_OBJECT_SIZE-sizeof(void *)-2)/U_SIZEOF_UCHAR,
  3419. kInvalidUChar=0xffff, // U+FFFF returned by charAt(invalid index)
  3420. kInvalidHashCode=0, // invalid hash code
  3421. kEmptyHashCode=1, // hash code for empty string
  3422. // bit flag values for fLengthAndFlags
  3423. kIsBogus=1, // this string is bogus, i.e., not valid or nullptr
  3424. kUsingStackBuffer=2,// using fUnion.fStackFields instead of fUnion.fFields
  3425. kRefCounted=4, // there is a refCount field before the characters in fArray
  3426. kBufferIsReadonly=8,// do not write to this buffer
  3427. kOpenGetBuffer=16, // getBuffer(minCapacity) was called (is "open"),
  3428. // and releaseBuffer(newLength) must be called
  3429. kAllStorageFlags=0x1f,
  3430. kLengthShift=5, // remaining 11 bits for non-negative short length, or negative if long
  3431. kLength1=1<<kLengthShift,
  3432. kMaxShortLength=0x3ff, // max non-negative short length (leaves top bit 0)
  3433. kLengthIsLarge=0xffe0, // short length < 0, real length is in fUnion.fFields.fLength
  3434. // combined values for convenience
  3435. kShortString=kUsingStackBuffer,
  3436. kLongString=kRefCounted,
  3437. kReadonlyAlias=kBufferIsReadonly,
  3438. kWritableAlias=0
  3439. };
  3440. friend class UnicodeStringAppendable;
  3441. union StackBufferOrFields; // forward declaration necessary before friend declaration
  3442. friend union StackBufferOrFields; // make US_STACKBUF_SIZE visible inside fUnion
  3443. /*
  3444. * The following are all the class fields that are stored
  3445. * in each UnicodeString object.
  3446. * Note that UnicodeString has virtual functions,
  3447. * therefore there is an implicit vtable pointer
  3448. * as the first real field.
  3449. * The fields should be aligned such that no padding is necessary.
  3450. * On 32-bit machines, the size should be 32 bytes,
  3451. * on 64-bit machines (8-byte pointers), it should be 40 bytes.
  3452. *
  3453. * We use a hack to achieve this.
  3454. *
  3455. * With at least some compilers, each of the following is forced to
  3456. * a multiple of sizeof(pointer) [the largest field base unit here is a data pointer],
  3457. * rounded up with additional padding if the fields do not already fit that requirement:
  3458. * - sizeof(class UnicodeString)
  3459. * - offsetof(UnicodeString, fUnion)
  3460. * - sizeof(fUnion)
  3461. * - sizeof(fStackFields)
  3462. *
  3463. * We optimize for the longest possible internal buffer for short strings.
  3464. * fUnion.fStackFields begins with 2 bytes for storage flags
  3465. * and the length of relatively short strings,
  3466. * followed by the buffer for short string contents.
  3467. * There is no padding inside fStackFields.
  3468. *
  3469. * Heap-allocated and aliased strings use fUnion.fFields.
  3470. * Both fStackFields and fFields must begin with the same fields for flags and short length,
  3471. * that is, those must have the same memory offsets inside the object,
  3472. * because the flags must be inspected in order to decide which half of fUnion is being used.
  3473. * We assume that the compiler does not reorder the fields.
  3474. *
  3475. * (Padding at the end of fFields is ok:
  3476. * As long as it is no larger than fStackFields, it is not wasted space.)
  3477. *
  3478. * For some of the history of the UnicodeString class fields layout, see
  3479. * - ICU ticket #11551 "longer UnicodeString contents in stack buffer"
  3480. * - ICU ticket #11336 "UnicodeString: recombine stack buffer arrays"
  3481. * - ICU ticket #8322 "why is sizeof(UnicodeString)==48?"
  3482. */
  3483. // (implicit) *vtable;
  3484. union StackBufferOrFields {
  3485. // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used.
  3486. // Each struct of the union must begin with fLengthAndFlags.
  3487. struct {
  3488. int16_t fLengthAndFlags; // bit fields: see constants above
  3489. char16_t fBuffer[US_STACKBUF_SIZE]; // buffer for short strings
  3490. } fStackFields;
  3491. struct {
  3492. int16_t fLengthAndFlags; // bit fields: see constants above
  3493. int32_t fLength; // number of characters in fArray if >127; else undefined
  3494. int32_t fCapacity; // capacity of fArray (in char16_ts)
  3495. // array pointer last to minimize padding for machines with P128 data model
  3496. // or pointer sizes that are not a power of 2
  3497. char16_t *fArray; // the Unicode data
  3498. } fFields;
  3499. } fUnion;
  3500. };
  3501. /**
  3502. * Create a new UnicodeString with the concatenation of two others.
  3503. *
  3504. * @param s1 The first string to be copied to the new one.
  3505. * @param s2 The second string to be copied to the new one, after s1.
  3506. * @return UnicodeString(s1).append(s2)
  3507. * @stable ICU 2.8
  3508. */
  3509. U_COMMON_API UnicodeString U_EXPORT2
  3510. operator+ (const UnicodeString &s1, const UnicodeString &s2);
  3511. //========================================
  3512. // Inline members
  3513. //========================================
  3514. //========================================
  3515. // Privates
  3516. //========================================
  3517. inline void
  3518. UnicodeString::pinIndex(int32_t& start) const
  3519. {
  3520. // pin index
  3521. if(start < 0) {
  3522. start = 0;
  3523. } else if(start > length()) {
  3524. start = length();
  3525. }
  3526. }
  3527. inline void
  3528. UnicodeString::pinIndices(int32_t& start,
  3529. int32_t& _length) const
  3530. {
  3531. // pin indices
  3532. int32_t len = length();
  3533. if(start < 0) {
  3534. start = 0;
  3535. } else if(start > len) {
  3536. start = len;
  3537. }
  3538. if(_length < 0) {
  3539. _length = 0;
  3540. } else if(_length > (len - start)) {
  3541. _length = (len - start);
  3542. }
  3543. }
  3544. inline char16_t*
  3545. UnicodeString::getArrayStart() {
  3546. return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
  3547. fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
  3548. }
  3549. inline const char16_t*
  3550. UnicodeString::getArrayStart() const {
  3551. return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
  3552. fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
  3553. }
  3554. //========================================
  3555. // Default constructor
  3556. //========================================
  3557. inline
  3558. UnicodeString::UnicodeString() {
  3559. fUnion.fStackFields.fLengthAndFlags=kShortString;
  3560. }
  3561. inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/) {
  3562. fUnion.fStackFields.fLengthAndFlags=kShortString;
  3563. }
  3564. inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/, int32_t /*length*/) {
  3565. fUnion.fStackFields.fLengthAndFlags=kShortString;
  3566. }
  3567. inline UnicodeString::UnicodeString(std::nullptr_t /*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) {
  3568. fUnion.fStackFields.fLengthAndFlags=kShortString;
  3569. }
  3570. //========================================
  3571. // Read-only implementation methods
  3572. //========================================
  3573. inline UBool
  3574. UnicodeString::hasShortLength() const {
  3575. return fUnion.fFields.fLengthAndFlags>=0;
  3576. }
  3577. inline int32_t
  3578. UnicodeString::getShortLength() const {
  3579. // fLengthAndFlags must be non-negative -> short length >= 0
  3580. // and arithmetic or logical shift does not matter.
  3581. return fUnion.fFields.fLengthAndFlags>>kLengthShift;
  3582. }
  3583. inline int32_t
  3584. UnicodeString::length() const {
  3585. return hasShortLength() ? getShortLength() : fUnion.fFields.fLength;
  3586. }
  3587. inline int32_t
  3588. UnicodeString::getCapacity() const {
  3589. return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
  3590. US_STACKBUF_SIZE : fUnion.fFields.fCapacity;
  3591. }
  3592. inline int32_t
  3593. UnicodeString::hashCode() const
  3594. { return doHashCode(); }
  3595. inline UBool
  3596. UnicodeString::isBogus() const
  3597. { return (UBool)(fUnion.fFields.fLengthAndFlags & kIsBogus); }
  3598. inline UBool
  3599. UnicodeString::isWritable() const
  3600. { return (UBool)!(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus)); }
  3601. inline UBool
  3602. UnicodeString::isBufferWritable() const
  3603. {
  3604. return (UBool)(
  3605. !(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus|kBufferIsReadonly)) &&
  3606. (!(fUnion.fFields.fLengthAndFlags&kRefCounted) || refCount()==1));
  3607. }
  3608. inline const char16_t *
  3609. UnicodeString::getBuffer() const {
  3610. if(fUnion.fFields.fLengthAndFlags&(kIsBogus|kOpenGetBuffer)) {
  3611. return nullptr;
  3612. } else if(fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) {
  3613. return fUnion.fStackFields.fBuffer;
  3614. } else {
  3615. return fUnion.fFields.fArray;
  3616. }
  3617. }
  3618. //========================================
  3619. // Read-only alias methods
  3620. //========================================
  3621. inline int8_t
  3622. UnicodeString::doCompare(int32_t start,
  3623. int32_t thisLength,
  3624. const UnicodeString& srcText,
  3625. int32_t srcStart,
  3626. int32_t srcLength) const
  3627. {
  3628. if(srcText.isBogus()) {
  3629. return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
  3630. } else {
  3631. srcText.pinIndices(srcStart, srcLength);
  3632. return doCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
  3633. }
  3634. }
  3635. inline UBool
  3636. UnicodeString::doEqualsSubstring(int32_t start,
  3637. int32_t thisLength,
  3638. const UnicodeString& srcText,
  3639. int32_t srcStart,
  3640. int32_t srcLength) const
  3641. {
  3642. if(srcText.isBogus()) {
  3643. return isBogus();
  3644. } else {
  3645. srcText.pinIndices(srcStart, srcLength);
  3646. return !isBogus() && doEqualsSubstring(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
  3647. }
  3648. }
  3649. inline bool
  3650. UnicodeString::operator== (const UnicodeString& text) const
  3651. {
  3652. if(isBogus()) {
  3653. return text.isBogus();
  3654. } else {
  3655. int32_t len = length(), textLength = text.length();
  3656. return !text.isBogus() && len == textLength && doEquals(text, len);
  3657. }
  3658. }
  3659. inline bool
  3660. UnicodeString::operator!= (const UnicodeString& text) const
  3661. { return (! operator==(text)); }
  3662. inline UBool
  3663. UnicodeString::operator> (const UnicodeString& text) const
  3664. { return doCompare(0, length(), text, 0, text.length()) == 1; }
  3665. inline UBool
  3666. UnicodeString::operator< (const UnicodeString& text) const
  3667. { return doCompare(0, length(), text, 0, text.length()) == -1; }
  3668. inline UBool
  3669. UnicodeString::operator>= (const UnicodeString& text) const
  3670. { return doCompare(0, length(), text, 0, text.length()) != -1; }
  3671. inline UBool
  3672. UnicodeString::operator<= (const UnicodeString& text) const
  3673. { return doCompare(0, length(), text, 0, text.length()) != 1; }
  3674. inline int8_t
  3675. UnicodeString::compare(const UnicodeString& text) const
  3676. { return doCompare(0, length(), text, 0, text.length()); }
  3677. inline int8_t
  3678. UnicodeString::compare(int32_t start,
  3679. int32_t _length,
  3680. const UnicodeString& srcText) const
  3681. { return doCompare(start, _length, srcText, 0, srcText.length()); }
  3682. inline int8_t
  3683. UnicodeString::compare(ConstChar16Ptr srcChars,
  3684. int32_t srcLength) const
  3685. { return doCompare(0, length(), srcChars, 0, srcLength); }
  3686. inline int8_t
  3687. UnicodeString::compare(int32_t start,
  3688. int32_t _length,
  3689. const UnicodeString& srcText,
  3690. int32_t srcStart,
  3691. int32_t srcLength) const
  3692. { return doCompare(start, _length, srcText, srcStart, srcLength); }
  3693. inline int8_t
  3694. UnicodeString::compare(int32_t start,
  3695. int32_t _length,
  3696. const char16_t *srcChars) const
  3697. { return doCompare(start, _length, srcChars, 0, _length); }
  3698. inline int8_t
  3699. UnicodeString::compare(int32_t start,
  3700. int32_t _length,
  3701. const char16_t *srcChars,
  3702. int32_t srcStart,
  3703. int32_t srcLength) const
  3704. { return doCompare(start, _length, srcChars, srcStart, srcLength); }
  3705. inline int8_t
  3706. UnicodeString::compareBetween(int32_t start,
  3707. int32_t limit,
  3708. const UnicodeString& srcText,
  3709. int32_t srcStart,
  3710. int32_t srcLimit) const
  3711. { return doCompare(start, limit - start,
  3712. srcText, srcStart, srcLimit - srcStart); }
  3713. inline int8_t
  3714. UnicodeString::doCompareCodePointOrder(int32_t start,
  3715. int32_t thisLength,
  3716. const UnicodeString& srcText,
  3717. int32_t srcStart,
  3718. int32_t srcLength) const
  3719. {
  3720. if(srcText.isBogus()) {
  3721. return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
  3722. } else {
  3723. srcText.pinIndices(srcStart, srcLength);
  3724. return doCompareCodePointOrder(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
  3725. }
  3726. }
  3727. inline int8_t
  3728. UnicodeString::compareCodePointOrder(const UnicodeString& text) const
  3729. { return doCompareCodePointOrder(0, length(), text, 0, text.length()); }
  3730. inline int8_t
  3731. UnicodeString::compareCodePointOrder(int32_t start,
  3732. int32_t _length,
  3733. const UnicodeString& srcText) const
  3734. { return doCompareCodePointOrder(start, _length, srcText, 0, srcText.length()); }
  3735. inline int8_t
  3736. UnicodeString::compareCodePointOrder(ConstChar16Ptr srcChars,
  3737. int32_t srcLength) const
  3738. { return doCompareCodePointOrder(0, length(), srcChars, 0, srcLength); }
  3739. inline int8_t
  3740. UnicodeString::compareCodePointOrder(int32_t start,
  3741. int32_t _length,
  3742. const UnicodeString& srcText,
  3743. int32_t srcStart,
  3744. int32_t srcLength) const
  3745. { return doCompareCodePointOrder(start, _length, srcText, srcStart, srcLength); }
  3746. inline int8_t
  3747. UnicodeString::compareCodePointOrder(int32_t start,
  3748. int32_t _length,
  3749. const char16_t *srcChars) const
  3750. { return doCompareCodePointOrder(start, _length, srcChars, 0, _length); }
  3751. inline int8_t
  3752. UnicodeString::compareCodePointOrder(int32_t start,
  3753. int32_t _length,
  3754. const char16_t *srcChars,
  3755. int32_t srcStart,
  3756. int32_t srcLength) const
  3757. { return doCompareCodePointOrder(start, _length, srcChars, srcStart, srcLength); }
  3758. inline int8_t
  3759. UnicodeString::compareCodePointOrderBetween(int32_t start,
  3760. int32_t limit,
  3761. const UnicodeString& srcText,
  3762. int32_t srcStart,
  3763. int32_t srcLimit) const
  3764. { return doCompareCodePointOrder(start, limit - start,
  3765. srcText, srcStart, srcLimit - srcStart); }
  3766. inline int8_t
  3767. UnicodeString::doCaseCompare(int32_t start,
  3768. int32_t thisLength,
  3769. const UnicodeString &srcText,
  3770. int32_t srcStart,
  3771. int32_t srcLength,
  3772. uint32_t options) const
  3773. {
  3774. if(srcText.isBogus()) {
  3775. return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
  3776. } else {
  3777. srcText.pinIndices(srcStart, srcLength);
  3778. return doCaseCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength, options);
  3779. }
  3780. }
  3781. inline int8_t
  3782. UnicodeString::caseCompare(const UnicodeString &text, uint32_t options) const {
  3783. return doCaseCompare(0, length(), text, 0, text.length(), options);
  3784. }
  3785. inline int8_t
  3786. UnicodeString::caseCompare(int32_t start,
  3787. int32_t _length,
  3788. const UnicodeString &srcText,
  3789. uint32_t options) const {
  3790. return doCaseCompare(start, _length, srcText, 0, srcText.length(), options);
  3791. }
  3792. inline int8_t
  3793. UnicodeString::caseCompare(ConstChar16Ptr srcChars,
  3794. int32_t srcLength,
  3795. uint32_t options) const {
  3796. return doCaseCompare(0, length(), srcChars, 0, srcLength, options);
  3797. }
  3798. inline int8_t
  3799. UnicodeString::caseCompare(int32_t start,
  3800. int32_t _length,
  3801. const UnicodeString &srcText,
  3802. int32_t srcStart,
  3803. int32_t srcLength,
  3804. uint32_t options) const {
  3805. return doCaseCompare(start, _length, srcText, srcStart, srcLength, options);
  3806. }
  3807. inline int8_t
  3808. UnicodeString::caseCompare(int32_t start,
  3809. int32_t _length,
  3810. const char16_t *srcChars,
  3811. uint32_t options) const {
  3812. return doCaseCompare(start, _length, srcChars, 0, _length, options);
  3813. }
  3814. inline int8_t
  3815. UnicodeString::caseCompare(int32_t start,
  3816. int32_t _length,
  3817. const char16_t *srcChars,
  3818. int32_t srcStart,
  3819. int32_t srcLength,
  3820. uint32_t options) const {
  3821. return doCaseCompare(start, _length, srcChars, srcStart, srcLength, options);
  3822. }
  3823. inline int8_t
  3824. UnicodeString::caseCompareBetween(int32_t start,
  3825. int32_t limit,
  3826. const UnicodeString &srcText,
  3827. int32_t srcStart,
  3828. int32_t srcLimit,
  3829. uint32_t options) const {
  3830. return doCaseCompare(start, limit - start, srcText, srcStart, srcLimit - srcStart, options);
  3831. }
  3832. inline int32_t
  3833. UnicodeString::indexOf(const UnicodeString& srcText,
  3834. int32_t srcStart,
  3835. int32_t srcLength,
  3836. int32_t start,
  3837. int32_t _length) const
  3838. {
  3839. if(!srcText.isBogus()) {
  3840. srcText.pinIndices(srcStart, srcLength);
  3841. if(srcLength > 0) {
  3842. return indexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
  3843. }
  3844. }
  3845. return -1;
  3846. }
  3847. inline int32_t
  3848. UnicodeString::indexOf(const UnicodeString& text) const
  3849. { return indexOf(text, 0, text.length(), 0, length()); }
  3850. inline int32_t
  3851. UnicodeString::indexOf(const UnicodeString& text,
  3852. int32_t start) const {
  3853. pinIndex(start);
  3854. return indexOf(text, 0, text.length(), start, length() - start);
  3855. }
  3856. inline int32_t
  3857. UnicodeString::indexOf(const UnicodeString& text,
  3858. int32_t start,
  3859. int32_t _length) const
  3860. { return indexOf(text, 0, text.length(), start, _length); }
  3861. inline int32_t
  3862. UnicodeString::indexOf(const char16_t *srcChars,
  3863. int32_t srcLength,
  3864. int32_t start) const {
  3865. pinIndex(start);
  3866. return indexOf(srcChars, 0, srcLength, start, length() - start);
  3867. }
  3868. inline int32_t
  3869. UnicodeString::indexOf(ConstChar16Ptr srcChars,
  3870. int32_t srcLength,
  3871. int32_t start,
  3872. int32_t _length) const
  3873. { return indexOf(srcChars, 0, srcLength, start, _length); }
  3874. inline int32_t
  3875. UnicodeString::indexOf(char16_t c,
  3876. int32_t start,
  3877. int32_t _length) const
  3878. { return doIndexOf(c, start, _length); }
  3879. inline int32_t
  3880. UnicodeString::indexOf(UChar32 c,
  3881. int32_t start,
  3882. int32_t _length) const
  3883. { return doIndexOf(c, start, _length); }
  3884. inline int32_t
  3885. UnicodeString::indexOf(char16_t c) const
  3886. { return doIndexOf(c, 0, length()); }
  3887. inline int32_t
  3888. UnicodeString::indexOf(UChar32 c) const
  3889. { return indexOf(c, 0, length()); }
  3890. inline int32_t
  3891. UnicodeString::indexOf(char16_t c,
  3892. int32_t start) const {
  3893. pinIndex(start);
  3894. return doIndexOf(c, start, length() - start);
  3895. }
  3896. inline int32_t
  3897. UnicodeString::indexOf(UChar32 c,
  3898. int32_t start) const {
  3899. pinIndex(start);
  3900. return indexOf(c, start, length() - start);
  3901. }
  3902. inline int32_t
  3903. UnicodeString::lastIndexOf(ConstChar16Ptr srcChars,
  3904. int32_t srcLength,
  3905. int32_t start,
  3906. int32_t _length) const
  3907. { return lastIndexOf(srcChars, 0, srcLength, start, _length); }
  3908. inline int32_t
  3909. UnicodeString::lastIndexOf(const char16_t *srcChars,
  3910. int32_t srcLength,
  3911. int32_t start) const {
  3912. pinIndex(start);
  3913. return lastIndexOf(srcChars, 0, srcLength, start, length() - start);
  3914. }
  3915. inline int32_t
  3916. UnicodeString::lastIndexOf(const UnicodeString& srcText,
  3917. int32_t srcStart,
  3918. int32_t srcLength,
  3919. int32_t start,
  3920. int32_t _length) const
  3921. {
  3922. if(!srcText.isBogus()) {
  3923. srcText.pinIndices(srcStart, srcLength);
  3924. if(srcLength > 0) {
  3925. return lastIndexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
  3926. }
  3927. }
  3928. return -1;
  3929. }
  3930. inline int32_t
  3931. UnicodeString::lastIndexOf(const UnicodeString& text,
  3932. int32_t start,
  3933. int32_t _length) const
  3934. { return lastIndexOf(text, 0, text.length(), start, _length); }
  3935. inline int32_t
  3936. UnicodeString::lastIndexOf(const UnicodeString& text,
  3937. int32_t start) const {
  3938. pinIndex(start);
  3939. return lastIndexOf(text, 0, text.length(), start, length() - start);
  3940. }
  3941. inline int32_t
  3942. UnicodeString::lastIndexOf(const UnicodeString& text) const
  3943. { return lastIndexOf(text, 0, text.length(), 0, length()); }
  3944. inline int32_t
  3945. UnicodeString::lastIndexOf(char16_t c,
  3946. int32_t start,
  3947. int32_t _length) const
  3948. { return doLastIndexOf(c, start, _length); }
  3949. inline int32_t
  3950. UnicodeString::lastIndexOf(UChar32 c,
  3951. int32_t start,
  3952. int32_t _length) const {
  3953. return doLastIndexOf(c, start, _length);
  3954. }
  3955. inline int32_t
  3956. UnicodeString::lastIndexOf(char16_t c) const
  3957. { return doLastIndexOf(c, 0, length()); }
  3958. inline int32_t
  3959. UnicodeString::lastIndexOf(UChar32 c) const {
  3960. return lastIndexOf(c, 0, length());
  3961. }
  3962. inline int32_t
  3963. UnicodeString::lastIndexOf(char16_t c,
  3964. int32_t start) const {
  3965. pinIndex(start);
  3966. return doLastIndexOf(c, start, length() - start);
  3967. }
  3968. inline int32_t
  3969. UnicodeString::lastIndexOf(UChar32 c,
  3970. int32_t start) const {
  3971. pinIndex(start);
  3972. return lastIndexOf(c, start, length() - start);
  3973. }
  3974. inline UBool
  3975. UnicodeString::startsWith(const UnicodeString& text) const
  3976. { return doEqualsSubstring(0, text.length(), text, 0, text.length()); }
  3977. inline UBool
  3978. UnicodeString::startsWith(const UnicodeString& srcText,
  3979. int32_t srcStart,
  3980. int32_t srcLength) const
  3981. { return doEqualsSubstring(0, srcLength, srcText, srcStart, srcLength); }
  3982. inline UBool
  3983. UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
  3984. if(srcLength < 0) {
  3985. srcLength = u_strlen(toUCharPtr(srcChars));
  3986. }
  3987. return doEqualsSubstring(0, srcLength, srcChars, 0, srcLength);
  3988. }
  3989. inline UBool
  3990. UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const {
  3991. if(srcLength < 0) {
  3992. srcLength = u_strlen(toUCharPtr(srcChars));
  3993. }
  3994. return doEqualsSubstring(0, srcLength, srcChars, srcStart, srcLength);
  3995. }
  3996. inline UBool
  3997. UnicodeString::endsWith(const UnicodeString& text) const
  3998. { return doEqualsSubstring(length() - text.length(), text.length(),
  3999. text, 0, text.length()); }
  4000. inline UBool
  4001. UnicodeString::endsWith(const UnicodeString& srcText,
  4002. int32_t srcStart,
  4003. int32_t srcLength) const {
  4004. srcText.pinIndices(srcStart, srcLength);
  4005. return doEqualsSubstring(length() - srcLength, srcLength,
  4006. srcText, srcStart, srcLength);
  4007. }
  4008. inline UBool
  4009. UnicodeString::endsWith(ConstChar16Ptr srcChars,
  4010. int32_t srcLength) const {
  4011. if(srcLength < 0) {
  4012. srcLength = u_strlen(toUCharPtr(srcChars));
  4013. }
  4014. return doEqualsSubstring(length() - srcLength, srcLength, srcChars, 0, srcLength);
  4015. }
  4016. inline UBool
  4017. UnicodeString::endsWith(const char16_t *srcChars,
  4018. int32_t srcStart,
  4019. int32_t srcLength) const {
  4020. if(srcLength < 0) {
  4021. srcLength = u_strlen(toUCharPtr(srcChars + srcStart));
  4022. }
  4023. return doEqualsSubstring(length() - srcLength, srcLength,
  4024. srcChars, srcStart, srcLength);
  4025. }
  4026. //========================================
  4027. // replace
  4028. //========================================
  4029. inline UnicodeString&
  4030. UnicodeString::replace(int32_t start,
  4031. int32_t _length,
  4032. const UnicodeString& srcText)
  4033. { return doReplace(start, _length, srcText, 0, srcText.length()); }
  4034. inline UnicodeString&
  4035. UnicodeString::replace(int32_t start,
  4036. int32_t _length,
  4037. const UnicodeString& srcText,
  4038. int32_t srcStart,
  4039. int32_t srcLength)
  4040. { return doReplace(start, _length, srcText, srcStart, srcLength); }
  4041. inline UnicodeString&
  4042. UnicodeString::replace(int32_t start,
  4043. int32_t _length,
  4044. ConstChar16Ptr srcChars,
  4045. int32_t srcLength)
  4046. { return doReplace(start, _length, srcChars, 0, srcLength); }
  4047. inline UnicodeString&
  4048. UnicodeString::replace(int32_t start,
  4049. int32_t _length,
  4050. const char16_t *srcChars,
  4051. int32_t srcStart,
  4052. int32_t srcLength)
  4053. { return doReplace(start, _length, srcChars, srcStart, srcLength); }
  4054. inline UnicodeString&
  4055. UnicodeString::replace(int32_t start,
  4056. int32_t _length,
  4057. char16_t srcChar)
  4058. { return doReplace(start, _length, &srcChar, 0, 1); }
  4059. inline UnicodeString&
  4060. UnicodeString::replaceBetween(int32_t start,
  4061. int32_t limit,
  4062. const UnicodeString& srcText)
  4063. { return doReplace(start, limit - start, srcText, 0, srcText.length()); }
  4064. inline UnicodeString&
  4065. UnicodeString::replaceBetween(int32_t start,
  4066. int32_t limit,
  4067. const UnicodeString& srcText,
  4068. int32_t srcStart,
  4069. int32_t srcLimit)
  4070. { return doReplace(start, limit - start, srcText, srcStart, srcLimit - srcStart); }
  4071. inline UnicodeString&
  4072. UnicodeString::findAndReplace(const UnicodeString& oldText,
  4073. const UnicodeString& newText)
  4074. { return findAndReplace(0, length(), oldText, 0, oldText.length(),
  4075. newText, 0, newText.length()); }
  4076. inline UnicodeString&
  4077. UnicodeString::findAndReplace(int32_t start,
  4078. int32_t _length,
  4079. const UnicodeString& oldText,
  4080. const UnicodeString& newText)
  4081. { return findAndReplace(start, _length, oldText, 0, oldText.length(),
  4082. newText, 0, newText.length()); }
  4083. // ============================
  4084. // extract
  4085. // ============================
  4086. inline void
  4087. UnicodeString::doExtract(int32_t start,
  4088. int32_t _length,
  4089. UnicodeString& target) const
  4090. { target.replace(0, target.length(), *this, start, _length); }
  4091. inline void
  4092. UnicodeString::extract(int32_t start,
  4093. int32_t _length,
  4094. Char16Ptr target,
  4095. int32_t targetStart) const
  4096. { doExtract(start, _length, target, targetStart); }
  4097. inline void
  4098. UnicodeString::extract(int32_t start,
  4099. int32_t _length,
  4100. UnicodeString& target) const
  4101. { doExtract(start, _length, target); }
  4102. #if !UCONFIG_NO_CONVERSION
  4103. inline int32_t
  4104. UnicodeString::extract(int32_t start,
  4105. int32_t _length,
  4106. char *dst,
  4107. const char *codepage) const
  4108. {
  4109. // This dstSize value will be checked explicitly
  4110. return extract(start, _length, dst, dst != nullptr ? 0xffffffff : 0, codepage);
  4111. }
  4112. #endif
  4113. inline void
  4114. UnicodeString::extractBetween(int32_t start,
  4115. int32_t limit,
  4116. char16_t *dst,
  4117. int32_t dstStart) const {
  4118. pinIndex(start);
  4119. pinIndex(limit);
  4120. doExtract(start, limit - start, dst, dstStart);
  4121. }
  4122. inline UnicodeString
  4123. UnicodeString::tempSubStringBetween(int32_t start, int32_t limit) const {
  4124. return tempSubString(start, limit - start);
  4125. }
  4126. inline char16_t
  4127. UnicodeString::doCharAt(int32_t offset) const
  4128. {
  4129. if((uint32_t)offset < (uint32_t)length()) {
  4130. return getArrayStart()[offset];
  4131. } else {
  4132. return kInvalidUChar;
  4133. }
  4134. }
  4135. inline char16_t
  4136. UnicodeString::charAt(int32_t offset) const
  4137. { return doCharAt(offset); }
  4138. inline char16_t
  4139. UnicodeString::operator[] (int32_t offset) const
  4140. { return doCharAt(offset); }
  4141. inline UBool
  4142. UnicodeString::isEmpty() const {
  4143. // Arithmetic or logical right shift does not matter: only testing for 0.
  4144. return (fUnion.fFields.fLengthAndFlags>>kLengthShift) == 0;
  4145. }
  4146. //========================================
  4147. // Write implementation methods
  4148. //========================================
  4149. inline void
  4150. UnicodeString::setZeroLength() {
  4151. fUnion.fFields.fLengthAndFlags &= kAllStorageFlags;
  4152. }
  4153. inline void
  4154. UnicodeString::setShortLength(int32_t len) {
  4155. // requires 0 <= len <= kMaxShortLength
  4156. fUnion.fFields.fLengthAndFlags =
  4157. (int16_t)((fUnion.fFields.fLengthAndFlags & kAllStorageFlags) | (len << kLengthShift));
  4158. }
  4159. inline void
  4160. UnicodeString::setLength(int32_t len) {
  4161. if(len <= kMaxShortLength) {
  4162. setShortLength(len);
  4163. } else {
  4164. fUnion.fFields.fLengthAndFlags |= kLengthIsLarge;
  4165. fUnion.fFields.fLength = len;
  4166. }
  4167. }
  4168. inline void
  4169. UnicodeString::setToEmpty() {
  4170. fUnion.fFields.fLengthAndFlags = kShortString;
  4171. }
  4172. inline void
  4173. UnicodeString::setArray(char16_t *array, int32_t len, int32_t capacity) {
  4174. setLength(len);
  4175. fUnion.fFields.fArray = array;
  4176. fUnion.fFields.fCapacity = capacity;
  4177. }
  4178. inline UnicodeString&
  4179. UnicodeString::operator= (char16_t ch)
  4180. { return doReplace(0, length(), &ch, 0, 1); }
  4181. inline UnicodeString&
  4182. UnicodeString::operator= (UChar32 ch)
  4183. { return replace(0, length(), ch); }
  4184. inline UnicodeString&
  4185. UnicodeString::setTo(const UnicodeString& srcText,
  4186. int32_t srcStart,
  4187. int32_t srcLength)
  4188. {
  4189. unBogus();
  4190. return doReplace(0, length(), srcText, srcStart, srcLength);
  4191. }
  4192. inline UnicodeString&
  4193. UnicodeString::setTo(const UnicodeString& srcText,
  4194. int32_t srcStart)
  4195. {
  4196. unBogus();
  4197. srcText.pinIndex(srcStart);
  4198. return doReplace(0, length(), srcText, srcStart, srcText.length() - srcStart);
  4199. }
  4200. inline UnicodeString&
  4201. UnicodeString::setTo(const UnicodeString& srcText)
  4202. {
  4203. return copyFrom(srcText);
  4204. }
  4205. inline UnicodeString&
  4206. UnicodeString::setTo(const char16_t *srcChars,
  4207. int32_t srcLength)
  4208. {
  4209. unBogus();
  4210. return doReplace(0, length(), srcChars, 0, srcLength);
  4211. }
  4212. inline UnicodeString&
  4213. UnicodeString::setTo(char16_t srcChar)
  4214. {
  4215. unBogus();
  4216. return doReplace(0, length(), &srcChar, 0, 1);
  4217. }
  4218. inline UnicodeString&
  4219. UnicodeString::setTo(UChar32 srcChar)
  4220. {
  4221. unBogus();
  4222. return replace(0, length(), srcChar);
  4223. }
  4224. inline UnicodeString&
  4225. UnicodeString::append(const UnicodeString& srcText,
  4226. int32_t srcStart,
  4227. int32_t srcLength)
  4228. { return doAppend(srcText, srcStart, srcLength); }
  4229. inline UnicodeString&
  4230. UnicodeString::append(const UnicodeString& srcText)
  4231. { return doAppend(srcText, 0, srcText.length()); }
  4232. inline UnicodeString&
  4233. UnicodeString::append(const char16_t *srcChars,
  4234. int32_t srcStart,
  4235. int32_t srcLength)
  4236. { return doAppend(srcChars, srcStart, srcLength); }
  4237. inline UnicodeString&
  4238. UnicodeString::append(ConstChar16Ptr srcChars,
  4239. int32_t srcLength)
  4240. { return doAppend(srcChars, 0, srcLength); }
  4241. inline UnicodeString&
  4242. UnicodeString::append(char16_t srcChar)
  4243. { return doAppend(&srcChar, 0, 1); }
  4244. inline UnicodeString&
  4245. UnicodeString::operator+= (char16_t ch)
  4246. { return doAppend(&ch, 0, 1); }
  4247. inline UnicodeString&
  4248. UnicodeString::operator+= (UChar32 ch) {
  4249. return append(ch);
  4250. }
  4251. inline UnicodeString&
  4252. UnicodeString::operator+= (const UnicodeString& srcText)
  4253. { return doAppend(srcText, 0, srcText.length()); }
  4254. inline UnicodeString&
  4255. UnicodeString::insert(int32_t start,
  4256. const UnicodeString& srcText,
  4257. int32_t srcStart,
  4258. int32_t srcLength)
  4259. { return doReplace(start, 0, srcText, srcStart, srcLength); }
  4260. inline UnicodeString&
  4261. UnicodeString::insert(int32_t start,
  4262. const UnicodeString& srcText)
  4263. { return doReplace(start, 0, srcText, 0, srcText.length()); }
  4264. inline UnicodeString&
  4265. UnicodeString::insert(int32_t start,
  4266. const char16_t *srcChars,
  4267. int32_t srcStart,
  4268. int32_t srcLength)
  4269. { return doReplace(start, 0, srcChars, srcStart, srcLength); }
  4270. inline UnicodeString&
  4271. UnicodeString::insert(int32_t start,
  4272. ConstChar16Ptr srcChars,
  4273. int32_t srcLength)
  4274. { return doReplace(start, 0, srcChars, 0, srcLength); }
  4275. inline UnicodeString&
  4276. UnicodeString::insert(int32_t start,
  4277. char16_t srcChar)
  4278. { return doReplace(start, 0, &srcChar, 0, 1); }
  4279. inline UnicodeString&
  4280. UnicodeString::insert(int32_t start,
  4281. UChar32 srcChar)
  4282. { return replace(start, 0, srcChar); }
  4283. inline UnicodeString&
  4284. UnicodeString::remove()
  4285. {
  4286. // remove() of a bogus string makes the string empty and non-bogus
  4287. if(isBogus()) {
  4288. setToEmpty();
  4289. } else {
  4290. setZeroLength();
  4291. }
  4292. return *this;
  4293. }
  4294. inline UnicodeString&
  4295. UnicodeString::remove(int32_t start,
  4296. int32_t _length)
  4297. {
  4298. if(start <= 0 && _length == INT32_MAX) {
  4299. // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
  4300. return remove();
  4301. }
  4302. return doReplace(start, _length, nullptr, 0, 0);
  4303. }
  4304. inline UnicodeString&
  4305. UnicodeString::removeBetween(int32_t start,
  4306. int32_t limit)
  4307. { return doReplace(start, limit - start, nullptr, 0, 0); }
  4308. inline UnicodeString &
  4309. UnicodeString::retainBetween(int32_t start, int32_t limit) {
  4310. truncate(limit);
  4311. return doReplace(0, start, nullptr, 0, 0);
  4312. }
  4313. inline UBool
  4314. UnicodeString::truncate(int32_t targetLength)
  4315. {
  4316. if(isBogus() && targetLength == 0) {
  4317. // truncate(0) of a bogus string makes the string empty and non-bogus
  4318. unBogus();
  4319. return false;
  4320. } else if((uint32_t)targetLength < (uint32_t)length()) {
  4321. setLength(targetLength);
  4322. return true;
  4323. } else {
  4324. return false;
  4325. }
  4326. }
  4327. inline UnicodeString&
  4328. UnicodeString::reverse()
  4329. { return doReverse(0, length()); }
  4330. inline UnicodeString&
  4331. UnicodeString::reverse(int32_t start,
  4332. int32_t _length)
  4333. { return doReverse(start, _length); }
  4334. U_NAMESPACE_END
  4335. #endif /* U_SHOW_CPLUSPLUS_API */
  4336. #endif