Update contrib.
2 *******************************************************************************
4 * Copyright (C) 2004-2005, 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 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.
109 #include "unicode/utypes.h"
111 #include "unicode/rep.h"
112 #include "unicode/unistr.h"
115 #ifndef U_HIDE_DRAFT_API
120 typedef struct UText UText; /**< C typedef for struct UText. @draft ICU 3.4 */
123 typedef struct UTextChunk UTextChunk; /**< C typedef for struct UTextChunk. @draft ICU 3.4 */
127 /***************************************************************************************
129 * C Functions for creating UText wrappers around various kinds of text strings.
131 ****************************************************************************************/
135 * utext_close Close function for UText instances.
136 * Cleans up, releases any resources being held by an
139 * If the UText was originally allocated by one of the utext_open functions,
140 * the storage associated with the utext will also be freed.
141 * If the UText storage originated with the application, as it would with
142 * a local or static instance, the storage will not be deleted.
144 * An open UText can be reset to refer to new string by using one of the utext_open()
145 * functions without first closing the UText.
147 * @param ut The UText to be closed.
148 * @return NULL if the UText struct was deleted by the close. If the UText struct
149 * was originally provided by the caller to the open function, it is
150 * returned by this function, and may be safely used again in
151 * a subsequent utext_open.
155 U_DRAFT UText * U_EXPORT2
156 utext_close(UText *ut);
160 * Open a read-only UText implementation for UTF-8 strings.
163 * Any invalid UTF-8 in the input will be handled in this way:
164 * a sequence of bytes that has the form of a truncated, but otherwise valid,
165 * UTF-8 sequence will be replaced by a single unicode replacement character, \uFFFD.
166 * Any other illegal bytes will each be replaced by a \uFFFD.
169 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
170 * If non-NULL, must refer to an initialized UText struct, which will then
171 * be reset to reference the specified UTF-8 string.
172 * @param s A UTF-8 string
173 * @param length The length of the UTF-8 string in bytes, or -1 if the string is
175 * @param status Errors are returned here.
176 * @return A pointer to the UText. If a pre-allocated UText was provided, it
177 * will always be used and returned.
180 U_DRAFT UText * U_EXPORT2
181 utext_openUTF8(UText *ut, const char *s, int32_t length, UErrorCode *status);
185 * Open a read-only UText for UChar * string.
187 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
188 * If non-NULL, must refer to an initialized UText struct, which will then
189 * be reset to reference the specified UChar string.
190 * @param s A UChar (UTF-16) string
191 * @param length The number of UChars in the input string, or -1 if the string is
193 * @param status Errors are returned here.
194 * @return A pointer to the UText. If a pre-allocated UText was provided, it
195 * will always be used and returned.
198 U_DRAFT UText * U_EXPORT2
199 utext_openUChars(UText *ut, const UChar *s, int32_t length, UErrorCode *status);
204 * Open a writable UText for a non-const UnicodeString.
206 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
207 * If non-NULL, must refer to an initialized UText struct, which will then
208 * be reset to reference the specified input string.
209 * @param s A UnicodeString.
210 * @param status Errors are returned here.
211 * @return Pointer to the UText. If a UText was supplied as input, this
212 * will always be used and returned.
215 U_DRAFT UText * U_EXPORT2
216 utext_openUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status);
220 * Open a UText for a const UnicodeString. The resulting UText will not be writable.
222 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
223 * If non-NULL, must refer to an initialized UText struct, which will then
224 * be reset to reference the specified input string.
225 * @param s A const UnicodeString to be wrapped.
226 * @param status Errors are returned here.
227 * @return Pointer to the UText. If a UText was supplied as input, this
228 * will always be used and returned.
231 U_DRAFT UText * U_EXPORT2
232 utext_openConstUnicodeString(UText *ut, const UnicodeString *s, UErrorCode *status);
236 * Open a writable UText implementation for an ICU Replaceable object.
237 * @param ut Pointer to a UText struct. If NULL, a new UText will be created.
238 * If non-NULL, must refer to an already existing UText, which will then
239 * be reset to reference the specified replaceable text.
240 * @param rep A Replaceable text object.
241 * @param status Errors are returned here.
242 * @return Pointer to the UText. If a UText was supplied as input, this
243 * will always be used and returned.
247 U_DRAFT UText * U_EXPORT2
248 utext_openReplaceable(UText *ut, Replaceable *rep, UErrorCode *status);
254 * clone a UText. Much like opening a UText where the source text is itself
257 * A deep clone will copy both the UText data structures and the underlying text.
258 * The original and cloned UText will operate completely independently; modifications
259 * made to the text in one will not effect the other. Text providers are not
260 * required to support deep clones. The user of clone() must check the status return
261 * and be prepared to handle failures.
263 * A shallow clone replicates only the UText data structures; it does not make
264 * a copy of the underlying text. Shallow clones can be used as an efficient way to
265 * have multiple iterators active in a single text string that is not being
268 * A shallow clone operation will not fail, barring truly exceptional conditions such
269 * as memory allocation failures.
271 * A UText and its clone may be safely concurrently accessed by separate threads.
272 * This is true for both shallow and deep clones.
273 * It is the responsibility of the Text Provider to ensure that this thread safety
276 * @param dest A UText struct to be filled in with the result of the clone operation,
277 * or NULL if the clone function should heap-allocate a new UText struct.
278 * @param src The UText to be cloned.
279 * @param deep TRUE to request a deep clone, FALSE for a shallow clone.
280 * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
281 * will be returned if the text provider is unable to clone the
283 * @return The newly created clone, or NULL if the clone operation failed.
286 U_DRAFT UText * U_EXPORT2
287 utext_clone(UText *dest, const UText *src, UBool deep, UErrorCode *status);
290 /*****************************************************************************
292 * C Functions to work with the text represeted by a UText wrapper
294 *****************************************************************************/
297 * Get the length of the text. Depending on the characteristics
298 * of the underlying text representation, this may be expensive.
299 * @see utext_isLengthExpensive()
302 * @param ut the text to be accessed.
303 * @return the length of the text, expressed in native units.
307 U_DRAFT int32_t U_EXPORT2
308 utext_nativeLength(UText *ut);
311 * Return TRUE if calculating the length of the text could be expensive.
312 * Finding the length of NUL terminated strings is considered to be expensive.
314 * Note that the value of this function may change
315 * as the result of other operations on a UText.
316 * Once the length of a string has been discovered, it will no longer
317 * be expensive to report it.
319 * @param ut the text to be accessed.
320 * @return TRUE if determining the length of the text could be time consuming.
323 U_DRAFT UBool U_EXPORT2
324 utext_isLengthExpensive(const UText *ut);
327 * Returns the code point at the requested index,
328 * or U_SENTINEL (-1) if it is out of bounds.
330 * If the specified index points to the interior of a multi-unit
331 * character - one of the trail bytes of a UTF-8 sequence, for example -
332 * the complete code point will be returned.
334 * The iteration position will be set to the start of the returned code point.
336 * This function is roughly equivalent to the the sequence
337 * utext_setNativeIndex(index);
339 * (There is a difference if the index is out of bounds by being less than zero)
341 * @param ut the text to be accessed
342 * @param nativeIndex the native index of the character to be accessed. If the index points
343 * to other than the first unit of a multi-unit character, it will be adjusted
344 * to the start of the character.
345 * @return the code point at the specified index.
348 U_DRAFT UChar32 U_EXPORT2
349 utext_char32At(UText *ut, int32_t nativeIndex);
354 * Get the code point at the current iteration position,
355 * or U_SENTINEL (-1) if the iteration has reached the end of
358 * @param ut the text to be accessed.
359 * @return the Unicode code point at the current iterator position.
362 U_DRAFT UChar32 U_EXPORT2
363 utext_current32(UText *ut);
367 * Get the code point at the current iteration position of the UText, and
368 * advance the position to the first index following the character.
369 * Returns U_SENTINEL (-1) if the position is at the end of the
371 * This is a post-increment operation
373 * An inline macro version of this function, UTEXT_NEXT32(),
374 * is available for performance critical use.
376 * @param ut the text to be accessed.
377 * @return the Unicode code point at the iteration position.
381 U_DRAFT UChar32 U_EXPORT2
382 utext_next32(UText *ut);
386 * Move the iterator position to the character (code point) whose
387 * index precedes the current position, and return that character.
388 * This is a pre-decrement operation.
389 * Returns U_SENTINEL (-1) if the position is at the start of the text.
390 * This is a pre-decrement operation.
392 * An inline macro version of this function, UTEXT_PREVIOUS32(),
393 * is available for performance critical use.
395 * @param ut the text to be accessed.
396 * @return the previous UChar32 code point, or U_SENTINEL (-1)
397 * if the iteration has reached the start of the text.
398 * @see UTEXT_PREVIOUS32
401 U_DRAFT UChar32 U_EXPORT2
402 utext_previous32(UText *ut);
406 * Set the iteration index, access the text for forward iteration,
407 * and return the code point starting at or before that index.
408 * Leave the iteration index at the start of the following code point.
410 * This function is the most efficient and convenient way to
411 * begin a forward iteration.
413 * @param ut the text to be accessed.
414 * @param nativeIndex Iteration index, in the native units of the text provider.
415 * @return Code point which starts at or before index,
416 * or U_SENTINEL (-1) if it is out of bounds.
419 U_DRAFT UChar32 U_EXPORT2
420 utext_next32From(UText *ut, int32_t nativeIndex);
425 * Set the iteration index, and return the code point preceding the
426 * one specified by the initial index. Leave the iteration position
427 * at the start of the returned code point.
429 * This function is the most efficient and convenient way to
430 * begin a backwards iteration.
432 * @param ut the text to be accessed.
433 * @param nativeIndex Iteration index in the native units of the text provider.
434 * @return Code point preceding the one at the initial index,
435 * or U_SENTINEL (-1) if it is out of bounds.
439 U_DRAFT UChar32 U_EXPORT2
440 utext_previous32From(UText *ut, int32_t nativeIndex);
443 * Get the current iterator position, which can range from 0 to
444 * the length of the text.
445 * The position is a native index into the input text, in whatever format it
446 * may have, and may not always correspond to a UChar (UTF-16) index
447 * into the text. The returned position will always be aligned to a
448 * code point boundary
450 * @param ut the text to be accessed.
451 * @return the current index position, in the native units of the text provider.
454 U_DRAFT int32_t U_EXPORT2
455 utext_getNativeIndex(UText *ut);
458 * Set the current iteration position to the nearest code point
459 * boundary at or preceding the specified index.
460 * The index is in the native units of the original input text.
461 * If the index is out of range, it will be trimmed to be within
462 * the range of the input text.
464 * It will usually be more efficient to begin an iteration
465 * using the functions utext_next32From() or utext_previous32From()
466 * rather than setIndex().
468 * Moving the index position to an adjacent character is best done
469 * with utext_next32(), utext_previous32() or utext_moveIndex32().
470 * Attempting to do direct arithmetic on the index position is
471 * complicated by the fact that the size (in native units) of a
472 * character depends on the underlying representation of the character
473 * (UTF-8, UTF-16, UTF-32, arbitrary codepage), and is not
476 * @param ut the text to be accessed.
477 * @param nativeIndex the native unit index of the new iteration position.
480 U_DRAFT void U_EXPORT2
481 utext_setNativeIndex(UText *ut, int32_t nativeIndex);
484 * Move the iterator postion by delta code points. The number of code points
485 * is a signed number; a negative delta will move the iterator backwards,
486 * towards the start of the text.
488 * The index is moved by <code>delta</code> code points
489 * forward or backward, but no further backward than to 0 and
490 * no further forward than to utext_nativeLength().
491 * The resulting index value will be in between 0 and length, inclusive.
493 * Because the index is kept in the native units of the text provider, the
494 * actual numeric amount by which the index moves depends on the
495 * underlying text storage representation of the text provider.
497 * @param ut the text to be accessed.
498 * @param delta the signed number of code points to move the iteration position.
499 * @return TRUE if the position could be moved the requested number of positions while
500 * staying within the range [0 - text length].
503 U_DRAFT UBool U_EXPORT2
504 utext_moveIndex32(UText *ut, int32_t delta);
509 * Extract text from a UText into a UChar buffer. The range of text to be extracted
510 * is specified in the native indices of the UText provider. These may not necessarily
513 * The size (number of 16 bit UChars) in the data to be extracted is returned. The
514 * full number of UChars is returned, even when the extracted text is truncated
515 * because the specified buffer size is too small.
517 * The extracted string will (if you are a user) / must (if you are a text provider)
518 * be NUL-terminated if there is sufficient space in the destination buffer. This
519 * terminating NUL is not included in the returned length.
521 * @param ut the UText from which to extract data.
522 * @param nativeStart the native index of the first character to extract.
523 * @param nativeLimit the native string index of the position following the last
524 * character to extract. If the specified limit is greater than the length
525 * of the text, the limit will be trimmed back to the text length.
526 * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
527 * @param destCapacity The size, in UChars, of the destination buffer. May be zero
528 * for precomputing the required size.
529 * @param status receives any error status.
530 * U_BUFFER_OVERFLOW_ERROR: the extracted text was truncated because the
531 * buffer was too small. Returns number of UChars for preflighting.
532 * @return Number of UChars in the data to be extracted. Does not include a trailing NUL.
536 U_DRAFT int32_t U_EXPORT2
537 utext_extract(UText *ut,
538 int32_t nativeStart, int32_t nativeLimit,
539 UChar *dest, int32_t destCapacity,
544 /************************************************************************************
546 * #define inline versions of selected performance-critical text access functions
547 * Caution: do not use auto increment++ or decrement-- expressions
548 * as parameters to these macros.
550 * For most use, where there is no extreme performance constraint, the
551 * normal, non-inline functions are a better choice. The resulting code
552 * will be smaller, and, if the need ever arises, easier to debug.
554 * These are implemented as #defines rather than real functions
555 * because there is no fully portable way to do inline functions in plain C.
557 ************************************************************************************/
560 * inline version of utext_next32(), for performance-critical situations.
562 * Get the code point at the current iteration position of the UText, and
563 * advance the position to the first index following the character.
564 * This is a post-increment operation.
565 * Returns U_SENTINEL (-1) if the position is at the end of the
570 #define UTEXT_NEXT32(ut) \
571 ((ut)->chunk.offset < (ut)->chunk.length && ((ut)->chunk.contents)[(ut)->chunk.offset]<0xd800 ? \
572 ((ut)->chunk.contents)[((ut)->chunk.offset)++] : utext_next32(ut))
575 * inline version of utext_previous32(), for performance-critical situations.
577 * Move the iterator position to the character (code point) whose
578 * index precedes the current position, and return that character.
579 * This is a pre-decrement operation.
580 * Returns U_SENTINEL (-1) if the position is at the start of the text.
584 #define UTEXT_PREVIOUS32(ut) \
585 ((ut)->chunk.offset > 0 && \
586 (ut)->chunk.contents[(ut)->chunk.offset-1] < 0xd800 ? \
587 (ut)->chunk.contents[--((ut)->chunk.offset)] : utext_previous32(ut))
592 /************************************************************************************
594 * Functions related to writing or modifying the text.
595 * These will work only with modifiable UTexts. Attempting to
596 * modify a read-only UText will return an error status.
598 ************************************************************************************/
602 * Return TRUE if the text can be written with utext_replace() or
603 * utext_copy(). For the text to be writable, the text provider must
604 * be of a type that supports writing.
606 * @param ut the UText to be tested.
607 * @return TRUE if the text is modifiable.
611 U_DRAFT UBool U_EXPORT2
612 utext_isWritable(const UText *ut);
616 * Test whether there is meta data associated with the text.
617 * @see Replaceable::hasMetaData()
619 * @param ut The UText to be tested
620 * @return TRUE if the underlying text includes meta data.
623 U_DRAFT UBool U_EXPORT2
624 utext_hasMetaData(const UText *ut);
628 * Replace a range of the original text with a replacement text.
630 * Leaves the current iteration position at the position following the
631 * newly inserted replacement text.
633 * This function is only available on UText types that support writing,
634 * that is, ones where utext_isWritable() returns TRUE.
636 * When using this function, there should be only a single UText opened onto the
637 * underlying native text string. Behavior after a replace operation
638 * on a UText is undefined for any other additional UTexts that refer to the
641 * @param ut the UText representing the text to be operated on.
642 * @param nativeStart the native index of the start of the region to be replaced
643 * @param nativeLimit the native index of the character following the region to be replaced.
644 * @param replacementText pointer to the replacement text
645 * @param replacementLength length of the replacement text, or -1 if the text is NUL terminated.
646 * @param status receives any error status. Possible errors include
647 * U_NO_WRITE_PERMISSION
649 * @return The signed number of (native) storage units by which
650 * the length of the text expanded or contracted.
654 U_DRAFT int32_t U_EXPORT2
655 utext_replace(UText *ut,
656 int32_t nativeStart, int32_t nativeLimit,
657 const UChar *replacementText, int32_t replacementLength,
664 * Copy or move a substring from one position to another within the text,
665 * while retaining any metadata associated with the text.
666 * This function is used to duplicate or reorder substrings.
667 * The destination index must not overlap the source range.
669 * The text to be copied or moved is inserted at destIndex;
670 * it does not replace or overwrite any existing text.
672 * This function is only available on UText types that support writing,
673 * that is, ones where utext_isWritable() returns TRUE.
675 * When using this function, there should be only a single UText opened onto the
676 * underlying native text string. Behavior after a copy operation
677 * on a UText is undefined in any other additional UTexts that refer to the
680 * @param ut The UText representing the text to be operated on.
681 * @param nativeStart The native index of the start of the region to be copied or moved
682 * @param nativeLimit The native index of the character position following the region to be copied.
683 * @param destIndex The native destination index to which the source substring is copied or moved.
684 * @param move If TRUE, then the substring is moved, not copied/duplicated.
685 * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
689 U_DRAFT void U_EXPORT2
690 utext_copy(UText *ut,
691 int32_t nativeStart, int32_t nativeLimit,
700 /****************************************************************************************
702 * The following items are required by text providers implementations -
703 * by packages that are writing UText wrappers for additional types of text strings.
704 * These declarations are not needed by applications that use already existing
705 * UText functions for wrapping strings or accessing text data that has been
706 * wrapped in a UText.
708 *****************************************************************************************/
712 * Descriptor of a chunk, or segment of text in UChar format.
714 * UText provider implementations surface their text in the form of UTextChunks.
716 * If the native form of the text if UTF-16, a chunk will typically refer back to the
717 * original native text storage. If the native format is something else, chunks
718 * will typically refer to a buffer maintained by the provider that contains
719 * some amount input that has been converted to UTF-16 (UChar) form.
724 /** Pointer to contents of text chunk. UChar format. */
725 const UChar *contents;
727 /** Index within the contents of the current iteration position. */
730 /** Number of UChars in the chunk. */
733 /** (Native) text index corresponding to the start of the chunk. */
736 /** (Native) text index corresponding to the end of the chunk (contents+length). */
739 /** If TRUE, then non-UTF-16 indexes are used in this chunk. */
740 UBool nonUTF16Indexes;
743 UBool padding1, padding2, padding3;
746 int32_t padInt1, padInt2;
748 /** Contains sizeof(UTextChunk) and allows the future addition of fields. */
749 int32_t sizeOfStruct;
754 * UText provider properties (bit field indexes).
761 * The provider works with non-UTF-16 ("native") text indexes.
762 * For example, byte indexes into UTF-8 text or UTF-32 indexes into UTF-32 text.
765 UTEXT_PROVIDER_NON_UTF16_INDEXES = 0,
767 * It is potentially time consuming for the provider to determine the length of the text.
770 UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE = 1,
772 * Text chunks remain valid and usable until the text object is modified or
773 * deleted, not just until the next time the access() function is called
774 * (which is the default).
777 UTEXT_PROVIDER_STABLE_CHUNKS = 2,
779 * The provider supports modifying the text via the replace() and copy()
784 UTEXT_PROVIDER_WRITABLE = 3,
786 * There is meta data associated with the text.
787 * @see Replaceable::hasMetaData()
790 UTEXT_PROVIDER_HAS_META_DATA = 4
794 * Function type declaration for UText.clone().
796 * clone a UText. Much like opening a UText where the source text is itself
799 * A deep clone will copy both the UText data structures and the underlying text.
800 * The original and cloned UText will operate completely independently; modifications
801 * made to the text in one will not effect the other. Text providers are not
802 * required to support deep clones. The user of clone() must check the status return
803 * and be prepared to handle failures.
805 * A shallow clone replicates only the UText data structures; it does not make
806 * a copy of the underlying text. Shallow clones can be used as an efficient way to
807 * have multiple iterators active in a single text string that is not being
810 * A shallow clone operation must not fail except for truly exceptional conditions such
811 * as memory allocation failures.
813 * A UText and its clone may be safely concurrently accessed by separate threads.
814 * This is true for both shallow and deep clones.
815 * It is the responsibility of the Text Provider to ensure that this thread safety
819 * @param dest A UText struct to be filled in with the result of the clone operation,
820 * or NULL if the clone function should heap-allocate a new UText struct.
821 * @param src The UText to be cloned.
822 * @param deep TRUE to request a deep clone, FALSE for a shallow clone.
823 * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR
824 * should be returned if the text provider is unable to clone the
826 * @return The newly created clone, or NULL if the clone operation failed.
830 typedef UText * U_CALLCONV
831 UTextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status);
835 * Function type declaration for UText.nativeLength().
837 * @param ut the UText to get the length of.
838 * @return the length, in the native units of the original text string.
842 typedef int32_t U_CALLCONV
843 UTextNativeLength(UText *ut);
846 * Function type declaration for UText.access(). Get the description of the text chunk
847 * containing the text at a requested native index. The UText's iteration
848 * position will be left at the requested index. If the index is out
849 * of bounds, the iteration position will be left at the start or end
850 * of the string, as appropriate.
852 * Chunks must begin and end on code point boundaries. A single code point
853 * comprised of multiple storage units must never span a chunk boundary.
856 * @param ut the UText being accessed.
857 * @param nativeIndex Requested index of the text to be accessed.
858 * @param forward If TRUE, then the returned chunk must contain text
859 * starting from the index, so that start<=index<limit.
860 * If FALSE, then the returned chunk must contain text
861 * before the index, so that start<index<=limit.
862 * @return True if the requested index could be accessed. The chunk
863 * will contain the requested text.
864 * False value if a chunk cannot be accessed
865 * (the requested index is out of bounds).
870 typedef UBool U_CALLCONV
871 UTextAccess(UText *ut, int32_t nativeIndex, UBool forward, UTextChunk *chunk);
874 * Function type declaration for UText.extract().
876 * Extract text from a UText into a UChar buffer. The range of text to be extracted
877 * is specified in the native indices of the UText provider. These may not necessarily
880 * The size (number of 16 bit UChars) in the data to be extracted is returned. The
881 * full amount is returned, even when the specified buffer size is smaller.
883 * The extracted string will (if you are a user) / must (if you are a text provider)
884 * be NUL-terminated if there is sufficient space in the destination buffer.
886 * @param ut the UText from which to extract data.
887 * @param nativeStart the native index of the first characer to extract.
888 * @param nativeLimit the native string index of the position following the last
889 * character to extract.
890 * @param dest the UChar (UTF-16) buffer into which the extracted text is placed
891 * @param destCapacity The size, in UChars, of the destination buffer. May be zero
892 * for precomputing the required size.
893 * @param status receives any error status.
894 * If U_BUFFER_OVERFLOW_ERROR: Returns number of UChars for
896 * @return Number of UChars in the data. Does not include a trailing NUL.
900 typedef int32_t U_CALLCONV
901 UTextExtract(UText *ut,
902 int32_t nativeStart, int32_t nativeLimit,
903 UChar *dest, int32_t destCapacity,
907 * Function type declaration for UText.replace().
909 * Replace a range of the original text with a replacement text.
911 * Leaves the current iteration position at the position following the
912 * newly inserted replacement text.
914 * This function need only be implemented on UText types that support writing.
916 * When using this function, there should be only a single UText opened onto the
917 * underlying native text string. The function is responsible for updating the
918 * text chunk within the UText to reflect the updated iteration position,
919 * taking into account any changes to the underlying string's structure caused
920 * by the replace operation.
922 * @param ut the UText representing the text to be operated on.
923 * @param nativeStart the index of the start of the region to be replaced
924 * @param nativeLimit the index of the character following the region to be replaced.
925 * @param replacementText pointer to the replacement text
926 * @param replacmentLength length of the replacement text in UChars, or -1 if the text is NUL terminated.
927 * @param status receives any error status. Possible errors include
928 * U_NO_WRITE_PERMISSION
930 * @return The signed number of (native) storage units by which
931 * the length of the text expanded or contracted.
935 typedef int32_t U_CALLCONV
936 UTextReplace(UText *ut,
937 int32_t nativeStart, int32_t nativeLimit,
938 const UChar *replacementText, int32_t replacmentLength,
942 * Function type declaration for UText.copy().
944 * Copy or move a substring from one position to another within the text,
945 * while retaining any metadata associated with the text.
946 * This function is used to duplicate or reorder substrings.
947 * The destination index must not overlap the source range.
949 * The text to be copied or moved is inserted at destIndex;
950 * it does not replace or overwrite any existing text.
952 * This function need only be implemented for UText types that support writing.
954 * When using this function, there should be only a single UText opened onto the
955 * underlying native text string. The function is responsible for updating the
956 * text chunk within the UText to reflect the updated iteration position,
957 * taking into account any changes to the underlying string's structure caused
958 * by the replace operation.
960 * @param ut The UText representing the text to be operated on.
961 * @param nativeStart The index of the start of the region to be copied or moved
962 * @param nativeLimit The index of the character following the region to be replaced.
963 * @param nativeDest The destination index to which the source substring is copied or moved.
964 * @param move If TRUE, then the substring is moved, not copied/duplicated.
965 * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION
969 typedef void U_CALLCONV
971 int32_t nativeStart, int32_t nativeLimit,
977 * Function type declaration for UText.mapOffsetToNative().
978 * Map from a UChar offset within the current text chunk within the UText to
979 * the corresponding native index in the original source text.
981 * This is required only for text providers that do not use native UTF-16 indexes.
983 * TODO: specify behavior with out-of-bounds offset? Shouldn't ever occur.
985 * @param ut the UText.
986 * @param offset UTF-16 offset within text chunk
987 * 0<=offset<=chunk->length.
988 * @return Absolute (native) index corresponding to the specified chunk offset.
989 * The returned native index should always be to a code point boundary.
993 typedef int32_t U_CALLCONV
994 UTextMapOffsetToNative(UText *ut, int32_t offset);
997 * Function type declaration for UText.mapIndexToUTF16().
998 * Map from a native index to a UChar offset within a text chunk
1000 * This function is required only for text providers that do not use native UTF-16 indexes.
1002 * @param ut The UText containing the text chunk.
1003 * @param nativeIndex Absolute (native) text index, chunk->start<=index<=chunk->limit.
1004 * @return Chunk-relative UTF-16 offset corresponding to the specified native
1007 * TODO: specify behavior with out-of-bounds index? Shouldn't ever occur.
1010 typedef int32_t U_CALLCONV
1011 UTextMapNativeIndexToUTF16(UText *ut, int32_t nativeIndex);
1015 * Function type declaration for UText.utextClose().
1017 * A Text Provider close function is only required for provider types that make
1018 * allocations in their open function (or other functions) that must be
1019 * cleaned when the UText is closed.
1021 * The allocation of the UText struct itself and any "extra" storage
1022 * associated with the UText is handled by the common UText implementation
1023 * and does not require provider specific cleanup in a close function.
1025 * Most UText provider implementations do not need to implement this function.
1027 * @param ut A UText object to be closed.
1031 typedef void U_CALLCONV
1032 UTextClose(UText *ut);
1036 * UText struct. Provides the interface between the generic UText access code
1037 * and the UText provider code that works on specific kinds of
1038 * text (UTF-8, noncontiguous UTF-16, whatever.)
1040 * Applications that are using predefined types of text providers
1041 * to pass text data to ICU services will have no need to view the
1042 * internals of the UText structs that they open.
1048 * (protected) Pointer to string or wrapped object or similar.
1049 * Not used by caller.
1052 const void *context;
1055 * (protected) Pointer fields available for use by the text provider.
1056 * Not used by UText common code.
1059 const void *p, *q, *r;
1062 * (protected) Pointer to additional space requested by the
1063 * text provider during the utext_open operation.
1069 * (protected) Size in bytes of the extra space (pExtra).
1075 * (private) Flags for managing the allocation and freeing of
1076 * memory associated with this UText.
1082 * (private) Magic. Try to detect when we are handed junk.
1083 * utext_openXYZ() functions take an initialized,
1084 * but not necessarily open, UText struct as an,
1085 * optional fill-in parameter. This magic field
1086 * is used to check for that initialization.
1087 * Text provider close functions must NOT clear
1088 * the magic field because that would prevent
1089 * reuse of the UText struct.
1096 * (public) sizeOfStruct=sizeof(UText)
1097 * Allows possible backward compatible extension.
1101 int32_t sizeOfStruct;
1104 * (protected) Integer fields for use by text provider.
1105 * Not used by caller.
1112 * Text provider properties. This set of flags is maintainted by the
1113 * text provider implementation.
1116 int32_t providerProperties;
1120 /** descriptor for the text chunk that includes or is adjacent to
1121 * the current iteration position.
1128 * (public) Function pointer for UTextClone
1136 * (public) function pointer for UTextLength
1137 * May be expensive to compute!
1142 UTextNativeLength *nativeLength;
1145 * (public) Function pointer for UTextAccess.
1150 UTextAccess *access;
1153 * (public) Function pointer for UTextExtract.
1158 UTextExtract *extract;
1161 * (public) Function pointer for UTextReplace.
1166 UTextReplace *replace;
1169 * (public) Function pointer for UTextCopy.
1177 * (public) Function pointer for UTextMapOffsetToNative.
1179 * @see UTextMapOffsetToNative
1182 UTextMapOffsetToNative *mapOffsetToNative;
1185 * (public) Function pointer for UTextMapNativeIndexToUTF16.
1187 * @see UTextMapNativeIndexToUTF16
1190 UTextMapNativeIndexToUTF16 *mapNativeIndexToUTF16;
1193 * (public) Function pointer for UTextClose.
1203 * Common function for use by Text Provider implementations to allocate and/or initialize
1204 * a new UText struct. To be called in the implementation of utext_open() functions.
1205 * If the supplied UText parameter is null, a new UText struct will be allocated on the heap.
1206 * If the supplied UText is already open, the provider's close function will be called
1207 * so that the struct can be reused by the open that is in progress.
1209 * @param ut pointer to a UText struct to be re-used, or null if a new UText
1210 * should be allocated.
1211 * @param extraSpace The amount of additional space to be allocated as part
1212 * of this UText, for use by types of providers that require
1213 * additional storage.
1214 * @param status Errors are returned here.
1215 * @return pointer to the UText, allocated if necessary, with extra space set up if requested.
1218 U_DRAFT UText * U_EXPORT2
1219 utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status);
1225 UTEXT_MAGIC = 0x345ad82c
1230 * Initializer for a UTextChunk
1233 #define UTEXT_CHUNK_INIT { \
1234 NULL, /* contents */ \
1239 FALSE, /* nonUTF16idx */ \
1240 FALSE, FALSE, FALSE, /* padding1,2,3 */ \
1241 0, 0, /* padInt1, 2 */ \
1242 sizeof(UTextChunk) \
1248 * Initializer for the first part of a UText struct, the part that is
1249 * in common for all types of text providers.
1253 #define UTEXT_INITIALIZER_HEAD \
1254 NULL, /* context */ \
1255 NULL, NULL, NULL, /* p, q, r */ \
1256 NULL, /* pExtra */ \
1257 0, /* extraSize */ \
1259 UTEXT_MAGIC, /* magic */ \
1260 sizeof(UText), /* sizeOfStruct */ \
1261 0, 0, 0, /* a, b, c */ \
1262 0, /* providerProps */ \
1263 UTEXT_CHUNK_INIT /* UTextChunk */
1268 * initializer to be used with local (stack) instances of a UText
1269 * struct. UText structs must be initialized before passing
1270 * them to one of the utext_open functions.
1274 #define UTEXT_INITIALIZER { \
1275 UTEXT_INITIALIZER_HEAD, \
1276 NULL, /* clone () */ \
1277 NULL, /* length () */ \
1278 NULL, /* access () */ \
1279 NULL, /* extract () */ \
1280 NULL, /* replace () */ \
1281 NULL, /* copy () */ \
1282 NULL, NULL, /* map * 2 () */ \
1283 NULL /* close () */ \
1291 #endif /* U_HIDE_DRAFT_API */