michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2005-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: utext.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2005apr12 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/chariter.h" michael@0: #include "unicode/utext.h" michael@0: #include "unicode/utf.h" michael@0: #include "unicode/utf8.h" michael@0: #include "unicode/utf16.h" michael@0: #include "ustr_imp.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "uassert.h" michael@0: #include "putilimp.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: #define I32_FLAG(bitIndex) ((int32_t)1<<(bitIndex)) michael@0: michael@0: michael@0: static UBool michael@0: utext_access(UText *ut, int64_t index, UBool forward) { michael@0: return ut->pFuncs->access(ut, index, forward); michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: utext_moveIndex32(UText *ut, int32_t delta) { michael@0: UChar32 c; michael@0: if (delta > 0) { michael@0: do { michael@0: if(ut->chunkOffset>=ut->chunkLength && !utext_access(ut, ut->chunkNativeLimit, TRUE)) { michael@0: return FALSE; michael@0: } michael@0: c = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_SURROGATE(c)) { michael@0: c = utext_next32(ut); michael@0: if (c == U_SENTINEL) { michael@0: return FALSE; michael@0: } michael@0: } else { michael@0: ut->chunkOffset++; michael@0: } michael@0: } while(--delta>0); michael@0: michael@0: } else if (delta<0) { michael@0: do { michael@0: if(ut->chunkOffset<=0 && !utext_access(ut, ut->chunkNativeStart, FALSE)) { michael@0: return FALSE; michael@0: } michael@0: c = ut->chunkContents[ut->chunkOffset-1]; michael@0: if (U16_IS_SURROGATE(c)) { michael@0: c = utext_previous32(ut); michael@0: if (c == U_SENTINEL) { michael@0: return FALSE; michael@0: } michael@0: } else { michael@0: ut->chunkOffset--; michael@0: } michael@0: } while(++delta<0); michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: U_CAPI int64_t U_EXPORT2 michael@0: utext_nativeLength(UText *ut) { michael@0: return ut->pFuncs->nativeLength(ut); michael@0: } michael@0: michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: utext_isLengthExpensive(const UText *ut) { michael@0: UBool r = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE)) != 0; michael@0: return r; michael@0: } michael@0: michael@0: michael@0: U_CAPI int64_t U_EXPORT2 michael@0: utext_getNativeIndex(const UText *ut) { michael@0: if(ut->chunkOffset <= ut->nativeIndexingLimit) { michael@0: return ut->chunkNativeStart+ut->chunkOffset; michael@0: } else { michael@0: return ut->pFuncs->mapOffsetToNative(ut); michael@0: } michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: utext_setNativeIndex(UText *ut, int64_t index) { michael@0: if(indexchunkNativeStart || index>=ut->chunkNativeLimit) { michael@0: // The desired position is outside of the current chunk. michael@0: // Access the new position. Assume a forward iteration from here, michael@0: // which will also be optimimum for a single random access. michael@0: // Reverse iterations may suffer slightly. michael@0: ut->pFuncs->access(ut, index, TRUE); michael@0: } else if((int32_t)(index - ut->chunkNativeStart) <= ut->nativeIndexingLimit) { michael@0: // utf-16 indexing. michael@0: ut->chunkOffset=(int32_t)(index-ut->chunkNativeStart); michael@0: } else { michael@0: ut->chunkOffset=ut->pFuncs->mapNativeIndexToUTF16(ut, index); michael@0: } michael@0: // The convention is that the index must always be on a code point boundary. michael@0: // Adjust the index position if it is in the middle of a surrogate pair. michael@0: if (ut->chunkOffsetchunkLength) { michael@0: UChar c= ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_TRAIL(c)) { michael@0: if (ut->chunkOffset==0) { michael@0: ut->pFuncs->access(ut, ut->chunkNativeStart, FALSE); michael@0: } michael@0: if (ut->chunkOffset>0) { michael@0: UChar lead = ut->chunkContents[ut->chunkOffset-1]; michael@0: if (U16_IS_LEAD(lead)) { michael@0: ut->chunkOffset--; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI int64_t U_EXPORT2 michael@0: utext_getPreviousNativeIndex(UText *ut) { michael@0: // michael@0: // Fast-path the common case. michael@0: // Common means current position is not at the beginning of a chunk michael@0: // and the preceding character is not supplementary. michael@0: // michael@0: int32_t i = ut->chunkOffset - 1; michael@0: int64_t result; michael@0: if (i >= 0) { michael@0: UChar c = ut->chunkContents[i]; michael@0: if (U16_IS_TRAIL(c) == FALSE) { michael@0: if (i <= ut->nativeIndexingLimit) { michael@0: result = ut->chunkNativeStart + i; michael@0: } else { michael@0: ut->chunkOffset = i; michael@0: result = ut->pFuncs->mapOffsetToNative(ut); michael@0: ut->chunkOffset++; michael@0: } michael@0: return result; michael@0: } michael@0: } michael@0: michael@0: // If at the start of text, simply return 0. michael@0: if (ut->chunkOffset==0 && ut->chunkNativeStart==0) { michael@0: return 0; michael@0: } michael@0: michael@0: // Harder, less common cases. We are at a chunk boundary, or on a surrogate. michael@0: // Keep it simple, use other functions to handle the edges. michael@0: // michael@0: utext_previous32(ut); michael@0: result = UTEXT_GETNATIVEINDEX(ut); michael@0: utext_next32(ut); michael@0: return result; michael@0: } michael@0: michael@0: michael@0: // michael@0: // utext_current32. Get the UChar32 at the current position. michael@0: // UText iteration position is always on a code point boundary, michael@0: // never on the trail half of a surrogate pair. michael@0: // michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_current32(UText *ut) { michael@0: UChar32 c; michael@0: if (ut->chunkOffset==ut->chunkLength) { michael@0: // Current position is just off the end of the chunk. michael@0: if (ut->pFuncs->access(ut, ut->chunkNativeLimit, TRUE) == FALSE) { michael@0: // Off the end of the text. michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: c = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_LEAD(c) == FALSE) { michael@0: // Normal, non-supplementary case. michael@0: return c; michael@0: } michael@0: michael@0: // michael@0: // Possible supplementary char. michael@0: // michael@0: UChar32 trail = 0; michael@0: UChar32 supplementaryC = c; michael@0: if ((ut->chunkOffset+1) < ut->chunkLength) { michael@0: // The trail surrogate is in the same chunk. michael@0: trail = ut->chunkContents[ut->chunkOffset+1]; michael@0: } else { michael@0: // The trail surrogate is in a different chunk. michael@0: // Because we must maintain the iteration position, we need to switch forward michael@0: // into the new chunk, get the trail surrogate, then revert the chunk back to the michael@0: // original one. michael@0: // An edge case to be careful of: the entire text may end with an unpaired michael@0: // leading surrogate. The attempt to access the trail will fail, but michael@0: // the original position before the unpaired lead still needs to be restored. michael@0: int64_t nativePosition = ut->chunkNativeLimit; michael@0: int32_t originalOffset = ut->chunkOffset; michael@0: if (ut->pFuncs->access(ut, nativePosition, TRUE)) { michael@0: trail = ut->chunkContents[ut->chunkOffset]; michael@0: } michael@0: UBool r = ut->pFuncs->access(ut, nativePosition, FALSE); // reverse iteration flag loads preceding chunk michael@0: U_ASSERT(r==TRUE); michael@0: ut->chunkOffset = originalOffset; michael@0: if(!r) { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: if (U16_IS_TRAIL(trail)) { michael@0: supplementaryC = U16_GET_SUPPLEMENTARY(c, trail); michael@0: } michael@0: return supplementaryC; michael@0: michael@0: } michael@0: michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_char32At(UText *ut, int64_t nativeIndex) { michael@0: UChar32 c = U_SENTINEL; michael@0: michael@0: // Fast path the common case. michael@0: if (nativeIndex>=ut->chunkNativeStart && nativeIndex < ut->chunkNativeStart + ut->nativeIndexingLimit) { michael@0: ut->chunkOffset = (int32_t)(nativeIndex - ut->chunkNativeStart); michael@0: c = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_SURROGATE(c) == FALSE) { michael@0: return c; michael@0: } michael@0: } michael@0: michael@0: michael@0: utext_setNativeIndex(ut, nativeIndex); michael@0: if (nativeIndex>=ut->chunkNativeStart && ut->chunkOffsetchunkLength) { michael@0: c = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_SURROGATE(c)) { michael@0: // For surrogates, let current32() deal with the complications michael@0: // of supplementaries that may span chunk boundaries. michael@0: c = utext_current32(ut); michael@0: } michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_next32(UText *ut) { michael@0: UChar32 c; michael@0: michael@0: if (ut->chunkOffset >= ut->chunkLength) { michael@0: if (ut->pFuncs->access(ut, ut->chunkNativeLimit, TRUE) == FALSE) { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: c = ut->chunkContents[ut->chunkOffset++]; michael@0: if (U16_IS_LEAD(c) == FALSE) { michael@0: // Normal case, not supplementary. michael@0: // (A trail surrogate seen here is just returned as is, as a surrogate value. michael@0: // It cannot be part of a pair.) michael@0: return c; michael@0: } michael@0: michael@0: if (ut->chunkOffset >= ut->chunkLength) { michael@0: if (ut->pFuncs->access(ut, ut->chunkNativeLimit, TRUE) == FALSE) { michael@0: // c is an unpaired lead surrogate at the end of the text. michael@0: // return it as it is. michael@0: return c; michael@0: } michael@0: } michael@0: UChar32 trail = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_TRAIL(trail) == FALSE) { michael@0: // c was an unpaired lead surrogate, not at the end of the text. michael@0: // return it as it is (unpaired). Iteration position is on the michael@0: // following character, possibly in the next chunk, where the michael@0: // trail surrogate would have been if it had existed. michael@0: return c; michael@0: } michael@0: michael@0: UChar32 supplementary = U16_GET_SUPPLEMENTARY(c, trail); michael@0: ut->chunkOffset++; // move iteration position over the trail surrogate. michael@0: return supplementary; michael@0: } michael@0: michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_previous32(UText *ut) { michael@0: UChar32 c; michael@0: michael@0: if (ut->chunkOffset <= 0) { michael@0: if (ut->pFuncs->access(ut, ut->chunkNativeStart, FALSE) == FALSE) { michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: ut->chunkOffset--; michael@0: c = ut->chunkContents[ut->chunkOffset]; michael@0: if (U16_IS_TRAIL(c) == FALSE) { michael@0: // Normal case, not supplementary. michael@0: // (A lead surrogate seen here is just returned as is, as a surrogate value. michael@0: // It cannot be part of a pair.) michael@0: return c; michael@0: } michael@0: michael@0: if (ut->chunkOffset <= 0) { michael@0: if (ut->pFuncs->access(ut, ut->chunkNativeStart, FALSE) == FALSE) { michael@0: // c is an unpaired trail surrogate at the start of the text. michael@0: // return it as it is. michael@0: return c; michael@0: } michael@0: } michael@0: michael@0: UChar32 lead = ut->chunkContents[ut->chunkOffset-1]; michael@0: if (U16_IS_LEAD(lead) == FALSE) { michael@0: // c was an unpaired trail surrogate, not at the end of the text. michael@0: // return it as it is (unpaired). Iteration position is at c michael@0: return c; michael@0: } michael@0: michael@0: UChar32 supplementary = U16_GET_SUPPLEMENTARY(lead, c); michael@0: ut->chunkOffset--; // move iteration position over the lead surrogate. michael@0: return supplementary; michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_next32From(UText *ut, int64_t index) { michael@0: UChar32 c = U_SENTINEL; michael@0: michael@0: if(indexchunkNativeStart || index>=ut->chunkNativeLimit) { michael@0: // Desired position is outside of the current chunk. michael@0: if(!ut->pFuncs->access(ut, index, TRUE)) { michael@0: // no chunk available here michael@0: return U_SENTINEL; michael@0: } michael@0: } else if (index - ut->chunkNativeStart <= (int64_t)ut->nativeIndexingLimit) { michael@0: // Desired position is in chunk, with direct 1:1 native to UTF16 indexing michael@0: ut->chunkOffset = (int32_t)(index - ut->chunkNativeStart); michael@0: } else { michael@0: // Desired position is in chunk, with non-UTF16 indexing. michael@0: ut->chunkOffset = ut->pFuncs->mapNativeIndexToUTF16(ut, index); michael@0: } michael@0: michael@0: c = ut->chunkContents[ut->chunkOffset++]; michael@0: if (U16_IS_SURROGATE(c)) { michael@0: // Surrogates. Many edge cases. Use other functions that already michael@0: // deal with the problems. michael@0: utext_setNativeIndex(ut, index); michael@0: c = utext_next32(ut); michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: michael@0: U_CAPI UChar32 U_EXPORT2 michael@0: utext_previous32From(UText *ut, int64_t index) { michael@0: // michael@0: // Return the character preceding the specified index. michael@0: // Leave the iteration position at the start of the character that was returned. michael@0: // michael@0: UChar32 cPrev; // The character preceding cCurr, which is what we will return. michael@0: michael@0: // Address the chunk containg the position preceding the incoming index michael@0: // A tricky edge case: michael@0: // We try to test the requested native index against the chunkNativeStart to determine michael@0: // whether the character preceding the one at the index is in the current chunk. michael@0: // BUT, this test can fail with UTF-8 (or any other multibyte encoding), when the michael@0: // requested index is on something other than the first position of the first char. michael@0: // michael@0: if(index<=ut->chunkNativeStart || index>ut->chunkNativeLimit) { michael@0: // Requested native index is outside of the current chunk. michael@0: if(!ut->pFuncs->access(ut, index, FALSE)) { michael@0: // no chunk available here michael@0: return U_SENTINEL; michael@0: } michael@0: } else if(index - ut->chunkNativeStart <= (int64_t)ut->nativeIndexingLimit) { michael@0: // Direct UTF-16 indexing. michael@0: ut->chunkOffset = (int32_t)(index - ut->chunkNativeStart); michael@0: } else { michael@0: ut->chunkOffset=ut->pFuncs->mapNativeIndexToUTF16(ut, index); michael@0: if (ut->chunkOffset==0 && !ut->pFuncs->access(ut, index, FALSE)) { michael@0: // no chunk available here michael@0: return U_SENTINEL; michael@0: } michael@0: } michael@0: michael@0: // michael@0: // Simple case with no surrogates. michael@0: // michael@0: ut->chunkOffset--; michael@0: cPrev = ut->chunkContents[ut->chunkOffset]; michael@0: michael@0: if (U16_IS_SURROGATE(cPrev)) { michael@0: // Possible supplementary. Many edge cases. michael@0: // Let other functions do the heavy lifting. michael@0: utext_setNativeIndex(ut, index); michael@0: cPrev = utext_previous32(ut); michael@0: } michael@0: return cPrev; michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: utext_extract(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *status) { michael@0: return ut->pFuncs->extract(ut, start, limit, dest, destCapacity, status); michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: utext_equals(const UText *a, const UText *b) { michael@0: if (a==NULL || b==NULL || michael@0: a->magic != UTEXT_MAGIC || michael@0: b->magic != UTEXT_MAGIC) { michael@0: // Null or invalid arguments don't compare equal to anything. michael@0: return FALSE; michael@0: } michael@0: michael@0: if (a->pFuncs != b->pFuncs) { michael@0: // Different types of text providers. michael@0: return FALSE; michael@0: } michael@0: michael@0: if (a->context != b->context) { michael@0: // Different sources (different strings) michael@0: return FALSE; michael@0: } michael@0: if (utext_getNativeIndex(a) != utext_getNativeIndex(b)) { michael@0: // Different current position in the string. michael@0: return FALSE; michael@0: } michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: utext_isWritable(const UText *ut) michael@0: { michael@0: UBool b = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) != 0; michael@0: return b; michael@0: } michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: utext_freeze(UText *ut) { michael@0: // Zero out the WRITABLE flag. michael@0: ut->providerProperties &= ~(I32_FLAG(UTEXT_PROVIDER_WRITABLE)); michael@0: } michael@0: michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: utext_hasMetaData(const UText *ut) michael@0: { michael@0: UBool b = (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_HAS_META_DATA)) != 0; michael@0: return b; michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: utext_replace(UText *ut, michael@0: int64_t nativeStart, int64_t nativeLimit, michael@0: const UChar *replacementText, int32_t replacementLength, michael@0: UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if ((ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) == 0) { michael@0: *status = U_NO_WRITE_PERMISSION; michael@0: return 0; michael@0: } michael@0: int32_t i = ut->pFuncs->replace(ut, nativeStart, nativeLimit, replacementText, replacementLength, status); michael@0: return i; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: utext_copy(UText *ut, michael@0: int64_t nativeStart, int64_t nativeLimit, michael@0: int64_t destIndex, michael@0: UBool move, michael@0: UErrorCode *status) michael@0: { michael@0: if (U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if ((ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_WRITABLE)) == 0) { michael@0: *status = U_NO_WRITE_PERMISSION; michael@0: return; michael@0: } michael@0: ut->pFuncs->copy(ut, nativeStart, nativeLimit, destIndex, move, status); michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_clone(UText *dest, const UText *src, UBool deep, UBool readOnly, UErrorCode *status) { michael@0: UText *result; michael@0: result = src->pFuncs->clone(dest, src, deep, status); michael@0: if (readOnly) { michael@0: utext_freeze(result); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText common functions implementation michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: // michael@0: // UText.flags bit definitions michael@0: // michael@0: enum { michael@0: UTEXT_HEAP_ALLOCATED = 1, // 1 if ICU has allocated this UText struct on the heap. michael@0: // 0 if caller provided storage for the UText. michael@0: michael@0: UTEXT_EXTRA_HEAP_ALLOCATED = 2, // 1 if ICU has allocated extra storage as a separate michael@0: // heap block. michael@0: // 0 if there is no separate allocation. Either no extra michael@0: // storage was requested, or it is appended to the end michael@0: // of the main UText storage. michael@0: michael@0: UTEXT_OPEN = 4 // 1 if this UText is currently open michael@0: // 0 if this UText is not open. michael@0: }; michael@0: michael@0: michael@0: // michael@0: // Extended form of a UText. The purpose is to aid in computing the total size required michael@0: // when a provider asks for a UText to be allocated with extra storage. michael@0: michael@0: struct ExtendedUText { michael@0: UText ut; michael@0: UAlignedMemory extension; michael@0: }; michael@0: michael@0: static const UText emptyText = UTEXT_INITIALIZER; michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return ut; michael@0: } michael@0: michael@0: if (ut == NULL) { michael@0: // We need to heap-allocate storage for the new UText michael@0: int32_t spaceRequired = sizeof(UText); michael@0: if (extraSpace > 0) { michael@0: spaceRequired = sizeof(ExtendedUText) + extraSpace - sizeof(UAlignedMemory); michael@0: } michael@0: ut = (UText *)uprv_malloc(spaceRequired); michael@0: if (ut == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return NULL; michael@0: } else { michael@0: *ut = emptyText; michael@0: ut->flags |= UTEXT_HEAP_ALLOCATED; michael@0: if (spaceRequired>0) { michael@0: ut->extraSize = extraSpace; michael@0: ut->pExtra = &((ExtendedUText *)ut)->extension; michael@0: } michael@0: } michael@0: } else { michael@0: // We have been supplied with an already existing UText. michael@0: // Verify that it really appears to be a UText. michael@0: if (ut->magic != UTEXT_MAGIC) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return ut; michael@0: } michael@0: // If the ut is already open and there's a provider supplied close michael@0: // function, call it. michael@0: if ((ut->flags & UTEXT_OPEN) && ut->pFuncs->close != NULL) { michael@0: ut->pFuncs->close(ut); michael@0: } michael@0: ut->flags &= ~UTEXT_OPEN; michael@0: michael@0: // If extra space was requested by our caller, check whether michael@0: // sufficient already exists, and allocate new if needed. michael@0: if (extraSpace > ut->extraSize) { michael@0: // Need more space. If there is existing separately allocated space, michael@0: // delete it first, then allocate new space. michael@0: if (ut->flags & UTEXT_EXTRA_HEAP_ALLOCATED) { michael@0: uprv_free(ut->pExtra); michael@0: ut->extraSize = 0; michael@0: } michael@0: ut->pExtra = uprv_malloc(extraSpace); michael@0: if (ut->pExtra == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: ut->extraSize = extraSpace; michael@0: ut->flags |= UTEXT_EXTRA_HEAP_ALLOCATED; michael@0: } michael@0: } michael@0: } michael@0: if (U_SUCCESS(*status)) { michael@0: ut->flags |= UTEXT_OPEN; michael@0: michael@0: // Initialize all remaining fields of the UText. michael@0: // michael@0: ut->context = NULL; michael@0: ut->chunkContents = NULL; michael@0: ut->p = NULL; michael@0: ut->q = NULL; michael@0: ut->r = NULL; michael@0: ut->a = 0; michael@0: ut->b = 0; michael@0: ut->c = 0; michael@0: ut->chunkOffset = 0; michael@0: ut->chunkLength = 0; michael@0: ut->chunkNativeStart = 0; michael@0: ut->chunkNativeLimit = 0; michael@0: ut->nativeIndexingLimit = 0; michael@0: ut->providerProperties = 0; michael@0: ut->privA = 0; michael@0: ut->privB = 0; michael@0: ut->privC = 0; michael@0: ut->privP = NULL; michael@0: if (ut->pExtra!=NULL && ut->extraSize>0) michael@0: uprv_memset(ut->pExtra, 0, ut->extraSize); michael@0: michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_close(UText *ut) { michael@0: if (ut==NULL || michael@0: ut->magic != UTEXT_MAGIC || michael@0: (ut->flags & UTEXT_OPEN) == 0) michael@0: { michael@0: // The supplied ut is not an open UText. michael@0: // Do nothing. michael@0: return ut; michael@0: } michael@0: michael@0: // If the provider gave us a close function, call it now. michael@0: // This will clean up anything allocated specifically by the provider. michael@0: if (ut->pFuncs->close != NULL) { michael@0: ut->pFuncs->close(ut); michael@0: } michael@0: ut->flags &= ~UTEXT_OPEN; michael@0: michael@0: // If we (the framework) allocated the UText or subsidiary storage, michael@0: // delete it. michael@0: if (ut->flags & UTEXT_EXTRA_HEAP_ALLOCATED) { michael@0: uprv_free(ut->pExtra); michael@0: ut->pExtra = NULL; michael@0: ut->flags &= ~UTEXT_EXTRA_HEAP_ALLOCATED; michael@0: ut->extraSize = 0; michael@0: } michael@0: michael@0: // Zero out function table of the closed UText. This is a defensive move, michael@0: // inteded to cause applications that inadvertantly use a closed michael@0: // utext to crash with null pointer errors. michael@0: ut->pFuncs = NULL; michael@0: michael@0: if (ut->flags & UTEXT_HEAP_ALLOCATED) { michael@0: // This UText was allocated by UText setup. We need to free it. michael@0: // Clear magic, so we can detect if the user messes up and immediately michael@0: // tries to reopen another UText using the deleted storage. michael@0: ut->magic = 0; michael@0: uprv_free(ut); michael@0: ut = NULL; michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: // michael@0: // invalidateChunk Reset a chunk to have no contents, so that the next call michael@0: // to access will cause new data to load. michael@0: // This is needed when copy/move/replace operate directly on the michael@0: // backing text, potentially putting it out of sync with the michael@0: // contents in the chunk. michael@0: // michael@0: static void michael@0: invalidateChunk(UText *ut) { michael@0: ut->chunkLength = 0; michael@0: ut->chunkNativeLimit = 0; michael@0: ut->chunkNativeStart = 0; michael@0: ut->chunkOffset = 0; michael@0: ut->nativeIndexingLimit = 0; michael@0: } michael@0: michael@0: // michael@0: // pinIndex Do range pinning on a native index parameter. michael@0: // 64 bit pinning is done in place. michael@0: // 32 bit truncated result is returned as a convenience for michael@0: // use in providers that don't need 64 bits. michael@0: static int32_t michael@0: pinIndex(int64_t &index, int64_t limit) { michael@0: if (index<0) { michael@0: index = 0; michael@0: } else if (index > limit) { michael@0: index = limit; michael@0: } michael@0: return (int32_t)index; michael@0: } michael@0: michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: // michael@0: // Pointer relocation function, michael@0: // a utility used by shallow clone. michael@0: // Adjust a pointer that refers to something within one UText (the source) michael@0: // to refer to the same relative offset within a another UText (the target) michael@0: // michael@0: static void adjustPointer(UText *dest, const void **destPtr, const UText *src) { michael@0: // convert all pointers to (char *) so that byte address arithmetic will work. michael@0: char *dptr = (char *)*destPtr; michael@0: char *dUText = (char *)dest; michael@0: char *sUText = (char *)src; michael@0: michael@0: if (dptr >= (char *)src->pExtra && dptr < ((char*)src->pExtra)+src->extraSize) { michael@0: // target ptr was to something within the src UText's pExtra storage. michael@0: // relocate it into the target UText's pExtra region. michael@0: *destPtr = ((char *)dest->pExtra) + (dptr - (char *)src->pExtra); michael@0: } else if (dptr>=sUText && dptr < sUText+src->sizeOfStruct) { michael@0: // target ptr was pointing to somewhere within the source UText itself. michael@0: // Move it to the same offset within the target UText. michael@0: *destPtr = dUText + (dptr-sUText); michael@0: } michael@0: } michael@0: michael@0: michael@0: // michael@0: // Clone. This is a generic copy-the-utext-by-value clone function that can be michael@0: // used as-is with some utext types, and as a helper by other clones. michael@0: // michael@0: static UText * U_CALLCONV michael@0: shallowTextClone(UText * dest, const UText * src, UErrorCode * status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: int32_t srcExtraSize = src->extraSize; michael@0: michael@0: // michael@0: // Use the generic text_setup to allocate storage if required. michael@0: // michael@0: dest = utext_setup(dest, srcExtraSize, status); michael@0: if (U_FAILURE(*status)) { michael@0: return dest; michael@0: } michael@0: michael@0: // michael@0: // flags (how the UText was allocated) and the pointer to the michael@0: // extra storage must retain the values in the cloned utext that michael@0: // were set up by utext_setup. Save them separately before michael@0: // copying the whole struct. michael@0: // michael@0: void *destExtra = dest->pExtra; michael@0: int32_t flags = dest->flags; michael@0: michael@0: michael@0: // michael@0: // Copy the whole UText struct by value. michael@0: // Any "Extra" storage is copied also. michael@0: // michael@0: int sizeToCopy = src->sizeOfStruct; michael@0: if (sizeToCopy > dest->sizeOfStruct) { michael@0: sizeToCopy = dest->sizeOfStruct; michael@0: } michael@0: uprv_memcpy(dest, src, sizeToCopy); michael@0: dest->pExtra = destExtra; michael@0: dest->flags = flags; michael@0: if (srcExtraSize > 0) { michael@0: uprv_memcpy(dest->pExtra, src->pExtra, srcExtraSize); michael@0: } michael@0: michael@0: // michael@0: // Relocate any pointers in the target that refer to the UText itself michael@0: // to point to the cloned copy rather than the original source. michael@0: // michael@0: adjustPointer(dest, &dest->context, src); michael@0: adjustPointer(dest, &dest->p, src); michael@0: adjustPointer(dest, &dest->q, src); michael@0: adjustPointer(dest, &dest->r, src); michael@0: adjustPointer(dest, (const void **)&dest->chunkContents, src); michael@0: michael@0: return dest; michael@0: } michael@0: michael@0: michael@0: U_CDECL_END michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText implementation for UTF-8 char * strings (read-only) michael@0: // Limitation: string length must be <= 0x7fffffff in length. michael@0: // (length must for in an int32_t variable) michael@0: // michael@0: // Use of UText data members: michael@0: // context pointer to UTF-8 string michael@0: // utext.b is the input string length (bytes). michael@0: // utext.c Length scanned so far in string michael@0: // (for optimizing finding length of zero terminated strings.) michael@0: // utext.p pointer to the current buffer michael@0: // utext.q pointer to the other buffer. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: // Chunk size. michael@0: // Must be less than 85, because of byte mapping from UChar indexes to native indexes. michael@0: // Worst case is three native bytes to one UChar. (Supplemenaries are 4 native bytes michael@0: // to two UChars.) michael@0: // michael@0: enum { UTF8_TEXT_CHUNK_SIZE=32 }; michael@0: michael@0: // michael@0: // UTF8Buf Two of these structs will be set up in the UText's extra allocated space. michael@0: // Each contains the UChar chunk buffer, the to and from native maps, and michael@0: // header info. michael@0: // michael@0: // because backwards iteration fills the buffers starting at the end and michael@0: // working towards the front, the filled part of the buffers may not begin michael@0: // at the start of the available storage for the buffers. michael@0: // michael@0: // Buffer size is one bigger than the specified UTF8_TEXT_CHUNK_SIZE to allow for michael@0: // the last character added being a supplementary, and thus requiring a surrogate michael@0: // pair. Doing this is simpler than checking for the edge case. michael@0: // michael@0: michael@0: struct UTF8Buf { michael@0: int32_t bufNativeStart; // Native index of first char in UChar buf michael@0: int32_t bufNativeLimit; // Native index following last char in buf. michael@0: int32_t bufStartIdx; // First filled position in buf. michael@0: int32_t bufLimitIdx; // Limit of filled range in buf. michael@0: int32_t bufNILimit; // Limit of native indexing part of buf michael@0: int32_t toUCharsMapStart; // Native index corresponding to michael@0: // mapToUChars[0]. michael@0: // Set to bufNativeStart when filling forwards. michael@0: // Set to computed value when filling backwards. michael@0: michael@0: UChar buf[UTF8_TEXT_CHUNK_SIZE+4]; // The UChar buffer. Requires one extra position beyond the michael@0: // the chunk size, to allow for surrogate at the end. michael@0: // Length must be identical to mapToNative array, below, michael@0: // because of the way indexing works when the array is michael@0: // filled backwards during a reverse iteration. Thus, michael@0: // the additional extra size. michael@0: uint8_t mapToNative[UTF8_TEXT_CHUNK_SIZE+4]; // map UChar index in buf to michael@0: // native offset from bufNativeStart. michael@0: // Requires two extra slots, michael@0: // one for a supplementary starting in the last normal position, michael@0: // and one for an entry for the buffer limit position. michael@0: uint8_t mapToUChars[UTF8_TEXT_CHUNK_SIZE*3+6]; // Map native offset from bufNativeStart to michael@0: // correspoding offset in filled part of buf. michael@0: int32_t align; michael@0: }; michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: // michael@0: // utf8TextLength michael@0: // michael@0: // Get the length of the string. If we don't already know it, michael@0: // we'll need to scan for the trailing nul. michael@0: // michael@0: static int64_t U_CALLCONV michael@0: utf8TextLength(UText *ut) { michael@0: if (ut->b < 0) { michael@0: // Zero terminated string, and we haven't scanned to the end yet. michael@0: // Scan it now. michael@0: const char *r = (const char *)ut->context + ut->c; michael@0: while (*r != 0) { michael@0: r++; michael@0: } michael@0: if ((r - (const char *)ut->context) < 0x7fffffff) { michael@0: ut->b = (int32_t)(r - (const char *)ut->context); michael@0: } else { michael@0: // Actual string was bigger (more than 2 gig) than we michael@0: // can handle. Clip it to 2 GB. michael@0: ut->b = 0x7fffffff; michael@0: } michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: return ut->b; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: static UBool U_CALLCONV michael@0: utf8TextAccess(UText *ut, int64_t index, UBool forward) { michael@0: // michael@0: // Apologies to those who are allergic to goto statements. michael@0: // Consider each goto to a labelled block to be the equivalent of michael@0: // call the named block as if it were a function(); michael@0: // return; michael@0: // michael@0: const uint8_t *s8=(const uint8_t *)ut->context; michael@0: UTF8Buf *u8b = NULL; michael@0: int32_t length = ut->b; // Length of original utf-8 michael@0: int32_t ix= (int32_t)index; // Requested index, trimmed to 32 bits. michael@0: int32_t mapIndex = 0; michael@0: if (index<0) { michael@0: ix=0; michael@0: } else if (index > 0x7fffffff) { michael@0: // Strings with 64 bit lengths not supported by this UTF-8 provider. michael@0: ix = 0x7fffffff; michael@0: } michael@0: michael@0: // Pin requested index to the string length. michael@0: if (ix>length) { michael@0: if (length>=0) { michael@0: ix=length; michael@0: } else if (ix>=ut->c) { michael@0: // Zero terminated string, and requested index is beyond michael@0: // the region that has already been scanned. michael@0: // Scan up to either the end of the string or to the michael@0: // requested position, whichever comes first. michael@0: while (ut->cc]!=0) { michael@0: ut->c++; michael@0: } michael@0: // TODO: support for null terminated string length > 32 bits. michael@0: if (s8[ut->c] == 0) { michael@0: // We just found the actual length of the string. michael@0: // Trim the requested index back to that. michael@0: ix = ut->c; michael@0: ut->b = ut->c; michael@0: length = ut->c; michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // michael@0: // Dispatch to the appropriate action for a forward iteration request. michael@0: // michael@0: if (forward) { michael@0: if (ix==ut->chunkNativeLimit) { michael@0: // Check for normal sequential iteration cases first. michael@0: if (ix==length) { michael@0: // Just reached end of string michael@0: // Don't swap buffers, but do set the michael@0: // current buffer position. michael@0: ut->chunkOffset = ut->chunkLength; michael@0: return FALSE; michael@0: } else { michael@0: // End of current buffer. michael@0: // check whether other buffer already has what we need. michael@0: UTF8Buf *altB = (UTF8Buf *)ut->q; michael@0: if (ix>=altB->bufNativeStart && ixbufNativeLimit) { michael@0: goto swapBuffers; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // A random access. Desired index could be in either or niether buf. michael@0: // For optimizing the order of testing, first check for the index michael@0: // being in the other buffer. This will be the case for uses that michael@0: // move back and forth over a fairly limited range michael@0: { michael@0: u8b = (UTF8Buf *)ut->q; // the alternate buffer michael@0: if (ix>=u8b->bufNativeStart && ixbufNativeLimit) { michael@0: // Requested index is in the other buffer. michael@0: goto swapBuffers; michael@0: } michael@0: if (ix == length) { michael@0: // Requested index is end-of-string. michael@0: // (this is the case of randomly seeking to the end. michael@0: // The case of iterating off the end is handled earlier.) michael@0: if (ix == ut->chunkNativeLimit) { michael@0: // Current buffer extends up to the end of the string. michael@0: // Leave it as the current buffer. michael@0: ut->chunkOffset = ut->chunkLength; michael@0: return FALSE; michael@0: } michael@0: if (ix == u8b->bufNativeLimit) { michael@0: // Alternate buffer extends to the end of string. michael@0: // Swap it in as the current buffer. michael@0: goto swapBuffersAndFail; michael@0: } michael@0: michael@0: // Neither existing buffer extends to the end of the string. michael@0: goto makeStubBuffer; michael@0: } michael@0: michael@0: if (ixchunkNativeStart || ix>=ut->chunkNativeLimit) { michael@0: // Requested index is in neither buffer. michael@0: goto fillForward; michael@0: } michael@0: michael@0: // Requested index is in this buffer. michael@0: u8b = (UTF8Buf *)ut->p; // the current buffer michael@0: mapIndex = ix - u8b->toUCharsMapStart; michael@0: ut->chunkOffset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx; michael@0: return TRUE; michael@0: michael@0: } michael@0: } michael@0: michael@0: michael@0: // michael@0: // Dispatch to the appropriate action for a michael@0: // Backwards Diretion iteration request. michael@0: // michael@0: if (ix==ut->chunkNativeStart) { michael@0: // Check for normal sequential iteration cases first. michael@0: if (ix==0) { michael@0: // Just reached the start of string michael@0: // Don't swap buffers, but do set the michael@0: // current buffer position. michael@0: ut->chunkOffset = 0; michael@0: return FALSE; michael@0: } else { michael@0: // Start of current buffer. michael@0: // check whether other buffer already has what we need. michael@0: UTF8Buf *altB = (UTF8Buf *)ut->q; michael@0: if (ix>altB->bufNativeStart && ix<=altB->bufNativeLimit) { michael@0: goto swapBuffers; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // A random access. Desired index could be in either or niether buf. michael@0: // For optimizing the order of testing, michael@0: // Most likely case: in the other buffer. michael@0: // Second most likely: in neither buffer. michael@0: // Unlikely, but must work: in the current buffer. michael@0: u8b = (UTF8Buf *)ut->q; // the alternate buffer michael@0: if (ix>u8b->bufNativeStart && ix<=u8b->bufNativeLimit) { michael@0: // Requested index is in the other buffer. michael@0: goto swapBuffers; michael@0: } michael@0: // Requested index is start-of-string. michael@0: // (this is the case of randomly seeking to the start. michael@0: // The case of iterating off the start is handled earlier.) michael@0: if (ix==0) { michael@0: if (u8b->bufNativeStart==0) { michael@0: // Alternate buffer contains the data for the start string. michael@0: // Make it be the current buffer. michael@0: goto swapBuffersAndFail; michael@0: } else { michael@0: // Request for data before the start of string, michael@0: // neither buffer is usable. michael@0: // set up a zero-length buffer. michael@0: goto makeStubBuffer; michael@0: } michael@0: } michael@0: michael@0: if (ix<=ut->chunkNativeStart || ix>ut->chunkNativeLimit) { michael@0: // Requested index is in neither buffer. michael@0: goto fillReverse; michael@0: } michael@0: michael@0: // Requested index is in this buffer. michael@0: // Set the utf16 buffer index. michael@0: u8b = (UTF8Buf *)ut->p; michael@0: mapIndex = ix - u8b->toUCharsMapStart; michael@0: ut->chunkOffset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx; michael@0: if (ut->chunkOffset==0) { michael@0: // This occurs when the first character in the text is michael@0: // a multi-byte UTF-8 char, and the requested index is to michael@0: // one of the trailing bytes. Because there is no preceding , michael@0: // character, this access fails. We can't pick up on the michael@0: // situation sooner because the requested index is not zero. michael@0: return FALSE; michael@0: } else { michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: michael@0: swapBuffers: michael@0: // The alternate buffer (ut->q) has the string data that was requested. michael@0: // Swap the primary and alternate buffers, and set the michael@0: // chunk index into the new primary buffer. michael@0: { michael@0: u8b = (UTF8Buf *)ut->q; michael@0: ut->q = ut->p; michael@0: ut->p = u8b; michael@0: ut->chunkContents = &u8b->buf[u8b->bufStartIdx]; michael@0: ut->chunkLength = u8b->bufLimitIdx - u8b->bufStartIdx; michael@0: ut->chunkNativeStart = u8b->bufNativeStart; michael@0: ut->chunkNativeLimit = u8b->bufNativeLimit; michael@0: ut->nativeIndexingLimit = u8b->bufNILimit; michael@0: michael@0: // Index into the (now current) chunk michael@0: // Use the map to set the chunk index. It's more trouble than it's worth michael@0: // to check whether native indexing can be used. michael@0: U_ASSERT(ix>=u8b->bufNativeStart); michael@0: U_ASSERT(ix<=u8b->bufNativeLimit); michael@0: mapIndex = ix - u8b->toUCharsMapStart; michael@0: U_ASSERT(mapIndex>=0); michael@0: U_ASSERT(mapIndex<(int32_t)sizeof(u8b->mapToUChars)); michael@0: ut->chunkOffset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: swapBuffersAndFail: michael@0: // We got a request for either the start or end of the string, michael@0: // with iteration continuing in the out-of-bounds direction. michael@0: // The alternate buffer already contains the data up to the michael@0: // start/end. michael@0: // Swap the buffers, then return failure, indicating that we couldn't michael@0: // make things correct for continuing the iteration in the requested michael@0: // direction. The position & buffer are correct should the michael@0: // user decide to iterate in the opposite direction. michael@0: u8b = (UTF8Buf *)ut->q; michael@0: ut->q = ut->p; michael@0: ut->p = u8b; michael@0: ut->chunkContents = &u8b->buf[u8b->bufStartIdx]; michael@0: ut->chunkLength = u8b->bufLimitIdx - u8b->bufStartIdx; michael@0: ut->chunkNativeStart = u8b->bufNativeStart; michael@0: ut->chunkNativeLimit = u8b->bufNativeLimit; michael@0: ut->nativeIndexingLimit = u8b->bufNILimit; michael@0: michael@0: // Index into the (now current) chunk michael@0: // For this function (swapBuffersAndFail), the requested index michael@0: // will always be at either the start or end of the chunk. michael@0: if (ix==u8b->bufNativeLimit) { michael@0: ut->chunkOffset = ut->chunkLength; michael@0: } else { michael@0: ut->chunkOffset = 0; michael@0: U_ASSERT(ix == u8b->bufNativeStart); michael@0: } michael@0: return FALSE; michael@0: michael@0: makeStubBuffer: michael@0: // The user has done a seek/access past the start or end michael@0: // of the string. Rather than loading data that is likely michael@0: // to never be used, just set up a zero-length buffer at michael@0: // the position. michael@0: u8b = (UTF8Buf *)ut->q; michael@0: u8b->bufNativeStart = ix; michael@0: u8b->bufNativeLimit = ix; michael@0: u8b->bufStartIdx = 0; michael@0: u8b->bufLimitIdx = 0; michael@0: u8b->bufNILimit = 0; michael@0: u8b->toUCharsMapStart = ix; michael@0: u8b->mapToNative[0] = 0; michael@0: u8b->mapToUChars[0] = 0; michael@0: goto swapBuffersAndFail; michael@0: michael@0: michael@0: michael@0: fillForward: michael@0: { michael@0: // Move the incoming index to a code point boundary. michael@0: U8_SET_CP_START(s8, 0, ix); michael@0: michael@0: // Swap the UText buffers. michael@0: // We want to fill what was previously the alternate buffer, michael@0: // and make what was the current buffer be the new alternate. michael@0: UTF8Buf *u8b = (UTF8Buf *)ut->q; michael@0: ut->q = ut->p; michael@0: ut->p = u8b; michael@0: michael@0: int32_t strLen = ut->b; michael@0: UBool nulTerminated = FALSE; michael@0: if (strLen < 0) { michael@0: strLen = 0x7fffffff; michael@0: nulTerminated = TRUE; michael@0: } michael@0: michael@0: UChar *buf = u8b->buf; michael@0: uint8_t *mapToNative = u8b->mapToNative; michael@0: uint8_t *mapToUChars = u8b->mapToUChars; michael@0: int32_t destIx = 0; michael@0: int32_t srcIx = ix; michael@0: UBool seenNonAscii = FALSE; michael@0: UChar32 c = 0; michael@0: michael@0: // Fill the chunk buffer and mapping arrays. michael@0: while (destIx0 && c<0x80) { michael@0: // Special case ASCII range for speed. michael@0: // zero is excluded to simplify bounds checking. michael@0: buf[destIx] = (UChar)c; michael@0: mapToNative[destIx] = (uint8_t)(srcIx - ix); michael@0: mapToUChars[srcIx-ix] = (uint8_t)destIx; michael@0: srcIx++; michael@0: destIx++; michael@0: } else { michael@0: // General case, handle everything. michael@0: if (seenNonAscii == FALSE) { michael@0: seenNonAscii = TRUE; michael@0: u8b->bufNILimit = destIx; michael@0: } michael@0: michael@0: int32_t cIx = srcIx; michael@0: int32_t dIx = destIx; michael@0: int32_t dIxSaved = destIx; michael@0: U8_NEXT_OR_FFFD(s8, srcIx, strLen, c); michael@0: if (c==0 && nulTerminated) { michael@0: srcIx--; michael@0: break; michael@0: } michael@0: michael@0: U16_APPEND_UNSAFE(buf, destIx, c); michael@0: do { michael@0: mapToNative[dIx++] = (uint8_t)(cIx - ix); michael@0: } while (dIx < destIx); michael@0: michael@0: do { michael@0: mapToUChars[cIx++ - ix] = (uint8_t)dIxSaved; michael@0: } while (cIx < srcIx); michael@0: } michael@0: if (srcIx>=strLen) { michael@0: break; michael@0: } michael@0: michael@0: } michael@0: michael@0: // store Native <--> Chunk Map entries for the end of the buffer. michael@0: // There is no actual character here, but the index position is valid. michael@0: mapToNative[destIx] = (uint8_t)(srcIx - ix); michael@0: mapToUChars[srcIx - ix] = (uint8_t)destIx; michael@0: michael@0: // fill in Buffer descriptor michael@0: u8b->bufNativeStart = ix; michael@0: u8b->bufNativeLimit = srcIx; michael@0: u8b->bufStartIdx = 0; michael@0: u8b->bufLimitIdx = destIx; michael@0: if (seenNonAscii == FALSE) { michael@0: u8b->bufNILimit = destIx; michael@0: } michael@0: u8b->toUCharsMapStart = u8b->bufNativeStart; michael@0: michael@0: // Set UText chunk to refer to this buffer. michael@0: ut->chunkContents = buf; michael@0: ut->chunkOffset = 0; michael@0: ut->chunkLength = u8b->bufLimitIdx; michael@0: ut->chunkNativeStart = u8b->bufNativeStart; michael@0: ut->chunkNativeLimit = u8b->bufNativeLimit; michael@0: ut->nativeIndexingLimit = u8b->bufNILimit; michael@0: michael@0: // For zero terminated strings, keep track of the maximum point michael@0: // scanned so far. michael@0: if (nulTerminated && srcIx>ut->c) { michael@0: ut->c = srcIx; michael@0: if (c==0) { michael@0: // We scanned to the end. michael@0: // Remember the actual length. michael@0: ut->b = srcIx; michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: fillReverse: michael@0: { michael@0: // Move the incoming index to a code point boundary. michael@0: // Can only do this if the incoming index is somewhere in the interior of the string. michael@0: // If index is at the end, there is no character there to look at. michael@0: if (ix != ut->b) { michael@0: U8_SET_CP_START(s8, 0, ix); michael@0: } michael@0: michael@0: // Swap the UText buffers. michael@0: // We want to fill what was previously the alternate buffer, michael@0: // and make what was the current buffer be the new alternate. michael@0: UTF8Buf *u8b = (UTF8Buf *)ut->q; michael@0: ut->q = ut->p; michael@0: ut->p = u8b; michael@0: michael@0: UChar *buf = u8b->buf; michael@0: uint8_t *mapToNative = u8b->mapToNative; michael@0: uint8_t *mapToUChars = u8b->mapToUChars; michael@0: int32_t toUCharsMapStart = ix - (UTF8_TEXT_CHUNK_SIZE*3 + 1); michael@0: int32_t destIx = UTF8_TEXT_CHUNK_SIZE+2; // Start in the overflow region michael@0: // at end of buffer to leave room michael@0: // for a surrogate pair at the michael@0: // buffer start. michael@0: int32_t srcIx = ix; michael@0: int32_t bufNILimit = destIx; michael@0: UChar32 c; michael@0: michael@0: // Map to/from Native Indexes, fill in for the position at the end of michael@0: // the buffer. michael@0: // michael@0: mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart); michael@0: mapToUChars[srcIx - toUCharsMapStart] = (uint8_t)destIx; michael@0: michael@0: // Fill the chunk buffer michael@0: // Work backwards, filling from the end of the buffer towards the front. michael@0: // michael@0: while (destIx>2 && (srcIx - toUCharsMapStart > 5) && (srcIx > 0)) { michael@0: srcIx--; michael@0: destIx--; michael@0: michael@0: // Get last byte of the UTF-8 character michael@0: c = s8[srcIx]; michael@0: if (c<0x80) { michael@0: // Special case ASCII range for speed. michael@0: buf[destIx] = (UChar)c; michael@0: mapToUChars[srcIx - toUCharsMapStart] = (uint8_t)destIx; michael@0: mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart); michael@0: } else { michael@0: // General case, handle everything non-ASCII. michael@0: michael@0: int32_t sIx = srcIx; // ix of last byte of multi-byte u8 char michael@0: michael@0: // Get the full character from the UTF8 string. michael@0: // use code derived from tbe macros in utf8.h michael@0: // Leaves srcIx pointing at the first byte of the UTF-8 char. michael@0: // michael@0: c=utf8_prevCharSafeBody(s8, 0, &srcIx, c, -3); michael@0: // leaves srcIx at first byte of the multi-byte char. michael@0: michael@0: // Store the character in UTF-16 buffer. michael@0: if (c<0x10000) { michael@0: buf[destIx] = (UChar)c; michael@0: mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart); michael@0: } else { michael@0: buf[destIx] = U16_TRAIL(c); michael@0: mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart); michael@0: buf[--destIx] = U16_LEAD(c); michael@0: mapToNative[destIx] = (uint8_t)(srcIx - toUCharsMapStart); michael@0: } michael@0: michael@0: // Fill in the map from native indexes to UChars buf index. michael@0: do { michael@0: mapToUChars[sIx-- - toUCharsMapStart] = (uint8_t)destIx; michael@0: } while (sIx >= srcIx); michael@0: michael@0: // Set native indexing limit to be the current position. michael@0: // We are processing a non-ascii, non-native-indexing char now; michael@0: // the limit will be here if the rest of the chars to be michael@0: // added to this buffer are ascii. michael@0: bufNILimit = destIx; michael@0: } michael@0: } michael@0: u8b->bufNativeStart = srcIx; michael@0: u8b->bufNativeLimit = ix; michael@0: u8b->bufStartIdx = destIx; michael@0: u8b->bufLimitIdx = UTF8_TEXT_CHUNK_SIZE+2; michael@0: u8b->bufNILimit = bufNILimit - u8b->bufStartIdx; michael@0: u8b->toUCharsMapStart = toUCharsMapStart; michael@0: michael@0: ut->chunkContents = &buf[u8b->bufStartIdx]; michael@0: ut->chunkLength = u8b->bufLimitIdx - u8b->bufStartIdx; michael@0: ut->chunkOffset = ut->chunkLength; michael@0: ut->chunkNativeStart = u8b->bufNativeStart; michael@0: ut->chunkNativeLimit = u8b->bufNativeLimit; michael@0: ut->nativeIndexingLimit = u8b->bufNILimit; michael@0: return TRUE; michael@0: } michael@0: michael@0: } michael@0: michael@0: michael@0: michael@0: // michael@0: // This is a slightly modified copy of u_strFromUTF8, michael@0: // Inserts a Replacement Char rather than failing on invalid UTF-8 michael@0: // Removes unnecessary features. michael@0: // michael@0: static UChar* michael@0: utext_strFromUTF8(UChar *dest, michael@0: int32_t destCapacity, michael@0: int32_t *pDestLength, michael@0: const char* src, michael@0: int32_t srcLength, // required. NUL terminated not supported. michael@0: UErrorCode *pErrorCode michael@0: ) michael@0: { michael@0: michael@0: UChar *pDest = dest; michael@0: UChar *pDestLimit = (dest!=NULL)?(dest+destCapacity):NULL; michael@0: UChar32 ch=0; michael@0: int32_t index = 0; michael@0: int32_t reqLength = 0; michael@0: uint8_t* pSrc = (uint8_t*) src; michael@0: michael@0: michael@0: while((index < srcLength)&&(pDest0)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: int32_t length = ut->b; michael@0: int32_t start32 = pinIndex(start, length); michael@0: int32_t limit32 = pinIndex(limit, length); michael@0: michael@0: if(start32>limit32) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: // adjust the incoming indexes to land on code point boundaries if needed. michael@0: // adjust by no more than three, because that is the largest number of trail bytes michael@0: // in a well formed UTF8 character. michael@0: const uint8_t *buf = (const uint8_t *)ut->context; michael@0: int i; michael@0: if (start32 < ut->chunkNativeLimit) { michael@0: for (i=0; i<3; i++) { michael@0: if (U8_IS_SINGLE(buf[start32]) || U8_IS_LEAD(buf[start32]) || start32==0) { michael@0: break; michael@0: } michael@0: start32--; michael@0: } michael@0: } michael@0: michael@0: if (limit32 < ut->chunkNativeLimit) { michael@0: for (i=0; i<3; i++) { michael@0: if (U8_IS_SINGLE(buf[limit32]) || U8_IS_LEAD(buf[limit32]) || limit32==0) { michael@0: break; michael@0: } michael@0: limit32--; michael@0: } michael@0: } michael@0: michael@0: // Do the actual extract. michael@0: int32_t destLength=0; michael@0: utext_strFromUTF8(dest, destCapacity, &destLength, michael@0: (const char *)ut->context+start32, limit32-start32, michael@0: pErrorCode); michael@0: utf8TextAccess(ut, limit32, TRUE); michael@0: return destLength; michael@0: } michael@0: michael@0: // michael@0: // utf8TextMapOffsetToNative michael@0: // michael@0: // Map a chunk (UTF-16) offset to a native index. michael@0: static int64_t U_CALLCONV michael@0: utf8TextMapOffsetToNative(const UText *ut) { michael@0: // michael@0: UTF8Buf *u8b = (UTF8Buf *)ut->p; michael@0: U_ASSERT(ut->chunkOffset>ut->nativeIndexingLimit && ut->chunkOffset<=ut->chunkLength); michael@0: int32_t nativeOffset = u8b->mapToNative[ut->chunkOffset + u8b->bufStartIdx] + u8b->toUCharsMapStart; michael@0: U_ASSERT(nativeOffset >= ut->chunkNativeStart && nativeOffset <= ut->chunkNativeLimit); michael@0: return nativeOffset; michael@0: } michael@0: michael@0: // michael@0: // Map a native index to the corrsponding chunk offset michael@0: // michael@0: static int32_t U_CALLCONV michael@0: utf8TextMapIndexToUTF16(const UText *ut, int64_t index64) { michael@0: U_ASSERT(index64 <= 0x7fffffff); michael@0: int32_t index = (int32_t)index64; michael@0: UTF8Buf *u8b = (UTF8Buf *)ut->p; michael@0: U_ASSERT(index>=ut->chunkNativeStart+ut->nativeIndexingLimit); michael@0: U_ASSERT(index<=ut->chunkNativeLimit); michael@0: int32_t mapIndex = index - u8b->toUCharsMapStart; michael@0: int32_t offset = u8b->mapToUChars[mapIndex] - u8b->bufStartIdx; michael@0: U_ASSERT(offset>=0 && offset<=ut->chunkLength); michael@0: return offset; michael@0: } michael@0: michael@0: static UText * U_CALLCONV michael@0: utf8TextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status) michael@0: { michael@0: // First do a generic shallow clone. Does everything needed for the UText struct itself. michael@0: dest = shallowTextClone(dest, src, status); michael@0: michael@0: // For deep clones, make a copy of the string. michael@0: // The copied storage is owned by the newly created clone. michael@0: // michael@0: // TODO: There is an isssue with using utext_nativeLength(). michael@0: // That function is non-const in cases where the input was NUL terminated michael@0: // and the length has not yet been determined. michael@0: // This function (clone()) is const. michael@0: // There potentially a thread safety issue lurking here. michael@0: // michael@0: if (deep && U_SUCCESS(*status)) { michael@0: int32_t len = (int32_t)utext_nativeLength((UText *)src); michael@0: char *copyStr = (char *)uprv_malloc(len+1); michael@0: if (copyStr == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: uprv_memcpy(copyStr, src->context, len+1); michael@0: dest->context = copyStr; michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT); michael@0: } michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: michael@0: static void U_CALLCONV michael@0: utf8TextClose(UText *ut) { michael@0: // Most of the work of close is done by the generic UText framework close. michael@0: // All that needs to be done here is to delete the UTF8 string if the UText michael@0: // owns it. This occurs if the UText was created by cloning. michael@0: if (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT)) { michael@0: char *s = (char *)ut->context; michael@0: uprv_free(s); michael@0: ut->context = NULL; michael@0: } michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: michael@0: static const struct UTextFuncs utf8Funcs = michael@0: { michael@0: sizeof(UTextFuncs), michael@0: 0, 0, 0, // Reserved alignment padding michael@0: utf8TextClone, michael@0: utf8TextLength, michael@0: utf8TextAccess, michael@0: utf8TextExtract, michael@0: NULL, /* replace*/ michael@0: NULL, /* copy */ michael@0: utf8TextMapOffsetToNative, michael@0: utf8TextMapIndexToUTF16, michael@0: utf8TextClose, michael@0: NULL, // spare 1 michael@0: NULL, // spare 2 michael@0: NULL // spare 3 michael@0: }; michael@0: michael@0: michael@0: static const char gEmptyString[] = {0}; michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openUTF8(UText *ut, const char *s, int64_t length, UErrorCode *status) { michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(s==NULL && length==0) { michael@0: s = gEmptyString; michael@0: } michael@0: michael@0: if(s==NULL || length<-1 || length>INT32_MAX) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: ut = utext_setup(ut, sizeof(UTF8Buf) * 2, status); michael@0: if (U_FAILURE(*status)) { michael@0: return ut; michael@0: } michael@0: michael@0: ut->pFuncs = &utf8Funcs; michael@0: ut->context = s; michael@0: ut->b = (int32_t)length; michael@0: ut->c = (int32_t)length; michael@0: if (ut->c < 0) { michael@0: ut->c = 0; michael@0: ut->providerProperties |= I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: ut->p = ut->pExtra; michael@0: ut->q = (char *)ut->pExtra + sizeof(UTF8Buf); michael@0: return ut; michael@0: michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText implementation wrapper for Replaceable (read/write) michael@0: // michael@0: // Use of UText data members: michael@0: // context pointer to Replaceable. michael@0: // p pointer to Replaceable if it is owned by the UText. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: michael@0: michael@0: // minimum chunk size for this implementation: 3 michael@0: // to allow for possible trimming for code point boundaries michael@0: enum { REP_TEXT_CHUNK_SIZE=10 }; michael@0: michael@0: struct ReplExtra { michael@0: /* michael@0: * Chunk UChars. michael@0: * +1 to simplify filling with surrogate pair at the end. michael@0: */ michael@0: UChar s[REP_TEXT_CHUNK_SIZE+1]; michael@0: }; michael@0: michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: static UText * U_CALLCONV michael@0: repTextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status) { michael@0: // First do a generic shallow clone. Does everything needed for the UText struct itself. michael@0: dest = shallowTextClone(dest, src, status); michael@0: michael@0: // For deep clones, make a copy of the Replaceable. michael@0: // The copied Replaceable storage is owned by the newly created UText clone. michael@0: // A non-NULL pointer in UText.p is the signal to the close() function to delete michael@0: // it. michael@0: // michael@0: if (deep && U_SUCCESS(*status)) { michael@0: const Replaceable *replSrc = (const Replaceable *)src->context; michael@0: dest->context = replSrc->clone(); michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT); michael@0: michael@0: // with deep clone, the copy is writable, even when the source is not. michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_WRITABLE); michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: michael@0: static void U_CALLCONV michael@0: repTextClose(UText *ut) { michael@0: // Most of the work of close is done by the generic UText framework close. michael@0: // All that needs to be done here is delete the Replaceable if the UText michael@0: // owns it. This occurs if the UText was created by cloning. michael@0: if (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT)) { michael@0: Replaceable *rep = (Replaceable *)ut->context; michael@0: delete rep; michael@0: ut->context = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: static int64_t U_CALLCONV michael@0: repTextLength(UText *ut) { michael@0: const Replaceable *replSrc = (const Replaceable *)ut->context; michael@0: int32_t len = replSrc->length(); michael@0: return len; michael@0: } michael@0: michael@0: michael@0: static UBool U_CALLCONV michael@0: repTextAccess(UText *ut, int64_t index, UBool forward) { michael@0: const Replaceable *rep=(const Replaceable *)ut->context; michael@0: int32_t length=rep->length(); // Full length of the input text (bigger than a chunk) michael@0: michael@0: // clip the requested index to the limits of the text. michael@0: int32_t index32 = pinIndex(index, length); michael@0: U_ASSERT(index<=INT32_MAX); michael@0: michael@0: michael@0: /* michael@0: * Compute start/limit boundaries around index, for a segment of text michael@0: * to be extracted. michael@0: * To allow for the possibility that our user gave an index to the trailing michael@0: * half of a surrogate pair, we must request one extra preceding UChar when michael@0: * going in the forward direction. This will ensure that the buffer has the michael@0: * entire code point at the specified index. michael@0: */ michael@0: if(forward) { michael@0: michael@0: if (index32>=ut->chunkNativeStart && index32chunkNativeLimit) { michael@0: // Buffer already contains the requested position. michael@0: ut->chunkOffset = (int32_t)(index - ut->chunkNativeStart); michael@0: return TRUE; michael@0: } michael@0: if (index32>=length && ut->chunkNativeLimit==length) { michael@0: // Request for end of string, and buffer already extends up to it. michael@0: // Can't get the data, but don't change the buffer. michael@0: ut->chunkOffset = length - (int32_t)ut->chunkNativeStart; michael@0: return FALSE; michael@0: } michael@0: michael@0: ut->chunkNativeLimit = index + REP_TEXT_CHUNK_SIZE - 1; michael@0: // Going forward, so we want to have the buffer with stuff at and beyond michael@0: // the requested index. The -1 gets us one code point before the michael@0: // requested index also, to handle the case of the index being on michael@0: // a trail surrogate of a surrogate pair. michael@0: if(ut->chunkNativeLimit > length) { michael@0: ut->chunkNativeLimit = length; michael@0: } michael@0: // unless buffer ran off end, start is index-1. michael@0: ut->chunkNativeStart = ut->chunkNativeLimit - REP_TEXT_CHUNK_SIZE; michael@0: if(ut->chunkNativeStart < 0) { michael@0: ut->chunkNativeStart = 0; michael@0: } michael@0: } else { michael@0: // Reverse iteration. Fill buffer with data preceding the requested index. michael@0: if (index32>ut->chunkNativeStart && index32<=ut->chunkNativeLimit) { michael@0: // Requested position already in buffer. michael@0: ut->chunkOffset = index32 - (int32_t)ut->chunkNativeStart; michael@0: return TRUE; michael@0: } michael@0: if (index32==0 && ut->chunkNativeStart==0) { michael@0: // Request for start, buffer already begins at start. michael@0: // No data, but keep the buffer as is. michael@0: ut->chunkOffset = 0; michael@0: return FALSE; michael@0: } michael@0: michael@0: // Figure out the bounds of the chunk to extract for reverse iteration. michael@0: // Need to worry about chunk not splitting surrogate pairs, and while still michael@0: // containing the data we need. michael@0: // Fix by requesting a chunk that includes an extra UChar at the end. michael@0: // If this turns out to be a lead surrogate, we can lop it off and still have michael@0: // the data we wanted. michael@0: ut->chunkNativeStart = index32 + 1 - REP_TEXT_CHUNK_SIZE; michael@0: if (ut->chunkNativeStart < 0) { michael@0: ut->chunkNativeStart = 0; michael@0: } michael@0: michael@0: ut->chunkNativeLimit = index32 + 1; michael@0: if (ut->chunkNativeLimit > length) { michael@0: ut->chunkNativeLimit = length; michael@0: } michael@0: } michael@0: michael@0: // Extract the new chunk of text from the Replaceable source. michael@0: ReplExtra *ex = (ReplExtra *)ut->pExtra; michael@0: // UnicodeString with its buffer a writable alias to the chunk buffer michael@0: UnicodeString buffer(ex->s, 0 /*buffer length*/, REP_TEXT_CHUNK_SIZE /*buffer capacity*/); michael@0: rep->extractBetween((int32_t)ut->chunkNativeStart, (int32_t)ut->chunkNativeLimit, buffer); michael@0: michael@0: ut->chunkContents = ex->s; michael@0: ut->chunkLength = (int32_t)(ut->chunkNativeLimit - ut->chunkNativeStart); michael@0: ut->chunkOffset = (int32_t)(index32 - ut->chunkNativeStart); michael@0: michael@0: // Surrogate pairs from the input text must not span chunk boundaries. michael@0: // If end of chunk could be the start of a surrogate, trim it off. michael@0: if (ut->chunkNativeLimit < length && michael@0: U16_IS_LEAD(ex->s[ut->chunkLength-1])) { michael@0: ut->chunkLength--; michael@0: ut->chunkNativeLimit--; michael@0: if (ut->chunkOffset > ut->chunkLength) { michael@0: ut->chunkOffset = ut->chunkLength; michael@0: } michael@0: } michael@0: michael@0: // if the first UChar in the chunk could be the trailing half of a surrogate pair, michael@0: // trim it off. michael@0: if(ut->chunkNativeStart>0 && U16_IS_TRAIL(ex->s[0])) { michael@0: ++(ut->chunkContents); michael@0: ++(ut->chunkNativeStart); michael@0: --(ut->chunkLength); michael@0: --(ut->chunkOffset); michael@0: } michael@0: michael@0: // adjust the index/chunkOffset to a code point boundary michael@0: U16_SET_CP_START(ut->chunkContents, 0, ut->chunkOffset); michael@0: michael@0: // Use fast indexing for get/setNativeIndex() michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: michael@0: static int32_t U_CALLCONV michael@0: repTextExtract(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *status) { michael@0: const Replaceable *rep=(const Replaceable *)ut->context; michael@0: int32_t length=rep->length(); michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if(destCapacity<0 || (dest==NULL && destCapacity>0)) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: if(start>limit) { michael@0: *status=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: int32_t start32 = pinIndex(start, length); michael@0: int32_t limit32 = pinIndex(limit, length); michael@0: michael@0: // adjust start, limit if they point to trail half of surrogates michael@0: if (start32charAt(start32)) && michael@0: U_IS_SUPPLEMENTARY(rep->char32At(start32))){ michael@0: start32--; michael@0: } michael@0: if (limit32charAt(limit32)) && michael@0: U_IS_SUPPLEMENTARY(rep->char32At(limit32))){ michael@0: limit32--; michael@0: } michael@0: michael@0: length=limit32-start32; michael@0: if(length>destCapacity) { michael@0: limit32 = start32 + destCapacity; michael@0: } michael@0: UnicodeString buffer(dest, 0, destCapacity); // writable alias michael@0: rep->extractBetween(start32, limit32, buffer); michael@0: repTextAccess(ut, limit32, TRUE); michael@0: michael@0: return u_terminateUChars(dest, destCapacity, length, status); michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: repTextReplace(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: const UChar *src, int32_t length, michael@0: UErrorCode *status) { michael@0: Replaceable *rep=(Replaceable *)ut->context; michael@0: int32_t oldLength; michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if(src==NULL && length!=0) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: oldLength=rep->length(); // will subtract from new length michael@0: if(start>limit ) { michael@0: *status=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: int32_t start32 = pinIndex(start, oldLength); michael@0: int32_t limit32 = pinIndex(limit, oldLength); michael@0: michael@0: // Snap start & limit to code point boundaries. michael@0: if (start32charAt(start32)) && michael@0: start32>0 && U16_IS_LEAD(rep->charAt(start32-1))) michael@0: { michael@0: start32--; michael@0: } michael@0: if (limit32charAt(limit32-1)) && michael@0: U16_IS_TRAIL(rep->charAt(limit32))) michael@0: { michael@0: limit32++; michael@0: } michael@0: michael@0: // Do the actual replace operation using methods of the Replaceable class michael@0: UnicodeString replStr((UBool)(length<0), src, length); // read-only alias michael@0: rep->handleReplaceBetween(start32, limit32, replStr); michael@0: int32_t newLength = rep->length(); michael@0: int32_t lengthDelta = newLength - oldLength; michael@0: michael@0: // Is the UText chunk buffer OK? michael@0: if (ut->chunkNativeLimit > start32) { michael@0: // this replace operation may have impacted the current chunk. michael@0: // invalidate it, which will force a reload on the next access. michael@0: invalidateChunk(ut); michael@0: } michael@0: michael@0: // set the iteration position to the end of the newly inserted replacement text. michael@0: int32_t newIndexPos = limit32 + lengthDelta; michael@0: repTextAccess(ut, newIndexPos, TRUE); michael@0: michael@0: return lengthDelta; michael@0: } michael@0: michael@0: michael@0: static void U_CALLCONV michael@0: repTextCopy(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: int64_t destIndex, michael@0: UBool move, michael@0: UErrorCode *status) michael@0: { michael@0: Replaceable *rep=(Replaceable *)ut->context; michael@0: int32_t length=rep->length(); michael@0: michael@0: if(U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: if (start>limit || (startcopy(start32, limit32, destIndex32); michael@0: if(destIndex32handleReplaceBetween(start32, limit32, UnicodeString()); michael@0: } else { michael@0: // copy michael@0: rep->copy(start32, limit32, destIndex32); michael@0: } michael@0: michael@0: // If the change to the text touched the region in the chunk buffer, michael@0: // invalidate the buffer. michael@0: int32_t firstAffectedIndex = destIndex32; michael@0: if (move && start32chunkNativeLimit) { michael@0: // changes may have affected range covered by the chunk michael@0: invalidateChunk(ut); michael@0: } michael@0: michael@0: // Put iteration position at the newly inserted (moved) block, michael@0: int32_t nativeIterIndex = destIndex32 + limit32 - start32; michael@0: if (move && destIndex32>start32) { michael@0: // moved a block of text towards the end of the string. michael@0: nativeIterIndex = destIndex32; michael@0: } michael@0: michael@0: // Set position, reload chunk if needed. michael@0: repTextAccess(ut, nativeIterIndex, TRUE); michael@0: } michael@0: michael@0: static const struct UTextFuncs repFuncs = michael@0: { michael@0: sizeof(UTextFuncs), michael@0: 0, 0, 0, // Reserved alignment padding michael@0: repTextClone, michael@0: repTextLength, michael@0: repTextAccess, michael@0: repTextExtract, michael@0: repTextReplace, michael@0: repTextCopy, michael@0: NULL, // MapOffsetToNative, michael@0: NULL, // MapIndexToUTF16, michael@0: repTextClose, michael@0: NULL, // spare 1 michael@0: NULL, // spare 2 michael@0: NULL // spare 3 michael@0: }; michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openReplaceable(UText *ut, Replaceable *rep, UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(rep==NULL) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: ut = utext_setup(ut, sizeof(ReplExtra), status); michael@0: michael@0: ut->providerProperties = I32_FLAG(UTEXT_PROVIDER_WRITABLE); michael@0: if(rep->hasMetaData()) { michael@0: ut->providerProperties |=I32_FLAG(UTEXT_PROVIDER_HAS_META_DATA); michael@0: } michael@0: michael@0: ut->pFuncs = &repFuncs; michael@0: ut->context = rep; michael@0: return ut; michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText implementation for UnicodeString (read/write) and michael@0: // for const UnicodeString (read only) michael@0: // (same implementation, only the flags are different) michael@0: // michael@0: // Use of UText data members: michael@0: // context pointer to UnicodeString michael@0: // p pointer to UnicodeString IF this UText owns the string michael@0: // and it must be deleted on close(). NULL otherwise. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: michael@0: static UText * U_CALLCONV michael@0: unistrTextClone(UText *dest, const UText *src, UBool deep, UErrorCode *status) { michael@0: // First do a generic shallow clone. Does everything needed for the UText struct itself. michael@0: dest = shallowTextClone(dest, src, status); michael@0: michael@0: // For deep clones, make a copy of the UnicodeSring. michael@0: // The copied UnicodeString storage is owned by the newly created UText clone. michael@0: // A non-NULL pointer in UText.p is the signal to the close() function to delete michael@0: // the UText. michael@0: // michael@0: if (deep && U_SUCCESS(*status)) { michael@0: const UnicodeString *srcString = (const UnicodeString *)src->context; michael@0: dest->context = new UnicodeString(*srcString); michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT); michael@0: michael@0: // with deep clone, the copy is writable, even when the source is not. michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_WRITABLE); michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: unistrTextClose(UText *ut) { michael@0: // Most of the work of close is done by the generic UText framework close. michael@0: // All that needs to be done here is delete the UnicodeString if the UText michael@0: // owns it. This occurs if the UText was created by cloning. michael@0: if (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT)) { michael@0: UnicodeString *str = (UnicodeString *)ut->context; michael@0: delete str; michael@0: ut->context = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: static int64_t U_CALLCONV michael@0: unistrTextLength(UText *t) { michael@0: return ((const UnicodeString *)t->context)->length(); michael@0: } michael@0: michael@0: michael@0: static UBool U_CALLCONV michael@0: unistrTextAccess(UText *ut, int64_t index, UBool forward) { michael@0: int32_t length = ut->chunkLength; michael@0: ut->chunkOffset = pinIndex(index, length); michael@0: michael@0: // Check whether request is at the start or end michael@0: UBool retVal = (forward && index0); michael@0: return retVal; michael@0: } michael@0: michael@0: michael@0: michael@0: static int32_t U_CALLCONV michael@0: unistrTextExtract(UText *t, michael@0: int64_t start, int64_t limit, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: const UnicodeString *us=(const UnicodeString *)t->context; michael@0: int32_t length=us->length(); michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(destCapacity<0 || (dest==NULL && destCapacity>0)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: if(start<0 || start>limit) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: int32_t start32 = startgetChar32Start((int32_t)start) : length; michael@0: int32_t limit32 = limitgetChar32Start((int32_t)limit) : length; michael@0: michael@0: length=limit32-start32; michael@0: if (destCapacity>0 && dest!=NULL) { michael@0: int32_t trimmedLength = length; michael@0: if(trimmedLength>destCapacity) { michael@0: trimmedLength=destCapacity; michael@0: } michael@0: us->extract(start32, trimmedLength, dest); michael@0: t->chunkOffset = start32+trimmedLength; michael@0: } else { michael@0: t->chunkOffset = start32; michael@0: } michael@0: u_terminateUChars(dest, destCapacity, length, pErrorCode); michael@0: return length; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: unistrTextReplace(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: const UChar *src, int32_t length, michael@0: UErrorCode *pErrorCode) { michael@0: UnicodeString *us=(UnicodeString *)ut->context; michael@0: int32_t oldLength; michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(src==NULL && length!=0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: if(start>limit) { michael@0: *pErrorCode=U_INDEX_OUTOFBOUNDS_ERROR; michael@0: return 0; michael@0: } michael@0: oldLength=us->length(); michael@0: int32_t start32 = pinIndex(start, oldLength); michael@0: int32_t limit32 = pinIndex(limit, oldLength); michael@0: if (start32 < oldLength) { michael@0: start32 = us->getChar32Start(start32); michael@0: } michael@0: if (limit32 < oldLength) { michael@0: limit32 = us->getChar32Start(limit32); michael@0: } michael@0: michael@0: // replace michael@0: us->replace(start32, limit32-start32, src, length); michael@0: int32_t newLength = us->length(); michael@0: michael@0: // Update the chunk description. michael@0: ut->chunkContents = us->getBuffer(); michael@0: ut->chunkLength = newLength; michael@0: ut->chunkNativeLimit = newLength; michael@0: ut->nativeIndexingLimit = newLength; michael@0: michael@0: // Set iteration position to the point just following the newly inserted text. michael@0: int32_t lengthDelta = newLength - oldLength; michael@0: ut->chunkOffset = limit32 + lengthDelta; michael@0: michael@0: return lengthDelta; michael@0: } michael@0: michael@0: static void U_CALLCONV michael@0: unistrTextCopy(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: int64_t destIndex, michael@0: UBool move, michael@0: UErrorCode *pErrorCode) { michael@0: UnicodeString *us=(UnicodeString *)ut->context; michael@0: int32_t length=us->length(); michael@0: michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return; michael@0: } michael@0: int32_t start32 = pinIndex(start, length); michael@0: int32_t limit32 = pinIndex(limit, length); michael@0: int32_t destIndex32 = pinIndex(destIndex, length); michael@0: michael@0: if( start32>limit32 || (start32copy(start32, limit32, destIndex32); michael@0: if(destIndex32replace(start32, segLength, NULL, 0); michael@0: } else { michael@0: // copy michael@0: us->copy(start32, limit32, destIndex32); michael@0: } michael@0: michael@0: // update chunk description, set iteration position. michael@0: ut->chunkContents = us->getBuffer(); michael@0: if (move==FALSE) { michael@0: // copy operation, string length grows michael@0: ut->chunkLength += limit32-start32; michael@0: ut->chunkNativeLimit = ut->chunkLength; michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: } michael@0: michael@0: // Iteration position to end of the newly inserted text. michael@0: ut->chunkOffset = destIndex32+limit32-start32; michael@0: if (move && destIndex32>start32) { michael@0: ut->chunkOffset = destIndex32; michael@0: } michael@0: michael@0: } michael@0: michael@0: static const struct UTextFuncs unistrFuncs = michael@0: { michael@0: sizeof(UTextFuncs), michael@0: 0, 0, 0, // Reserved alignment padding michael@0: unistrTextClone, michael@0: unistrTextLength, michael@0: unistrTextAccess, michael@0: unistrTextExtract, michael@0: unistrTextReplace, michael@0: unistrTextCopy, michael@0: NULL, // MapOffsetToNative, michael@0: NULL, // MapIndexToUTF16, michael@0: unistrTextClose, michael@0: NULL, // spare 1 michael@0: NULL, // spare 2 michael@0: NULL // spare 3 michael@0: }; michael@0: michael@0: michael@0: michael@0: U_CDECL_END michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) { michael@0: ut = utext_openConstUnicodeString(ut, s, status); michael@0: if (U_SUCCESS(*status)) { michael@0: ut->providerProperties |= I32_FLAG(UTEXT_PROVIDER_WRITABLE); michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openConstUnicodeString(UText *ut, const UnicodeString *s, UErrorCode *status) { michael@0: if (U_SUCCESS(*status) && s->isBogus()) { michael@0: // The UnicodeString is bogus, but we still need to detach the UText michael@0: // from whatever it was hooked to before, if anything. michael@0: utext_openUChars(ut, NULL, 0, status); michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return ut; michael@0: } michael@0: ut = utext_setup(ut, 0, status); michael@0: // note: use the standard (writable) function table for UnicodeString. michael@0: // The flag settings disable writing, so having the functions in michael@0: // the table is harmless. michael@0: if (U_SUCCESS(*status)) { michael@0: ut->pFuncs = &unistrFuncs; michael@0: ut->context = s; michael@0: ut->providerProperties = I32_FLAG(UTEXT_PROVIDER_STABLE_CHUNKS); michael@0: ut->chunkContents = s->getBuffer(); michael@0: ut->chunkLength = s->length(); michael@0: ut->chunkNativeStart = 0; michael@0: ut->chunkNativeLimit = ut->chunkLength; michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText implementation for const UChar * strings michael@0: // michael@0: // Use of UText data members: michael@0: // context pointer to UnicodeString michael@0: // a length. -1 if not yet known. michael@0: // michael@0: // TODO: support 64 bit lengths. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: michael@0: static UText * U_CALLCONV michael@0: ucstrTextClone(UText *dest, const UText * src, UBool deep, UErrorCode * status) { michael@0: // First do a generic shallow clone. michael@0: dest = shallowTextClone(dest, src, status); michael@0: michael@0: // For deep clones, make a copy of the string. michael@0: // The copied storage is owned by the newly created clone. michael@0: // A non-NULL pointer in UText.p is the signal to the close() function to delete michael@0: // it. michael@0: // michael@0: if (deep && U_SUCCESS(*status)) { michael@0: U_ASSERT(utext_nativeLength(dest) < INT32_MAX); michael@0: int32_t len = (int32_t)utext_nativeLength(dest); michael@0: michael@0: // The cloned string IS going to be NUL terminated, whether or not the original was. michael@0: const UChar *srcStr = (const UChar *)src->context; michael@0: UChar *copyStr = (UChar *)uprv_malloc((len+1) * sizeof(UChar)); michael@0: if (copyStr == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: int64_t i; michael@0: for (i=0; icontext = copyStr; michael@0: dest->providerProperties |= I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT); michael@0: } michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: michael@0: static void U_CALLCONV michael@0: ucstrTextClose(UText *ut) { michael@0: // Most of the work of close is done by the generic UText framework close. michael@0: // All that needs to be done here is delete the string if the UText michael@0: // owns it. This occurs if the UText was created by cloning. michael@0: if (ut->providerProperties & I32_FLAG(UTEXT_PROVIDER_OWNS_TEXT)) { michael@0: UChar *s = (UChar *)ut->context; michael@0: uprv_free(s); michael@0: ut->context = NULL; michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: static int64_t U_CALLCONV michael@0: ucstrTextLength(UText *ut) { michael@0: if (ut->a < 0) { michael@0: // null terminated, we don't yet know the length. Scan for it. michael@0: // Access is not convenient for doing this michael@0: // because the current interation postion can't be changed. michael@0: const UChar *str = (const UChar *)ut->context; michael@0: for (;;) { michael@0: if (str[ut->chunkNativeLimit] == 0) { michael@0: break; michael@0: } michael@0: ut->chunkNativeLimit++; michael@0: } michael@0: ut->a = ut->chunkNativeLimit; michael@0: ut->chunkLength = (int32_t)ut->chunkNativeLimit; michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: return ut->a; michael@0: } michael@0: michael@0: michael@0: static UBool U_CALLCONV michael@0: ucstrTextAccess(UText *ut, int64_t index, UBool forward) { michael@0: const UChar *str = (const UChar *)ut->context; michael@0: michael@0: // pin the requested index to the bounds of the string, michael@0: // and set current iteration position. michael@0: if (index<0) { michael@0: index = 0; michael@0: } else if (index < ut->chunkNativeLimit) { michael@0: // The request data is within the chunk as it is known so far. michael@0: // Put index on a code point boundary. michael@0: U16_SET_CP_START(str, 0, index); michael@0: } else if (ut->a >= 0) { michael@0: // We know the length of this string, and the user is requesting something michael@0: // at or beyond the length. Pin the requested index to the length. michael@0: index = ut->a; michael@0: } else { michael@0: // Null terminated string, length not yet known, and the requested index michael@0: // is beyond where we have scanned so far. michael@0: // Scan to 32 UChars beyond the requested index. The strategy here is michael@0: // to avoid fully scanning a long string when the caller only wants to michael@0: // see a few characters at its beginning. michael@0: int32_t scanLimit = (int32_t)index + 32; michael@0: if ((index + 32)>INT32_MAX || (index + 32)<0 ) { // note: int64 expression michael@0: scanLimit = INT32_MAX; michael@0: } michael@0: michael@0: int32_t chunkLimit = (int32_t)ut->chunkNativeLimit; michael@0: for (; chunkLimita = chunkLimit; michael@0: ut->chunkLength = chunkLimit; michael@0: ut->nativeIndexingLimit = chunkLimit; michael@0: if (index >= chunkLimit) { michael@0: index = chunkLimit; michael@0: } else { michael@0: U16_SET_CP_START(str, 0, index); michael@0: } michael@0: michael@0: ut->chunkNativeLimit = chunkLimit; michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: goto breakout; michael@0: } michael@0: } michael@0: // We scanned through the next batch of UChars without finding the end. michael@0: U16_SET_CP_START(str, 0, index); michael@0: if (chunkLimit == INT32_MAX) { michael@0: // Scanned to the limit of a 32 bit length. michael@0: // Forceably trim the overlength string back so length fits in int32 michael@0: // TODO: add support for 64 bit strings. michael@0: ut->a = chunkLimit; michael@0: ut->chunkLength = chunkLimit; michael@0: ut->nativeIndexingLimit = chunkLimit; michael@0: if (index > chunkLimit) { michael@0: index = chunkLimit; michael@0: } michael@0: ut->chunkNativeLimit = chunkLimit; michael@0: ut->providerProperties &= ~I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } else { michael@0: // The endpoint of a chunk must not be left in the middle of a surrogate pair. michael@0: // If the current end is on a lead surrogate, back the end up by one. michael@0: // It doesn't matter if the end char happens to be an unpaired surrogate, michael@0: // and it's simpler not to worry about it. michael@0: if (U16_IS_LEAD(str[chunkLimit-1])) { michael@0: --chunkLimit; michael@0: } michael@0: // Null-terminated chunk with end still unknown. michael@0: // Update the chunk length to reflect what has been scanned thus far. michael@0: // That the full length is still unknown is (still) flagged by michael@0: // ut->a being < 0. michael@0: ut->chunkNativeLimit = chunkLimit; michael@0: ut->nativeIndexingLimit = chunkLimit; michael@0: ut->chunkLength = chunkLimit; michael@0: } michael@0: michael@0: } michael@0: breakout: michael@0: U_ASSERT(index<=INT32_MAX); michael@0: ut->chunkOffset = (int32_t)index; michael@0: michael@0: // Check whether request is at the start or end michael@0: UBool retVal = (forward && indexchunkNativeLimit) || (!forward && index>0); michael@0: return retVal; michael@0: } michael@0: michael@0: michael@0: michael@0: static int32_t U_CALLCONV michael@0: ucstrTextExtract(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) michael@0: { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(destCapacity<0 || (dest==NULL && destCapacity>0) || start>limit) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: //const UChar *s=(const UChar *)ut->context; michael@0: int32_t si, di; michael@0: michael@0: int32_t start32; michael@0: int32_t limit32; michael@0: michael@0: // Access the start. Does two things we need: michael@0: // Pins 'start' to the length of the string, if it came in out-of-bounds. michael@0: // Snaps 'start' to the beginning of a code point. michael@0: ucstrTextAccess(ut, start, TRUE); michael@0: const UChar *s=ut->chunkContents; michael@0: start32 = ut->chunkOffset; michael@0: michael@0: int32_t strLength=(int32_t)ut->a; michael@0: if (strLength >= 0) { michael@0: limit32 = pinIndex(limit, strLength); michael@0: } else { michael@0: limit32 = pinIndex(limit, INT32_MAX); michael@0: } michael@0: di = 0; michael@0: for (si=start32; sia = si; // set string length for this UText michael@0: ut->chunkNativeLimit = si; michael@0: ut->chunkLength = si; michael@0: ut->nativeIndexingLimit = si; michael@0: strLength = si; michael@0: break; michael@0: } michael@0: U_ASSERT(di>=0); /* to ensure di never exceeds INT32_MAX, which must not happen logically */ michael@0: if (di=0) { michael@0: // We have filled the destination buffer, and the string length is known. michael@0: // Cut the loop short. There is no need to scan string termination. michael@0: di = limit32 - start32; michael@0: si = limit32; michael@0: break; michael@0: } michael@0: } michael@0: di++; michael@0: } michael@0: michael@0: // If the limit index points to a lead surrogate of a pair, michael@0: // add the corresponding trail surrogate to the destination. michael@0: if (si>0 && U16_IS_LEAD(s[si-1]) && michael@0: ((sichunkOffset = uprv_min(strLength, start32 + destCapacity); michael@0: michael@0: // Add a terminating NUL if space in the buffer permits, michael@0: // and set the error status as required. michael@0: u_terminateUChars(dest, destCapacity, di, pErrorCode); michael@0: return di; michael@0: } michael@0: michael@0: static const struct UTextFuncs ucstrFuncs = michael@0: { michael@0: sizeof(UTextFuncs), michael@0: 0, 0, 0, // Reserved alignment padding michael@0: ucstrTextClone, michael@0: ucstrTextLength, michael@0: ucstrTextAccess, michael@0: ucstrTextExtract, michael@0: NULL, // Replace michael@0: NULL, // Copy michael@0: NULL, // MapOffsetToNative, michael@0: NULL, // MapIndexToUTF16, michael@0: ucstrTextClose, michael@0: NULL, // spare 1 michael@0: NULL, // spare 2 michael@0: NULL, // spare 3 michael@0: }; michael@0: michael@0: U_CDECL_END michael@0: michael@0: static const UChar gEmptyUString[] = {0}; michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openUChars(UText *ut, const UChar *s, int64_t length, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: if(s==NULL && length==0) { michael@0: s = gEmptyUString; michael@0: } michael@0: if (s==NULL || length < -1 || length>INT32_MAX) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: ut = utext_setup(ut, 0, status); michael@0: if (U_SUCCESS(*status)) { michael@0: ut->pFuncs = &ucstrFuncs; michael@0: ut->context = s; michael@0: ut->providerProperties = I32_FLAG(UTEXT_PROVIDER_STABLE_CHUNKS); michael@0: if (length==-1) { michael@0: ut->providerProperties |= I32_FLAG(UTEXT_PROVIDER_LENGTH_IS_EXPENSIVE); michael@0: } michael@0: ut->a = length; michael@0: ut->chunkContents = s; michael@0: ut->chunkNativeStart = 0; michael@0: ut->chunkNativeLimit = length>=0? length : 0; michael@0: ut->chunkLength = (int32_t)ut->chunkNativeLimit; michael@0: ut->chunkOffset = 0; michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // UText implementation for text from ICU CharacterIterators michael@0: // michael@0: // Use of UText data members: michael@0: // context pointer to the CharacterIterator michael@0: // a length of the full text. michael@0: // p pointer to buffer 1 michael@0: // b start index of local buffer 1 contents michael@0: // q pointer to buffer 2 michael@0: // c start index of local buffer 2 contents michael@0: // r pointer to the character iterator if the UText owns it. michael@0: // Null otherwise. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: #define CIBufSize 16 michael@0: michael@0: U_CDECL_BEGIN michael@0: static void U_CALLCONV michael@0: charIterTextClose(UText *ut) { michael@0: // Most of the work of close is done by the generic UText framework close. michael@0: // All that needs to be done here is delete the CharacterIterator if the UText michael@0: // owns it. This occurs if the UText was created by cloning. michael@0: CharacterIterator *ci = (CharacterIterator *)ut->r; michael@0: delete ci; michael@0: ut->r = NULL; michael@0: } michael@0: michael@0: static int64_t U_CALLCONV michael@0: charIterTextLength(UText *ut) { michael@0: return (int32_t)ut->a; michael@0: } michael@0: michael@0: static UBool U_CALLCONV michael@0: charIterTextAccess(UText *ut, int64_t index, UBool forward) { michael@0: CharacterIterator *ci = (CharacterIterator *)ut->context; michael@0: michael@0: int32_t clippedIndex = (int32_t)index; michael@0: if (clippedIndex<0) { michael@0: clippedIndex=0; michael@0: } else if (clippedIndex>=ut->a) { michael@0: clippedIndex=(int32_t)ut->a; michael@0: } michael@0: int32_t neededIndex = clippedIndex; michael@0: if (!forward && neededIndex>0) { michael@0: // reverse iteration, want the position just before what was asked for. michael@0: neededIndex--; michael@0: } else if (forward && neededIndex==ut->a && neededIndex>0) { michael@0: // Forward iteration, don't ask for something past the end of the text. michael@0: neededIndex--; michael@0: } michael@0: michael@0: // Find the native index of the start of the buffer containing what we want. michael@0: neededIndex -= neededIndex % CIBufSize; michael@0: michael@0: UChar *buf = NULL; michael@0: UBool needChunkSetup = TRUE; michael@0: int i; michael@0: if (ut->chunkNativeStart == neededIndex) { michael@0: // The buffer we want is already the current chunk. michael@0: needChunkSetup = FALSE; michael@0: } else if (ut->b == neededIndex) { michael@0: // The first buffer (buffer p) has what we need. michael@0: buf = (UChar *)ut->p; michael@0: } else if (ut->c == neededIndex) { michael@0: // The second buffer (buffer q) has what we need. michael@0: buf = (UChar *)ut->q; michael@0: } else { michael@0: // Neither buffer already has what we need. michael@0: // Load new data from the character iterator. michael@0: // Use the buf that is not the current buffer. michael@0: buf = (UChar *)ut->p; michael@0: if (ut->p == ut->chunkContents) { michael@0: buf = (UChar *)ut->q; michael@0: } michael@0: ci->setIndex(neededIndex); michael@0: for (i=0; inextPostInc(); michael@0: if (i+neededIndex > ut->a) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We have a buffer with the data we need. michael@0: // Set it up as the current chunk, if it wasn't already. michael@0: if (needChunkSetup) { michael@0: ut->chunkContents = buf; michael@0: ut->chunkLength = CIBufSize; michael@0: ut->chunkNativeStart = neededIndex; michael@0: ut->chunkNativeLimit = neededIndex + CIBufSize; michael@0: if (ut->chunkNativeLimit > ut->a) { michael@0: ut->chunkNativeLimit = ut->a; michael@0: ut->chunkLength = (int32_t)(ut->chunkNativeLimit)-(int32_t)(ut->chunkNativeStart); michael@0: } michael@0: ut->nativeIndexingLimit = ut->chunkLength; michael@0: U_ASSERT(ut->chunkOffset>=0 && ut->chunkOffset<=CIBufSize); michael@0: } michael@0: ut->chunkOffset = clippedIndex - (int32_t)ut->chunkNativeStart; michael@0: UBool success = (forward? ut->chunkOffsetchunkLength : ut->chunkOffset>0); michael@0: return success; michael@0: } michael@0: michael@0: static UText * U_CALLCONV michael@0: charIterTextClone(UText *dest, const UText *src, UBool deep, UErrorCode * status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if (deep) { michael@0: // There is no CharacterIterator API for cloning the underlying text storage. michael@0: *status = U_UNSUPPORTED_ERROR; michael@0: return NULL; michael@0: } else { michael@0: CharacterIterator *srcCI =(CharacterIterator *)src->context; michael@0: srcCI = srcCI->clone(); michael@0: dest = utext_openCharacterIterator(dest, srcCI, status); michael@0: // cast off const on getNativeIndex. michael@0: // For CharacterIterator based UTexts, this is safe, the operation is const. michael@0: int64_t ix = utext_getNativeIndex((UText *)src); michael@0: utext_setNativeIndex(dest, ix); michael@0: dest->r = srcCI; // flags that this UText owns the CharacterIterator michael@0: } michael@0: return dest; michael@0: } michael@0: michael@0: static int32_t U_CALLCONV michael@0: charIterTextExtract(UText *ut, michael@0: int64_t start, int64_t limit, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *status) michael@0: { michael@0: if(U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if(destCapacity<0 || (dest==NULL && destCapacity>0) || start>limit) { michael@0: *status=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: int32_t length = (int32_t)ut->a; michael@0: int32_t start32 = pinIndex(start, length); michael@0: int32_t limit32 = pinIndex(limit, length); michael@0: int32_t desti = 0; michael@0: int32_t srci; michael@0: int32_t copyLimit; michael@0: michael@0: CharacterIterator *ci = (CharacterIterator *)ut->context; michael@0: ci->setIndex32(start32); // Moves ix to lead of surrogate pair, if needed. michael@0: srci = ci->getIndex(); michael@0: copyLimit = srci; michael@0: while (srcinext32PostInc(); michael@0: int32_t len = U16_LENGTH(c); michael@0: U_ASSERT(desti+len>0); /* to ensure desti+len never exceeds MAX_INT32, which must not happen logically */ michael@0: if (desti+len <= destCapacity) { michael@0: U16_APPEND_UNSAFE(dest, desti, c); michael@0: copyLimit = srci+len; michael@0: } else { michael@0: desti += len; michael@0: *status = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: srci += len; michael@0: } michael@0: michael@0: charIterTextAccess(ut, copyLimit, TRUE); michael@0: michael@0: u_terminateUChars(dest, destCapacity, desti, status); michael@0: return desti; michael@0: } michael@0: michael@0: static const struct UTextFuncs charIterFuncs = michael@0: { michael@0: sizeof(UTextFuncs), michael@0: 0, 0, 0, // Reserved alignment padding michael@0: charIterTextClone, michael@0: charIterTextLength, michael@0: charIterTextAccess, michael@0: charIterTextExtract, michael@0: NULL, // Replace michael@0: NULL, // Copy michael@0: NULL, // MapOffsetToNative, michael@0: NULL, // MapIndexToUTF16, michael@0: charIterTextClose, michael@0: NULL, // spare 1 michael@0: NULL, // spare 2 michael@0: NULL // spare 3 michael@0: }; michael@0: U_CDECL_END michael@0: michael@0: michael@0: U_CAPI UText * U_EXPORT2 michael@0: utext_openCharacterIterator(UText *ut, CharacterIterator *ci, UErrorCode *status) { michael@0: if (U_FAILURE(*status)) { michael@0: return NULL; michael@0: } michael@0: michael@0: if (ci->startIndex() > 0) { michael@0: // No support for CharacterIterators that do not start indexing from zero. michael@0: *status = U_UNSUPPORTED_ERROR; michael@0: return NULL; michael@0: } michael@0: michael@0: // Extra space in UText for 2 buffers of CIBufSize UChars each. michael@0: int32_t extraSpace = 2 * CIBufSize * sizeof(UChar); michael@0: ut = utext_setup(ut, extraSpace, status); michael@0: if (U_SUCCESS(*status)) { michael@0: ut->pFuncs = &charIterFuncs; michael@0: ut->context = ci; michael@0: ut->providerProperties = 0; michael@0: ut->a = ci->endIndex(); // Length of text michael@0: ut->p = ut->pExtra; // First buffer michael@0: ut->b = -1; // Native index of first buffer contents michael@0: ut->q = (UChar*)ut->pExtra+CIBufSize; // Second buffer michael@0: ut->c = -1; // Native index of second buffer contents michael@0: michael@0: // Initialize current chunk contents to be empty. michael@0: // First access will fault something in. michael@0: // Note: The initial nativeStart and chunkOffset must sum to zero michael@0: // so that getNativeIndex() will correctly compute to zero michael@0: // if no call to Access() has ever been made. They can't be both michael@0: // zero without Access() thinking that the chunk is valid. michael@0: ut->chunkContents = (UChar *)ut->p; michael@0: ut->chunkNativeStart = -1; michael@0: ut->chunkOffset = 1; michael@0: ut->chunkNativeLimit = 0; michael@0: ut->chunkLength = 0; michael@0: ut->nativeIndexingLimit = ut->chunkOffset; // enables native indexing michael@0: } michael@0: return ut; michael@0: } michael@0: michael@0: michael@0: