jpayne@69: // © 2016 and later: Unicode, Inc. and others. jpayne@69: // License & terms of use: http://www.unicode.org/copyright.html jpayne@69: /* jpayne@69: ******************************************************************************* jpayne@69: * jpayne@69: * Copyright (C) 2004-2012, International Business Machines jpayne@69: * Corporation and others. All Rights Reserved. jpayne@69: * jpayne@69: ******************************************************************************* jpayne@69: * file name: utext.h jpayne@69: * encoding: UTF-8 jpayne@69: * tab size: 8 (not used) jpayne@69: * indentation:4 jpayne@69: * jpayne@69: * created on: 2004oct06 jpayne@69: * created by: Markus W. Scherer jpayne@69: */ jpayne@69: jpayne@69: #ifndef __UTEXT_H__ jpayne@69: #define __UTEXT_H__ jpayne@69: jpayne@69: /** jpayne@69: * \file jpayne@69: * \brief C API: Abstract Unicode Text API jpayne@69: * jpayne@69: * The Text Access API provides a means to allow text that is stored in alternative jpayne@69: * formats to work with ICU services. ICU normally operates on text that is jpayne@69: * stored in UTF-16 format, in (UChar *) arrays for the C APIs or as type jpayne@69: * UnicodeString for C++ APIs. jpayne@69: * jpayne@69: * ICU Text Access allows other formats, such as UTF-8 or non-contiguous jpayne@69: * UTF-16 strings, to be placed in a UText wrapper and then passed to ICU services. jpayne@69: * jpayne@69: * There are three general classes of usage for UText: jpayne@69: * jpayne@69: * Application Level Use. This is the simplest usage - applications would jpayne@69: * use one of the utext_open() functions on their input text, and pass jpayne@69: * the resulting UText to the desired ICU service. jpayne@69: * jpayne@69: * Second is usage in ICU Services, such as break iteration, that will need to jpayne@69: * operate on input presented to them as a UText. These implementations jpayne@69: * will need to use the iteration and related UText functions to gain jpayne@69: * access to the actual text. jpayne@69: * jpayne@69: * The third class of UText users are "text providers." These are the jpayne@69: * UText implementations for the various text storage formats. An application jpayne@69: * or system with a unique text storage format can implement a set of jpayne@69: * UText provider functions for that format, which will then allow jpayne@69: * ICU services to operate on that format. jpayne@69: * jpayne@69: * jpayne@69: * Iterating over text jpayne@69: * jpayne@69: * Here is sample code for a forward iteration over the contents of a UText jpayne@69: * jpayne@69: * \code jpayne@69: * UChar32 c; jpayne@69: * UText *ut = whatever(); jpayne@69: * jpayne@69: * for (c=utext_next32From(ut, 0); c>=0; c=utext_next32(ut)) { jpayne@69: * // do whatever with the codepoint c here. jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: * And here is similar code to iterate in the reverse direction, from the end jpayne@69: * of the text towards the beginning. jpayne@69: * jpayne@69: * \code jpayne@69: * UChar32 c; jpayne@69: * UText *ut = whatever(); jpayne@69: * int textLength = utext_nativeLength(ut); jpayne@69: * for (c=utext_previous32From(ut, textLength); c>=0; c=utext_previous32(ut)) { jpayne@69: * // do whatever with the codepoint c here. jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: * Characters and Indexing jpayne@69: * jpayne@69: * Indexing into text by UText functions is nearly always in terms of the native jpayne@69: * indexing of the underlying text storage. The storage format could be UTF-8 jpayne@69: * or UTF-32, for example. When coding to the UText access API, no assumptions jpayne@69: * can be made regarding the size of characters, or how far an index jpayne@69: * may move when iterating between characters. jpayne@69: * jpayne@69: * All indices supplied to UText functions are pinned to the length of the jpayne@69: * text. An out-of-bounds index is not considered to be an error, but is jpayne@69: * adjusted to be in the range 0 <= index <= length of input text. jpayne@69: * jpayne@69: * jpayne@69: * When an index position is returned from a UText function, it will be jpayne@69: * a native index to the underlying text. In the case of multi-unit characters, jpayne@69: * it will always refer to the first position of the character, jpayne@69: * never to the interior. This is essentially the same thing as saying that jpayne@69: * a returned index will always point to a boundary between characters. jpayne@69: * jpayne@69: * When a native index is supplied to a UText function, all indices that jpayne@69: * refer to any part of a multi-unit character representation are considered jpayne@69: * to be equivalent. In the case of multi-unit characters, an incoming index jpayne@69: * will be logically normalized to refer to the start of the character. jpayne@69: * jpayne@69: * It is possible to test whether a native index is on a code point boundary jpayne@69: * by doing a utext_setNativeIndex() followed by a utext_getNativeIndex(). jpayne@69: * If the index is returned unchanged, it was on a code point boundary. If jpayne@69: * an adjusted index is returned, the original index referred to the jpayne@69: * interior of a character. jpayne@69: * jpayne@69: * Conventions for calling UText functions jpayne@69: * jpayne@69: * Most UText access functions have as their first parameter a (UText *) pointer, jpayne@69: * which specifies the UText to be used. Unless otherwise noted, the jpayne@69: * pointer must refer to a valid, open UText. Attempting to jpayne@69: * use a closed UText or passing a NULL pointer is a programming error and jpayne@69: * will produce undefined results or NULL pointer exceptions. jpayne@69: * jpayne@69: * The UText_Open family of functions can either open an existing (closed) jpayne@69: * UText, or heap allocate a new UText. Here is sample code for creating jpayne@69: * a stack-allocated UText. jpayne@69: * jpayne@69: * \code jpayne@69: * char *s = whatever(); // A utf-8 string jpayne@69: * U_ErrorCode status = U_ZERO_ERROR; jpayne@69: * UText ut = UTEXT_INITIALIZER; jpayne@69: * utext_openUTF8(ut, s, -1, &status); jpayne@69: * if (U_FAILURE(status)) { jpayne@69: * // error handling jpayne@69: * } else { jpayne@69: * // work with the UText jpayne@69: * } jpayne@69: * \endcode jpayne@69: * jpayne@69: * Any existing UText passed to an open function _must_ have been initialized, jpayne@69: * either by the UTEXT_INITIALIZER, or by having been originally heap-allocated jpayne@69: * by an open function. Passing NULL will cause the open function to jpayne@69: * heap-allocate and fully initialize a new UText. jpayne@69: * jpayne@69: */ jpayne@69: jpayne@69: jpayne@69: jpayne@69: #include "unicode/utypes.h" jpayne@69: #include "unicode/uchar.h" jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: #include "unicode/localpointer.h" jpayne@69: #include "unicode/rep.h" jpayne@69: #include "unicode/unistr.h" jpayne@69: #include "unicode/chariter.h" jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: U_CDECL_BEGIN jpayne@69: jpayne@69: struct UText; jpayne@69: typedef struct UText UText; /**< C typedef for struct UText. @stable ICU 3.6 */ jpayne@69: jpayne@69: jpayne@69: /*************************************************************************************** jpayne@69: * jpayne@69: * C Functions for creating UText wrappers around various kinds of text strings. jpayne@69: * jpayne@69: ****************************************************************************************/ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Close function for UText instances. jpayne@69: * Cleans up, releases any resources being held by an open UText. jpayne@69: *

