2 *******************************************************************************
4 * Copyright (C) 2004-2006, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 * tab size: 8 (not used)
13 * created on: 2004oct06
14 * created by: Markus W. Scherer
22 * \brief C API: Abstract Unicode Text API
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.
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.
32 * There are three general classes of usage for UText:
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.
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.
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.
50 * <em>Iterating over text</em>
52 * Here is sample code for a forward iteration over the contents of a UText
56 * UText *ut = whatever();
58 * for (c=utext_next32From(ut, 0); c>=0; c=utext_next32(ut)) {
59 * // do whatever with the codepoint c here.
63 * And here is similar code to iterate in the reverse direction, from the end
64 * of the text towards the beginning.
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.
75 * <em>Characters and Indexing</em>
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.
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.
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.
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.
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.
105 * <em>Conventions for calling UText functions</em>
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.
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.
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)) {
125 * // work with the UText
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.
138 #include "unicode/utypes.h"
140 #include "unicode/rep.h"
141 #include "unicode/unistr.h"
142 #include "unicode/chariter.h"
149 typedef struct UText UText
; /**< C typedef for struct UText. @draft ICU 3.6 */
152 /***************************************************************************************
154 * C Functions for creating UText wrappers around various kinds of text strings.
156 ****************************************************************************************/
160 * Close function for UText instances.
161 * Cleans up, releases any resources being held by an open UText.
163 * If the UText was originally allocated by one of the utext_open functions,
164 * the storage associated with the utext will also be freed.
165 * If the UText storage originated with the application, as it would with
166 * a local or static instance, the storage will not be deleted.
168 * An open UText can be reset to refer to new string by using one of the utext_open()
169 * functions without first closing the UText.
171 * @param ut The UText to be closed.
172 * @return NULL if the UText struct was deleted by the close. If the UText struct
173 * was originally provided by the caller to the open function, it is
174 * returned by this function, and may be safely used again in
175 * a subsequent utext_open.
179 U_DRAFT UText
* U_EXPORT2
180 utext_close(UText
*ut
);
184 * Open a read-only UText implementation for UTF-8 strings.
187 * Any invalid UTF-8 in the input will be handled in this way:
188 * a sequence of bytes that has the form of a truncated, but otherwise valid,
189 * UTF-8 sequence will be replaced by a single unicode replacement character, \uFFFD.
190 * Any other illegal bytes will each be replaced by a \uFFFD.
193 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
194 * If non-NULL, must refer to an initialized UText struct, which will then
195 * be reset to reference the specified UTF-8 string.
196 * @param s A UTF-8 string. Must not be NULL.
197 * @param length The length of the UTF-8 string in bytes, or -1 if the string is
199 * @param status Errors are returned here.
200 * @return A pointer to the UText. If a pre-allocated UText was provided, it
201 * will always be used and returned.
204 U_DRAFT UText
* U_EXPORT2
205 utext_openUTF8(UText
*ut
, const char *s
, int64_t length
, UErrorCode
*status
);
209 * Open a read-only UText for UChar * string.
211 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
212 * If non-NULL, must refer to an initialized UText struct, which will then
213 * be reset to reference the specified UChar string.
214 * @param s A UChar (UTF-16) string
215 * @param length The number of UChars in the input string, or -1 if the string is
217 * @param status Errors are returned here.
218 * @return A pointer to the UText. If a pre-allocated UText was provided, it
219 * will always be used and returned.
222 U_DRAFT UText
* U_EXPORT2
223 utext_openUChars(UText
*ut
, const UChar
*s
, int64_t length
, UErrorCode
*status
);
228 * Open a writable UText for a non-const UnicodeString.
230 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
231 * If non-NULL, must refer to an initialized UText struct, which will then
232 * be reset to reference the specified input string.
233 * @param s A UnicodeString.
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.
239 U_DRAFT UText
* U_EXPORT2
240 utext_openUnicodeString(UText
*ut
, UnicodeString
*s
, UErrorCode
*status
);
244 * Open a UText for a const UnicodeString. The resulting UText will not be writable.
246 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
247 * If non-NULL, must refer to an initialized UText struct, which will then
248 * be reset to reference the specified input string.
249 * @param s A const UnicodeString to be wrapped.
250 * @param status Errors are returned here.
251 * @return Pointer to the UText. If a UText was supplied as input, this
252 * will always be used and returned.
255 U_DRAFT UText
* U_EXPORT2
256 utext_openConstUnicodeString(UText
*ut
, const UnicodeString
*s
, UErrorCode
*status
);
260 * Open a writable UText implementation for an ICU Replaceable object.
261 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
262 * If non-NULL, must refer to an already existing UText, which will then
263 * be reset to reference the specified replaceable text.
264 * @param rep A Replaceable text object.
265 * @param status Errors are returned here.
266 * @return Pointer to the UText. If a UText was supplied as input, this
267 * will always be used and returned.
271 U_DRAFT UText
* U_EXPORT2
272 utext_openReplaceable(UText
*ut
, Replaceable
*rep
, UErrorCode
*status
);
275 * Open a UText implementation over an ICU CharacterIterator.
276 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
277 * If non-NULL, must refer to an already existing UText, which will then
278 * be reset to reference the specified replaceable text.
279 * @param ci A Character Iterator.
280 * @param status Errors are returned here.
281 * @return Pointer to the UText. If a UText was supplied as input, this
282 * will always be used and returned.
286 U_DRAFT UText
* U_EXPORT2
287 utext_openCharacterIterator(UText
*ut
, CharacterIterator
*ic
, UErrorCode
*status
);
293 * Clone a UText. This is much like opening a UText where the source text is itself
296 * A deep clone will copy both the UText data structures and the underlying text.
297 * The original and cloned UText will operate completely independently; modifications
298 * made to the text in one will not affect the other. Text providers are not
299 * required to support deep clones. The user of clone() must check the status return
300 * and be prepared to handle failures.
302 * The standard UText implementations for UTF8, UChar *, UnicodeString and
303 * Replaceable all support deep cloning.
305 * The UText returned from a deep clone will be writable, assuming that the text
306 * provider is able to support writing, even if the source UText had been made
307 * non-writable by means of UText_freeze().
309 * A shallow clone replicates only the UText data structures; it does not make
310 * a copy of the underlying text. Shallow clones can be used as an efficient way to
311 * have multiple iterators active in a single text string that is not being
314 * A shallow clone operation will not fail, barring truly exceptional conditions such
315 * as memory allocation failures.
317 * Shallow UText clones should be avoided if the UText functions that modify the
318 * text are expected to be used, either on the original or the cloned UText.
319 * Any such modifications can cause unpredictable behavior. Read Only
320 * shallow clones provide some protection against errors of this type by
321 * disabling text modification via the cloned UText.
323 * A shallow clone made with the readOnly parameter == FALSE will preserve the
324 * utext_isWritable() state of the source object. Note, however, that
325 * write operations must be avoided while more than one UText exists that refer
326 * to the same underlying text.
328 * A UText and its clone may be safely concurrently accessed by separate threads.
329 * This is true for read access only with shallow clones, and for both read and
330 * write access with deep clones.
331 * It is the responsibility of the Text Provider to ensure that this thread safety
334 * @param dest A UText struct to be filled in with the result of the clone operation,
335 * or NULL if the clone function should heap-allocate a new UText struct.
336 * If non-NULL, must refer to an already existing UText, which will then
337 * be reset to become the clone.
338 * @param src The UText to be cloned.
339 * @param deep TRUE to request a deep clone, FALSE for a shallow clone.
340 * @param readOnly TRUE to request that the cloned UText have read only access to the
343 * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
344 * will be returned if the text provider is unable to clone the
346 * @return The newly created clone, or NULL if the clone operation failed.
349 U_DRAFT UText
* U_EXPORT2
350 utext_clone(UText
*dest
, const UText
*src
, UBool deep
, UBool readOnly
, UErrorCode
*status
);
354 * Compare two UText objects for equality.
355 * UTexts are equal if they are iterating over the same text, and
356 * have the same iteration position within the text.
357 * If either or both of the parameters are NULL, the comparison is FALSE.
359 * @param a The first of the two UTexts to compare.
360 * @param b The other UText to be compared.
361 * @return TRUE if the two UTexts are equal.
364 U_DRAFT UBool U_EXPORT2
365 utext_equals(const UText
*a
, const UText
*b
);
368 /*****************************************************************************
370 * Functions to work with the text represeted by a UText wrapper
372 *****************************************************************************/
375 * Get the length of the text. Depending on the characteristics
376 * of the underlying text representation, this may be expensive.
377 * @see utext_isLengthExpensive()
380 * @param ut the text to be accessed.
381 * @return the length of the text, expressed in native units.
385 U_DRAFT
int64_t U_EXPORT2
386 utext_nativeLength(UText
*ut
);
389 * Return TRUE if calculating the length of the text could be expensive.
390 * Finding the length of NUL terminated strings is considered to be expensive.
392 * Note that the value of this function may change
393 * as the result of other operations on a UText.
394 * Once the length of a string has been discovered, it will no longer
395 * be expensive to report it.
397 * @param ut the text to be accessed.
398 * @return TRUE if determining the length of the text could be time consuming.
401 U_DRAFT UBool U_EXPORT2
402 utext_isLengthExpensive(const UText
*ut
);
405 * Returns the code point at the requested index,
406 * or U_SENTINEL (-1) if it is out of bounds.
408 * If the specified index points to the interior of a multi-unit
409 * character - one of the trail bytes of a UTF-8 sequence, for example -
410 * the complete code point will be returned.
412 * The iteration position will be set to the start of the returned code point.
414 * This function is roughly equivalent to the the sequence
415 * utext_setNativeIndex(index);
417 * (There is a subtle difference if the index is out of bounds by being less than zero -
418 * utext_setNativeIndex(negative value) sets the index to zero, after which utext_current()
419 * will return the char at zero. utext_char32At(negative index), on the other hand, will
420 * return the U_SENTINEL value of -1.)
422 * @param ut the text to be accessed
423 * @param nativeIndex the native index of the character to be accessed. If the index points
424 * to other than the first unit of a multi-unit character, it will be adjusted
425 * to the start of the character.
426 * @return the code point at the specified index.
429 U_DRAFT UChar32 U_EXPORT2
430 utext_char32At(UText
*ut
, int64_t nativeIndex
);
435 * Get the code point at the current iteration position,
436 * or U_SENTINEL (-1) if the iteration has reached the end of
439 * @param ut the text to be accessed.
440 * @return the Unicode code point at the current iterator position.
443 U_DRAFT UChar32 U_EXPORT2
444 utext_current32(UText
*ut
);
448 * Get the code point at the current iteration position of the UText, and
449 * advance the position to the first index following the character.
451 * If the position is at the end of the text (the index following
452 * the last character, which is also the length of the text),
453 * return U_SENTINEL (-1) and do not advance the index.
455 * This is a post-increment operation.
457 * An inline macro version of this function, UTEXT_NEXT32(),
458 * is available for performance critical use.
460 * @param ut the text to be accessed.
461 * @return the Unicode code point at the iteration position.
465 U_DRAFT UChar32 U_EXPORT2
466 utext_next32(UText
*ut
);
470 * Move the iterator position to the character (code point) whose
471 * index precedes the current position, and return that character.
472 * This is a pre-decrement operation.
474 * If the initial position is at the start of the text (index of 0)
475 * return U_SENTINEL (-1), and leave the position unchanged.
477 * An inline macro version of this function, UTEXT_PREVIOUS32(),
478 * is available for performance critical use.
480 * @param ut the text to be accessed.
481 * @return the previous UChar32 code point, or U_SENTINEL (-1)
482 * if the iteration has reached the start of the text.
483 * @see UTEXT_PREVIOUS32
486 U_DRAFT UChar32 U_EXPORT2
487 utext_previous32(UText
*ut
);
491 * Set the iteration index and return the code point at that index.
492 * Leave the iteration index at the start of the following code point.
494 * This function is the most efficient and convenient way to
495 * begin a forward iteration. The results are identical to the those
502 * @param ut the text to be accessed.
503 * @param nativeIndex Iteration index, in the native units of the text provider.
504 * @return Code point which starts at or before index,
505 * or U_SENTINEL (-1) if it is out of bounds.
508 U_DRAFT UChar32 U_EXPORT2
509 utext_next32From(UText
*ut
, int64_t nativeIndex
);
514 * Set the iteration index, and return the code point preceding the
515 * one specified by the initial index. Leave the iteration position
516 * at the start of the returned code point.
518 * This function is the most efficient and convenient way to
519 * begin a backwards iteration.
521 * @param ut the text to be accessed.
522 * @param nativeIndex Iteration index in the native units of the text provider.
523 * @return Code point preceding the one at the initial index,
524 * or U_SENTINEL (-1) if it is out of bounds.
528 U_DRAFT UChar32 U_EXPORT2
529 utext_previous32From(UText
*ut
, int64_t nativeIndex
);
532 * Get the current iterator position, which can range from 0 to
533 * the length of the text.
534 * The position is a native index into the input text, in whatever format it
535 * may have (possibly UTF-8 for example), and may not always be the same as
536 * the corresponding UChar (UTF-16) index.
537 * The returned position will always be aligned to a code point boundary.
539 * @param ut the text to be accessed.
540 * @return the current index position, in the native units of the text provider.
543 U_DRAFT
int64_t U_EXPORT2
544 utext_getNativeIndex(const UText
*ut
);
547 * Set the current iteration position to the nearest code point
548 * boundary at or preceding the specified index.
549 * The index is in the native units of the original input text.
550 * If the index is out of range, it will be pinned to be within
551 * the range of the input text.
553 * It will usually be more efficient to begin an iteration
554 * using the functions utext_next32From() or utext_previous32From()
555 * rather than setIndex().
557 * Moving the index position to an adjacent character is best done
558 * with utext_next32(), utext_previous32() or utext_moveIndex32().
559 * Attempting to do direct arithmetic on the index position is
560 * complicated by the fact that the size (in native units) of a
561 * character depends on the underlying representation of the character
562 * (UTF-8, UTF-16, UTF-32, arbitrary codepage), and is not
565 * @param ut the text to be accessed.
566 * @param nativeIndex the native unit index of the new iteration position.
569 U_DRAFT
void U_EXPORT2
570 utext_setNativeIndex(UText
*ut
, int64_t nativeIndex
);
573 * Move the iterator postion by delta code points. The number of code points
574 * is a signed number; a negative delta will move the iterator backwards,
575 * towards the start of the text.
577 * The index is moved by <code>delta</code> code points
578 * forward or backward, but no further backward than to 0 and
579 * no further forward than to utext_nativeLength().
580 * The resulting index value will be in between 0 and length, inclusive.
582 * @param ut the text to be accessed.
583 * @param delta the signed number of code points to move the iteration position.
584 * @return TRUE if the position could be moved the requested number of positions while
585 * staying within the range [0 - text length].
588 U_DRAFT UBool U_EXPORT2
589 utext_moveIndex32(UText
*ut
, int32_t delta
);
592 * Get the native index of the character preceeding the current position.
593 * If the iteration position is already at the start of the text, zero
595 * The value returned is the same as that obtained from the following sequence,
596 * but without the side effect of changing the iteration position.
599 * UText *ut = whatever;
602 * utext_getNativeIndex(ut);
605 * This function is most useful during forwards iteration, where it will get the
606 * native index of the character most recently returned from utext_next().
608 * @param ut the text to be accessed
609 * @return the native index of the character preceeding the current index position,
610 * or zero if the current position is at the start of the text.
613 U_DRAFT
int64_t U_EXPORT2
614 utext_getPreviousNativeIndex(UText
*ut
);
619 * Extract text from a UText into a UChar buffer. The range of text to be extracted
620 * is specified in the native indices of the UText provider. These may not necessarily
623 * The size (number of 16 bit UChars) of the data to be extracted is returned. The
624 * full number of UChars is returned, even when the extracted text is truncated
625 * because the specified buffer size is too small.
627 * The extracted string will (if you are a user) / must (if you are a text provider)
628 * be NUL-terminated if there is sufficient space in the destination buffer. This
629 * terminating NUL is not included in the returned length.
631 * The iteration index is left at the position following the last extracted character.
633 * @param ut the UText from which to extract data.
634 * @param nativeStart the native index of the first character to extract.\
635 * If the specified index is out of range,
636 * it will be pinned to to be within 0 <= index <= textLength
637 * @param nativeLimit the native string index of the position following the last
638 * character to extract. If the specified index is out of range,
639 * it will be pinned to to be within 0 <= index <= textLength.
640 * nativeLimit must be >= nativeStart.
641 * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
642 * @param destCapacity The size, in UChars, of the destination buffer. May be zero
643 * for precomputing the required size.
644 * @param status receives any error status.
645 * U_BUFFER_OVERFLOW_ERROR: the extracted text was truncated because the
646 * buffer was too small. Returns number of UChars for preflighting.
647 * @return Number of UChars in the data to be extracted. Does not include a trailing NUL.
651 U_DRAFT
int32_t U_EXPORT2
652 utext_extract(UText
*ut
,
653 int64_t nativeStart
, int64_t nativeLimit
,
654 UChar
*dest
, int32_t destCapacity
,
658 #ifndef U_HIDE_DRAFT_API
659 /************************************************************************************
661 * #define inline versions of selected performance-critical text access functions
662 * Caution: do not use auto increment++ or decrement-- expressions
663 * as parameters to these macros.
665 * For most use, where there is no extreme performance constraint, the
666 * normal, non-inline functions are a better choice. The resulting code
667 * will be smaller, and, if the need ever arises, easier to debug.
669 * These are implemented as #defines rather than real functions
670 * because there is no fully portable way to do inline functions in plain C.
672 ************************************************************************************/
675 * inline version of utext_next32(), for performance-critical situations.
677 * Get the code point at the current iteration position of the UText, and
678 * advance the position to the first index following the character.
679 * This is a post-increment operation.
680 * Returns U_SENTINEL (-1) if the position is at the end of the
685 #define UTEXT_NEXT32(ut) \
686 ((ut)->chunkOffset < (ut)->chunkLength && ((ut)->chunkContents)[(ut)->chunkOffset]<0xd800 ? \
687 ((ut)->chunkContents)[((ut)->chunkOffset)++] : utext_next32(ut))
690 * inline version of utext_previous32(), for performance-critical situations.
692 * Move the iterator position to the character (code point) whose
693 * index precedes the current position, and return that character.
694 * This is a pre-decrement operation.
695 * Returns U_SENTINEL (-1) if the position is at the start of the text.
699 #define UTEXT_PREVIOUS32(ut) \
700 ((ut)->chunkOffset > 0 && \
701 (ut)->chunkContents[(ut)->chunkOffset-1] < 0xd800 ? \
702 (ut)->chunkContents[--((ut)->chunkOffset)] : utext_previous32(ut))
705 * inline version of utext_getNativeIndex(), for performance-critical situations.
707 * Get the current iterator position, which can range from 0 to
708 * the length of the text.
709 * The position is a native index into the input text, in whatever format it
710 * may have (possibly UTF-8 for example), and may not always be the same as
711 * the corresponding UChar (UTF-16) index.
712 * The returned position will always be aligned to a code point boundary.
716 #define UTEXT_GETNATIVEINDEX(ut) \
717 ((ut)->chunkOffset <= (ut)->nativeIndexingLimit? \
718 (ut)->chunkNativeStart+(ut)->chunkOffset : \
719 (ut)->pFuncs->mapOffsetToNative(ut))
722 * inline version of utext_setNativeIndex(), for performance-critical situations.
724 * Set the current iteration position to the nearest code point
725 * boundary at or preceding the specified index.
726 * The index is in the native units of the original input text.
727 * If the index is out of range, it will be pinned to be within
728 * the range of the input text.
732 #define UTEXT_SETNATIVEINDEX(ut, ix) \
733 { int64_t __offset = (ix) - (ut)->chunkNativeStart; \
734 if (__offset>=0 && __offset<=(int64_t)(ut)->nativeIndexingLimit) { \
735 (ut)->chunkOffset=(int32_t)__offset; \
737 utext_setNativeIndex((ut), (ix)); } }
743 /************************************************************************************
745 * Functions related to writing or modifying the text.
746 * These will work only with modifiable UTexts. Attempting to
747 * modify a read-only UText will return an error status.
749 ************************************************************************************/
753 * Return TRUE if the text can be written (modified) with utext_replace() or
754 * utext_copy(). For the text to be writable, the text provider must
755 * be of a type that supports writing and the UText must not be frozen.
757 * Attempting to modify text when utext_isWriteable() is FALSE will fail -
758 * the text will not be modified, and an error will be returned from the function
759 * that attempted the modification.
761 * @param ut the UText to be tested.
762 * @return TRUE if the text is modifiable.
764 * @see utext_freeze()
765 * @see utext_replace()
770 U_DRAFT UBool U_EXPORT2
771 utext_isWritable(const UText
*ut
);
775 * Test whether there is meta data associated with the text.
776 * @see Replaceable::hasMetaData()
778 * @param ut The UText to be tested
779 * @return TRUE if the underlying text includes meta data.
782 U_DRAFT UBool U_EXPORT2
783 utext_hasMetaData(const UText
*ut
);
787 * Replace a range of the original text with a replacement text.
789 * Leaves the current iteration position at the position following the
790 * newly inserted replacement text.
792 * This function is only available on UText types that support writing,
793 * that is, ones where utext_isWritable() returns TRUE.
795 * When using this function, there should be only a single UText opened onto the
796 * underlying native text string. Behavior after a replace operation
797 * on a UText is undefined for any other additional UTexts that refer to the
800 * @param ut the UText representing the text to be operated on.
801 * @param nativeStart the native index of the start of the region to be replaced
802 * @param nativeLimit the native index of the character following the region to be replaced.
803 * @param replacementText pointer to the replacement text
804 * @param replacementLength length of the replacement text, or -1 if the text is NUL terminated.
805 * @param status receives any error status. Possible errors include
806 * U_NO_WRITE_PERMISSION
808 * @return The signed number of (native) storage units by which
809 * the length of the text expanded or contracted.
813 U_DRAFT
int32_t U_EXPORT2
814 utext_replace(UText
*ut
,
815 int64_t nativeStart
, int64_t nativeLimit
,
816 const UChar
*replacementText
, int32_t replacementLength
,
823 * Copy or move a substring from one position to another within the text,
824 * while retaining any metadata associated with the text.
825 * This function is used to duplicate or reorder substrings.
826 * The destination index must not overlap the source range.
828 * The text to be copied or moved is inserted at destIndex;
829 * it does not replace or overwrite any existing text.
831 * The iteration position is left following the newly inserted text
832 * at the destination position.
834 * This function is only available on UText types that support writing,
835 * that is, ones where utext_isWritable() returns TRUE.
837 * When using this function, there should be only a single UText opened onto the
838 * underlying native text string. Behavior after a copy operation
839 * on a UText is undefined in any other additional UTexts that refer to the
842 * @param ut The UText representing the text to be operated on.
843 * @param nativeStart The native index of the start of the region to be copied or moved
844 * @param nativeLimit The native index of the character position following the region
846 * @param destIndex The native destination index to which the source substring is
848 * @param move If TRUE, then the substring is moved, not copied/duplicated.
849 * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
853 U_DRAFT
void U_EXPORT2
854 utext_copy(UText
*ut
,
855 int64_t nativeStart
, int64_t nativeLimit
,
863 * Freeze a UText. This prevents any modification to the underlying text itself
864 * by means of functions operating on this UText.
867 * Once frozen, a UText can not be unfrozen. The intent is to ensure
868 * that a the text underlying a frozen UText wrapper cannot be modified via that UText.
871 * Caution: freezing a UText will disable changes made via the specific
872 * frozen UText wrapper only; it will not have any effect on the ability to
873 * directly modify the text by bypassing the UText. Any such backdoor modifications
874 * are always an error while UText access is occuring because the underlying
875 * text can get out of sync with UText's buffering.
878 * @param ut The UText to be frozen.
879 * @see utext_isWritable()
882 U_DRAFT
void U_EXPORT2
883 utext_freeze(UText
*ut
);
886 #ifndef U_HIDE_DRAFT_API
888 * UText provider properties (bit field indexes).
895 * It is potentially time consuming for the provider to determine the length of the text.
898 UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE
= 1,
900 * Text chunks remain valid and usable until the text object is modified or
901 * deleted, not just until the next time the access() function is called
902 * (which is the default).
905 UTEXT_PROVIDER_STABLE_CHUNKS
= 2,
907 * The provider supports modifying the text via the replace() and copy()
912 UTEXT_PROVIDER_WRITABLE
= 3,
914 * There is meta data associated with the text.
915 * @see Replaceable::hasMetaData()
918 UTEXT_PROVIDER_HAS_META_DATA
= 4,
920 * Text provider owns the text storage.
921 * Generally occurs as the result of a deep clone of the UText.
922 * When closing the UText, the associated text must
923 * also be closed/deleted/freed/ whatever is appropriate.
926 UTEXT_PROVIDER_OWNS_TEXT
= 5
930 * Function type declaration for UText.clone().
932 * clone a UText. Much like opening a UText where the source text is itself
935 * A deep clone will copy both the UText data structures and the underlying text.
936 * The original and cloned UText will operate completely independently; modifications
937 * made to the text in one will not effect the other. Text providers are not
938 * required to support deep clones. The user of clone() must check the status return
939 * and be prepared to handle failures.
941 * A shallow clone replicates only the UText data structures; it does not make
942 * a copy of the underlying text. Shallow clones can be used as an efficient way to
943 * have multiple iterators active in a single text string that is not being
946 * A shallow clone operation must not fail except for truly exceptional conditions such
947 * as memory allocation failures.
949 * A UText and its clone may be safely concurrently accessed by separate threads.
950 * This is true for both shallow and deep clones.
951 * It is the responsibility of the Text Provider to ensure that this thread safety
955 * @param dest A UText struct to be filled in with the result of the clone operation,
956 * or NULL if the clone function should heap-allocate a new UText struct.
957 * @param src The UText to be cloned.
958 * @param deep TRUE to request a deep clone, FALSE for a shallow clone.
959 * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
960 * should be returned if the text provider is unable to clone the
962 * @return The newly created clone, or NULL if the clone operation failed.
966 typedef UText
* U_CALLCONV
967 UTextClone(UText
*dest
, const UText
*src
, UBool deep
, UErrorCode
*status
);
971 * Function type declaration for UText.nativeLength().
973 * @param ut the UText to get the length of.
974 * @return the length, in the native units of the original text string.
978 typedef int64_t U_CALLCONV
979 UTextNativeLength(UText
*ut
);
982 * Function type declaration for UText.access(). Get the description of the text chunk
983 * containing the text at a requested native index. The UText's iteration
984 * position will be left at the requested index. If the index is out
985 * of bounds, the iteration position will be left at the start or end
986 * of the string, as appropriate.
988 * Chunks must begin and end on code point boundaries. A single code point
989 * comprised of multiple storage units must never span a chunk boundary.
992 * @param ut the UText being accessed.
993 * @param nativeIndex Requested index of the text to be accessed.
994 * @param forward If TRUE, then the returned chunk must contain text
995 * starting from the index, so that start<=index<limit.
996 * If FALSE, then the returned chunk must contain text
997 * before the index, so that start<index<=limit.
998 * @return True if the requested index could be accessed. The chunk
999 * will contain the requested text.
1000 * False value if a chunk cannot be accessed
1001 * (the requested index is out of bounds).
1006 typedef UBool U_CALLCONV
1007 UTextAccess(UText
*ut
, int64_t nativeIndex
, UBool forward
);
1010 * Function type declaration for UText.extract().
1012 * Extract text from a UText into a UChar buffer. The range of text to be extracted
1013 * is specified in the native indices of the UText provider. These may not necessarily
1014 * be UTF-16 indices.
1016 * The size (number of 16 bit UChars) in the data to be extracted is returned. The
1017 * full amount is returned, even when the specified buffer size is smaller.
1019 * The extracted string will (if you are a user) / must (if you are a text provider)
1020 * be NUL-terminated if there is sufficient space in the destination buffer.
1022 * @param ut the UText from which to extract data.
1023 * @param nativeStart the native index of the first characer to extract.
1024 * @param nativeLimit the native string index of the position following the last
1025 * character to extract.
1026 * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
1027 * @param destCapacity The size, in UChars, of the destination buffer. May be zero
1028 * for precomputing the required size.
1029 * @param status receives any error status.
1030 * If U_BUFFER_OVERFLOW_ERROR: Returns number of UChars for
1032 * @return Number of UChars in the data. Does not include a trailing NUL.
1036 typedef int32_t U_CALLCONV
1037 UTextExtract(UText
*ut
,
1038 int64_t nativeStart
, int64_t nativeLimit
,
1039 UChar
*dest
, int32_t destCapacity
,
1040 UErrorCode
*status
);
1043 * Function type declaration for UText.replace().
1045 * Replace a range of the original text with a replacement text.
1047 * Leaves the current iteration position at the position following the
1048 * newly inserted replacement text.
1050 * This function need only be implemented on UText types that support writing.
1052 * When using this function, there should be only a single UText opened onto the
1053 * underlying native text string. The function is responsible for updating the
1054 * text chunk within the UText to reflect the updated iteration position,
1055 * taking into account any changes to the underlying string's structure caused
1056 * by the replace operation.
1058 * @param ut the UText representing the text to be operated on.
1059 * @param nativeStart the index of the start of the region to be replaced
1060 * @param nativeLimit the index of the character following the region to be replaced.
1061 * @param replacementText pointer to the replacement text
1062 * @param replacmentLength length of the replacement text in UChars, or -1 if the text is NUL terminated.
1063 * @param status receives any error status. Possible errors include
1064 * U_NO_WRITE_PERMISSION
1066 * @return The signed number of (native) storage units by which
1067 * the length of the text expanded or contracted.
1071 typedef int32_t U_CALLCONV
1072 UTextReplace(UText
*ut
,
1073 int64_t nativeStart
, int64_t nativeLimit
,
1074 const UChar
*replacementText
, int32_t replacmentLength
,
1075 UErrorCode
*status
);
1078 * Function type declaration for UText.copy().
1080 * Copy or move a substring from one position to another within the text,
1081 * while retaining any metadata associated with the text.
1082 * This function is used to duplicate or reorder substrings.
1083 * The destination index must not overlap the source range.
1085 * The text to be copied or moved is inserted at destIndex;
1086 * it does not replace or overwrite any existing text.
1088 * This function need only be implemented for UText types that support writing.
1090 * When using this function, there should be only a single UText opened onto the
1091 * underlying native text string. The function is responsible for updating the
1092 * text chunk within the UText to reflect the updated iteration position,
1093 * taking into account any changes to the underlying string's structure caused
1094 * by the replace operation.
1096 * @param ut The UText representing the text to be operated on.
1097 * @param nativeStart The index of the start of the region to be copied or moved
1098 * @param nativeLimit The index of the character following the region to be replaced.
1099 * @param nativeDest The destination index to which the source substring is copied or moved.
1100 * @param move If TRUE, then the substring is moved, not copied/duplicated.
1101 * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
1105 typedef void U_CALLCONV
1106 UTextCopy(UText
*ut
,
1107 int64_t nativeStart
, int64_t nativeLimit
,
1110 UErrorCode
*status
);
1113 * Function type declaration for UText.mapOffsetToNative().
1114 * Map from the current UChar offset within the current text chunk to
1115 * the corresponding native index in the original source text.
1117 * This is required only for text providers that do not use native UTF-16 indexes.
1119 * @param ut the UText.
1120 * @return Absolute (native) index corresponding to chunkOffset in the current chunk.
1121 * The returned native index should always be to a code point boundary.
1125 typedef int64_t U_CALLCONV
1126 UTextMapOffsetToNative(const UText
*ut
);
1129 * Function type declaration for UText.mapIndexToUTF16().
1130 * Map from a native index to a UChar offset within a text chunk.
1131 * Behavior is undefined if the native index does not fall within the
1134 * This function is required only for text providers that do not use native UTF-16 indexes.
1136 * @param ut The UText containing the text chunk.
1137 * @param nativeIndex Absolute (native) text index, chunk->start<=index<=chunk->limit.
1138 * @return Chunk-relative UTF-16 offset corresponding to the specified native
1143 typedef int32_t U_CALLCONV
1144 UTextMapNativeIndexToUTF16(const UText
*ut
, int64_t nativeIndex
);
1148 * Function type declaration for UText.utextClose().
1150 * A Text Provider close function is only required for provider types that make
1151 * allocations in their open function (or other functions) that must be
1152 * cleaned when the UText is closed.
1154 * The allocation of the UText struct itself and any "extra" storage
1155 * associated with the UText is handled by the common UText implementation
1156 * and does not require provider specific cleanup in a close function.
1158 * Most UText provider implementations do not need to implement this function.
1160 * @param ut A UText object to be closed.
1164 typedef void U_CALLCONV
1165 UTextClose(UText
*ut
);
1169 * (public) Function dispatch table for UText.
1170 * Conceptually very much like a C++ Virtual Function Table.
1171 * This struct defines the organization of the table.
1172 * Each text provider implementation must provide an
1173 * actual table that is initialized with the appropriate functions
1174 * for the type of text being handled.
1179 * (public) Function table size, sizeof(UTextFuncs)
1180 * Intended for use should the table grow to accomodate added
1181 * functions in the future, to allow tests for older format
1182 * function tables that do not contain the extensions.
1184 * Fields are placed for optimal alignment on
1185 * 32/64/128-bit-pointer machines, by normally grouping together
1195 * (private) Alignment padding.
1196 * Do not use, reserved for use by the UText framework only.
1199 int32_t reserved1
, reserved2
, reserved3
;
1203 * (public) Function pointer for UTextClone
1211 * (public) function pointer for UTextLength
1212 * May be expensive to compute!
1217 UTextNativeLength
*nativeLength
;
1220 * (public) Function pointer for UTextAccess.
1225 UTextAccess
*access
;
1228 * (public) Function pointer for UTextExtract.
1233 UTextExtract
*extract
;
1236 * (public) Function pointer for UTextReplace.
1241 UTextReplace
*replace
;
1244 * (public) Function pointer for UTextCopy.
1252 * (public) Function pointer for UTextMapOffsetToNative.
1254 * @see UTextMapOffsetToNative
1257 UTextMapOffsetToNative
*mapOffsetToNative
;
1260 * (public) Function pointer for UTextMapNativeIndexToUTF16.
1262 * @see UTextMapNativeIndexToUTF16
1265 UTextMapNativeIndexToUTF16
*mapNativeIndexToUTF16
;
1268 * (public) Function pointer for UTextClose.
1276 * (private) Spare function pointer
1282 * (private) Spare function pointer
1288 * (private) Spare function pointer
1294 typedef struct UTextFuncs UTextFuncs
;
1298 #ifndef U_HIDE_DRAFT_API
1300 * UText struct. Provides the interface between the generic UText access code
1301 * and the UText provider code that works on specific kinds of
1302 * text (UTF-8, noncontiguous UTF-16, whatever.)
1304 * Applications that are using predefined types of text providers
1305 * to pass text data to ICU services will have no need to view the
1306 * internals of the UText structs that they open.
1312 * (private) Magic. Used to help detect when UText functions are handed
1313 * invalid or unitialized UText structs.
1314 * utext_openXYZ() functions take an initialized,
1315 * but not necessarily open, UText struct as an
1316 * optional fill-in parameter. This magic field
1317 * is used to check for that initialization.
1318 * Text provider close functions must NOT clear
1319 * the magic field because that would prevent
1320 * reuse of the UText struct.
1327 * (private) Flags for managing the allocation and freeing of
1328 * memory associated with this UText.
1335 * Text provider properties. This set of flags is maintainted by the
1336 * text provider implementation.
1339 int32_t providerProperties
;
1342 * (public) sizeOfStruct=sizeof(UText)
1343 * Allows possible backward compatible extension.
1347 int32_t sizeOfStruct
;
1349 /* ------ 16 byte alignment boundary ----------- */
1353 * (protected) Native index of the first character position following
1354 * the current chunk.
1357 int64_t chunkNativeLimit
;
1360 * (protected) Size in bytes of the extra space (pExtra).
1366 * (protected) The highest chunk offset where native indexing and
1367 * chunk (UTF-16) indexing correspond. For UTF-16 sources, value
1368 * will be equal to chunkLength.
1372 int32_t nativeIndexingLimit
;
1374 /* ---- 16 byte alignment boundary------ */
1377 * (protected) Native index of the first character in the text chunk.
1380 int64_t chunkNativeStart
;
1383 * (protected) Current iteration position within the text chunk (UTF-16 buffer).
1384 * This is the index to the character that will be returned by utext_next32().
1387 int32_t chunkOffset
;
1390 * (protected) Length the text chunk (UTF-16 buffer), in UChars.
1393 int32_t chunkLength
;
1395 /* ---- 16 byte alignment boundary-- */
1399 * (protected) pointer to a chunk of text in UTF-16 format.
1400 * May refer either to original storage of the source of the text, or
1401 * if conversion was required, to a buffer owned by the UText.
1404 const UChar
*chunkContents
;
1407 * (public) Pointer to Dispatch table for accessing functions for this UText.
1413 * (protected) Pointer to additional space requested by the
1414 * text provider during the utext_open operation.
1420 * (protected) Pointer to string or text-containin object or similar.
1421 * This is the source of the text that this UText is wrapping, in a format
1422 * that is known to the text provider functions.
1425 const void *context
;
1427 /* --- 16 byte alignment boundary--- */
1430 * (protected) Pointer fields available for use by the text provider.
1431 * Not used by UText common code.
1436 * (protected) Pointer fields available for use by the text provider.
1437 * Not used by UText common code.
1442 * (protected) Pointer fields available for use by the text provider.
1443 * Not used by UText common code.
1449 * Private field reserved for future use by the UText framework
1450 * itself. This is not to be touched by the text providers.
1456 /* --- 16 byte alignment boundary--- */
1460 * (protected) Integer field reserved for use by the text provider.
1461 * Not used by the UText framework, or by the client (user) of the UText.
1467 * (protected) Integer field reserved for use by the text provider.
1468 * Not used by the UText framework, or by the client (user) of the UText.
1474 * (protected) Integer field reserved for use by the text provider.
1475 * Not used by the UText framework, or by the client (user) of the UText.
1480 /* ---- 16 byte alignment boundary---- */
1484 * Private field reserved for future use by the UText framework
1485 * itself. This is not to be touched by the text providers.
1490 * Private field reserved for future use by the UText framework
1491 * itself. This is not to be touched by the text providers.
1496 * Private field reserved for future use by the UText framework
1497 * itself. This is not to be touched by the text providers.
1506 * Common function for use by Text Provider implementations to allocate and/or initialize
1507 * a new UText struct. To be called in the implementation of utext_open() functions.
1508 * If the supplied UText parameter is null, a new UText struct will be allocated on the heap.
1509 * If the supplied UText is already open, the provider's close function will be called
1510 * so that the struct can be reused by the open that is in progress.
1512 * @param ut pointer to a UText struct to be re-used, or null if a new UText
1513 * should be allocated.
1514 * @param extraSpace The amount of additional space to be allocated as part
1515 * of this UText, for use by types of providers that require
1516 * additional storage.
1517 * @param status Errors are returned here.
1518 * @return pointer to the UText, allocated if necessary, with extra space set up if requested.
1521 U_DRAFT UText
* U_EXPORT2
1522 utext_setup(UText
*ut
, int32_t extraSpace
, UErrorCode
*status
);
1526 * Value used to help identify correctly initialized UText structs.
1527 * Note: must be publicly visible so that UTEXT_INITIALIZER can access it.
1530 UTEXT_MAGIC
= 0x345ad82c
1532 #ifndef U_HIDE_DRAFT_API
1535 * initializer to be used with local (stack) instances of a UText
1536 * struct. UText structs must be initialized before passing
1537 * them to one of the utext_open functions.
1541 #define UTEXT_INITIALIZER { \
1542 UTEXT_MAGIC, /* magic */ \
1544 0, /* providerProps */ \
1545 sizeof(UText), /* sizeOfStruct */ \
1546 0, /* chunkNativeLimit */ \
1547 0, /* extraSize */ \
1548 0, /* nativeIndexingLimit */ \
1549 0, /* chunkNativeStart */ \
1550 0, /* chunkOffset */ \
1551 0, /* chunkLength */ \
1552 NULL, /* chunkContents */ \
1553 NULL, /* pFuncs */ \
1554 NULL, /* pExtra */ \
1555 NULL, /* context */ \
1556 NULL, NULL, NULL, /* p, q, r */ \
1558 0, 0, 0, /* a, b, c */ \
1559 0, 0, 0 /* privA,B,C, */ \
1563 #endif /* U_HIDE_DRAFT_API */