unistr.h 161 KB

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