jpayne@69: * If the UText was originally allocated by one of the utext_open functions, jpayne@69: * the storage associated with the utext will also be freed. jpayne@69: * If the UText storage originated with the application, as it would with jpayne@69: * a local or static instance, the storage will not be deleted. jpayne@69: * jpayne@69: * An open UText can be reset to refer to new string by using one of the utext_open() jpayne@69: * functions without first closing the UText. jpayne@69: * jpayne@69: * @param ut The UText to be closed. jpayne@69: * @return NULL if the UText struct was deleted by the close. If the UText struct jpayne@69: * was originally provided by the caller to the open function, it is jpayne@69: * returned by this function, and may be safely used again in jpayne@69: * a subsequent utext_open. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_close(UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Open a read-only UText implementation for UTF-8 strings. jpayne@69: * jpayne@69: * \htmlonly jpayne@69: * Any invalid UTF-8 in the input will be handled in this way: jpayne@69: * a sequence of bytes that has the form of a truncated, but otherwise valid, jpayne@69: * UTF-8 sequence will be replaced by a single unicode replacement character, \uFFFD. jpayne@69: * Any other illegal bytes will each be replaced by a \uFFFD. jpayne@69: * \endhtmlonly jpayne@69: * jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an initialized UText struct, which will then jpayne@69: * be reset to reference the specified UTF-8 string. jpayne@69: * @param s A UTF-8 string. Must not be NULL. jpayne@69: * @param length The length of the UTF-8 string in bytes, or -1 if the string is jpayne@69: * zero terminated. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return A pointer to the UText. If a pre-allocated UText was provided, it jpayne@69: * will always be used and returned. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openUTF8(UText *ut, const char *s, int64_t length, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Open a read-only UText for UChar * string. jpayne@69: * jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an initialized UText struct, which will then jpayne@69: * be reset to reference the specified UChar string. jpayne@69: * @param s A UChar (UTF-16) string jpayne@69: * @param length The number of UChars in the input string, or -1 if the string is jpayne@69: * zero terminated. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return A pointer to the UText. If a pre-allocated UText was provided, it jpayne@69: * will always be used and returned. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openUChars(UText *ut, const UChar *s, int64_t length, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: /** jpayne@69: * Open a writable UText for a non-const UnicodeString. jpayne@69: * jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an initialized UText struct, which will then jpayne@69: * be reset to reference the specified input string. jpayne@69: * @param s A UnicodeString. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return Pointer to the UText. If a UText was supplied as input, this jpayne@69: * will always be used and returned. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openUnicodeString(UText *ut, icu::UnicodeString *s, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Open a UText for a const UnicodeString. The resulting UText will not be writable. jpayne@69: * jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an initialized UText struct, which will then jpayne@69: * be reset to reference the specified input string. jpayne@69: * @param s A const UnicodeString to be wrapped. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return Pointer to the UText. If a UText was supplied as input, this jpayne@69: * will always be used and returned. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openConstUnicodeString(UText *ut, const icu::UnicodeString *s, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Open a writable UText implementation for an ICU Replaceable object. jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an already existing UText, which will then jpayne@69: * be reset to reference the specified replaceable text. jpayne@69: * @param rep A Replaceable text object. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return Pointer to the UText. If a UText was supplied as input, this jpayne@69: * will always be used and returned. jpayne@69: * @see Replaceable jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openReplaceable(UText *ut, icu::Replaceable *rep, UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Open a UText implementation over an ICU CharacterIterator. jpayne@69: * @param ut Pointer to a UText struct. If NULL, a new UText will be created. jpayne@69: * If non-NULL, must refer to an already existing UText, which will then jpayne@69: * be reset to reference the specified replaceable text. jpayne@69: * @param ci A Character Iterator. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return Pointer to the UText. If a UText was supplied as input, this jpayne@69: * will always be used and returned. jpayne@69: * @see Replaceable jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_openCharacterIterator(UText *ut, icu::CharacterIterator *ci, UErrorCode *status); jpayne@69: jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Clone a UText. This is much like opening a UText where the source text is itself jpayne@69: * another UText. jpayne@69: * jpayne@69: * A deep clone will copy both the UText data structures and the underlying text. jpayne@69: * The original and cloned UText will operate completely independently; modifications jpayne@69: * made to the text in one will not affect the other. Text providers are not jpayne@69: * required to support deep clones. The user of clone() must check the status return jpayne@69: * and be prepared to handle failures. jpayne@69: * jpayne@69: * The standard UText implementations for UTF8, UChar *, UnicodeString and jpayne@69: * Replaceable all support deep cloning. jpayne@69: * jpayne@69: * The UText returned from a deep clone will be writable, assuming that the text jpayne@69: * provider is able to support writing, even if the source UText had been made jpayne@69: * non-writable by means of UText_freeze(). jpayne@69: * jpayne@69: * A shallow clone replicates only the UText data structures; it does not make jpayne@69: * a copy of the underlying text. Shallow clones can be used as an efficient way to jpayne@69: * have multiple iterators active in a single text string that is not being jpayne@69: * modified. jpayne@69: * jpayne@69: * A shallow clone operation will not fail, barring truly exceptional conditions such jpayne@69: * as memory allocation failures. jpayne@69: * jpayne@69: * Shallow UText clones should be avoided if the UText functions that modify the jpayne@69: * text are expected to be used, either on the original or the cloned UText. jpayne@69: * Any such modifications can cause unpredictable behavior. Read Only jpayne@69: * shallow clones provide some protection against errors of this type by jpayne@69: * disabling text modification via the cloned UText. jpayne@69: * jpayne@69: * A shallow clone made with the readOnly parameter == FALSE will preserve the jpayne@69: * utext_isWritable() state of the source object. Note, however, that jpayne@69: * write operations must be avoided while more than one UText exists that refer jpayne@69: * to the same underlying text. jpayne@69: * jpayne@69: * A UText and its clone may be safely concurrently accessed by separate threads. jpayne@69: * This is true for read access only with shallow clones, and for both read and jpayne@69: * write access with deep clones. jpayne@69: * It is the responsibility of the Text Provider to ensure that this thread safety jpayne@69: * constraint is met. jpayne@69: * jpayne@69: * @param dest A UText struct to be filled in with the result of the clone operation, jpayne@69: * or NULL if the clone function should heap-allocate a new UText struct. jpayne@69: * If non-NULL, must refer to an already existing UText, which will then jpayne@69: * be reset to become the clone. jpayne@69: * @param src The UText to be cloned. jpayne@69: * @param deep TRUE to request a deep clone, FALSE for a shallow clone. jpayne@69: * @param readOnly TRUE to request that the cloned UText have read only access to the jpayne@69: * underlying text. jpayne@69: jpayne@69: * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR jpayne@69: * will be returned if the text provider is unable to clone the jpayne@69: * original text. jpayne@69: * @return The newly created clone, or NULL if the clone operation failed. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_clone(UText *dest, const UText *src, UBool deep, UBool readOnly, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Compare two UText objects for equality. jpayne@69: * UTexts are equal if they are iterating over the same text, and jpayne@69: * have the same iteration position within the text. jpayne@69: * If either or both of the parameters are NULL, the comparison is FALSE. jpayne@69: * jpayne@69: * @param a The first of the two UTexts to compare. jpayne@69: * @param b The other UText to be compared. jpayne@69: * @return TRUE if the two UTexts are equal. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: utext_equals(const UText *a, const UText *b); jpayne@69: jpayne@69: jpayne@69: /***************************************************************************** jpayne@69: * jpayne@69: * Functions to work with the text represented by a UText wrapper jpayne@69: * jpayne@69: *****************************************************************************/ jpayne@69: jpayne@69: /** jpayne@69: * Get the length of the text. Depending on the characteristics jpayne@69: * of the underlying text representation, this may be expensive. jpayne@69: * @see utext_isLengthExpensive() jpayne@69: * jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return the length of the text, expressed in native units. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE int64_t U_EXPORT2 jpayne@69: utext_nativeLength(UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Return TRUE if calculating the length of the text could be expensive. jpayne@69: * Finding the length of NUL terminated strings is considered to be expensive. jpayne@69: * jpayne@69: * Note that the value of this function may change jpayne@69: * as the result of other operations on a UText. jpayne@69: * Once the length of a string has been discovered, it will no longer jpayne@69: * be expensive to report it. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return TRUE if determining the length of the text could be time consuming. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: utext_isLengthExpensive(const UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Returns the code point at the requested index, jpayne@69: * or U_SENTINEL (-1) if it is out of bounds. jpayne@69: * jpayne@69: * If the specified index points to the interior of a multi-unit jpayne@69: * character - one of the trail bytes of a UTF-8 sequence, for example - jpayne@69: * the complete code point will be returned. jpayne@69: * jpayne@69: * The iteration position will be set to the start of the returned code point. jpayne@69: * jpayne@69: * This function is roughly equivalent to the sequence jpayne@69: * utext_setNativeIndex(index); jpayne@69: * utext_current32(); jpayne@69: * (There is a subtle difference if the index is out of bounds by being less than zero - jpayne@69: * utext_setNativeIndex(negative value) sets the index to zero, after which utext_current() jpayne@69: * will return the char at zero. utext_char32At(negative index), on the other hand, will jpayne@69: * return the U_SENTINEL value of -1.) jpayne@69: * jpayne@69: * @param ut the text to be accessed jpayne@69: * @param nativeIndex the native index of the character to be accessed. If the index points jpayne@69: * to other than the first unit of a multi-unit character, it will be adjusted jpayne@69: * to the start of the character. jpayne@69: * @return the code point at the specified index. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_char32At(UText *ut, int64_t nativeIndex); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * jpayne@69: * Get the code point at the current iteration position, jpayne@69: * or U_SENTINEL (-1) if the iteration has reached the end of jpayne@69: * the input text. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return the Unicode code point at the current iterator position. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_current32(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Get the code point at the current iteration position of the UText, and jpayne@69: * advance the position to the first index following the character. jpayne@69: * jpayne@69: * If the position is at the end of the text (the index following jpayne@69: * the last character, which is also the length of the text), jpayne@69: * return U_SENTINEL (-1) and do not advance the index. jpayne@69: * jpayne@69: * This is a post-increment operation. jpayne@69: * jpayne@69: * An inline macro version of this function, UTEXT_NEXT32(), jpayne@69: * is available for performance critical use. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return the Unicode code point at the iteration position. jpayne@69: * @see UTEXT_NEXT32 jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_next32(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Move the iterator position to the character (code point) whose jpayne@69: * index precedes the current position, and return that character. jpayne@69: * This is a pre-decrement operation. jpayne@69: * jpayne@69: * If the initial position is at the start of the text (index of 0) jpayne@69: * return U_SENTINEL (-1), and leave the position unchanged. jpayne@69: * jpayne@69: * An inline macro version of this function, UTEXT_PREVIOUS32(), jpayne@69: * is available for performance critical use. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return the previous UChar32 code point, or U_SENTINEL (-1) jpayne@69: * if the iteration has reached the start of the text. jpayne@69: * @see UTEXT_PREVIOUS32 jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_previous32(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Set the iteration index and return the code point at that index. jpayne@69: * Leave the iteration index at the start of the following code point. jpayne@69: * jpayne@69: * This function is the most efficient and convenient way to jpayne@69: * begin a forward iteration. The results are identical to the those jpayne@69: * from the sequence jpayne@69: * \code jpayne@69: * utext_setIndex(); jpayne@69: * utext_next32(); jpayne@69: * \endcode jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @param nativeIndex Iteration index, in the native units of the text provider. jpayne@69: * @return Code point which starts at or before index, jpayne@69: * or U_SENTINEL (-1) if it is out of bounds. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_next32From(UText *ut, int64_t nativeIndex); jpayne@69: jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Set the iteration index, and return the code point preceding the jpayne@69: * one specified by the initial index. Leave the iteration position jpayne@69: * at the start of the returned code point. jpayne@69: * jpayne@69: * This function is the most efficient and convenient way to jpayne@69: * begin a backwards iteration. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @param nativeIndex Iteration index in the native units of the text provider. jpayne@69: * @return Code point preceding the one at the initial index, jpayne@69: * or U_SENTINEL (-1) if it is out of bounds. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UChar32 U_EXPORT2 jpayne@69: utext_previous32From(UText *ut, int64_t nativeIndex); jpayne@69: jpayne@69: /** jpayne@69: * Get the current iterator position, which can range from 0 to jpayne@69: * the length of the text. jpayne@69: * The position is a native index into the input text, in whatever format it jpayne@69: * may have (possibly UTF-8 for example), and may not always be the same as jpayne@69: * the corresponding UChar (UTF-16) index. jpayne@69: * The returned position will always be aligned to a code point boundary. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @return the current index position, in the native units of the text provider. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE int64_t U_EXPORT2 jpayne@69: utext_getNativeIndex(const UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Set the current iteration position to the nearest code point jpayne@69: * boundary at or preceding the specified index. jpayne@69: * The index is in the native units of the original input text. jpayne@69: * If the index is out of range, it will be pinned to be within jpayne@69: * the range of the input text. jpayne@69: *

jpayne@69: * It will usually be more efficient to begin an iteration jpayne@69: * using the functions utext_next32From() or utext_previous32From() jpayne@69: * rather than setIndex(). jpayne@69: *

jpayne@69: * Moving the index position to an adjacent character is best done jpayne@69: * with utext_next32(), utext_previous32() or utext_moveIndex32(). jpayne@69: * Attempting to do direct arithmetic on the index position is jpayne@69: * complicated by the fact that the size (in native units) of a jpayne@69: * character depends on the underlying representation of the character jpayne@69: * (UTF-8, UTF-16, UTF-32, arbitrary codepage), and is not jpayne@69: * easily knowable. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @param nativeIndex the native unit index of the new iteration position. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: utext_setNativeIndex(UText *ut, int64_t nativeIndex); jpayne@69: jpayne@69: /** jpayne@69: * Move the iterator position by delta code points. The number of code points jpayne@69: * is a signed number; a negative delta will move the iterator backwards, jpayne@69: * towards the start of the text. jpayne@69: *

jpayne@69: * The index is moved by delta code points jpayne@69: * forward or backward, but no further backward than to 0 and jpayne@69: * no further forward than to utext_nativeLength(). jpayne@69: * The resulting index value will be in between 0 and length, inclusive. jpayne@69: * jpayne@69: * @param ut the text to be accessed. jpayne@69: * @param delta the signed number of code points to move the iteration position. jpayne@69: * @return TRUE if the position could be moved the requested number of positions while jpayne@69: * staying within the range [0 - text length]. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: utext_moveIndex32(UText *ut, int32_t delta); jpayne@69: jpayne@69: /** jpayne@69: * Get the native index of the character preceding the current position. jpayne@69: * If the iteration position is already at the start of the text, zero jpayne@69: * is returned. jpayne@69: * The value returned is the same as that obtained from the following sequence, jpayne@69: * but without the side effect of changing the iteration position. jpayne@69: * jpayne@69: * \code jpayne@69: * UText *ut = whatever; jpayne@69: * ... jpayne@69: * utext_previous(ut) jpayne@69: * utext_getNativeIndex(ut); jpayne@69: * \endcode jpayne@69: * jpayne@69: * This function is most useful during forwards iteration, where it will get the jpayne@69: * native index of the character most recently returned from utext_next(). jpayne@69: * jpayne@69: * @param ut the text to be accessed jpayne@69: * @return the native index of the character preceding the current index position, jpayne@69: * or zero if the current position is at the start of the text. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: U_STABLE int64_t U_EXPORT2 jpayne@69: utext_getPreviousNativeIndex(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * jpayne@69: * Extract text from a UText into a UChar buffer. The range of text to be extracted jpayne@69: * is specified in the native indices of the UText provider. These may not necessarily jpayne@69: * be UTF-16 indices. jpayne@69: *

jpayne@69: * The size (number of 16 bit UChars) of the data to be extracted is returned. The jpayne@69: * full number of UChars is returned, even when the extracted text is truncated jpayne@69: * because the specified buffer size is too small. jpayne@69: *

jpayne@69: * The extracted string will (if you are a user) / must (if you are a text provider) jpayne@69: * be NUL-terminated if there is sufficient space in the destination buffer. This jpayne@69: * terminating NUL is not included in the returned length. jpayne@69: *

jpayne@69: * The iteration index is left at the position following the last extracted character. jpayne@69: * jpayne@69: * @param ut the UText from which to extract data. jpayne@69: * @param nativeStart the native index of the first character to extract.\ jpayne@69: * If the specified index is out of range, jpayne@69: * it will be pinned to be within 0 <= index <= textLength jpayne@69: * @param nativeLimit the native string index of the position following the last jpayne@69: * character to extract. If the specified index is out of range, jpayne@69: * it will be pinned to be within 0 <= index <= textLength. jpayne@69: * nativeLimit must be >= nativeStart. jpayne@69: * @param dest the UChar (UTF-16) buffer into which the extracted text is placed jpayne@69: * @param destCapacity The size, in UChars, of the destination buffer. May be zero jpayne@69: * for precomputing the required size. jpayne@69: * @param status receives any error status. jpayne@69: * U_BUFFER_OVERFLOW_ERROR: the extracted text was truncated because the jpayne@69: * buffer was too small. Returns number of UChars for preflighting. jpayne@69: * @return Number of UChars in the data to be extracted. Does not include a trailing NUL. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE int32_t U_EXPORT2 jpayne@69: utext_extract(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: UChar *dest, int32_t destCapacity, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: jpayne@69: /************************************************************************************ jpayne@69: * jpayne@69: * #define inline versions of selected performance-critical text access functions jpayne@69: * Caution: do not use auto increment++ or decrement-- expressions jpayne@69: * as parameters to these macros. jpayne@69: * jpayne@69: * For most use, where there is no extreme performance constraint, the jpayne@69: * normal, non-inline functions are a better choice. The resulting code jpayne@69: * will be smaller, and, if the need ever arises, easier to debug. jpayne@69: * jpayne@69: * These are implemented as #defines rather than real functions jpayne@69: * because there is no fully portable way to do inline functions in plain C. jpayne@69: * jpayne@69: ************************************************************************************/ jpayne@69: jpayne@69: #ifndef U_HIDE_INTERNAL_API jpayne@69: /** jpayne@69: * inline version of utext_current32(), for performance-critical situations. jpayne@69: * jpayne@69: * Get the code point at the current iteration position of the UText. jpayne@69: * Returns U_SENTINEL (-1) if the position is at the end of the jpayne@69: * text. jpayne@69: * jpayne@69: * @internal ICU 4.4 technology preview jpayne@69: */ jpayne@69: #define UTEXT_CURRENT32(ut) \ jpayne@69: ((ut)->chunkOffset < (ut)->chunkLength && ((ut)->chunkContents)[(ut)->chunkOffset]<0xd800 ? \ jpayne@69: ((ut)->chunkContents)[((ut)->chunkOffset)] : utext_current32(ut)) jpayne@69: #endif /* U_HIDE_INTERNAL_API */ jpayne@69: jpayne@69: /** jpayne@69: * inline version of utext_next32(), for performance-critical situations. jpayne@69: * jpayne@69: * Get the code point at the current iteration position of the UText, and jpayne@69: * advance the position to the first index following the character. jpayne@69: * This is a post-increment operation. jpayne@69: * Returns U_SENTINEL (-1) if the position is at the end of the jpayne@69: * text. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: #define UTEXT_NEXT32(ut) \ jpayne@69: ((ut)->chunkOffset < (ut)->chunkLength && ((ut)->chunkContents)[(ut)->chunkOffset]<0xd800 ? \ jpayne@69: ((ut)->chunkContents)[((ut)->chunkOffset)++] : utext_next32(ut)) jpayne@69: jpayne@69: /** jpayne@69: * inline version of utext_previous32(), for performance-critical situations. jpayne@69: * jpayne@69: * Move the iterator position to the character (code point) whose jpayne@69: * index precedes the current position, and return that character. jpayne@69: * This is a pre-decrement operation. jpayne@69: * Returns U_SENTINEL (-1) if the position is at the start of the text. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: #define UTEXT_PREVIOUS32(ut) \ jpayne@69: ((ut)->chunkOffset > 0 && \ jpayne@69: (ut)->chunkContents[(ut)->chunkOffset-1] < 0xd800 ? \ jpayne@69: (ut)->chunkContents[--((ut)->chunkOffset)] : utext_previous32(ut)) jpayne@69: jpayne@69: /** jpayne@69: * inline version of utext_getNativeIndex(), for performance-critical situations. jpayne@69: * jpayne@69: * Get the current iterator position, which can range from 0 to jpayne@69: * the length of the text. jpayne@69: * The position is a native index into the input text, in whatever format it jpayne@69: * may have (possibly UTF-8 for example), and may not always be the same as jpayne@69: * the corresponding UChar (UTF-16) index. jpayne@69: * The returned position will always be aligned to a code point boundary. jpayne@69: * jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: #define UTEXT_GETNATIVEINDEX(ut) \ jpayne@69: ((ut)->chunkOffset <= (ut)->nativeIndexingLimit? \ jpayne@69: (ut)->chunkNativeStart+(ut)->chunkOffset : \ jpayne@69: (ut)->pFuncs->mapOffsetToNative(ut)) jpayne@69: jpayne@69: /** jpayne@69: * inline version of utext_setNativeIndex(), for performance-critical situations. jpayne@69: * jpayne@69: * Set the current iteration position to the nearest code point jpayne@69: * boundary at or preceding the specified index. jpayne@69: * The index is in the native units of the original input text. jpayne@69: * If the index is out of range, it will be pinned to be within jpayne@69: * the range of the input text. jpayne@69: * jpayne@69: * @stable ICU 3.8 jpayne@69: */ jpayne@69: #define UTEXT_SETNATIVEINDEX(ut, ix) UPRV_BLOCK_MACRO_BEGIN { \ jpayne@69: int64_t __offset = (ix) - (ut)->chunkNativeStart; \ jpayne@69: if (__offset>=0 && __offset<(int64_t)(ut)->nativeIndexingLimit && (ut)->chunkContents[__offset]<0xdc00) { \ jpayne@69: (ut)->chunkOffset=(int32_t)__offset; \ jpayne@69: } else { \ jpayne@69: utext_setNativeIndex((ut), (ix)); \ jpayne@69: } \ jpayne@69: } UPRV_BLOCK_MACRO_END jpayne@69: jpayne@69: jpayne@69: jpayne@69: /************************************************************************************ jpayne@69: * jpayne@69: * Functions related to writing or modifying the text. jpayne@69: * These will work only with modifiable UTexts. Attempting to jpayne@69: * modify a read-only UText will return an error status. jpayne@69: * jpayne@69: ************************************************************************************/ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Return TRUE if the text can be written (modified) with utext_replace() or jpayne@69: * utext_copy(). For the text to be writable, the text provider must jpayne@69: * be of a type that supports writing and the UText must not be frozen. jpayne@69: * jpayne@69: * Attempting to modify text when utext_isWriteable() is FALSE will fail - jpayne@69: * the text will not be modified, and an error will be returned from the function jpayne@69: * that attempted the modification. jpayne@69: * jpayne@69: * @param ut the UText to be tested. jpayne@69: * @return TRUE if the text is modifiable. jpayne@69: * jpayne@69: * @see utext_freeze() jpayne@69: * @see utext_replace() jpayne@69: * @see utext_copy() jpayne@69: * @stable ICU 3.4 jpayne@69: * jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: utext_isWritable(const UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Test whether there is meta data associated with the text. jpayne@69: * @see Replaceable::hasMetaData() jpayne@69: * jpayne@69: * @param ut The UText to be tested jpayne@69: * @return TRUE if the underlying text includes meta data. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UBool U_EXPORT2 jpayne@69: utext_hasMetaData(const UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Replace a range of the original text with a replacement text. jpayne@69: * jpayne@69: * Leaves the current iteration position at the position following the jpayne@69: * newly inserted replacement text. jpayne@69: * jpayne@69: * This function is only available on UText types that support writing, jpayne@69: * that is, ones where utext_isWritable() returns TRUE. jpayne@69: * jpayne@69: * When using this function, there should be only a single UText opened onto the jpayne@69: * underlying native text string. Behavior after a replace operation jpayne@69: * on a UText is undefined for any other additional UTexts that refer to the jpayne@69: * modified string. jpayne@69: * jpayne@69: * @param ut the UText representing the text to be operated on. jpayne@69: * @param nativeStart the native index of the start of the region to be replaced jpayne@69: * @param nativeLimit the native index of the character following the region to be replaced. jpayne@69: * @param replacementText pointer to the replacement text jpayne@69: * @param replacementLength length of the replacement text, or -1 if the text is NUL terminated. jpayne@69: * @param status receives any error status. Possible errors include jpayne@69: * U_NO_WRITE_PERMISSION jpayne@69: * jpayne@69: * @return The signed number of (native) storage units by which jpayne@69: * the length of the text expanded or contracted. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE int32_t U_EXPORT2 jpayne@69: utext_replace(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: const UChar *replacementText, int32_t replacementLength, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * jpayne@69: * Copy or move a substring from one position to another within the text, jpayne@69: * while retaining any metadata associated with the text. jpayne@69: * This function is used to duplicate or reorder substrings. jpayne@69: * The destination index must not overlap the source range. jpayne@69: * jpayne@69: * The text to be copied or moved is inserted at destIndex; jpayne@69: * it does not replace or overwrite any existing text. jpayne@69: * jpayne@69: * The iteration position is left following the newly inserted text jpayne@69: * at the destination position. jpayne@69: * jpayne@69: * This function is only available on UText types that support writing, jpayne@69: * that is, ones where utext_isWritable() returns TRUE. jpayne@69: * jpayne@69: * When using this function, there should be only a single UText opened onto the jpayne@69: * underlying native text string. Behavior after a copy operation jpayne@69: * on a UText is undefined in any other additional UTexts that refer to the jpayne@69: * modified string. jpayne@69: * jpayne@69: * @param ut The UText representing the text to be operated on. jpayne@69: * @param nativeStart The native index of the start of the region to be copied or moved jpayne@69: * @param nativeLimit The native index of the character position following the region jpayne@69: * to be copied. jpayne@69: * @param destIndex The native destination index to which the source substring is jpayne@69: * copied or moved. jpayne@69: * @param move If TRUE, then the substring is moved, not copied/duplicated. jpayne@69: * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: utext_copy(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: int64_t destIndex, jpayne@69: UBool move, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: *

jpayne@69: * Freeze a UText. This prevents any modification to the underlying text itself jpayne@69: * by means of functions operating on this UText. jpayne@69: *

jpayne@69: *

jpayne@69: * Once frozen, a UText can not be unfrozen. The intent is to ensure jpayne@69: * that a the text underlying a frozen UText wrapper cannot be modified via that UText. jpayne@69: *

jpayne@69: *

jpayne@69: * Caution: freezing a UText will disable changes made via the specific jpayne@69: * frozen UText wrapper only; it will not have any effect on the ability to jpayne@69: * directly modify the text by bypassing the UText. Any such backdoor modifications jpayne@69: * are always an error while UText access is occurring because the underlying jpayne@69: * text can get out of sync with UText's buffering. jpayne@69: *

jpayne@69: * jpayne@69: * @param ut The UText to be frozen. jpayne@69: * @see utext_isWritable() jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: U_STABLE void U_EXPORT2 jpayne@69: utext_freeze(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * UText provider properties (bit field indexes). jpayne@69: * jpayne@69: * @see UText jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: enum { jpayne@69: /** jpayne@69: * It is potentially time consuming for the provider to determine the length of the text. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE = 1, jpayne@69: /** jpayne@69: * Text chunks remain valid and usable until the text object is modified or jpayne@69: * deleted, not just until the next time the access() function is called jpayne@69: * (which is the default). jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: UTEXT_PROVIDER_STABLE_CHUNKS = 2, jpayne@69: /** jpayne@69: * The provider supports modifying the text via the replace() and copy() jpayne@69: * functions. jpayne@69: * @see Replaceable jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: UTEXT_PROVIDER_WRITABLE = 3, jpayne@69: /** jpayne@69: * There is meta data associated with the text. jpayne@69: * @see Replaceable::hasMetaData() jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: UTEXT_PROVIDER_HAS_META_DATA = 4, jpayne@69: /** jpayne@69: * Text provider owns the text storage. jpayne@69: * Generally occurs as the result of a deep clone of the UText. jpayne@69: * When closing the UText, the associated text must jpayne@69: * also be closed/deleted/freed/ whatever is appropriate. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTEXT_PROVIDER_OWNS_TEXT = 5 jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.clone(). jpayne@69: * jpayne@69: * clone a UText. Much like opening a UText where the source text is itself jpayne@69: * another UText. jpayne@69: * jpayne@69: * A deep clone will copy both the UText data structures and the underlying text. jpayne@69: * The original and cloned UText will operate completely independently; modifications jpayne@69: * made to the text in one will not effect the other. Text providers are not jpayne@69: * required to support deep clones. The user of clone() must check the status return jpayne@69: * and be prepared to handle failures. jpayne@69: * jpayne@69: * A shallow clone replicates only the UText data structures; it does not make jpayne@69: * a copy of the underlying text. Shallow clones can be used as an efficient way to jpayne@69: * have multiple iterators active in a single text string that is not being jpayne@69: * modified. jpayne@69: * jpayne@69: * A shallow clone operation must not fail except for truly exceptional conditions such jpayne@69: * as memory allocation failures. jpayne@69: * jpayne@69: * A UText and its clone may be safely concurrently accessed by separate threads. jpayne@69: * This is true for both shallow and deep clones. jpayne@69: * It is the responsibility of the Text Provider to ensure that this thread safety jpayne@69: * constraint is met. jpayne@69: jpayne@69: * jpayne@69: * @param dest A UText struct to be filled in with the result of the clone operation, jpayne@69: * or NULL if the clone function should heap-allocate a new UText struct. jpayne@69: * @param src The UText to be cloned. jpayne@69: * @param deep TRUE to request a deep clone, FALSE for a shallow clone. jpayne@69: * @param status Errors are returned here. For deep clones, U_UNSUPPORTED_ERROR jpayne@69: * should be returned if the text provider is unable to clone the jpayne@69: * original text. jpayne@69: * @return The newly created clone, or NULL if the clone operation failed. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef UText * U_CALLCONV jpayne@69: UTextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.nativeLength(). jpayne@69: * jpayne@69: * @param ut the UText to get the length of. jpayne@69: * @return the length, in the native units of the original text string. jpayne@69: * @see UText jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef int64_t U_CALLCONV jpayne@69: UTextNativeLength(UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.access(). Get the description of the text chunk jpayne@69: * containing the text at a requested native index. The UText's iteration jpayne@69: * position will be left at the requested index. If the index is out jpayne@69: * of bounds, the iteration position will be left at the start or end jpayne@69: * of the string, as appropriate. jpayne@69: * jpayne@69: * Chunks must begin and end on code point boundaries. A single code point jpayne@69: * comprised of multiple storage units must never span a chunk boundary. jpayne@69: * jpayne@69: * jpayne@69: * @param ut the UText being accessed. jpayne@69: * @param nativeIndex Requested index of the text to be accessed. jpayne@69: * @param forward If TRUE, then the returned chunk must contain text jpayne@69: * starting from the index, so that start<=index jpayne@69: * The size (number of 16 bit UChars) in the data to be extracted is returned. The jpayne@69: * full amount is returned, even when the specified buffer size is smaller. jpayne@69: *

jpayne@69: * The extracted string will (if you are a user) / must (if you are a text provider) jpayne@69: * be NUL-terminated if there is sufficient space in the destination buffer. jpayne@69: * jpayne@69: * @param ut the UText from which to extract data. jpayne@69: * @param nativeStart the native index of the first character to extract. jpayne@69: * @param nativeLimit the native string index of the position following the last jpayne@69: * character to extract. jpayne@69: * @param dest the UChar (UTF-16) buffer into which the extracted text is placed jpayne@69: * @param destCapacity The size, in UChars, of the destination buffer. May be zero jpayne@69: * for precomputing the required size. jpayne@69: * @param status receives any error status. jpayne@69: * If U_BUFFER_OVERFLOW_ERROR: Returns number of UChars for jpayne@69: * preflighting. jpayne@69: * @return Number of UChars in the data. Does not include a trailing NUL. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef int32_t U_CALLCONV jpayne@69: UTextExtract(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: UChar *dest, int32_t destCapacity, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.replace(). jpayne@69: * jpayne@69: * Replace a range of the original text with a replacement text. jpayne@69: * jpayne@69: * Leaves the current iteration position at the position following the jpayne@69: * newly inserted replacement text. jpayne@69: * jpayne@69: * This function need only be implemented on UText types that support writing. jpayne@69: * jpayne@69: * When using this function, there should be only a single UText opened onto the jpayne@69: * underlying native text string. The function is responsible for updating the jpayne@69: * text chunk within the UText to reflect the updated iteration position, jpayne@69: * taking into account any changes to the underlying string's structure caused jpayne@69: * by the replace operation. jpayne@69: * jpayne@69: * @param ut the UText representing the text to be operated on. jpayne@69: * @param nativeStart the index of the start of the region to be replaced jpayne@69: * @param nativeLimit the index of the character following the region to be replaced. jpayne@69: * @param replacementText pointer to the replacement text jpayne@69: * @param replacmentLength length of the replacement text in UChars, or -1 if the text is NUL terminated. jpayne@69: * @param status receives any error status. Possible errors include jpayne@69: * U_NO_WRITE_PERMISSION jpayne@69: * jpayne@69: * @return The signed number of (native) storage units by which jpayne@69: * the length of the text expanded or contracted. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef int32_t U_CALLCONV jpayne@69: UTextReplace(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: const UChar *replacementText, int32_t replacmentLength, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.copy(). jpayne@69: * jpayne@69: * Copy or move a substring from one position to another within the text, jpayne@69: * while retaining any metadata associated with the text. jpayne@69: * This function is used to duplicate or reorder substrings. jpayne@69: * The destination index must not overlap the source range. jpayne@69: * jpayne@69: * The text to be copied or moved is inserted at destIndex; jpayne@69: * it does not replace or overwrite any existing text. jpayne@69: * jpayne@69: * This function need only be implemented for UText types that support writing. jpayne@69: * jpayne@69: * When using this function, there should be only a single UText opened onto the jpayne@69: * underlying native text string. The function is responsible for updating the jpayne@69: * text chunk within the UText to reflect the updated iteration position, jpayne@69: * taking into account any changes to the underlying string's structure caused jpayne@69: * by the replace operation. jpayne@69: * jpayne@69: * @param ut The UText representing the text to be operated on. jpayne@69: * @param nativeStart The index of the start of the region to be copied or moved jpayne@69: * @param nativeLimit The index of the character following the region to be replaced. jpayne@69: * @param nativeDest The destination index to which the source substring is copied or moved. jpayne@69: * @param move If TRUE, then the substring is moved, not copied/duplicated. jpayne@69: * @param status receives any error status. Possible errors include U_NO_WRITE_PERMISSION jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef void U_CALLCONV jpayne@69: UTextCopy(UText *ut, jpayne@69: int64_t nativeStart, int64_t nativeLimit, jpayne@69: int64_t nativeDest, jpayne@69: UBool move, jpayne@69: UErrorCode *status); jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.mapOffsetToNative(). jpayne@69: * Map from the current UChar offset within the current text chunk to jpayne@69: * the corresponding native index in the original source text. jpayne@69: * jpayne@69: * This is required only for text providers that do not use native UTF-16 indexes. jpayne@69: * jpayne@69: * @param ut the UText. jpayne@69: * @return Absolute (native) index corresponding to chunkOffset in the current chunk. jpayne@69: * The returned native index should always be to a code point boundary. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef int64_t U_CALLCONV jpayne@69: UTextMapOffsetToNative(const UText *ut); jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.mapIndexToUTF16(). jpayne@69: * Map from a native index to a UChar offset within a text chunk. jpayne@69: * Behavior is undefined if the native index does not fall within the jpayne@69: * current chunk. jpayne@69: * jpayne@69: * This function is required only for text providers that do not use native UTF-16 indexes. jpayne@69: * jpayne@69: * @param ut The UText containing the text chunk. jpayne@69: * @param nativeIndex Absolute (native) text index, chunk->start<=index<=chunk->limit. jpayne@69: * @return Chunk-relative UTF-16 offset corresponding to the specified native jpayne@69: * index. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef int32_t U_CALLCONV jpayne@69: UTextMapNativeIndexToUTF16(const UText *ut, int64_t nativeIndex); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Function type declaration for UText.utextClose(). jpayne@69: * jpayne@69: * A Text Provider close function is only required for provider types that make jpayne@69: * allocations in their open function (or other functions) that must be jpayne@69: * cleaned when the UText is closed. jpayne@69: * jpayne@69: * The allocation of the UText struct itself and any "extra" storage jpayne@69: * associated with the UText is handled by the common UText implementation jpayne@69: * and does not require provider specific cleanup in a close function. jpayne@69: * jpayne@69: * Most UText provider implementations do not need to implement this function. jpayne@69: * jpayne@69: * @param ut A UText object to be closed. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: typedef void U_CALLCONV jpayne@69: UTextClose(UText *ut); jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (public) Function dispatch table for UText. jpayne@69: * Conceptually very much like a C++ Virtual Function Table. jpayne@69: * This struct defines the organization of the table. jpayne@69: * Each text provider implementation must provide an jpayne@69: * actual table that is initialized with the appropriate functions jpayne@69: * for the type of text being handled. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: struct UTextFuncs { jpayne@69: /** jpayne@69: * (public) Function table size, sizeof(UTextFuncs) jpayne@69: * Intended for use should the table grow to accommodate added jpayne@69: * functions in the future, to allow tests for older format jpayne@69: * function tables that do not contain the extensions. jpayne@69: * jpayne@69: * Fields are placed for optimal alignment on jpayne@69: * 32/64/128-bit-pointer machines, by normally grouping together jpayne@69: * 4 32-bit fields, jpayne@69: * 4 pointers, jpayne@69: * 2 64-bit fields jpayne@69: * in sequence. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int32_t tableSize; jpayne@69: jpayne@69: /** jpayne@69: * (private) Alignment padding. jpayne@69: * Do not use, reserved for use by the UText framework only. jpayne@69: * @internal jpayne@69: */ jpayne@69: int32_t reserved1, /** @internal */ reserved2, /** @internal */ reserved3; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextClone jpayne@69: * jpayne@69: * @see UTextClone jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextClone *clone; jpayne@69: jpayne@69: /** jpayne@69: * (public) function pointer for UTextLength jpayne@69: * May be expensive to compute! jpayne@69: * jpayne@69: * @see UTextLength jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextNativeLength *nativeLength; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextAccess. jpayne@69: * jpayne@69: * @see UTextAccess jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextAccess *access; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextExtract. jpayne@69: * jpayne@69: * @see UTextExtract jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextExtract *extract; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextReplace. jpayne@69: * jpayne@69: * @see UTextReplace jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextReplace *replace; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextCopy. jpayne@69: * jpayne@69: * @see UTextCopy jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextCopy *copy; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextMapOffsetToNative. jpayne@69: * jpayne@69: * @see UTextMapOffsetToNative jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextMapOffsetToNative *mapOffsetToNative; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextMapNativeIndexToUTF16. jpayne@69: * jpayne@69: * @see UTextMapNativeIndexToUTF16 jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextMapNativeIndexToUTF16 *mapNativeIndexToUTF16; jpayne@69: jpayne@69: /** jpayne@69: * (public) Function pointer for UTextClose. jpayne@69: * jpayne@69: * @see UTextClose jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: UTextClose *close; jpayne@69: jpayne@69: /** jpayne@69: * (private) Spare function pointer jpayne@69: * @internal jpayne@69: */ jpayne@69: UTextClose *spare1; jpayne@69: jpayne@69: /** jpayne@69: * (private) Spare function pointer jpayne@69: * @internal jpayne@69: */ jpayne@69: UTextClose *spare2; jpayne@69: jpayne@69: /** jpayne@69: * (private) Spare function pointer jpayne@69: * @internal jpayne@69: */ jpayne@69: UTextClose *spare3; jpayne@69: jpayne@69: }; jpayne@69: /** jpayne@69: * Function dispatch table for UText jpayne@69: * @see UTextFuncs jpayne@69: */ jpayne@69: typedef struct UTextFuncs UTextFuncs; jpayne@69: jpayne@69: /** jpayne@69: * UText struct. Provides the interface between the generic UText access code jpayne@69: * and the UText provider code that works on specific kinds of jpayne@69: * text (UTF-8, noncontiguous UTF-16, whatever.) jpayne@69: * jpayne@69: * Applications that are using predefined types of text providers jpayne@69: * to pass text data to ICU services will have no need to view the jpayne@69: * internals of the UText structs that they open. jpayne@69: * jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: struct UText { jpayne@69: /** jpayne@69: * (private) Magic. Used to help detect when UText functions are handed jpayne@69: * invalid or uninitialized UText structs. jpayne@69: * utext_openXYZ() functions take an initialized, jpayne@69: * but not necessarily open, UText struct as an jpayne@69: * optional fill-in parameter. This magic field jpayne@69: * is used to check for that initialization. jpayne@69: * Text provider close functions must NOT clear jpayne@69: * the magic field because that would prevent jpayne@69: * reuse of the UText struct. jpayne@69: * @internal jpayne@69: */ jpayne@69: uint32_t magic; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (private) Flags for managing the allocation and freeing of jpayne@69: * memory associated with this UText. jpayne@69: * @internal jpayne@69: */ jpayne@69: int32_t flags; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Text provider properties. This set of flags is maintained by the jpayne@69: * text provider implementation. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int32_t providerProperties; jpayne@69: jpayne@69: /** jpayne@69: * (public) sizeOfStruct=sizeof(UText) jpayne@69: * Allows possible backward compatible extension. jpayne@69: * jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int32_t sizeOfStruct; jpayne@69: jpayne@69: /* ------ 16 byte alignment boundary ----------- */ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (protected) Native index of the first character position following jpayne@69: * the current chunk. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int64_t chunkNativeLimit; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Size in bytes of the extra space (pExtra). jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int32_t extraSize; jpayne@69: jpayne@69: /** jpayne@69: * (protected) The highest chunk offset where native indexing and jpayne@69: * chunk (UTF-16) indexing correspond. For UTF-16 sources, value jpayne@69: * will be equal to chunkLength. jpayne@69: * jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int32_t nativeIndexingLimit; jpayne@69: jpayne@69: /* ---- 16 byte alignment boundary------ */ jpayne@69: jpayne@69: /** jpayne@69: * (protected) Native index of the first character in the text chunk. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int64_t chunkNativeStart; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Current iteration position within the text chunk (UTF-16 buffer). jpayne@69: * This is the index to the character that will be returned by utext_next32(). jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int32_t chunkOffset; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Length the text chunk (UTF-16 buffer), in UChars. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: int32_t chunkLength; jpayne@69: jpayne@69: /* ---- 16 byte alignment boundary-- */ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (protected) pointer to a chunk of text in UTF-16 format. jpayne@69: * May refer either to original storage of the source of the text, or jpayne@69: * if conversion was required, to a buffer owned by the UText. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: const UChar *chunkContents; jpayne@69: jpayne@69: /** jpayne@69: * (public) Pointer to Dispatch table for accessing functions for this UText. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: const UTextFuncs *pFuncs; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Pointer to additional space requested by the jpayne@69: * text provider during the utext_open operation. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: void *pExtra; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Pointer to string or text-containing object or similar. jpayne@69: * This is the source of the text that this UText is wrapping, in a format jpayne@69: * that is known to the text provider functions. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: const void *context; jpayne@69: jpayne@69: /* --- 16 byte alignment boundary--- */ jpayne@69: jpayne@69: /** jpayne@69: * (protected) Pointer fields available for use by the text provider. jpayne@69: * Not used by UText common code. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: const void *p; jpayne@69: /** jpayne@69: * (protected) Pointer fields available for use by the text provider. jpayne@69: * Not used by UText common code. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: const void *q; jpayne@69: /** jpayne@69: * (protected) Pointer fields available for use by the text provider. jpayne@69: * Not used by UText common code. jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: const void *r; jpayne@69: jpayne@69: /** jpayne@69: * Private field reserved for future use by the UText framework jpayne@69: * itself. This is not to be touched by the text providers. jpayne@69: * @internal ICU 3.4 jpayne@69: */ jpayne@69: void *privP; jpayne@69: jpayne@69: jpayne@69: /* --- 16 byte alignment boundary--- */ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * (protected) Integer field reserved for use by the text provider. jpayne@69: * Not used by the UText framework, or by the client (user) of the UText. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int64_t a; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Integer field reserved for use by the text provider. jpayne@69: * Not used by the UText framework, or by the client (user) of the UText. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int32_t b; jpayne@69: jpayne@69: /** jpayne@69: * (protected) Integer field reserved for use by the text provider. jpayne@69: * Not used by the UText framework, or by the client (user) of the UText. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: int32_t c; jpayne@69: jpayne@69: /* ---- 16 byte alignment boundary---- */ jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Private field reserved for future use by the UText framework jpayne@69: * itself. This is not to be touched by the text providers. jpayne@69: * @internal ICU 3.4 jpayne@69: */ jpayne@69: int64_t privA; jpayne@69: /** jpayne@69: * Private field reserved for future use by the UText framework jpayne@69: * itself. This is not to be touched by the text providers. jpayne@69: * @internal ICU 3.4 jpayne@69: */ jpayne@69: int32_t privB; jpayne@69: /** jpayne@69: * Private field reserved for future use by the UText framework jpayne@69: * itself. This is not to be touched by the text providers. jpayne@69: * @internal ICU 3.4 jpayne@69: */ jpayne@69: int32_t privC; jpayne@69: }; jpayne@69: jpayne@69: jpayne@69: /** jpayne@69: * Common function for use by Text Provider implementations to allocate and/or initialize jpayne@69: * a new UText struct. To be called in the implementation of utext_open() functions. jpayne@69: * If the supplied UText parameter is null, a new UText struct will be allocated on the heap. jpayne@69: * If the supplied UText is already open, the provider's close function will be called jpayne@69: * so that the struct can be reused by the open that is in progress. jpayne@69: * jpayne@69: * @param ut pointer to a UText struct to be re-used, or null if a new UText jpayne@69: * should be allocated. jpayne@69: * @param extraSpace The amount of additional space to be allocated as part jpayne@69: * of this UText, for use by types of providers that require jpayne@69: * additional storage. jpayne@69: * @param status Errors are returned here. jpayne@69: * @return pointer to the UText, allocated if necessary, with extra space set up if requested. jpayne@69: * @stable ICU 3.4 jpayne@69: */ jpayne@69: U_STABLE UText * U_EXPORT2 jpayne@69: utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status); jpayne@69: jpayne@69: // do not use #ifndef U_HIDE_INTERNAL_API around the following! jpayne@69: /** jpayne@69: * @internal jpayne@69: * Value used to help identify correctly initialized UText structs. jpayne@69: * Note: must be publicly visible so that UTEXT_INITIALIZER can access it. jpayne@69: */ jpayne@69: enum { jpayne@69: UTEXT_MAGIC = 0x345ad82c jpayne@69: }; jpayne@69: jpayne@69: /** jpayne@69: * initializer to be used with local (stack) instances of a UText jpayne@69: * struct. UText structs must be initialized before passing jpayne@69: * them to one of the utext_open functions. jpayne@69: * jpayne@69: * @stable ICU 3.6 jpayne@69: */ jpayne@69: #define UTEXT_INITIALIZER { \ jpayne@69: UTEXT_MAGIC, /* magic */ \ jpayne@69: 0, /* flags */ \ jpayne@69: 0, /* providerProps */ \ jpayne@69: sizeof(UText), /* sizeOfStruct */ \ jpayne@69: 0, /* chunkNativeLimit */ \ jpayne@69: 0, /* extraSize */ \ jpayne@69: 0, /* nativeIndexingLimit */ \ jpayne@69: 0, /* chunkNativeStart */ \ jpayne@69: 0, /* chunkOffset */ \ jpayne@69: 0, /* chunkLength */ \ jpayne@69: NULL, /* chunkContents */ \ jpayne@69: NULL, /* pFuncs */ \ jpayne@69: NULL, /* pExtra */ \ jpayne@69: NULL, /* context */ \ jpayne@69: NULL, NULL, NULL, /* p, q, r */ \ jpayne@69: NULL, /* privP */ \ jpayne@69: 0, 0, 0, /* a, b, c */ \ jpayne@69: 0, 0, 0 /* privA,B,C, */ \ jpayne@69: } jpayne@69: jpayne@69: jpayne@69: U_CDECL_END jpayne@69: jpayne@69: jpayne@69: #if U_SHOW_CPLUSPLUS_API jpayne@69: jpayne@69: U_NAMESPACE_BEGIN jpayne@69: jpayne@69: /** jpayne@69: * \class LocalUTextPointer jpayne@69: * "Smart pointer" class, closes a UText via utext_close(). jpayne@69: * For most methods see the LocalPointerBase base class. jpayne@69: * jpayne@69: * @see LocalPointerBase jpayne@69: * @see LocalPointer jpayne@69: * @stable ICU 4.4 jpayne@69: */ jpayne@69: U_DEFINE_LOCAL_OPEN_POINTER(LocalUTextPointer, UText, utext_close); jpayne@69: jpayne@69: U_NAMESPACE_END jpayne@69: jpayne@69: #endif jpayne@69: jpayne@69: jpayne@69: #endif