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