utext.h 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. *
  6. * Copyright (C) 2004-2012, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: utext.h
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2004oct06
  16. * created by: Markus W. Scherer
  17. */
  18. #ifndef __UTEXT_H__
  19. #define __UTEXT_H__
  20. /**
  21. * \file
  22. * \brief C API: Abstract Unicode Text API
  23. *
  24. * The Text Access API provides a means to allow text that is stored in alternative
  25. * formats to work with ICU services. ICU normally operates on text that is
  26. * stored in UTF-16 format, in (UChar *) arrays for the C APIs or as type
  27. * UnicodeString for C++ APIs.
  28. *
  29. * ICU Text Access allows other formats, such as UTF-8 or non-contiguous
  30. * UTF-16 strings, to be placed in a UText wrapper and then passed to ICU services.
  31. *
  32. * There are three general classes of usage for UText:
  33. *
  34. * Application Level Use. This is the simplest usage - applications would
  35. * use one of the utext_open() functions on their input text, and pass
  36. * the resulting UText to the desired ICU service.
  37. *
  38. * Second is usage in ICU Services, such as break iteration, that will need to
  39. * operate on input presented to them as a UText. These implementations
  40. * will need to use the iteration and related UText functions to gain
  41. * access to the actual text.
  42. *
  43. * The third class of UText users are "text providers." These are the
  44. * UText implementations for the various text storage formats. An application
  45. * or system with a unique text storage format can implement a set of
  46. * UText provider functions for that format, which will then allow
  47. * ICU services to operate on that format.
  48. *
  49. *
  50. * <em>Iterating over text</em>
  51. *
  52. * Here is sample code for a forward iteration over the contents of a UText
  53. *
  54. * \code
  55. * UChar32 c;
  56. * UText *ut = whatever();
  57. *
  58. * for (c=utext_next32From(ut, 0); c>=0; c=utext_next32(ut)) {
  59. * // do whatever with the codepoint c here.
  60. * }
  61. * \endcode
  62. *
  63. * And here is similar code to iterate in the reverse direction, from the end
  64. * of the text towards the beginning.
  65. *
  66. * \code
  67. * UChar32 c;
  68. * UText *ut = whatever();
  69. * int textLength = utext_nativeLength(ut);
  70. * for (c=utext_previous32From(ut, textLength); c>=0; c=utext_previous32(ut)) {
  71. * // do whatever with the codepoint c here.
  72. * }
  73. * \endcode
  74. *
  75. * <em>Characters and Indexing</em>
  76. *
  77. * Indexing into text by UText functions is nearly always in terms of the native
  78. * indexing of the underlying text storage. The storage format could be UTF-8
  79. * or UTF-32, for example. When coding to the UText access API, no assumptions
  80. * can be made regarding the size of characters, or how far an index
  81. * may move when iterating between characters.
  82. *
  83. * All indices supplied to UText functions are pinned to the length of the
  84. * text. An out-of-bounds index is not considered to be an error, but is
  85. * adjusted to be in the range 0 <= index <= length of input text.
  86. *
  87. *
  88. * When an index position is returned from a UText function, it will be
  89. * a native index to the underlying text. In the case of multi-unit characters,
  90. * it will always refer to the first position of the character,
  91. * never to the interior. This is essentially the same thing as saying that
  92. * a returned index will always point to a boundary between characters.
  93. *
  94. * When a native index is supplied to a UText function, all indices that
  95. * refer to any part of a multi-unit character representation are considered
  96. * to be equivalent. In the case of multi-unit characters, an incoming index
  97. * will be logically normalized to refer to the start of the character.
  98. *
  99. * It is possible to test whether a native index is on a code point boundary
  100. * by doing a utext_setNativeIndex() followed by a utext_getNativeIndex().
  101. * If the index is returned unchanged, it was on a code point boundary. If
  102. * an adjusted index is returned, the original index referred to the
  103. * interior of a character.
  104. *
  105. * <em>Conventions for calling UText functions</em>
  106. *
  107. * Most UText access functions have as their first parameter a (UText *) pointer,
  108. * which specifies the UText to be used. Unless otherwise noted, the
  109. * pointer must refer to a valid, open UText. Attempting to
  110. * use a closed UText or passing a NULL pointer is a programming error and
  111. * will produce undefined results or NULL pointer exceptions.
  112. *
  113. * The UText_Open family of functions can either open an existing (closed)
  114. * UText, or heap allocate a new UText. Here is sample code for creating
  115. * a stack-allocated UText.
  116. *
  117. * \code
  118. * char *s = whatever(); // A utf-8 string
  119. * U_ErrorCode status = U_ZERO_ERROR;
  120. * UText ut = UTEXT_INITIALIZER;
  121. * utext_openUTF8(ut, s, -1, &status);
  122. * if (U_FAILURE(status)) {
  123. * // error handling
  124. * } else {
  125. * // work with the UText
  126. * }
  127. * \endcode
  128. *
  129. * Any existing UText passed to an open function _must_ have been initialized,
  130. * either by the UTEXT_INITIALIZER, or by having been originally heap-allocated
  131. * by an open function. Passing NULL will cause the open function to
  132. * heap-allocate and fully initialize a new UText.
  133. *
  134. */
  135. #include "unicode/utypes.h"
  136. #include "unicode/uchar.h"
  137. #if U_SHOW_CPLUSPLUS_API
  138. #include "unicode/localpointer.h"
  139. #include "unicode/rep.h"
  140. #include "unicode/unistr.h"
  141. #include "unicode/chariter.h"
  142. #endif
  143. U_CDECL_BEGIN
  144. struct UText;
  145. typedef struct UText UText; /**< C typedef for struct UText. @stable ICU 3.6 */
  146. /***************************************************************************************
  147. *
  148. * C Functions for creating UText wrappers around various kinds of text strings.
  149. *
  150. ****************************************************************************************/
  151. /**
  152. * Close function for UText instances.
  153. * Cleans up, releases any resources being held by an open UText.
  154. * <p>
  155. * If the UText was originally allocated by one of the utext_open functions,
  156. * the storage associated with the utext will also be freed.
  157. * If the UText storage originated with the application, as it would with
  158. * a local or static instance, the storage will not be deleted.
  159. *
  160. * An open UText can be reset to refer to new string by using one of the utext_open()
  161. * functions without first closing the UText.
  162. *
  163. * @param ut The UText to be closed.
  164. * @return NULL if the UText struct was deleted by the close. If the UText struct
  165. * was originally provided by the caller to the open function, it is
  166. * returned by this function, and may be safely used again in
  167. * a subsequent utext_open.
  168. *
  169. * @stable ICU 3.4
  170. */
  171. U_CAPI UText * U_EXPORT2
  172. utext_close(UText *ut);
  173. /**
  174. * Open a read-only UText implementation for UTF-8 strings.
  175. *
  176. * \htmlonly
  177. * Any invalid UTF-8 in the input will be handled in this way:
  178. * a sequence of bytes that has the form of a truncated, but otherwise valid,
  179. * UTF-8 sequence will be replaced by a single unicode replacement character, \uFFFD.
  180. * Any other illegal bytes will each be replaced by a \uFFFD.
  181. * \endhtmlonly
  182. *
  183. * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
  184. * If non-NULL, must refer to an initialized UText struct, which will then
  185. * be reset to reference the specified UTF-8 string.
  186. * @param s A UTF-8 string. Must not be NULL.
  187. * @param length The length of the UTF-8 string in bytes, or -1 if the string is
  188. * zero terminated.
  189. * @param status Errors are returned here.
  190. * @return A pointer to the UText. If a pre-allocated UText was provided, it
  191. * will always be used and returned.
  192. * @stable ICU 3.4
  193. */
  194. U_CAPI UText * U_EXPORT2
  195. utext_openUTF8(UText *ut, const char *s, int64_t length, UErrorCode *status);
  196. /**
  197. * Open a read-only UText for UChar * string.
  198. *
  199. * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
  200. * If non-NULL, must refer to an initialized UText struct, which will then
  201. * be reset to reference the specified UChar string.
  202. * @param s A UChar (UTF-16) string
  203. * @param length The number of UChars in the input string, or -1 if the string is
  204. * zero terminated.
  205. * @param status Errors are returned here.
  206. * @return A pointer to the UText. If a pre-allocated UText was provided, it
  207. * will always be used and returned.
  208. * @stable ICU 3.4
  209. */
  210. U_CAPI UText * U_EXPORT2
  211. utext_openUChars(UText *ut, const UChar *s, int64_t length, UErrorCode *status);
  212. #if U_SHOW_CPLUSPLUS_API
  213. /**
  214. * Open a writable UText for a non-const UnicodeString.
  215. *
  216. * @param ut Pointer to a UText struct. If nullptr, a new UText will be created.
  217. * If non-nullptr, must refer to an initialized UText struct, which will then
  218. * be reset to reference the specified input string.
  219. * @param s A UnicodeString.
  220. * @param status Errors are returned here.
  221. * @return Pointer to the UText. If a UText was supplied as input, this
  222. * will always be used and returned.
  223. * @stable ICU 3.4
  224. */
  225. U_CAPI UText * U_EXPORT2
  226. utext_openUnicodeString(UText *ut, icu::UnicodeString *s, UErrorCode *status);
  227. /**
  228. * Open a UText for a const UnicodeString. The resulting UText will not be writable.
  229. *
  230. * @param ut Pointer to a UText struct. If nullptr, a new UText will be created.
  231. * If non-nullptr, must refer to an initialized UText struct, which will then
  232. * be reset to reference the specified input string.
  233. * @param s A const UnicodeString to be wrapped.
  234. * @param status Errors are returned here.
  235. * @return Pointer to the UText. If a UText was supplied as input, this
  236. * will always be used and returned.
  237. * @stable ICU 3.4
  238. */
  239. U_CAPI UText * U_EXPORT2
  240. utext_openConstUnicodeString(UText *ut, const icu::UnicodeString *s, UErrorCode *status);
  241. /**
  242. * Open a writable UText implementation for an ICU Replaceable object.
  243. * @param ut Pointer to a UText struct. If nullptr, a new UText will be created.
  244. * If non-nullptr, must refer to an already existing UText, which will then
  245. * be reset to reference the specified replaceable text.
  246. * @param rep A Replaceable text object.
  247. * @param status Errors are returned here.
  248. * @return Pointer to the UText. If a UText was supplied as input, this
  249. * will always be used and returned.
  250. * @see Replaceable
  251. * @stable ICU 3.4
  252. */
  253. U_CAPI UText * U_EXPORT2
  254. utext_openReplaceable(UText *ut, icu::Replaceable *rep, UErrorCode *status);
  255. /**
  256. * Open a UText implementation over an ICU CharacterIterator.
  257. * @param ut Pointer to a UText struct. If nullptr, a new UText will be created.
  258. * If non-nullptr, must refer to an already existing UText, which will then
  259. * be reset to reference the specified replaceable text.
  260. * @param ci A Character Iterator.
  261. * @param status Errors are returned here.
  262. * @return Pointer to the UText. If a UText was supplied as input, this
  263. * will always be used and returned.
  264. * @see Replaceable
  265. * @stable ICU 3.4
  266. */
  267. U_CAPI UText * U_EXPORT2
  268. utext_openCharacterIterator(UText *ut, icu::CharacterIterator *ci, UErrorCode *status);
  269. #endif
  270. /**
  271. * Clone a UText. This is much like opening a UText where the source text is itself
  272. * another UText.
  273. *
  274. * A deep clone will copy both the UText data structures and the underlying text.
  275. * The original and cloned UText will operate completely independently; modifications
  276. * made to the text in one will not affect the other. Text providers are not
  277. * required to support deep clones. The user of clone() must check the status return
  278. * and be prepared to handle failures.
  279. *
  280. * The standard UText implementations for UTF8, UChar *, UnicodeString and
  281. * Replaceable all support deep cloning.
  282. *
  283. * The UText returned from a deep clone will be writable, assuming that the text
  284. * provider is able to support writing, even if the source UText had been made
  285. * non-writable by means of UText_freeze().
  286. *
  287. * A shallow clone replicates only the UText data structures; it does not make
  288. * a copy of the underlying text. Shallow clones can be used as an efficient way to
  289. * have multiple iterators active in a single text string that is not being
  290. * modified.
  291. *
  292. * A shallow clone operation will not fail, barring truly exceptional conditions such
  293. * as memory allocation failures.
  294. *
  295. * Shallow UText clones should be avoided if the UText functions that modify the
  296. * text are expected to be used, either on the original or the cloned UText.
  297. * Any such modifications can cause unpredictable behavior. Read Only
  298. * shallow clones provide some protection against errors of this type by
  299. * disabling text modification via the cloned UText.
  300. *
  301. * A shallow clone made with the readOnly parameter == false will preserve the
  302. * utext_isWritable() state of the source object. Note, however, that
  303. * write operations must be avoided while more than one UText exists that refer
  304. * to the same underlying text.
  305. *
  306. * A UText and its clone may be safely concurrently accessed by separate threads.
  307. * This is true for read access only with shallow clones, and for both read and
  308. * write access with deep clones.
  309. * It is the responsibility of the Text Provider to ensure that this thread safety
  310. * constraint is met.
  311. *
  312. * @param dest A UText struct to be filled in with the result of the clone operation,
  313. * or NULL if the clone function should heap-allocate a new UText struct.
  314. * If non-NULL, must refer to an already existing UText, which will then
  315. * be reset to become the clone.
  316. * @param src The UText to be cloned.
  317. * @param deep true to request a deep clone, false for a shallow clone.
  318. * @param readOnly true to request that the cloned UText have read only access to the
  319. * underlying text.
  320. * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
  321. * will be returned if the text provider is unable to clone the
  322. * original text.
  323. * @return The newly created clone, or NULL if the clone operation failed.
  324. * @stable ICU 3.4
  325. */
  326. U_CAPI UText * U_EXPORT2
  327. utext_clone(UText *dest, const UText *src, UBool deep, UBool readOnly, UErrorCode *status);
  328. /**
  329. * Compare two UText objects for equality.
  330. * UTexts are equal if they are iterating over the same text, and
  331. * have the same iteration position within the text.
  332. * If either or both of the parameters are NULL, the comparison is false.
  333. *
  334. * @param a The first of the two UTexts to compare.
  335. * @param b The other UText to be compared.
  336. * @return true if the two UTexts are equal.
  337. * @stable ICU 3.6
  338. */
  339. U_CAPI UBool U_EXPORT2
  340. utext_equals(const UText *a, const UText *b);
  341. /*****************************************************************************
  342. *
  343. * Functions to work with the text represented by a UText wrapper
  344. *
  345. *****************************************************************************/
  346. /**
  347. * Get the length of the text. Depending on the characteristics
  348. * of the underlying text representation, this may be expensive.
  349. * @see utext_isLengthExpensive()
  350. *
  351. *
  352. * @param ut the text to be accessed.
  353. * @return the length of the text, expressed in native units.
  354. *
  355. * @stable ICU 3.4
  356. */
  357. U_CAPI int64_t U_EXPORT2
  358. utext_nativeLength(UText *ut);
  359. /**
  360. * Return true if calculating the length of the text could be expensive.
  361. * Finding the length of NUL terminated strings is considered to be expensive.
  362. *
  363. * Note that the value of this function may change
  364. * as the result of other operations on a UText.
  365. * Once the length of a string has been discovered, it will no longer
  366. * be expensive to report it.
  367. *
  368. * @param ut the text to be accessed.
  369. * @return true if determining the length of the text could be time consuming.
  370. * @stable ICU 3.4
  371. */
  372. U_CAPI UBool U_EXPORT2
  373. utext_isLengthExpensive(const UText *ut);
  374. /**
  375. * Returns the code point at the requested index,
  376. * or U_SENTINEL (-1) if it is out of bounds.
  377. *
  378. * If the specified index points to the interior of a multi-unit
  379. * character - one of the trail bytes of a UTF-8 sequence, for example -
  380. * the complete code point will be returned.
  381. *
  382. * The iteration position will be set to the start of the returned code point.
  383. *
  384. * This function is roughly equivalent to the sequence
  385. * utext_setNativeIndex(index);
  386. * utext_current32();
  387. * (There is a subtle difference if the index is out of bounds by being less than zero -
  388. * utext_setNativeIndex(negative value) sets the index to zero, after which utext_current()
  389. * will return the char at zero. utext_char32At(negative index), on the other hand, will
  390. * return the U_SENTINEL value of -1.)
  391. *
  392. * @param ut the text to be accessed
  393. * @param nativeIndex the native index of the character to be accessed. If the index points
  394. * to other than the first unit of a multi-unit character, it will be adjusted
  395. * to the start of the character.
  396. * @return the code point at the specified index.
  397. * @stable ICU 3.4
  398. */
  399. U_CAPI UChar32 U_EXPORT2
  400. utext_char32At(UText *ut, int64_t nativeIndex);
  401. /**
  402. *
  403. * Get the code point at the current iteration position,
  404. * or U_SENTINEL (-1) if the iteration has reached the end of
  405. * the input text.
  406. *
  407. * @param ut the text to be accessed.
  408. * @return the Unicode code point at the current iterator position.
  409. * @stable ICU 3.4
  410. */
  411. U_CAPI UChar32 U_EXPORT2
  412. utext_current32(UText *ut);
  413. /**
  414. * Get the code point at the current iteration position of the UText, and
  415. * advance the position to the first index following the character.
  416. *
  417. * If the position is at the end of the text (the index following
  418. * the last character, which is also the length of the text),
  419. * return U_SENTINEL (-1) and do not advance the index.
  420. *
  421. * This is a post-increment operation.
  422. *
  423. * An inline macro version of this function, UTEXT_NEXT32(),
  424. * is available for performance critical use.
  425. *
  426. * @param ut the text to be accessed.
  427. * @return the Unicode code point at the iteration position.
  428. * @see UTEXT_NEXT32
  429. * @stable ICU 3.4
  430. */
  431. U_CAPI UChar32 U_EXPORT2
  432. utext_next32(UText *ut);
  433. /**
  434. * Move the iterator position to the character (code point) whose
  435. * index precedes the current position, and return that character.
  436. * This is a pre-decrement operation.
  437. *
  438. * If the initial position is at the start of the text (index of 0)
  439. * return U_SENTINEL (-1), and leave the position unchanged.
  440. *
  441. * An inline macro version of this function, UTEXT_PREVIOUS32(),
  442. * is available for performance critical use.
  443. *
  444. * @param ut the text to be accessed.
  445. * @return the previous UChar32 code point, or U_SENTINEL (-1)
  446. * if the iteration has reached the start of the text.
  447. * @see UTEXT_PREVIOUS32
  448. * @stable ICU 3.4
  449. */
  450. U_CAPI UChar32 U_EXPORT2
  451. utext_previous32(UText *ut);
  452. /**
  453. * Set the iteration index and return the code point at that index.
  454. * Leave the iteration index at the start of the following code point.
  455. *
  456. * This function is the most efficient and convenient way to
  457. * begin a forward iteration. The results are identical to the those
  458. * from the sequence
  459. * \code
  460. * utext_setIndex();
  461. * utext_next32();
  462. * \endcode
  463. *
  464. * @param ut the text to be accessed.
  465. * @param nativeIndex Iteration index, in the native units of the text provider.
  466. * @return Code point which starts at or before index,
  467. * or U_SENTINEL (-1) if it is out of bounds.
  468. * @stable ICU 3.4
  469. */
  470. U_CAPI UChar32 U_EXPORT2
  471. utext_next32From(UText *ut, int64_t nativeIndex);
  472. /**
  473. * Set the iteration index, and return the code point preceding the
  474. * one specified by the initial index. Leave the iteration position
  475. * at the start of the returned code point.
  476. *
  477. * This function is the most efficient and convenient way to
  478. * begin a backwards iteration.
  479. *
  480. * @param ut the text to be accessed.
  481. * @param nativeIndex Iteration index in the native units of the text provider.
  482. * @return Code point preceding the one at the initial index,
  483. * or U_SENTINEL (-1) if it is out of bounds.
  484. *
  485. * @stable ICU 3.4
  486. */
  487. U_CAPI UChar32 U_EXPORT2
  488. utext_previous32From(UText *ut, int64_t nativeIndex);
  489. /**
  490. * Get the current iterator position, which can range from 0 to
  491. * the length of the text.
  492. * The position is a native index into the input text, in whatever format it
  493. * may have (possibly UTF-8 for example), and may not always be the same as
  494. * the corresponding UChar (UTF-16) index.
  495. * The returned position will always be aligned to a code point boundary.
  496. *
  497. * @param ut the text to be accessed.
  498. * @return the current index position, in the native units of the text provider.
  499. * @stable ICU 3.4
  500. */
  501. U_CAPI int64_t U_EXPORT2
  502. utext_getNativeIndex(const UText *ut);
  503. /**
  504. * Set the current iteration position to the nearest code point
  505. * boundary at or preceding the specified index.
  506. * The index is in the native units of the original input text.
  507. * If the index is out of range, it will be pinned to be within
  508. * the range of the input text.
  509. * <p>
  510. * It will usually be more efficient to begin an iteration
  511. * using the functions utext_next32From() or utext_previous32From()
  512. * rather than setIndex().
  513. * <p>
  514. * Moving the index position to an adjacent character is best done
  515. * with utext_next32(), utext_previous32() or utext_moveIndex32().
  516. * Attempting to do direct arithmetic on the index position is
  517. * complicated by the fact that the size (in native units) of a
  518. * character depends on the underlying representation of the character
  519. * (UTF-8, UTF-16, UTF-32, arbitrary codepage), and is not
  520. * easily knowable.
  521. *
  522. * @param ut the text to be accessed.
  523. * @param nativeIndex the native unit index of the new iteration position.
  524. * @stable ICU 3.4
  525. */
  526. U_CAPI void U_EXPORT2
  527. utext_setNativeIndex(UText *ut, int64_t nativeIndex);
  528. /**
  529. * Move the iterator position by delta code points. The number of code points
  530. * is a signed number; a negative delta will move the iterator backwards,
  531. * towards the start of the text.
  532. * <p>
  533. * The index is moved by <code>delta</code> code points
  534. * forward or backward, but no further backward than to 0 and
  535. * no further forward than to utext_nativeLength().
  536. * The resulting index value will be in between 0 and length, inclusive.
  537. *
  538. * @param ut the text to be accessed.
  539. * @param delta the signed number of code points to move the iteration position.
  540. * @return true if the position could be moved the requested number of positions while
  541. * staying within the range [0 - text length].
  542. * @stable ICU 3.4
  543. */
  544. U_CAPI UBool U_EXPORT2
  545. utext_moveIndex32(UText *ut, int32_t delta);
  546. /**
  547. * Get the native index of the character preceding the current position.
  548. * If the iteration position is already at the start of the text, zero
  549. * is returned.
  550. * The value returned is the same as that obtained from the following sequence,
  551. * but without the side effect of changing the iteration position.
  552. *
  553. * \code
  554. * UText *ut = whatever;
  555. * ...
  556. * utext_previous(ut)
  557. * utext_getNativeIndex(ut);
  558. * \endcode
  559. *
  560. * This function is most useful during forwards iteration, where it will get the
  561. * native index of the character most recently returned from utext_next().
  562. *
  563. * @param ut the text to be accessed
  564. * @return the native index of the character preceding the current index position,
  565. * or zero if the current position is at the start of the text.
  566. * @stable ICU 3.6
  567. */
  568. U_CAPI int64_t U_EXPORT2
  569. utext_getPreviousNativeIndex(UText *ut);
  570. /**
  571. *
  572. * Extract text from a UText into a UChar buffer. The range of text to be extracted
  573. * is specified in the native indices of the UText provider. These may not necessarily
  574. * be UTF-16 indices.
  575. * <p>
  576. * The size (number of 16 bit UChars) of the data to be extracted is returned. The
  577. * full number of UChars is returned, even when the extracted text is truncated
  578. * because the specified buffer size is too small.
  579. * <p>
  580. * The extracted string will (if you are a user) / must (if you are a text provider)
  581. * be NUL-terminated if there is sufficient space in the destination buffer. This
  582. * terminating NUL is not included in the returned length.
  583. * <p>
  584. * The iteration index is left at the position following the last extracted character.
  585. *
  586. * @param ut the UText from which to extract data.
  587. * @param nativeStart the native index of the first character to extract.\
  588. * If the specified index is out of range,
  589. * it will be pinned to be within 0 <= index <= textLength
  590. * @param nativeLimit the native string index of the position following the last
  591. * character to extract. If the specified index is out of range,
  592. * it will be pinned to be within 0 <= index <= textLength.
  593. * nativeLimit must be >= nativeStart.
  594. * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
  595. * @param destCapacity The size, in UChars, of the destination buffer. May be zero
  596. * for precomputing the required size.
  597. * @param status receives any error status.
  598. * U_BUFFER_OVERFLOW_ERROR: the extracted text was truncated because the
  599. * buffer was too small. Returns number of UChars for preflighting.
  600. * @return Number of UChars in the data to be extracted. Does not include a trailing NUL.
  601. *
  602. * @stable ICU 3.4
  603. */
  604. U_CAPI int32_t U_EXPORT2
  605. utext_extract(UText *ut,
  606. int64_t nativeStart, int64_t nativeLimit,
  607. UChar *dest, int32_t destCapacity,
  608. UErrorCode *status);
  609. /************************************************************************************
  610. *
  611. * #define inline versions of selected performance-critical text access functions
  612. * Caution: do not use auto increment++ or decrement-- expressions
  613. * as parameters to these macros.
  614. *
  615. * For most use, where there is no extreme performance constraint, the
  616. * normal, non-inline functions are a better choice. The resulting code
  617. * will be smaller, and, if the need ever arises, easier to debug.
  618. *
  619. * These are implemented as #defines rather than real functions
  620. * because there is no fully portable way to do inline functions in plain C.
  621. *
  622. ************************************************************************************/
  623. #ifndef U_HIDE_INTERNAL_API
  624. /**
  625. * inline version of utext_current32(), for performance-critical situations.
  626. *
  627. * Get the code point at the current iteration position of the UText.
  628. * Returns U_SENTINEL (-1) if the position is at the end of the
  629. * text.
  630. *
  631. * @internal ICU 4.4 technology preview
  632. */
  633. #define UTEXT_CURRENT32(ut) \
  634. ((ut)->chunkOffset < (ut)->chunkLength && ((ut)->chunkContents)[(ut)->chunkOffset]<0xd800 ? \
  635. ((ut)->chunkContents)[((ut)->chunkOffset)] : utext_current32(ut))
  636. #endif /* U_HIDE_INTERNAL_API */
  637. /**
  638. * inline version of utext_next32(), for performance-critical situations.
  639. *
  640. * Get the code point at the current iteration position of the UText, and
  641. * advance the position to the first index following the character.
  642. * This is a post-increment operation.
  643. * Returns U_SENTINEL (-1) if the position is at the end of the
  644. * text.
  645. *
  646. * @stable ICU 3.4
  647. */
  648. #define UTEXT_NEXT32(ut) \
  649. ((ut)->chunkOffset < (ut)->chunkLength && ((ut)->chunkContents)[(ut)->chunkOffset]<0xd800 ? \
  650. ((ut)->chunkContents)[((ut)->chunkOffset)++] : utext_next32(ut))
  651. /**
  652. * inline version of utext_previous32(), for performance-critical situations.
  653. *
  654. * Move the iterator position to the character (code point) whose
  655. * index precedes the current position, and return that character.
  656. * This is a pre-decrement operation.
  657. * Returns U_SENTINEL (-1) if the position is at the start of the text.
  658. *
  659. * @stable ICU 3.4
  660. */
  661. #define UTEXT_PREVIOUS32(ut) \
  662. ((ut)->chunkOffset > 0 && \
  663. (ut)->chunkContents[(ut)->chunkOffset-1] < 0xd800 ? \
  664. (ut)->chunkContents[--((ut)->chunkOffset)] : utext_previous32(ut))
  665. /**
  666. * inline version of utext_getNativeIndex(), for performance-critical situations.
  667. *
  668. * Get the current iterator position, which can range from 0 to
  669. * the length of the text.
  670. * The position is a native index into the input text, in whatever format it
  671. * may have (possibly UTF-8 for example), and may not always be the same as
  672. * the corresponding UChar (UTF-16) index.
  673. * The returned position will always be aligned to a code point boundary.
  674. *
  675. * @stable ICU 3.6
  676. */
  677. #define UTEXT_GETNATIVEINDEX(ut) \
  678. ((ut)->chunkOffset <= (ut)->nativeIndexingLimit? \
  679. (ut)->chunkNativeStart+(ut)->chunkOffset : \
  680. (ut)->pFuncs->mapOffsetToNative(ut))
  681. /**
  682. * inline version of utext_setNativeIndex(), for performance-critical situations.
  683. *
  684. * Set the current iteration position to the nearest code point
  685. * boundary at or preceding the specified index.
  686. * The index is in the native units of the original input text.
  687. * If the index is out of range, it will be pinned to be within
  688. * the range of the input text.
  689. *
  690. * @stable ICU 3.8
  691. */
  692. #define UTEXT_SETNATIVEINDEX(ut, ix) UPRV_BLOCK_MACRO_BEGIN { \
  693. int64_t __offset = (ix) - (ut)->chunkNativeStart; \
  694. if (__offset>=0 && __offset<(int64_t)(ut)->nativeIndexingLimit && (ut)->chunkContents[__offset]<0xdc00) { \
  695. (ut)->chunkOffset=(int32_t)__offset; \
  696. } else { \
  697. utext_setNativeIndex((ut), (ix)); \
  698. } \
  699. } UPRV_BLOCK_MACRO_END
  700. /************************************************************************************
  701. *
  702. * Functions related to writing or modifying the text.
  703. * These will work only with modifiable UTexts. Attempting to
  704. * modify a read-only UText will return an error status.
  705. *
  706. ************************************************************************************/
  707. /**
  708. * Return true if the text can be written (modified) with utext_replace() or
  709. * utext_copy(). For the text to be writable, the text provider must
  710. * be of a type that supports writing and the UText must not be frozen.
  711. *
  712. * Attempting to modify text when utext_isWriteable() is false will fail -
  713. * the text will not be modified, and an error will be returned from the function
  714. * that attempted the modification.
  715. *
  716. * @param ut the UText to be tested.
  717. * @return true if the text is modifiable.
  718. *
  719. * @see utext_freeze()
  720. * @see utext_replace()
  721. * @see utext_copy()
  722. * @stable ICU 3.4
  723. *
  724. */
  725. U_CAPI UBool U_EXPORT2
  726. utext_isWritable(const UText *ut);
  727. /**
  728. * Test whether there is meta data associated with the text.
  729. * @see Replaceable::hasMetaData()
  730. *
  731. * @param ut The UText to be tested
  732. * @return true if the underlying text includes meta data.
  733. * @stable ICU 3.4
  734. */
  735. U_CAPI UBool U_EXPORT2
  736. utext_hasMetaData(const UText *ut);
  737. /**
  738. * Replace a range of the original text with a replacement text.
  739. *
  740. * Leaves the current iteration position at the position following the
  741. * newly inserted replacement text.
  742. *
  743. * This function is only available on UText types that support writing,
  744. * that is, ones where utext_isWritable() returns true.
  745. *
  746. * When using this function, there should be only a single UText opened onto the
  747. * underlying native text string. Behavior after a replace operation
  748. * on a UText is undefined for any other additional UTexts that refer to the
  749. * modified string.
  750. *
  751. * @param ut the UText representing the text to be operated on.
  752. * @param nativeStart the native index of the start of the region to be replaced
  753. * @param nativeLimit the native index of the character following the region to be replaced.
  754. * @param replacementText pointer to the replacement text
  755. * @param replacementLength length of the replacement text, or -1 if the text is NUL terminated.
  756. * @param status receives any error status. Possible errors include
  757. * U_NO_WRITE_PERMISSION
  758. *
  759. * @return The signed number of (native) storage units by which
  760. * the length of the text expanded or contracted.
  761. *
  762. * @stable ICU 3.4
  763. */
  764. U_CAPI int32_t U_EXPORT2
  765. utext_replace(UText *ut,
  766. int64_t nativeStart, int64_t nativeLimit,
  767. const UChar *replacementText, int32_t replacementLength,
  768. UErrorCode *status);
  769. /**
  770. *
  771. * Copy or move a substring from one position to another within the text,
  772. * while retaining any metadata associated with the text.
  773. * This function is used to duplicate or reorder substrings.
  774. * The destination index must not overlap the source range.
  775. *
  776. * The text to be copied or moved is inserted at destIndex;
  777. * it does not replace or overwrite any existing text.
  778. *
  779. * The iteration position is left following the newly inserted text
  780. * at the destination position.
  781. *
  782. * This function is only available on UText types that support writing,
  783. * that is, ones where utext_isWritable() returns true.
  784. *
  785. * When using this function, there should be only a single UText opened onto the
  786. * underlying native text string. Behavior after a copy operation
  787. * on a UText is undefined in any other additional UTexts that refer to the
  788. * modified string.
  789. *
  790. * @param ut The UText representing the text to be operated on.
  791. * @param nativeStart The native index of the start of the region to be copied or moved
  792. * @param nativeLimit The native index of the character position following the region
  793. * to be copied.
  794. * @param destIndex The native destination index to which the source substring is
  795. * copied or moved.
  796. * @param move If true, then the substring is moved, not copied/duplicated.
  797. * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
  798. *
  799. * @stable ICU 3.4
  800. */
  801. U_CAPI void U_EXPORT2
  802. utext_copy(UText *ut,
  803. int64_t nativeStart, int64_t nativeLimit,
  804. int64_t destIndex,
  805. UBool move,
  806. UErrorCode *status);
  807. /**
  808. * <p>
  809. * Freeze a UText. This prevents any modification to the underlying text itself
  810. * by means of functions operating on this UText.
  811. * </p>
  812. * <p>
  813. * Once frozen, a UText can not be unfrozen. The intent is to ensure
  814. * that a the text underlying a frozen UText wrapper cannot be modified via that UText.
  815. * </p>
  816. * <p>
  817. * Caution: freezing a UText will disable changes made via the specific
  818. * frozen UText wrapper only; it will not have any effect on the ability to
  819. * directly modify the text by bypassing the UText. Any such backdoor modifications
  820. * are always an error while UText access is occurring because the underlying
  821. * text can get out of sync with UText's buffering.
  822. * </p>
  823. *
  824. * @param ut The UText to be frozen.
  825. * @see utext_isWritable()
  826. * @stable ICU 3.6
  827. */
  828. U_CAPI void U_EXPORT2
  829. utext_freeze(UText *ut);
  830. /**
  831. * UText provider properties (bit field indexes).
  832. *
  833. * @see UText
  834. * @stable ICU 3.4
  835. */
  836. enum {
  837. /**
  838. * It is potentially time consuming for the provider to determine the length of the text.
  839. * @stable ICU 3.4
  840. */
  841. UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE = 1,
  842. /**
  843. * Text chunks remain valid and usable until the text object is modified or
  844. * deleted, not just until the next time the access() function is called
  845. * (which is the default).
  846. * @stable ICU 3.4
  847. */
  848. UTEXT_PROVIDER_STABLE_CHUNKS = 2,
  849. /**
  850. * The provider supports modifying the text via the replace() and copy()
  851. * functions.
  852. * @see Replaceable
  853. * @stable ICU 3.4
  854. */
  855. UTEXT_PROVIDER_WRITABLE = 3,
  856. /**
  857. * There is meta data associated with the text.
  858. * @see Replaceable::hasMetaData()
  859. * @stable ICU 3.4
  860. */
  861. UTEXT_PROVIDER_HAS_META_DATA = 4,
  862. /**
  863. * Text provider owns the text storage.
  864. * Generally occurs as the result of a deep clone of the UText.
  865. * When closing the UText, the associated text must
  866. * also be closed/deleted/freed/ whatever is appropriate.
  867. * @stable ICU 3.6
  868. */
  869. UTEXT_PROVIDER_OWNS_TEXT = 5
  870. };
  871. /**
  872. * Function type declaration for UText.clone().
  873. *
  874. * clone a UText. Much like opening a UText where the source text is itself
  875. * another UText.
  876. *
  877. * A deep clone will copy both the UText data structures and the underlying text.
  878. * The original and cloned UText will operate completely independently; modifications
  879. * made to the text in one will not effect the other. Text providers are not
  880. * required to support deep clones. The user of clone() must check the status return
  881. * and be prepared to handle failures.
  882. *
  883. * A shallow clone replicates only the UText data structures; it does not make
  884. * a copy of the underlying text. Shallow clones can be used as an efficient way to
  885. * have multiple iterators active in a single text string that is not being
  886. * modified.
  887. *
  888. * A shallow clone operation must not fail except for truly exceptional conditions such
  889. * as memory allocation failures.
  890. *
  891. * A UText and its clone may be safely concurrently accessed by separate threads.
  892. * This is true for both shallow and deep clones.
  893. * It is the responsibility of the Text Provider to ensure that this thread safety
  894. * constraint is met.
  895. *
  896. * @param dest A UText struct to be filled in with the result of the clone operation,
  897. * or NULL if the clone function should heap-allocate a new UText struct.
  898. * @param src The UText to be cloned.
  899. * @param deep true to request a deep clone, false for a shallow clone.
  900. * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
  901. * should be returned if the text provider is unable to clone the
  902. * original text.
  903. * @return The newly created clone, or NULL if the clone operation failed.
  904. *
  905. * @stable ICU 3.4
  906. */
  907. typedef UText * U_CALLCONV
  908. UTextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status);
  909. /**
  910. * Function type declaration for UText.nativeLength().
  911. *
  912. * @param ut the UText to get the length of.
  913. * @return the length, in the native units of the original text string.
  914. * @see UText
  915. * @stable ICU 3.4
  916. */
  917. typedef int64_t U_CALLCONV
  918. UTextNativeLength(UText *ut);
  919. /**
  920. * Function type declaration for UText.access(). Get the description of the text chunk
  921. * containing the text at a requested native index. The UText's iteration
  922. * position will be left at the requested index. If the index is out
  923. * of bounds, the iteration position will be left at the start or end
  924. * of the string, as appropriate.
  925. *
  926. * Chunks must begin and end on code point boundaries. A single code point
  927. * comprised of multiple storage units must never span a chunk boundary.
  928. *
  929. *
  930. * @param ut the UText being accessed.
  931. * @param nativeIndex Requested index of the text to be accessed.
  932. * @param forward If true, then the returned chunk must contain text
  933. * starting from the index, so that start<=index<limit.
  934. * If false, then the returned chunk must contain text
  935. * before the index, so that start<index<=limit.
  936. * @return True if the requested index could be accessed. The chunk
  937. * will contain the requested text.
  938. * False value if a chunk cannot be accessed
  939. * (the requested index is out of bounds).
  940. *
  941. * @see UText
  942. * @stable ICU 3.4
  943. */
  944. typedef UBool U_CALLCONV
  945. UTextAccess(UText *ut, int64_t nativeIndex, UBool forward);
  946. /**
  947. * Function type declaration for UText.extract().
  948. *
  949. * Extract text from a UText into a UChar buffer. The range of text to be extracted
  950. * is specified in the native indices of the UText provider. These may not necessarily
  951. * be UTF-16 indices.
  952. * <p>
  953. * The size (number of 16 bit UChars) in the data to be extracted is returned. The
  954. * full amount is returned, even when the specified buffer size is smaller.
  955. * <p>
  956. * The extracted string will (if you are a user) / must (if you are a text provider)
  957. * be NUL-terminated if there is sufficient space in the destination buffer.
  958. *
  959. * @param ut the UText from which to extract data.
  960. * @param nativeStart the native index of the first character to extract.
  961. * @param nativeLimit the native string index of the position following the last
  962. * character to extract.
  963. * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
  964. * @param destCapacity The size, in UChars, of the destination buffer. May be zero
  965. * for precomputing the required size.
  966. * @param status receives any error status.
  967. * If U_BUFFER_OVERFLOW_ERROR: Returns number of UChars for
  968. * preflighting.
  969. * @return Number of UChars in the data. Does not include a trailing NUL.
  970. *
  971. * @stable ICU 3.4
  972. */
  973. typedef int32_t U_CALLCONV
  974. UTextExtract(UText *ut,
  975. int64_t nativeStart, int64_t nativeLimit,
  976. UChar *dest, int32_t destCapacity,
  977. UErrorCode *status);
  978. /**
  979. * Function type declaration for UText.replace().
  980. *
  981. * Replace a range of the original text with a replacement text.
  982. *
  983. * Leaves the current iteration position at the position following the
  984. * newly inserted replacement text.
  985. *
  986. * This function need only be implemented on UText types that support writing.
  987. *
  988. * When using this function, there should be only a single UText opened onto the
  989. * underlying native text string. The function is responsible for updating the
  990. * text chunk within the UText to reflect the updated iteration position,
  991. * taking into account any changes to the underlying string's structure caused
  992. * by the replace operation.
  993. *
  994. * @param ut the UText representing the text to be operated on.
  995. * @param nativeStart the index of the start of the region to be replaced
  996. * @param nativeLimit the index of the character following the region to be replaced.
  997. * @param replacementText pointer to the replacement text
  998. * @param replacmentLength length of the replacement text in UChars, or -1 if the text is NUL terminated.
  999. * @param status receives any error status. Possible errors include
  1000. * U_NO_WRITE_PERMISSION
  1001. *
  1002. * @return The signed number of (native) storage units by which
  1003. * the length of the text expanded or contracted.
  1004. *
  1005. * @stable ICU 3.4
  1006. */
  1007. typedef int32_t U_CALLCONV
  1008. UTextReplace(UText *ut,
  1009. int64_t nativeStart, int64_t nativeLimit,
  1010. const UChar *replacementText, int32_t replacmentLength,
  1011. UErrorCode *status);
  1012. /**
  1013. * Function type declaration for UText.copy().
  1014. *
  1015. * Copy or move a substring from one position to another within the text,
  1016. * while retaining any metadata associated with the text.
  1017. * This function is used to duplicate or reorder substrings.
  1018. * The destination index must not overlap the source range.
  1019. *
  1020. * The text to be copied or moved is inserted at destIndex;
  1021. * it does not replace or overwrite any existing text.
  1022. *
  1023. * This function need only be implemented for UText types that support writing.
  1024. *
  1025. * When using this function, there should be only a single UText opened onto the
  1026. * underlying native text string. The function is responsible for updating the
  1027. * text chunk within the UText to reflect the updated iteration position,
  1028. * taking into account any changes to the underlying string's structure caused
  1029. * by the replace operation.
  1030. *
  1031. * @param ut The UText representing the text to be operated on.
  1032. * @param nativeStart The index of the start of the region to be copied or moved
  1033. * @param nativeLimit The index of the character following the region to be replaced.
  1034. * @param nativeDest The destination index to which the source substring is copied or moved.
  1035. * @param move If true, then the substring is moved, not copied/duplicated.
  1036. * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
  1037. *
  1038. * @stable ICU 3.4
  1039. */
  1040. typedef void U_CALLCONV
  1041. UTextCopy(UText *ut,
  1042. int64_t nativeStart, int64_t nativeLimit,
  1043. int64_t nativeDest,
  1044. UBool move,
  1045. UErrorCode *status);
  1046. /**
  1047. * Function type declaration for UText.mapOffsetToNative().
  1048. * Map from the current UChar offset within the current text chunk to
  1049. * the corresponding native index in the original source text.
  1050. *
  1051. * This is required only for text providers that do not use native UTF-16 indexes.
  1052. *
  1053. * @param ut the UText.
  1054. * @return Absolute (native) index corresponding to chunkOffset in the current chunk.
  1055. * The returned native index should always be to a code point boundary.
  1056. *
  1057. * @stable ICU 3.4
  1058. */
  1059. typedef int64_t U_CALLCONV
  1060. UTextMapOffsetToNative(const UText *ut);
  1061. /**
  1062. * Function type declaration for UText.mapIndexToUTF16().
  1063. * Map from a native index to a UChar offset within a text chunk.
  1064. * Behavior is undefined if the native index does not fall within the
  1065. * current chunk.
  1066. *
  1067. * This function is required only for text providers that do not use native UTF-16 indexes.
  1068. *
  1069. * @param ut The UText containing the text chunk.
  1070. * @param nativeIndex Absolute (native) text index, chunk->start<=index<=chunk->limit.
  1071. * @return Chunk-relative UTF-16 offset corresponding to the specified native
  1072. * index.
  1073. *
  1074. * @stable ICU 3.4
  1075. */
  1076. typedef int32_t U_CALLCONV
  1077. UTextMapNativeIndexToUTF16(const UText *ut, int64_t nativeIndex);
  1078. /**
  1079. * Function type declaration for UText.utextClose().
  1080. *
  1081. * A Text Provider close function is only required for provider types that make
  1082. * allocations in their open function (or other functions) that must be
  1083. * cleaned when the UText is closed.
  1084. *
  1085. * The allocation of the UText struct itself and any "extra" storage
  1086. * associated with the UText is handled by the common UText implementation
  1087. * and does not require provider specific cleanup in a close function.
  1088. *
  1089. * Most UText provider implementations do not need to implement this function.
  1090. *
  1091. * @param ut A UText object to be closed.
  1092. *
  1093. * @stable ICU 3.4
  1094. */
  1095. typedef void U_CALLCONV
  1096. UTextClose(UText *ut);
  1097. /**
  1098. * (public) Function dispatch table for UText.
  1099. * Conceptually very much like a C++ Virtual Function Table.
  1100. * This struct defines the organization of the table.
  1101. * Each text provider implementation must provide an
  1102. * actual table that is initialized with the appropriate functions
  1103. * for the type of text being handled.
  1104. * @stable ICU 3.6
  1105. */
  1106. struct UTextFuncs {
  1107. /**
  1108. * (public) Function table size, sizeof(UTextFuncs)
  1109. * Intended for use should the table grow to accommodate added
  1110. * functions in the future, to allow tests for older format
  1111. * function tables that do not contain the extensions.
  1112. *
  1113. * Fields are placed for optimal alignment on
  1114. * 32/64/128-bit-pointer machines, by normally grouping together
  1115. * 4 32-bit fields,
  1116. * 4 pointers,
  1117. * 2 64-bit fields
  1118. * in sequence.
  1119. * @stable ICU 3.6
  1120. */
  1121. int32_t tableSize;
  1122. /**
  1123. * (private) Alignment padding.
  1124. * Do not use, reserved for use by the UText framework only.
  1125. * @internal
  1126. */
  1127. int32_t reserved1, /** @internal */ reserved2, /** @internal */ reserved3;
  1128. /**
  1129. * (public) Function pointer for UTextClone
  1130. *
  1131. * @see UTextClone
  1132. * @stable ICU 3.6
  1133. */
  1134. UTextClone *clone;
  1135. /**
  1136. * (public) function pointer for UTextLength
  1137. * May be expensive to compute!
  1138. *
  1139. * @see UTextLength
  1140. * @stable ICU 3.6
  1141. */
  1142. UTextNativeLength *nativeLength;
  1143. /**
  1144. * (public) Function pointer for UTextAccess.
  1145. *
  1146. * @see UTextAccess
  1147. * @stable ICU 3.6
  1148. */
  1149. UTextAccess *access;
  1150. /**
  1151. * (public) Function pointer for UTextExtract.
  1152. *
  1153. * @see UTextExtract
  1154. * @stable ICU 3.6
  1155. */
  1156. UTextExtract *extract;
  1157. /**
  1158. * (public) Function pointer for UTextReplace.
  1159. *
  1160. * @see UTextReplace
  1161. * @stable ICU 3.6
  1162. */
  1163. UTextReplace *replace;
  1164. /**
  1165. * (public) Function pointer for UTextCopy.
  1166. *
  1167. * @see UTextCopy
  1168. * @stable ICU 3.6
  1169. */
  1170. UTextCopy *copy;
  1171. /**
  1172. * (public) Function pointer for UTextMapOffsetToNative.
  1173. *
  1174. * @see UTextMapOffsetToNative
  1175. * @stable ICU 3.6
  1176. */
  1177. UTextMapOffsetToNative *mapOffsetToNative;
  1178. /**
  1179. * (public) Function pointer for UTextMapNativeIndexToUTF16.
  1180. *
  1181. * @see UTextMapNativeIndexToUTF16
  1182. * @stable ICU 3.6
  1183. */
  1184. UTextMapNativeIndexToUTF16 *mapNativeIndexToUTF16;
  1185. /**
  1186. * (public) Function pointer for UTextClose.
  1187. *
  1188. * @see UTextClose
  1189. * @stable ICU 3.6
  1190. */
  1191. UTextClose *close;
  1192. /**
  1193. * (private) Spare function pointer
  1194. * @internal
  1195. */
  1196. UTextClose *spare1;
  1197. /**
  1198. * (private) Spare function pointer
  1199. * @internal
  1200. */
  1201. UTextClose *spare2;
  1202. /**
  1203. * (private) Spare function pointer
  1204. * @internal
  1205. */
  1206. UTextClose *spare3;
  1207. };
  1208. /**
  1209. * Function dispatch table for UText
  1210. * @see UTextFuncs
  1211. */
  1212. typedef struct UTextFuncs UTextFuncs;
  1213. /**
  1214. * UText struct. Provides the interface between the generic UText access code
  1215. * and the UText provider code that works on specific kinds of
  1216. * text (UTF-8, noncontiguous UTF-16, whatever.)
  1217. *
  1218. * Applications that are using predefined types of text providers
  1219. * to pass text data to ICU services will have no need to view the
  1220. * internals of the UText structs that they open.
  1221. *
  1222. * @stable ICU 3.6
  1223. */
  1224. struct UText {
  1225. /**
  1226. * (private) Magic. Used to help detect when UText functions are handed
  1227. * invalid or uninitialized UText structs.
  1228. * utext_openXYZ() functions take an initialized,
  1229. * but not necessarily open, UText struct as an
  1230. * optional fill-in parameter. This magic field
  1231. * is used to check for that initialization.
  1232. * Text provider close functions must NOT clear
  1233. * the magic field because that would prevent
  1234. * reuse of the UText struct.
  1235. * @internal
  1236. */
  1237. uint32_t magic;
  1238. /**
  1239. * (private) Flags for managing the allocation and freeing of
  1240. * memory associated with this UText.
  1241. * @internal
  1242. */
  1243. int32_t flags;
  1244. /**
  1245. * Text provider properties. This set of flags is maintained by the
  1246. * text provider implementation.
  1247. * @stable ICU 3.4
  1248. */
  1249. int32_t providerProperties;
  1250. /**
  1251. * (public) sizeOfStruct=sizeof(UText)
  1252. * Allows possible backward compatible extension.
  1253. *
  1254. * @stable ICU 3.4
  1255. */
  1256. int32_t sizeOfStruct;
  1257. /* ------ 16 byte alignment boundary ----------- */
  1258. /**
  1259. * (protected) Native index of the first character position following
  1260. * the current chunk.
  1261. * @stable ICU 3.6
  1262. */
  1263. int64_t chunkNativeLimit;
  1264. /**
  1265. * (protected) Size in bytes of the extra space (pExtra).
  1266. * @stable ICU 3.4
  1267. */
  1268. int32_t extraSize;
  1269. /**
  1270. * (protected) The highest chunk offset where native indexing and
  1271. * chunk (UTF-16) indexing correspond. For UTF-16 sources, value
  1272. * will be equal to chunkLength.
  1273. *
  1274. * @stable ICU 3.6
  1275. */
  1276. int32_t nativeIndexingLimit;
  1277. /* ---- 16 byte alignment boundary------ */
  1278. /**
  1279. * (protected) Native index of the first character in the text chunk.
  1280. * @stable ICU 3.6
  1281. */
  1282. int64_t chunkNativeStart;
  1283. /**
  1284. * (protected) Current iteration position within the text chunk (UTF-16 buffer).
  1285. * This is the index to the character that will be returned by utext_next32().
  1286. * @stable ICU 3.6
  1287. */
  1288. int32_t chunkOffset;
  1289. /**
  1290. * (protected) Length the text chunk (UTF-16 buffer), in UChars.
  1291. * @stable ICU 3.6
  1292. */
  1293. int32_t chunkLength;
  1294. /* ---- 16 byte alignment boundary-- */
  1295. /**
  1296. * (protected) pointer to a chunk of text in UTF-16 format.
  1297. * May refer either to original storage of the source of the text, or
  1298. * if conversion was required, to a buffer owned by the UText.
  1299. * @stable ICU 3.6
  1300. */
  1301. const UChar *chunkContents;
  1302. /**
  1303. * (public) Pointer to Dispatch table for accessing functions for this UText.
  1304. * @stable ICU 3.6
  1305. */
  1306. const UTextFuncs *pFuncs;
  1307. /**
  1308. * (protected) Pointer to additional space requested by the
  1309. * text provider during the utext_open operation.
  1310. * @stable ICU 3.4
  1311. */
  1312. void *pExtra;
  1313. /**
  1314. * (protected) Pointer to string or text-containing object or similar.
  1315. * This is the source of the text that this UText is wrapping, in a format
  1316. * that is known to the text provider functions.
  1317. * @stable ICU 3.4
  1318. */
  1319. const void *context;
  1320. /* --- 16 byte alignment boundary--- */
  1321. /**
  1322. * (protected) Pointer fields available for use by the text provider.
  1323. * Not used by UText common code.
  1324. * @stable ICU 3.6
  1325. */
  1326. const void *p;
  1327. /**
  1328. * (protected) Pointer fields available for use by the text provider.
  1329. * Not used by UText common code.
  1330. * @stable ICU 3.6
  1331. */
  1332. const void *q;
  1333. /**
  1334. * (protected) Pointer fields available for use by the text provider.
  1335. * Not used by UText common code.
  1336. * @stable ICU 3.6
  1337. */
  1338. const void *r;
  1339. /**
  1340. * Private field reserved for future use by the UText framework
  1341. * itself. This is not to be touched by the text providers.
  1342. * @internal ICU 3.4
  1343. */
  1344. void *privP;
  1345. /* --- 16 byte alignment boundary--- */
  1346. /**
  1347. * (protected) Integer field reserved for use by the text provider.
  1348. * Not used by the UText framework, or by the client (user) of the UText.
  1349. * @stable ICU 3.4
  1350. */
  1351. int64_t a;
  1352. /**
  1353. * (protected) Integer field reserved for use by the text provider.
  1354. * Not used by the UText framework, or by the client (user) of the UText.
  1355. * @stable ICU 3.4
  1356. */
  1357. int32_t b;
  1358. /**
  1359. * (protected) Integer field reserved for use by the text provider.
  1360. * Not used by the UText framework, or by the client (user) of the UText.
  1361. * @stable ICU 3.4
  1362. */
  1363. int32_t c;
  1364. /* ---- 16 byte alignment boundary---- */
  1365. /**
  1366. * Private field reserved for future use by the UText framework
  1367. * itself. This is not to be touched by the text providers.
  1368. * @internal ICU 3.4
  1369. */
  1370. int64_t privA;
  1371. /**
  1372. * Private field reserved for future use by the UText framework
  1373. * itself. This is not to be touched by the text providers.
  1374. * @internal ICU 3.4
  1375. */
  1376. int32_t privB;
  1377. /**
  1378. * Private field reserved for future use by the UText framework
  1379. * itself. This is not to be touched by the text providers.
  1380. * @internal ICU 3.4
  1381. */
  1382. int32_t privC;
  1383. };
  1384. /**
  1385. * Common function for use by Text Provider implementations to allocate and/or initialize
  1386. * a new UText struct. To be called in the implementation of utext_open() functions.
  1387. * If the supplied UText parameter is null, a new UText struct will be allocated on the heap.
  1388. * If the supplied UText is already open, the provider's close function will be called
  1389. * so that the struct can be reused by the open that is in progress.
  1390. *
  1391. * @param ut pointer to a UText struct to be re-used, or null if a new UText
  1392. * should be allocated.
  1393. * @param extraSpace The amount of additional space to be allocated as part
  1394. * of this UText, for use by types of providers that require
  1395. * additional storage.
  1396. * @param status Errors are returned here.
  1397. * @return pointer to the UText, allocated if necessary, with extra space set up if requested.
  1398. * @stable ICU 3.4
  1399. */
  1400. U_CAPI UText * U_EXPORT2
  1401. utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status);
  1402. // do not use #ifndef U_HIDE_INTERNAL_API around the following!
  1403. /**
  1404. * @internal
  1405. * Value used to help identify correctly initialized UText structs.
  1406. * Note: must be publicly visible so that UTEXT_INITIALIZER can access it.
  1407. */
  1408. enum {
  1409. UTEXT_MAGIC = 0x345ad82c
  1410. };
  1411. /**
  1412. * initializer to be used with local (stack) instances of a UText
  1413. * struct. UText structs must be initialized before passing
  1414. * them to one of the utext_open functions.
  1415. *
  1416. * @stable ICU 3.6
  1417. */
  1418. #define UTEXT_INITIALIZER { \
  1419. UTEXT_MAGIC, /* magic */ \
  1420. 0, /* flags */ \
  1421. 0, /* providerProps */ \
  1422. sizeof(UText), /* sizeOfStruct */ \
  1423. 0, /* chunkNativeLimit */ \
  1424. 0, /* extraSize */ \
  1425. 0, /* nativeIndexingLimit */ \
  1426. 0, /* chunkNativeStart */ \
  1427. 0, /* chunkOffset */ \
  1428. 0, /* chunkLength */ \
  1429. NULL, /* chunkContents */ \
  1430. NULL, /* pFuncs */ \
  1431. NULL, /* pExtra */ \
  1432. NULL, /* context */ \
  1433. NULL, NULL, NULL, /* p, q, r */ \
  1434. NULL, /* privP */ \
  1435. 0, 0, 0, /* a, b, c */ \
  1436. 0, 0, 0 /* privA,B,C, */ \
  1437. }
  1438. U_CDECL_END
  1439. #if U_SHOW_CPLUSPLUS_API
  1440. U_NAMESPACE_BEGIN
  1441. /**
  1442. * \class LocalUTextPointer
  1443. * "Smart pointer" class, closes a UText via utext_close().
  1444. * For most methods see the LocalPointerBase base class.
  1445. *
  1446. * @see LocalPointerBase
  1447. * @see LocalPointer
  1448. * @stable ICU 4.4
  1449. */
  1450. U_DEFINE_LOCAL_OPEN_POINTER(LocalUTextPointer, UText, utext_close);
  1451. U_NAMESPACE_END
  1452. #endif
  1453. #endif