#include "unicode/utypes.h"
#include "unicode/putil.h"
#include "unicode/locid.h"
#include "cstring.h"
#include "cmemory.h"
#include "unicode/ustring.h"
#include "unicode/unistr.h"
#include "unicode/uchar.h"
#include "unicode/ucnv.h"
#include "unicode/ubrk.h"
#include "uhash.h"
#include "ustr_imp.h"
#include "unormimp.h"
#include "umutex.h"
#if 0
#if U_IOSTREAM_SOURCE >= 199711
#include <iostream>
using namespace std;
#elif U_IOSTREAM_SOURCE >= 198506
#include <iostream.h>
#endif
void
print(const UnicodeString& s,
const char *name)
{
UChar c;
cout << name << ":|";
for(int i = 0; i < s.length(); ++i) {
c = s[i];
if(c>= 0x007E || c < 0x0020)
cout << "[0x" << hex << s[i] << "]";
else
cout << (char) s[i];
}
cout << '|' << endl;
}
void
print(const UChar *s,
int32_t len,
const char *name)
{
UChar c;
cout << name << ":|";
for(int i = 0; i < len; ++i) {
c = s[i];
if(c>= 0x007E || c < 0x0020)
cout << "[0x" << hex << s[i] << "]";
else
cout << (char) s[i];
}
cout << '|' << endl;
}
#endif
static
inline void
us_arrayCopy(const UChar *src, int32_t srcStart,
UChar *dst, int32_t dstStart, int32_t count)
{
if(count>0) {
uprv_memmove(dst+dstStart, src+srcStart, (size_t)(count*sizeof(*src)));
}
}
U_CDECL_BEGIN
static UChar U_CALLCONV
UnicodeString_charAt(int32_t offset, void *context) {
return ((UnicodeString*) context)->charAt(offset);
}
U_CDECL_END
U_NAMESPACE_BEGIN
const char UnicodeString::fgClassID=0;
void
UnicodeString::addRef()
{ umtx_atomic_inc((int32_t *)fArray - 1);}
int32_t
UnicodeString::removeRef()
{ return umtx_atomic_dec((int32_t *)fArray - 1);}
int32_t
UnicodeString::refCount() const
{
umtx_lock(NULL);
int32_t count = *((int32_t *)fArray - 1);
umtx_unlock(NULL);
return count;
}
void
UnicodeString::releaseArray() {
if((fFlags & kRefCounted) && removeRef() == 0) {
uprv_free((int32_t *)fArray - 1);
}
}
UnicodeString::UnicodeString()
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{}
UnicodeString::UnicodeString(int32_t capacity, UChar32 c, int32_t count)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(0),
fFlags(0)
{
if(count <= 0 || (uint32_t)c > 0x10ffff) {
allocate(capacity);
} else {
int32_t unitCount = UTF_CHAR_LENGTH(c), length = count * unitCount;
if(capacity < length) {
capacity = length;
}
if(allocate(capacity)) {
int32_t i = 0;
if(unitCount == 1) {
while(i < length) {
fArray[i++] = (UChar)c;
}
} else {
UChar units[UTF_MAX_CHAR_LENGTH];
UTF_APPEND_CHAR_UNSAFE(units, i, c);
i = 0;
while(i < length) {
int32_t unitIdx = 0;
while(unitIdx < unitCount) {
fArray[i++]=units[unitIdx++];
}
}
}
}
fLength = length;
}
}
UnicodeString::UnicodeString(UChar ch)
: fLength(1),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
fStackBuffer[0] = ch;
}
UnicodeString::UnicodeString(UChar32 ch)
: fLength(1),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
int32_t i = 0;
UBool isError = FALSE;
U16_APPEND(fStackBuffer, i, US_STACKBUF_SIZE, ch, isError);
fLength = i;
}
UnicodeString::UnicodeString(const UChar *text)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
doReplace(0, 0, text, 0, -1);
}
UnicodeString::UnicodeString(const UChar *text,
int32_t textLength)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
doReplace(0, 0, text, 0, textLength);
}
UnicodeString::UnicodeString(UBool isTerminated,
const UChar *text,
int32_t textLength)
: fLength(textLength),
fCapacity(isTerminated ? textLength + 1 : textLength),
fArray((UChar *)text),
fFlags(kReadonlyAlias)
{
if(text == NULL) {
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fArray = fStackBuffer;
fFlags = kShortString;
} else if(textLength < -1 ||
(textLength == -1 && !isTerminated) ||
(textLength >= 0 && isTerminated && text[textLength] != 0)
) {
setToBogus();
} else if(textLength == -1) {
fLength = u_strlen(text);
fCapacity = fLength + 1;
}
}
UnicodeString::UnicodeString(UChar *buff,
int32_t buffLength,
int32_t buffCapacity)
: fLength(buffLength),
fCapacity(buffCapacity),
fArray(buff),
fFlags(kWritableAlias)
{
if(buff == NULL) {
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fArray = fStackBuffer;
fFlags = kShortString;
} else if(buffLength < -1 || buffLength > buffCapacity) {
setToBogus();
} else if(buffLength == -1) {
const UChar *p = buff, *limit = buff + buffCapacity;
while(p != limit && *p != 0) {
++p;
}
fLength = (int32_t)(p - buff);
}
}
UnicodeString::UnicodeString(const char *codepageData,
const char *codepage)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
if(codepageData != 0) {
doCodepageCreate(codepageData, (int32_t)uprv_strlen(codepageData), codepage);
}
}
UnicodeString::UnicodeString(const char *codepageData,
int32_t dataLength,
const char *codepage)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
if(codepageData != 0) {
doCodepageCreate(codepageData, dataLength, codepage);
}
}
UnicodeString::UnicodeString(const char *src, int32_t srcLength,
UConverter *cnv,
UErrorCode &errorCode)
: fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
if(U_SUCCESS(errorCode)) {
if(src==NULL) {
} else if(srcLength<-1) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else {
if(srcLength==-1) {
srcLength=(int32_t)uprv_strlen(src);
}
if(srcLength>0) {
if(cnv!=0) {
ucnv_resetToUnicode(cnv);
doCodepageCreate(src, srcLength, cnv, errorCode);
} else {
cnv=u_getDefaultConverter(&errorCode);
doCodepageCreate(src, srcLength, cnv, errorCode);
u_releaseDefaultConverter(cnv);
}
}
}
if(U_FAILURE(errorCode)) {
setToBogus();
}
}
}
UnicodeString::UnicodeString(const UnicodeString& that)
: Replaceable(),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
copyFrom(that);
}
UnicodeString::UnicodeString(const UnicodeString& that,
int32_t srcStart)
: Replaceable(),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
setTo(that, srcStart);
}
UnicodeString::UnicodeString(const UnicodeString& that,
int32_t srcStart,
int32_t srcLength)
: Replaceable(),
fLength(0),
fCapacity(US_STACKBUF_SIZE),
fArray(fStackBuffer),
fFlags(kShortString)
{
setTo(that, srcStart, srcLength);
}
Replaceable *
Replaceable::clone() const {
return NULL;
}
Replaceable *
UnicodeString::clone() const {
return new UnicodeString(*this);
}
UBool
UnicodeString::allocate(int32_t capacity) {
if(capacity <= US_STACKBUF_SIZE) {
fArray = fStackBuffer;
fCapacity = US_STACKBUF_SIZE;
fFlags = kShortString;
} else {
int32_t words = (int32_t)(((sizeof(int32_t) + capacity * U_SIZEOF_UCHAR + 15) & ~15) >> 2);
int32_t *array = (int32_t*) uprv_malloc( sizeof(int32_t) * words );
if(array != 0) {
*array++ = 1;
fArray = (UChar *)array;
fCapacity = (int32_t)((words - 1) * (sizeof(int32_t) / U_SIZEOF_UCHAR));
fFlags = kLongString;
} else {
fLength = 0;
fCapacity = 0;
fFlags = kIsBogus;
return FALSE;
}
}
return TRUE;
}
UnicodeString::~UnicodeString()
{
releaseArray();
}
UnicodeString &
UnicodeString::operator=(const UnicodeString &src) {
return copyFrom(src);
}
UnicodeString &
UnicodeString::fastCopyFrom(const UnicodeString &src) {
return copyFrom(src, TRUE);
}
UnicodeString &
UnicodeString::copyFrom(const UnicodeString &src, UBool fastCopy) {
if(this == 0 || this == &src) {
return *this;
}
if(&src == 0 || src.isBogus()) {
setToBogus();
return *this;
}
releaseArray();
fLength = src.fLength;
if(fLength == 0) {
fArray = fStackBuffer;
fCapacity = US_STACKBUF_SIZE;
fFlags = kShortString;
return *this;
}
switch(src.fFlags) {
case kShortString:
fArray = fStackBuffer;
fCapacity = US_STACKBUF_SIZE;
fFlags = kShortString;
uprv_memcpy(fStackBuffer, src.fArray, fLength * U_SIZEOF_UCHAR);
break;
case kLongString:
((UnicodeString &)src).addRef();
fArray = src.fArray;
fCapacity = src.fCapacity;
fFlags = src.fFlags;
break;
case kReadonlyAlias:
if(fastCopy) {
fArray = src.fArray;
fCapacity = src.fCapacity;
fFlags = src.fFlags;
break;
}
case kWritableAlias:
if(allocate(fLength)) {
uprv_memcpy(fArray, src.fArray, fLength * U_SIZEOF_UCHAR);
break;
}
default:
fArray = 0;
fLength = 0;
fCapacity = 0;
fFlags = kIsBogus;
break;
}
return *this;
}
UnicodeString UnicodeString::unescape() const {
UnicodeString result;
for (int32_t i=0; i<length(); ) {
UChar32 c = charAt(i++);
if (c == 0x005C ) {
c = unescapeAt(i); if (c == (UChar32)0xFFFFFFFF) {
result.remove(); break; }
}
result.append(c);
}
return result;
}
UChar32 UnicodeString::unescapeAt(int32_t &offset) const {
return u_unescapeAt(UnicodeString_charAt, &offset, length(), (void*)this);
}
int8_t
UnicodeString::doCompare( int32_t start,
int32_t length,
const UChar *srcChars,
int32_t srcStart,
int32_t srcLength) const
{
if(isBogus()) {
return -1;
}
pinIndices(start, length);
if(srcChars == NULL) {
srcStart = srcLength = 0;
}
const UChar *chars = getArrayStart();
chars += start;
srcChars += srcStart;
int32_t minLength;
int8_t lengthResult;
if(srcLength < 0) {
srcLength = u_strlen(srcChars + srcStart);
}
if(length != srcLength) {
if(length < srcLength) {
minLength = length;
lengthResult = -1;
} else {
minLength = srcLength;
lengthResult = 1;
}
} else {
minLength = length;
lengthResult = 0;
}
if(minLength > 0 && chars != srcChars) {
int32_t result;
# if U_IS_BIG_ENDIAN
result = uprv_memcmp(chars, srcChars, minLength * sizeof(UChar));
if(result != 0) {
return (int8_t)(result >> 15 | 1);
}
# else
do {
result = ((int32_t)*(chars++) - (int32_t)*(srcChars++));
if(result != 0) {
return (int8_t)(result >> 15 | 1);
}
} while(--minLength > 0);
# endif
}
return lengthResult;
}
int8_t
UnicodeString::doCompareCodePointOrder(int32_t start,
int32_t length,
const UChar *srcChars,
int32_t srcStart,
int32_t srcLength) const
{
if(isBogus()) {
return -1;
}
pinIndices(start, length);
if(srcChars == NULL) {
srcStart = srcLength = 0;
}
int32_t diff = uprv_strCompare(fArray + start, length, srcChars + srcStart, srcLength, FALSE, TRUE);
if(diff!=0) {
return (int8_t)(diff >> 15 | 1);
} else {
return 0;
}
}
int8_t
UnicodeString::doCaseCompare(int32_t start,
int32_t length,
const UChar *srcChars,
int32_t srcStart,
int32_t srcLength,
uint32_t options) const
{
if(isBogus()) {
return -1;
}
pinIndices(start, length);
if(srcChars == NULL) {
srcStart = srcLength = 0;
}
const UChar *chars = getArrayStart();
chars += start;
srcChars += srcStart;
if(chars != srcChars) {
UErrorCode errorCode=U_ZERO_ERROR;
int32_t result=unorm_cmpEquivFold(chars, length, srcChars, srcLength,
options|U_COMPARE_IGNORE_CASE, &errorCode);
if(result!=0) {
return (int8_t)(result >> 24 | 1);
}
} else {
if(srcLength < 0) {
srcLength = u_strlen(srcChars + srcStart);
}
if(length != srcLength) {
return (int8_t)((length - srcLength) >> 24 | 1);
}
}
return 0;
}
int32_t
UnicodeString::getLength() const {
return length();
}
UChar
UnicodeString::getCharAt(int32_t offset) const {
return charAt(offset);
}
UChar32
UnicodeString::getChar32At(int32_t offset) const {
return char32At(offset);
}
int32_t
UnicodeString::countChar32(int32_t start, int32_t length) const {
pinIndices(start, length);
return u_countChar32(fArray+start, length);
}
UBool
UnicodeString::hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const {
pinIndices(start, length);
return u_strHasMoreChar32Than(fArray+start, length, number);
}
int32_t
UnicodeString::moveIndex32(int32_t index, int32_t delta) const {
if(index<0) {
index=0;
} else if(index>fLength) {
index=fLength;
}
if(delta>0) {
UTF_FWD_N(fArray, index, fLength, delta);
} else {
UTF_BACK_N(fArray, 0, index, -delta);
}
return index;
}
void
UnicodeString::doExtract(int32_t start,
int32_t length,
UChar *dst,
int32_t dstStart) const
{
pinIndices(start, length);
if(fArray + start != dst + dstStart) {
us_arrayCopy(getArrayStart(), start, dst, dstStart, length);
}
}
int32_t
UnicodeString::extract(UChar *dest, int32_t destCapacity,
UErrorCode &errorCode) const {
if(U_SUCCESS(errorCode)) {
if(isBogus() || destCapacity<0 || (destCapacity>0 && dest==0)) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
} else {
if(fLength>0 && fLength<=destCapacity && fArray!=dest) {
uprv_memcpy(dest, fArray, fLength*U_SIZEOF_UCHAR);
}
return u_terminateUChars(dest, destCapacity, fLength, &errorCode);
}
}
return fLength;
}
int32_t
UnicodeString::indexOf(const UChar *srcChars,
int32_t srcStart,
int32_t srcLength,
int32_t start,
int32_t length) const
{
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength == 0) {
return -1;
}
if(srcLength < 0 && srcChars[srcStart] == 0) {
return -1;
}
pinIndices(start, length);
const UChar *match = u_strFindFirst(fArray + start, length, srcChars + srcStart, srcLength);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
int32_t
UnicodeString::doIndexOf(UChar c,
int32_t start,
int32_t length) const
{
pinIndices(start, length);
const UChar *match = u_memchr(fArray + start, c, length);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
int32_t
UnicodeString::doIndexOf(UChar32 c,
int32_t start,
int32_t length) const {
pinIndices(start, length);
const UChar *match = u_memchr32(fArray + start, c, length);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
int32_t
UnicodeString::lastIndexOf(const UChar *srcChars,
int32_t srcStart,
int32_t srcLength,
int32_t start,
int32_t length) const
{
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength == 0) {
return -1;
}
if(srcLength < 0 && srcChars[srcStart] == 0) {
return -1;
}
pinIndices(start, length);
const UChar *match = u_strFindLast(fArray + start, length, srcChars + srcStart, srcLength);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
int32_t
UnicodeString::doLastIndexOf(UChar c,
int32_t start,
int32_t length) const
{
if(isBogus()) {
return -1;
}
pinIndices(start, length);
const UChar *match = u_memrchr(fArray + start, c, length);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
int32_t
UnicodeString::doLastIndexOf(UChar32 c,
int32_t start,
int32_t length) const {
pinIndices(start, length);
const UChar *match = u_memrchr32(fArray + start, c, length);
if(match == NULL) {
return -1;
} else {
return match - fArray;
}
}
UnicodeString&
UnicodeString::findAndReplace(int32_t start,
int32_t length,
const UnicodeString& oldText,
int32_t oldStart,
int32_t oldLength,
const UnicodeString& newText,
int32_t newStart,
int32_t newLength)
{
if(isBogus() || oldText.isBogus() || newText.isBogus()) {
return *this;
}
pinIndices(start, length);
oldText.pinIndices(oldStart, oldLength);
newText.pinIndices(newStart, newLength);
if(oldLength == 0) {
return *this;
}
while(length > 0 && length >= oldLength) {
int32_t pos = indexOf(oldText, oldStart, oldLength, start, length);
if(pos < 0) {
break;
} else {
replace(pos, oldLength, newText, newStart, newLength);
length -= pos + oldLength - start;
start = pos + newLength;
}
}
return *this;
}
void
UnicodeString::setToBogus()
{
releaseArray();
fArray = 0;
fCapacity = fLength = 0;
fFlags = kIsBogus;
}
void
UnicodeString::unBogus() {
if(fFlags & kIsBogus) {
fArray = fStackBuffer;
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fFlags = kShortString;
}
}
UnicodeString &
UnicodeString::setTo(UBool isTerminated,
const UChar *text,
int32_t textLength)
{
if(fFlags & kOpenGetBuffer) {
return *this;
}
if(text == NULL) {
releaseArray();
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fArray = fStackBuffer;
fFlags = kShortString;
return *this;
}
if( textLength < -1 ||
(textLength == -1 && !isTerminated) ||
(textLength >= 0 && isTerminated && text[textLength] != 0)
) {
setToBogus();
return *this;
}
releaseArray();
fArray = (UChar *)text;
if(textLength != -1) {
fLength = textLength;
fCapacity = isTerminated ? fLength + 1 : fLength;
} else {
fLength = u_strlen(text);
fCapacity = fLength + 1;
}
fFlags = kReadonlyAlias;
return *this;
}
UnicodeString &
UnicodeString::setTo(UChar *buffer,
int32_t buffLength,
int32_t buffCapacity) {
if(fFlags & kOpenGetBuffer) {
return *this;
}
if(buffer == NULL) {
releaseArray();
fLength = 0;
fCapacity = US_STACKBUF_SIZE;
fArray = fStackBuffer;
fFlags = kShortString;
return *this;
}
if(buffLength < 0 || buffLength > buffCapacity) {
setToBogus();
return *this;
}
releaseArray();
fArray = buffer;
fLength = buffLength;
fCapacity = buffCapacity;
fFlags = kWritableAlias;
return *this;
}
UnicodeString&
UnicodeString::setCharAt(int32_t offset,
UChar c)
{
if(cloneArrayIfNeeded() && fLength > 0) {
if(offset < 0) {
offset = 0;
} else if(offset >= fLength) {
offset = fLength - 1;
}
fArray[offset] = c;
}
return *this;
}
enum {
TO_LOWER,
TO_UPPER,
TO_TITLE,
FOLD_CASE
};
UnicodeString &
UnicodeString::toLower() {
return caseMap(0, Locale::getDefault(), 0, TO_LOWER);
}
UnicodeString &
UnicodeString::toLower(const Locale &locale) {
return caseMap(0, locale, 0, TO_LOWER);
}
UnicodeString &
UnicodeString::toUpper() {
return caseMap(0, Locale::getDefault(), 0, TO_UPPER);
}
UnicodeString &
UnicodeString::toUpper(const Locale &locale) {
return caseMap(0, locale, 0, TO_UPPER);
}
#if !UCONFIG_NO_BREAK_ITERATION
UnicodeString &
UnicodeString::toTitle(BreakIterator *titleIter) {
return caseMap(titleIter, Locale::getDefault(), 0, TO_TITLE);
}
UnicodeString &
UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) {
return caseMap(titleIter, locale, 0, TO_TITLE);
}
#endif
UnicodeString &
UnicodeString::foldCase(uint32_t options) {
return caseMap(0, Locale::getDefault(), options, FOLD_CASE);
}
UnicodeString &
UnicodeString::caseMap(BreakIterator *titleIter,
const Locale& locale,
uint32_t options,
int32_t toWhichCase) {
if(fLength <= 0) {
return *this;
}
UChar *oldArray = fArray;
int32_t oldLength = fLength;
int32_t *bufferToDelete = 0;
int32_t capacity;
if(fLength <= US_STACKBUF_SIZE) {
if(fArray == fStackBuffer) {
capacity = 2 * US_STACKBUF_SIZE; } else {
capacity = US_STACKBUF_SIZE;
}
} else {
capacity = fLength + 20;
}
if(!cloneArrayIfNeeded(capacity, capacity, FALSE, &bufferToDelete, TRUE)) {
return *this;
}
UErrorCode errorCode;
#if !UCONFIG_NO_BREAK_ITERATION
UBreakIterator *cTitleIter = 0;
if(toWhichCase == TO_TITLE) {
if(titleIter != 0) {
cTitleIter = (UBreakIterator *)titleIter;
} else {
errorCode = U_ZERO_ERROR;
cTitleIter = ubrk_open(UBRK_WORD, locale.getName(),
oldArray, oldLength,
&errorCode);
if(U_FAILURE(errorCode)) {
uprv_free(bufferToDelete);
setToBogus();
return *this;
}
}
}
#endif
do {
errorCode = U_ZERO_ERROR;
if(toWhichCase==TO_LOWER) {
fLength = u_internalStrToLower(fArray, fCapacity,
oldArray, oldLength,
0, oldLength,
locale.getName(),
&errorCode);
} else if(toWhichCase==TO_UPPER) {
fLength = u_internalStrToUpper(fArray, fCapacity,
oldArray, oldLength,
locale.getName(),
&errorCode);
#if !UCONFIG_NO_BREAK_ITERATION
} else if(toWhichCase==TO_TITLE) {
fLength = u_internalStrToTitle(fArray, fCapacity,
oldArray, oldLength,
cTitleIter, locale.getName(),
&errorCode);
#endif
} else {
fLength = u_internalStrFoldCase(fArray, fCapacity,
oldArray, oldLength,
options,
&errorCode);
}
} while(errorCode==U_BUFFER_OVERFLOW_ERROR && cloneArrayIfNeeded(fLength, fLength, FALSE));
#if !UCONFIG_NO_BREAK_ITERATION
if(cTitleIter != 0 && titleIter == 0) {
ubrk_close(cTitleIter);
}
#endif
if (bufferToDelete) {
uprv_free(bufferToDelete);
}
if(U_FAILURE(errorCode)) {
setToBogus();
}
return *this;
}
UnicodeString&
UnicodeString::doReplace( int32_t start,
int32_t length,
const UnicodeString& src,
int32_t srcStart,
int32_t srcLength)
{
if(!src.isBogus()) {
src.pinIndices(srcStart, srcLength);
return doReplace(start, length, src.getArrayStart(), srcStart, srcLength);
} else {
return doReplace(start, length, 0, 0, 0);
}
}
UnicodeString&
UnicodeString::doReplace(int32_t start,
int32_t length,
const UChar *srcChars,
int32_t srcStart,
int32_t srcLength)
{
if(isBogus()) {
return *this;
}
if(srcChars == 0) {
srcStart = srcLength = 0;
} else if(srcLength < 0) {
srcLength = u_strlen(srcChars + srcStart);
}
int32_t *bufferToDelete = 0;
UChar *oldArray = fArray;
int32_t oldLength = fLength;
pinIndices(start, length);
int32_t newSize = oldLength - length + srcLength;
if(!cloneArrayIfNeeded(newSize, newSize + (newSize >> 2) + kGrowSize,
FALSE, &bufferToDelete)
) {
return *this;
}
if(fArray != oldArray) {
us_arrayCopy(oldArray, 0, fArray, 0, start);
us_arrayCopy(oldArray, start + length,
fArray, start + srcLength,
oldLength - (start + length));
} else if(length != srcLength) {
us_arrayCopy(oldArray, start + length,
fArray, start + srcLength,
oldLength - (start + length));
}
us_arrayCopy(srcChars, srcStart, getArrayStart(), start, srcLength);
fLength = newSize;
if (bufferToDelete) {
uprv_free(bufferToDelete);
}
return *this;
}
void
UnicodeString::handleReplaceBetween(int32_t start,
int32_t limit,
const UnicodeString& text) {
replaceBetween(start, limit, text);
}
void
UnicodeString::copy(int32_t start, int32_t limit, int32_t dest) {
if (limit <= start) {
return; }
UChar* text = (UChar*) uprv_malloc( sizeof(UChar) * (limit - start) );
extractBetween(start, limit, text, 0);
insert(dest, text, 0, limit - start);
uprv_free(text);
}
UBool Replaceable::hasMetaData() const {
return TRUE;
}
UBool UnicodeString::hasMetaData() const {
return FALSE;
}
UnicodeString&
UnicodeString::doReverse(int32_t start,
int32_t length)
{
if(fLength <= 1 || !cloneArrayIfNeeded()) {
return *this;
}
pinIndices(start, length);
UChar *left = getArrayStart() + start;
UChar *right = getArrayStart() + start + length;
UChar swap;
UBool hasSupplementary = FALSE;
while(left < --right) {
hasSupplementary |= (UBool)UTF_IS_LEAD(swap = *left);
hasSupplementary |= (UBool)UTF_IS_LEAD(*left++ = *right);
*right = swap;
}
if(hasSupplementary) {
UChar swap2;
left = getArrayStart() + start;
right = getArrayStart() + start + length - 1; while(left < right) {
if(UTF_IS_TRAIL(swap = *left) && UTF_IS_LEAD(swap2 = *(left + 1))) {
*left++ = swap2;
*left++ = swap;
} else {
++left;
}
}
}
return *this;
}
UBool
UnicodeString::padLeading(int32_t targetLength,
UChar padChar)
{
if(fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
return FALSE;
} else {
int32_t start = targetLength - fLength;
us_arrayCopy(fArray, 0, fArray, start, fLength);
while(--start >= 0) {
fArray[start] = padChar;
}
fLength = targetLength;
return TRUE;
}
}
UBool
UnicodeString::padTrailing(int32_t targetLength,
UChar padChar)
{
if(fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
return FALSE;
} else {
int32_t length = targetLength;
while(--length >= fLength) {
fArray[length] = padChar;
}
fLength = targetLength;
return TRUE;
}
}
UnicodeString&
UnicodeString::trim()
{
if(isBogus()) {
return *this;
}
UChar32 c;
int32_t i = fLength, length;
for(;;) {
length = i;
if(i <= 0) {
break;
}
UTF_PREV_CHAR(fArray, 0, i, c);
if(!(c == 0x20 || u_isWhitespace(c))) {
break;
}
}
if(length < fLength) {
fLength = length;
}
int32_t start;
i = 0;
for(;;) {
start = i;
if(i >= length) {
break;
}
UTF_NEXT_CHAR(fArray, i, length, c);
if(!(c == 0x20 || u_isWhitespace(c))) {
break;
}
}
if(start > 0) {
doReplace(0, start, 0, 0, 0);
}
return *this;
}
int32_t
UnicodeString::doHashCode() const
{
int32_t hashCode = uhash_hashUCharsN(getArrayStart(), fLength);
if (hashCode == kInvalidHashCode) {
hashCode = kEmptyHashCode;
}
return hashCode;
}
int32_t
UnicodeString::extract(int32_t start,
int32_t length,
char *target,
uint32_t dstSize,
const char *codepage) const
{
if((dstSize > 0 && target == 0)) {
return 0;
}
pinIndices(start, length);
UConverter *converter;
UErrorCode status = U_ZERO_ERROR;
if(length == 0) {
if(dstSize >= 0x80000000) {
dstSize=0x7fffffff;
}
return u_terminateChars(target, dstSize, 0, &status);
}
if (codepage == 0) {
converter = u_getDefaultConverter(&status);
} else if (*codepage == 0) {
int32_t destLength;
if(dstSize >= 0x80000000) {
destLength = length;
dstSize=0x7fffffff;
} else if(length <= (int32_t)dstSize) {
destLength = length;
} else {
destLength = (int32_t)dstSize;
}
u_UCharsToChars(getArrayStart() + start, target, destLength);
return u_terminateChars(target, (int32_t)dstSize, length, &status);
} else {
converter = ucnv_open(codepage, &status);
}
length = doExtract(start, length, target, (int32_t)dstSize, converter, status);
if (codepage == 0) {
u_releaseDefaultConverter(converter);
} else {
ucnv_close(converter);
}
return length;
}
int32_t
UnicodeString::extract(char *dest, int32_t destCapacity,
UConverter *cnv,
UErrorCode &errorCode) const {
if(U_FAILURE(errorCode)) {
return 0;
}
if(isBogus() || destCapacity<0 || (destCapacity>0 && dest==0)) {
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
if(fLength<=0) {
return u_terminateChars(dest, destCapacity, 0, &errorCode);
}
UBool isDefaultConverter;
if(cnv==0) {
isDefaultConverter=TRUE;
cnv=u_getDefaultConverter(&errorCode);
if(U_FAILURE(errorCode)) {
return 0;
}
} else {
isDefaultConverter=FALSE;
ucnv_resetFromUnicode(cnv);
}
int32_t length=doExtract(0, fLength, dest, destCapacity, cnv, errorCode);
if(isDefaultConverter) {
u_releaseDefaultConverter(cnv);
}
return length;
}
void
UnicodeString::extractBetween(int32_t start,
int32_t limit,
UnicodeString& target) const
{ doExtract(start, limit - start, target); }
int32_t
UnicodeString::doExtract(int32_t start, int32_t length,
char *dest, int32_t destCapacity,
UConverter *cnv,
UErrorCode &errorCode) const {
if(U_FAILURE(errorCode)) {
if(destCapacity!=0) {
*dest=0;
}
return 0;
}
const UChar *src=fArray+start, *srcLimit=src+length;
char *originalDest=dest;
const char *destLimit;
if(destCapacity==0) {
destLimit=dest=0;
} else if(destCapacity==-1) {
destLimit=(char*)U_MAX_PTR(dest);
destCapacity=0x7fffffff;
} else {
destLimit=dest+destCapacity;
}
ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, &errorCode);
length=(int32_t)(dest-originalDest);
if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
char buffer[1024];
destLimit=buffer+sizeof(buffer);
do {
dest=buffer;
errorCode=U_ZERO_ERROR;
ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, &errorCode);
length+=(int32_t)(dest-buffer);
} while(errorCode==U_BUFFER_OVERFLOW_ERROR);
}
return u_terminateChars(originalDest, destCapacity, length, &errorCode);
}
void
UnicodeString::doCodepageCreate(const char *codepageData,
int32_t dataLength,
const char *codepage)
{
if(codepageData == 0 || dataLength <= 0) {
return;
}
UErrorCode status = U_ZERO_ERROR;
UConverter *converter = (codepage == 0 ?
u_getDefaultConverter(&status) :
*codepage == 0 ?
0 :
ucnv_open(codepage, &status));
if(U_FAILURE(status)) {
setToBogus();
return;
}
if(converter == 0) {
if(cloneArrayIfNeeded(dataLength, dataLength, FALSE)) {
u_charsToUChars(codepageData, getArrayStart(), dataLength);
fLength = dataLength;
} else {
setToBogus();
}
return;
}
doCodepageCreate(codepageData, dataLength, converter, status);
if(U_FAILURE(status)) {
setToBogus();
}
if(codepage == 0) {
u_releaseDefaultConverter(converter);
} else {
ucnv_close(converter);
}
}
void
UnicodeString::doCodepageCreate(const char *codepageData,
int32_t dataLength,
UConverter *converter,
UErrorCode &status) {
if(U_FAILURE(status)) {
return;
}
const char *mySource = codepageData;
const char *mySourceEnd = mySource + dataLength;
UChar *myTarget;
int32_t arraySize = dataLength + (dataLength >> 2);
UBool doCopyArray = FALSE;
for(;;) {
if(!cloneArrayIfNeeded(arraySize, arraySize, doCopyArray)) {
setToBogus();
break;
}
myTarget = fArray + fLength;
ucnv_toUnicode(converter, &myTarget, fArray + fCapacity,
&mySource, mySourceEnd, 0, TRUE, &status);
fLength = (int32_t)(myTarget - fArray);
if(status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
doCopyArray = TRUE;
arraySize = (int32_t)(fLength + 2 * (mySourceEnd - mySource));
} else {
break;
}
}
}
UChar *
UnicodeString::getBuffer(int32_t minCapacity) {
if(minCapacity>=-1 && cloneArrayIfNeeded(minCapacity)) {
fFlags|=kOpenGetBuffer;
fLength=0;
return fArray;
} else {
return 0;
}
}
void
UnicodeString::releaseBuffer(int32_t newLength) {
if(fFlags&kOpenGetBuffer && newLength>=-1) {
if(newLength==-1) {
const UChar *p=fArray, *limit=fArray+fCapacity;
while(p<limit && *p!=0) {
++p;
}
fLength=(int32_t)(p-fArray);
} else if(newLength<=fCapacity) {
fLength=newLength;
} else {
fLength=fCapacity;
}
fFlags&=~kOpenGetBuffer;
}
}
UBool
UnicodeString::cloneArrayIfNeeded(int32_t newCapacity,
int32_t growCapacity,
UBool doCopyArray,
int32_t **pBufferToDelete,
UBool forceClone) {
if(newCapacity == -1) {
newCapacity = fCapacity;
}
if((fFlags&(kOpenGetBuffer|kIsBogus))!=0) {
return FALSE;
}
if(forceClone ||
fFlags & kBufferIsReadonly ||
fFlags & kRefCounted && refCount() > 1 ||
newCapacity > fCapacity
) {
UChar *array = fArray;
uint16_t flags = fFlags;
if(growCapacity == -1) {
growCapacity = newCapacity;
} else if(newCapacity <= US_STACKBUF_SIZE && growCapacity > US_STACKBUF_SIZE) {
growCapacity = US_STACKBUF_SIZE;
}
if(allocate(growCapacity) ||
newCapacity < growCapacity && allocate(newCapacity)
) {
if(doCopyArray) {
if(fCapacity < fLength) {
fLength = fCapacity;
}
us_arrayCopy(array, 0, fArray, 0, fLength);
} else {
fLength = 0;
}
if(flags & kRefCounted) {
int32_t *pRefCount = ((int32_t *)array - 1);
if(umtx_atomic_dec(pRefCount) == 0) {
if(pBufferToDelete == 0) {
uprv_free(pRefCount);
} else {
*pBufferToDelete = pRefCount;
}
}
}
} else {
fArray = array;
fFlags = flags;
setToBogus();
return FALSE;
}
}
return TRUE;
}
U_NAMESPACE_END