1999-08-16 21:50:52 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
1999-11-22 20:25:35 +00:00
|
|
|
* Copyright (C) 1999, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*
|
|
|
|
* File unistr.cpp
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 09/25/98 stephen Creation.
|
|
|
|
* 04/20/99 stephen Overhauled per 4/16 code review.
|
|
|
|
* 07/09/99 stephen Renamed {hi,lo},{byte,word} to icu_X for HP/UX
|
1999-11-19 23:13:41 +00:00
|
|
|
* 11/18/99 aliu Added handleReplaceBetween() to make inherit from
|
|
|
|
* Replaceable.
|
1999-08-16 21:50:52 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "unicode/locid.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/ustring.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "mutex.h"
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/unistr.h"
|
2000-03-31 23:50:49 +00:00
|
|
|
#include "uhash.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-25 21:33:15 +00:00
|
|
|
#if U_IOSTREAM_SOURCE >= 199711
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
#elif U_IOSTREAM_SOURCE >= 198506
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <iostream.h>
|
2000-04-25 21:33:15 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-25 21:33:15 +00:00
|
|
|
#if 0
|
|
|
|
//DEBUGGING
|
1999-08-16 21:50:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
// END DEBUGGING
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Local function definitions for now
|
|
|
|
|
|
|
|
// need to copy areas that may overlap
|
|
|
|
inline void
|
|
|
|
us_arrayCopy(const UChar *src, int32_t srcStart,
|
|
|
|
UChar *dst, int32_t dstStart, int32_t count)
|
1999-10-06 00:05:42 +00:00
|
|
|
{
|
|
|
|
if(count>0) {
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_memmove(dst+dstStart, src+srcStart, (size_t)(count*sizeof(*src)));
|
1999-10-06 00:05:42 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UConverter* UnicodeString::fgDefaultConverter = 0;
|
|
|
|
|
2000-06-29 19:36:20 +00:00
|
|
|
//========================================
|
|
|
|
// Unescaping
|
|
|
|
//========================================
|
|
|
|
U_COMMON_API UnicodeString
|
|
|
|
u_unescape(const UnicodeString &s) {
|
|
|
|
/* ### TBD */
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_COMMON_API UChar32
|
|
|
|
u_unescape(const UnicodeString &s, int32_t &offset) {
|
|
|
|
/* ### TBD */
|
|
|
|
return 0xffff;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//========================================
|
|
|
|
// Constructors
|
|
|
|
//========================================
|
|
|
|
UnicodeString::UnicodeString()
|
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{}
|
|
|
|
|
2000-04-19 20:39:54 +00:00
|
|
|
UnicodeString::UnicodeString(int32_t capacity, UChar32 c, int32_t count)
|
1999-08-16 21:50:52 +00:00
|
|
|
: fArray(0),
|
|
|
|
fLength(0),
|
2000-03-29 01:13:49 +00:00
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
|
|
|
fFlags(0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-04-19 20:39:54 +00:00
|
|
|
if(count <= 0) {
|
|
|
|
// just allocate and do not do anything else
|
|
|
|
allocate(capacity);
|
|
|
|
} else {
|
|
|
|
// count > 0, allocate and fill the new string with count c's
|
|
|
|
int32_t unitCount = UTF_CHAR_LENGTH(c), length = count * unitCount;
|
|
|
|
if(capacity < length) {
|
|
|
|
capacity = length;
|
|
|
|
}
|
|
|
|
if(allocate(capacity)) {
|
|
|
|
int32_t i = 0;
|
|
|
|
|
|
|
|
// fill the new string with c
|
|
|
|
if(unitCount == 1) {
|
|
|
|
// fill with length UChars
|
|
|
|
while(i < length) {
|
|
|
|
fArray[i++] = (UChar)c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// get the code units for c
|
|
|
|
UChar units[UTF_MAX_CHAR_LENGTH];
|
|
|
|
UTF_APPEND_CHAR_UNSAFE(units, i, c);
|
|
|
|
|
|
|
|
// now it must be i==unitCount
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
// for Unicode, unitCount can only be 1, 2, 3, or 4
|
|
|
|
// 1 is handled above
|
|
|
|
switch(unitCount) {
|
|
|
|
case 2:
|
|
|
|
while(i < length) {
|
|
|
|
fArray[i++]=units[0];
|
|
|
|
fArray[i++]=units[1];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
while(i < length) {
|
|
|
|
fArray[i++]=units[0];
|
|
|
|
fArray[i++]=units[1];
|
|
|
|
fArray[i++]=units[2];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
while(i < length) {
|
|
|
|
fArray[i++]=units[0];
|
|
|
|
fArray[i++]=units[1];
|
|
|
|
fArray[i++]=units[2];
|
|
|
|
fArray[i++]=units[3];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fLength = length;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString::UnicodeString(UChar ch)
|
|
|
|
: fArray(fStackBuffer),
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength(1),
|
1999-08-16 21:50:52 +00:00
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
fStackBuffer[0] = ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-04-19 20:39:54 +00:00
|
|
|
UnicodeString::UnicodeString(UChar32 ch)
|
2000-04-01 00:21:57 +00:00
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(1),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
|
|
|
UTextOffset i = 0;
|
|
|
|
UTF_APPEND_CHAR(fStackBuffer, i, US_STACKBUF_SIZE, ch);
|
|
|
|
fLength = i;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString::UnicodeString(const UChar *text)
|
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
doReplace(0, 0, text, 0, u_strlen(text));
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::UnicodeString(const UChar *text,
|
|
|
|
int32_t textLength)
|
1999-08-16 21:50:52 +00:00
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
doReplace(0, 0, text, 0, textLength);
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UnicodeString::UnicodeString(UBool isTerminated,
|
2000-04-03 23:53:59 +00:00
|
|
|
const UChar *text,
|
1999-12-01 17:51:53 +00:00
|
|
|
int32_t textLength)
|
2000-04-03 23:53:59 +00:00
|
|
|
: fArray((UChar *)text),
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength(textLength),
|
|
|
|
fCapacity(isTerminated ? textLength + 1 : textLength),
|
|
|
|
fFlags(kReadonlyAlias)
|
1999-12-01 17:51:53 +00:00
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(text == 0 || textLength < -1 || textLength == -1 && !isTerminated) {
|
|
|
|
setToBogus();
|
|
|
|
} else if(textLength == -1) {
|
|
|
|
// text is terminated, or else it would have failed the above test
|
|
|
|
fLength = u_strlen(text);
|
|
|
|
fCapacity = fLength + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString::UnicodeString(UChar *buff,
|
|
|
|
int32_t bufLength,
|
|
|
|
int32_t buffCapacity)
|
|
|
|
: fArray(buff),
|
|
|
|
fLength(bufLength),
|
|
|
|
fCapacity(buffCapacity),
|
|
|
|
fFlags(kWriteableAlias)
|
|
|
|
{
|
|
|
|
if(buff == 0 || bufLength < 0 || bufLength > buffCapacity) {
|
1999-12-01 17:51:53 +00:00
|
|
|
setToBogus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString::UnicodeString(const char *codepageData,
|
2000-03-29 01:13:49 +00:00
|
|
|
const char *codepage)
|
1999-08-16 21:50:52 +00:00
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepageData != 0) {
|
1999-12-28 23:39:02 +00:00
|
|
|
doCodepageCreate(codepageData, uprv_strlen(codepageData), codepage);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString::UnicodeString(const char *codepageData,
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t dataLength,
|
|
|
|
const char *codepage)
|
1999-08-16 21:50:52 +00:00
|
|
|
: fArray(fStackBuffer),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
1999-12-03 00:04:14 +00:00
|
|
|
if(codepageData != 0) {
|
|
|
|
doCodepageCreate(codepageData, dataLength, codepage);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::UnicodeString(const UnicodeString& that)
|
2000-04-10 23:26:18 +00:00
|
|
|
: Replaceable(),
|
|
|
|
fArray(fStackBuffer),
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
|
|
|
*this = that;
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// array allocation
|
|
|
|
//========================================
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::allocate(int32_t capacity) {
|
|
|
|
if(capacity <= US_STACKBUF_SIZE) {
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
|
|
|
} else {
|
|
|
|
// count bytes for the refCounter and the string capacity, and
|
|
|
|
// round up to a multiple of 16; then divide by 4 and allocate int32_t's
|
|
|
|
// to be safely aligned for the refCount
|
|
|
|
int32_t words = ((sizeof(int32_t) + capacity * U_SIZEOF_UCHAR + 15) & ~15) >> 2;
|
|
|
|
int32_t *array = new int32_t[words];
|
|
|
|
if(array != 0) {
|
|
|
|
// set initial refCount and point behind the refCount
|
|
|
|
*array++ = 1;
|
|
|
|
|
|
|
|
// have fArray point to the first UChar
|
|
|
|
fArray = (UChar *)array;
|
|
|
|
fCapacity = (words - 1) * (sizeof(int32_t) / U_SIZEOF_UCHAR);
|
|
|
|
fFlags = kLongString;
|
|
|
|
} else {
|
|
|
|
fArray = 0;
|
|
|
|
fCapacity = 0;
|
|
|
|
fFlags = kIsBogus;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//========================================
|
|
|
|
// Destructor
|
|
|
|
//========================================
|
|
|
|
UnicodeString::~UnicodeString()
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
releaseArray();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Assignment
|
|
|
|
//========================================
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::operator= (const UnicodeString& src)
|
|
|
|
{
|
1999-12-01 21:28:18 +00:00
|
|
|
// if assigning to ourselves, do nothing
|
2000-03-29 01:13:49 +00:00
|
|
|
if(this == 0 || this == &src) {
|
1999-12-01 21:28:18 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// is the right side bogus?
|
|
|
|
if(&src == 0 || src.isBogus()) {
|
1999-12-01 21:28:18 +00:00
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// delete the current contents
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
// we always copy the length and the hash code
|
|
|
|
fLength = src.fLength;
|
|
|
|
|
|
|
|
switch(src.fFlags) {
|
|
|
|
case kShortString:
|
|
|
|
// short string using the stack buffer, do the same
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
|
|
|
if(fLength > 0) {
|
|
|
|
uprv_memcpy(fStackBuffer, src.fArray, fLength * U_SIZEOF_UCHAR);
|
1999-12-01 21:28:18 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
break;
|
|
|
|
case kLongString:
|
|
|
|
// src uses a refCounted string buffer, use that buffer with refCount
|
|
|
|
// src is const, use a cast - we don't really change it
|
|
|
|
((UnicodeString &)src).addRef();
|
|
|
|
// fall through to readonly alias copying: copy all fields
|
|
|
|
case kReadonlyAlias:
|
|
|
|
// src is a readonly alias, do the same
|
|
|
|
fArray = src.fArray;
|
|
|
|
fCapacity = src.fCapacity;
|
|
|
|
fFlags = src.fFlags;
|
|
|
|
break;
|
|
|
|
case kWriteableAlias:
|
|
|
|
// src is a writeable alias; we make a copy of that instead
|
|
|
|
if(allocate(fLength)) {
|
|
|
|
if(fLength > 0) {
|
|
|
|
uprv_memcpy(fArray, src.fArray, fLength * U_SIZEOF_UCHAR);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// if there is not enough memory, then fall through to setting to bogus
|
|
|
|
default:
|
|
|
|
// if src is bogus, set ourselves to bogus
|
|
|
|
// do not call setToBogus() here because fArray and fFlags are not consistent here
|
|
|
|
fArray = 0;
|
|
|
|
fLength = 0;
|
|
|
|
fCapacity = 0;
|
|
|
|
fFlags = kIsBogus;
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Miscellaneous operations
|
|
|
|
//========================================
|
|
|
|
int32_t
|
|
|
|
UnicodeString::numDisplayCells( UTextOffset start,
|
|
|
|
int32_t length,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool asian) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
2000-04-06 16:47:20 +00:00
|
|
|
UChar32 c;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t result = 0;
|
|
|
|
UTextOffset limit = start + length;
|
|
|
|
|
|
|
|
while(start < limit) {
|
2000-04-06 16:47:20 +00:00
|
|
|
UTF_NEXT_CHAR(fArray, start, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
switch(Unicode::getCellWidth(c)) {
|
|
|
|
case Unicode::ZERO_WIDTH:
|
|
|
|
break;;
|
|
|
|
|
|
|
|
case Unicode::HALF_WIDTH:
|
|
|
|
result += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Unicode::FULL_WIDTH:
|
|
|
|
result += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Unicode::NEUTRAL:
|
|
|
|
result += (asian ? 2 : 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCharReference
|
|
|
|
UnicodeString::operator[] (UTextOffset pos)
|
|
|
|
{
|
|
|
|
return UCharReference(this, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Read-only implementation
|
|
|
|
//========================================
|
|
|
|
int8_t
|
|
|
|
UnicodeString::doCompare( UTextOffset start,
|
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
|
|
|
UTextOffset srcStart,
|
|
|
|
int32_t srcLength) const
|
|
|
|
{
|
1999-12-07 00:12:31 +00:00
|
|
|
// compare illegal string values
|
|
|
|
if(isBogus()) {
|
|
|
|
if(srcChars==0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(srcChars==0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
// get the correct pointer
|
|
|
|
const UChar *chars = getArrayStart();
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// are we comparing the same buffer contents?
|
|
|
|
if(chars + start == srcChars + srcStart) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:12:31 +00:00
|
|
|
UTextOffset minLength;
|
|
|
|
int8_t lengthResult;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-07 00:12:31 +00:00
|
|
|
// are we comparing different lengths?
|
|
|
|
if(length != srcLength) {
|
|
|
|
if(length < srcLength) {
|
|
|
|
minLength = length;
|
|
|
|
lengthResult = -1;
|
|
|
|
} else {
|
|
|
|
minLength = srcLength;
|
|
|
|
lengthResult = 1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-07 00:12:31 +00:00
|
|
|
} else {
|
|
|
|
minLength = length;
|
|
|
|
lengthResult = 0;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-07 00:12:31 +00:00
|
|
|
/*
|
1999-12-28 23:39:02 +00:00
|
|
|
* note that uprv_memcmp() returns an int but we return an int8_t;
|
1999-12-07 00:12:31 +00:00
|
|
|
* we need to take care not to truncate the result -
|
|
|
|
* one way to do this is to right-shift the value to
|
|
|
|
* move the sign bit into the lower 8 bits and making sure that this
|
|
|
|
* does not become 0 itself
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(minLength > 0) {
|
|
|
|
int32_t result;
|
|
|
|
|
|
|
|
if(U_IS_BIG_ENDIAN) {
|
|
|
|
// big-endian: byte comparison works
|
1999-12-28 23:39:02 +00:00
|
|
|
result = uprv_memcmp(chars + start, srcChars + srcStart, minLength * sizeof(UChar));
|
1999-12-07 00:12:31 +00:00
|
|
|
if(result != 0) {
|
|
|
|
return (int8_t)(result >> 15 | 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// little-endian: compare UChar units
|
|
|
|
chars += start;
|
|
|
|
srcChars += srcStart;
|
|
|
|
do {
|
|
|
|
result = ((int32_t)*chars - (int32_t)*srcChars);
|
|
|
|
if(result != 0) {
|
|
|
|
return (int8_t)(result >> 15 | 1);
|
|
|
|
}
|
|
|
|
++chars;
|
|
|
|
++srcChars;
|
|
|
|
} while(--minLength > 0);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
1999-12-07 00:12:31 +00:00
|
|
|
return lengthResult;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeString::doExtract(UTextOffset start,
|
|
|
|
int32_t length,
|
|
|
|
UChar *dst,
|
|
|
|
UTextOffset dstStart) const
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
// do not copy anything if we alias dst itself
|
|
|
|
if(fArray + start != dst + dstStart) {
|
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
us_arrayCopy(getArrayStart(), start, dst, dstStart, length);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
UTextOffset
|
|
|
|
UnicodeString::indexOf(const UChar *srcChars,
|
|
|
|
UTextOffset srcStart,
|
|
|
|
int32_t srcLength,
|
|
|
|
UTextOffset start,
|
|
|
|
int32_t length) const
|
|
|
|
{
|
|
|
|
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we will only work with srcLength-1
|
|
|
|
--srcLength;
|
|
|
|
|
|
|
|
// get the indices within bounds
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
// set length for the last possible match start position
|
|
|
|
// note the --srcLength above
|
|
|
|
length -= srcLength;
|
|
|
|
|
|
|
|
if(length <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar *array = getArrayStart();
|
|
|
|
UTextOffset limit = start + length;
|
|
|
|
|
|
|
|
// search for the first char, then compare the rest of the string
|
|
|
|
// increment srcStart here for that, matching the --srcLength above
|
|
|
|
UChar ch = srcChars[srcStart++];
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(array[start] == ch && (srcLength == 0 || compare(start + 1, srcLength, srcChars, srcStart, srcLength) == 0)) {
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
} while(++start < limit);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UTextOffset
|
|
|
|
UnicodeString::doIndexOf(UChar c,
|
|
|
|
UTextOffset start,
|
|
|
|
int32_t length) const
|
|
|
|
{
|
|
|
|
// pin indices
|
|
|
|
pinIndices(start, length);
|
1999-12-03 00:04:14 +00:00
|
|
|
if(length == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// find the first occurrence of c
|
|
|
|
const UChar *begin = getArrayStart() + start;
|
|
|
|
const UChar *limit = begin + length;
|
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
do {
|
|
|
|
if(*begin == c) {
|
|
|
|
return begin - getArrayStart();
|
|
|
|
}
|
|
|
|
} while(++begin < limit);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
UTextOffset
|
|
|
|
UnicodeString::lastIndexOf(const UChar *srcChars,
|
|
|
|
UTextOffset srcStart,
|
|
|
|
int32_t srcLength,
|
|
|
|
UTextOffset start,
|
|
|
|
int32_t length) const
|
|
|
|
{
|
|
|
|
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we will only work with srcLength-1
|
|
|
|
--srcLength;
|
|
|
|
|
|
|
|
// get the indices within bounds
|
|
|
|
pinIndices(start, length);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
// set length for the last possible match start position
|
|
|
|
// note the --srcLength above
|
|
|
|
length -= srcLength;
|
|
|
|
|
|
|
|
if(length <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar *array = getArrayStart();
|
|
|
|
UTextOffset pos;
|
|
|
|
|
|
|
|
// search for the first char, then compare the rest of the string
|
|
|
|
// increment srcStart here for that, matching the --srcLength above
|
|
|
|
UChar ch = srcChars[srcStart++];
|
|
|
|
|
|
|
|
pos = start + length;
|
|
|
|
do {
|
|
|
|
if(array[--pos] == ch && (srcLength == 0 || compare(pos + 1, srcLength, srcChars, srcStart, srcLength) == 0)) {
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
} while(pos > start);
|
|
|
|
|
|
|
|
return -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UTextOffset
|
|
|
|
UnicodeString::doLastIndexOf(UChar c,
|
|
|
|
UTextOffset start,
|
|
|
|
int32_t length) const
|
|
|
|
{
|
1999-12-08 00:15:46 +00:00
|
|
|
if(isBogus()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// pin indices
|
|
|
|
pinIndices(start, length);
|
1999-12-03 00:04:14 +00:00
|
|
|
if(length == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
const UChar *begin = getArrayStart() + start;
|
|
|
|
const UChar *limit = begin + length;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
do {
|
|
|
|
if(*--limit == c) {
|
|
|
|
return limit - getArrayStart();
|
|
|
|
}
|
|
|
|
} while(limit > begin);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
return -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 03:22:12 +00:00
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::findAndReplace(UTextOffset start,
|
|
|
|
int32_t length,
|
|
|
|
const UnicodeString& oldText,
|
|
|
|
UTextOffset oldStart,
|
|
|
|
int32_t oldLength,
|
|
|
|
const UnicodeString& newText,
|
|
|
|
UTextOffset newStart,
|
|
|
|
int32_t newLength)
|
|
|
|
{
|
|
|
|
if(isBogus() || oldText.isBogus() || newText.isBogus()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
pinIndices(start, length);
|
|
|
|
oldText.pinIndices(oldStart, oldLength);
|
|
|
|
newText.pinIndices(newStart, newLength);
|
|
|
|
|
2000-04-01 00:21:57 +00:00
|
|
|
if(oldLength == 0) {
|
1999-12-07 03:22:12 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-04-01 00:21:57 +00:00
|
|
|
while(length > 0 && length >= oldLength) {
|
1999-12-07 03:22:12 +00:00
|
|
|
UTextOffset pos = indexOf(oldText, oldStart, oldLength, start, length);
|
|
|
|
if(pos < 0) {
|
|
|
|
// no more oldText's here: done
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// we found oldText, replace it by newText and go beyond it
|
|
|
|
replace(pos, oldLength, newText, newStart, newLength);
|
|
|
|
length -= pos + oldLength - start;
|
|
|
|
start = pos + newLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Write implementation
|
|
|
|
//========================================
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
void
|
|
|
|
UnicodeString::setToBogus()
|
|
|
|
{
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
fArray = 0;
|
|
|
|
fCapacity = fLength = 0;
|
|
|
|
fFlags = kIsBogus;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTo() analogous to the readonly-aliasing constructor with the same signature
|
|
|
|
UnicodeString &
|
2000-05-18 22:08:39 +00:00
|
|
|
UnicodeString::setTo(UBool isTerminated,
|
2000-03-29 01:13:49 +00:00
|
|
|
const UChar *text,
|
|
|
|
int32_t textLength)
|
|
|
|
{
|
|
|
|
if(text == 0 || textLength < -1 || textLength == -1 && !isTerminated) {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
fArray = (UChar *)text;
|
|
|
|
if(textLength != -1) {
|
|
|
|
fLength = textLength;
|
|
|
|
} else {
|
|
|
|
// text is terminated, or else it would have failed the above test
|
|
|
|
fLength = u_strlen(text);
|
|
|
|
fCapacity = fLength + 1;
|
|
|
|
}
|
|
|
|
|
2000-04-03 23:53:59 +00:00
|
|
|
fCapacity = isTerminated ? fLength + 1 : fLength;
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags = kReadonlyAlias;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTo() analogous to the writeable-aliasing constructor with the same signature
|
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::setTo(UChar *buffer,
|
|
|
|
int32_t buffLength,
|
|
|
|
int32_t buffCapacity) {
|
|
|
|
if(buffer == 0 || buffLength < 0 || buffLength > buffCapacity) {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
fArray = buffer;
|
|
|
|
fLength = buffLength;
|
|
|
|
fCapacity = buffCapacity;
|
|
|
|
fFlags = kWriteableAlias;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::setCharAt(UTextOffset offset,
|
|
|
|
UChar c)
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(cloneArrayIfNeeded()) {
|
|
|
|
if(offset < 0) {
|
|
|
|
offset = 0;
|
|
|
|
} else if(offset >= fLength) {
|
|
|
|
offset = fLength - 1;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
fArray[offset] = c;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::toUpper()
|
|
|
|
{ return toUpper(Locale::getDefault()); }
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::toLower()
|
|
|
|
{ return toLower(Locale::getDefault()); }
|
|
|
|
|
2000-04-03 23:53:59 +00:00
|
|
|
/*
|
|
|
|
* The following toUpper() and toLower() implementations are designed
|
|
|
|
* for UTF-16 and UTF-32, not for UTF-8.
|
|
|
|
* In UTF-16 and UTF-32, the number of code units per code point is fixed,
|
|
|
|
* and a case mapping is assumed to always stay within the same plane
|
|
|
|
* (64k code range) with the original code point. This allows to write
|
|
|
|
* the mapping into the same space as the source character without
|
|
|
|
* expansions or contractions except in the special cases.
|
|
|
|
*
|
|
|
|
* For UTF-8, where a source code point may take up a variable number
|
|
|
|
* of code units, it is more efficient to get the mapping and write
|
|
|
|
* the result only if it is a different code point from the original.
|
|
|
|
* Also, a sharp s and the "SS" string typically both take up 2 bytes in UTF-8,
|
|
|
|
* while the turkish i's typically result in expansions and contractions.
|
|
|
|
* Therefore, for UTF-8, these functions should be reimplemented.
|
|
|
|
* One single implementation for all UTF's would be either clumsy
|
|
|
|
* or inefficient.
|
|
|
|
*/
|
|
|
|
#if UTF_SIZE==8
|
|
|
|
# error reimplement toUpper() and toLower() for UTF-8, see comment above
|
|
|
|
#endif
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::toUpper(const Locale& locale)
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(!cloneArrayIfNeeded()) {
|
|
|
|
return *this;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-15 21:19:44 +00:00
|
|
|
const char *langChars = locale.getLanguage();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset start = 0, next = 0;
|
|
|
|
UTextOffset limit = fLength;
|
|
|
|
UChar32 c;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// The German sharp S character (U+00DF)'s uppercase equivalent is
|
|
|
|
// "SS", making it the only character that expands to two characters
|
|
|
|
// when its case is changed (we don't automatically convert "SS" to
|
|
|
|
// U+00DF going to lowercase because it can only be determined from
|
|
|
|
// knowing the language whether a particular "SS" should map to
|
|
|
|
// U+00DF or "ss"). So we make a preliminary pass through the
|
|
|
|
// string looking for sharp S characters and then go back and make
|
|
|
|
// room for the extra capital Ses if we find any. [For performance,
|
|
|
|
// we only do this extra work if the language is actually German]
|
2000-03-29 01:13:49 +00:00
|
|
|
if(uprv_strcmp(langChars, "de") == 0) {
|
2000-04-03 23:53:59 +00:00
|
|
|
static UChar SS [] = { 0x0053, 0x0053 };
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// A sharp s needs to be replaced with two capital S's.
|
|
|
|
if(c == 0x00DF) {
|
2000-03-29 01:13:49 +00:00
|
|
|
doReplace(start, 1, SS, 0, 2);
|
2000-04-03 23:53:59 +00:00
|
|
|
start += 2;
|
|
|
|
++next; // the string expanded by one
|
|
|
|
++limit;
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
2000-04-03 23:53:59 +00:00
|
|
|
// Otherwise, the case conversion can be handled by the Unicode code point.
|
|
|
|
c = Unicode::toUpperCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
} else if(uprv_strcmp(langChars, "tr") == 0) {
|
|
|
|
// If the specfied language is Turkish, then we have to special-case
|
|
|
|
// for the Turkish dotted and dotless Is. The regular lowercase i
|
|
|
|
// maps to the capital I with a dot (U+0130), and the lowercase i
|
|
|
|
// without the dot (U+0131) maps to the regular capital I
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
2000-03-29 01:13:49 +00:00
|
|
|
if(c == 0x0069/*'i'*/) {
|
2000-04-03 23:53:59 +00:00
|
|
|
fArray[start++] = 0x0130;
|
2000-03-29 01:13:49 +00:00
|
|
|
} else if(c == 0x0131) {
|
2000-04-03 23:53:59 +00:00
|
|
|
fArray[start++] = 0x0049/*'I'*/;
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
2000-04-03 23:53:59 +00:00
|
|
|
c = Unicode::toUpperCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
|
|
|
c = Unicode::toUpperCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::toLower(const Locale& locale)
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(!cloneArrayIfNeeded()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-04-15 21:19:44 +00:00
|
|
|
const char *langChars = locale.getLanguage();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-04-03 23:53:59 +00:00
|
|
|
UTextOffset start = 0, next = 0;
|
|
|
|
UTextOffset limit = fLength;
|
|
|
|
UChar32 c;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// if the specfied language is Turkish, then we have to special-case
|
|
|
|
// for the Turkish dotted and dotless Is. The capital I with a dot
|
|
|
|
// (U+0130) maps to the regular lowercase i, and the regular capital
|
|
|
|
// I maps to the lowercase i without the dot (U+0131)
|
2000-03-29 01:13:49 +00:00
|
|
|
if(uprv_strcmp(langChars, "tr") == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
|
|
|
if(c == 0x0049) { // 'I'
|
|
|
|
fArray[start++] = 0x0131;
|
|
|
|
} else if(c == 0x0130) {
|
|
|
|
fArray[start++] = 0x0069; // 'i'
|
|
|
|
} else {
|
|
|
|
c = Unicode::toLowerCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
} else if(uprv_strcmp(langChars, "el") == 0) {
|
|
|
|
// if the specfied language is Greek, then we have to special-case
|
|
|
|
// for the capital letter sigma (U+3A3), which has two lower-case
|
|
|
|
// forms. If the character following the capital sigma is a letter,
|
|
|
|
// we use the medial form (U+3C3); otherwise, we use the final form
|
|
|
|
// (U+3C2).
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
if(c == 0x3a3) {
|
2000-04-03 23:53:59 +00:00
|
|
|
if(next < limit) {
|
|
|
|
UTextOffset next2 = next;
|
|
|
|
UChar32 c2;
|
|
|
|
UTF_NEXT_CHAR(fArray, next2, limit, c2);
|
|
|
|
if(Unicode::isLetter(c2)) {
|
|
|
|
fArray[start++] = 0x3C3;
|
|
|
|
} else {
|
|
|
|
fArray[start++] = 0x3C2;
|
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
2000-04-03 23:53:59 +00:00
|
|
|
fArray[start++] = 0x3C2;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-04-03 23:53:59 +00:00
|
|
|
c = Unicode::toLowerCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
|
|
|
// if the specified language is anything other than Turkish or
|
|
|
|
// Greek, we rely on the Unicode class to do all our case mapping--
|
|
|
|
// there are no other special cases
|
1999-08-16 21:50:52 +00:00
|
|
|
while(start < limit) {
|
2000-04-03 23:53:59 +00:00
|
|
|
// start == next here by design
|
|
|
|
UTF_NEXT_CHAR(fArray, next, limit, c);
|
|
|
|
c = Unicode::toLowerCase(c);
|
|
|
|
UTF_APPEND_CHAR(fArray, start, limit, c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::doReplace( UTextOffset start,
|
|
|
|
int32_t length,
|
|
|
|
const UnicodeString& src,
|
|
|
|
UTextOffset srcStart,
|
|
|
|
int32_t srcLength)
|
|
|
|
{
|
1999-12-07 03:22:12 +00:00
|
|
|
if(!src.isBogus()) {
|
|
|
|
// pin the indices to legal values
|
|
|
|
src.pinIndices(srcStart, srcLength);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-07 03:22:12 +00:00
|
|
|
// get the characters from src
|
|
|
|
// and replace the range in ourselves with them
|
|
|
|
return doReplace(start, length, src.getArrayStart(), srcStart, srcLength);
|
|
|
|
} else {
|
|
|
|
// remove the range
|
|
|
|
return doReplace(start, length, 0, 0, 0);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::doReplace(UTextOffset start,
|
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
|
|
|
UTextOffset srcStart,
|
|
|
|
int32_t srcLength)
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
// if we're bogus, set us to empty first
|
|
|
|
if(isBogus()) {
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fLength = 0;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-07 03:22:12 +00:00
|
|
|
if(srcChars == 0) {
|
|
|
|
srcStart = srcLength = 0;
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t *bufferToDelete = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// the following may change fArray but will not copy the current contents;
|
|
|
|
// therefore we need to keep the current fArray
|
|
|
|
UChar *oldArray = fArray;
|
|
|
|
int32_t oldLength = fLength;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// pin the indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
// calculate the size of the string after the replace
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t newSize = oldLength - length + srcLength;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// clone our array and allocate a bigger array if needed
|
|
|
|
if(!cloneArrayIfNeeded(newSize, newSize + (newSize >> 2) + kGrowSize,
|
|
|
|
FALSE, &bufferToDelete)
|
|
|
|
) {
|
|
|
|
return *this;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now do the replace
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
if(fArray != oldArray) {
|
|
|
|
// if fArray changed, then we need to copy everything except what will change
|
|
|
|
us_arrayCopy(oldArray, 0, fArray, 0, start);
|
|
|
|
us_arrayCopy(oldArray, start + length,
|
|
|
|
fArray, start + srcLength,
|
|
|
|
oldLength - (start + length));
|
|
|
|
} else if(length != srcLength) {
|
|
|
|
// fArray did not change; copy only the portion that isn't changing, leaving a hole
|
|
|
|
us_arrayCopy(oldArray, start + length,
|
|
|
|
fArray, start + srcLength,
|
|
|
|
oldLength - (start + length));
|
1999-12-07 03:22:12 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// now fill in the hole with the new string
|
|
|
|
us_arrayCopy(srcChars, srcStart, getArrayStart(), start, srcLength);
|
|
|
|
|
|
|
|
fLength = newSize;
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// delayed delete in case srcChars == fArray when we started, and
|
|
|
|
// to keep oldArray alive for the above operations
|
|
|
|
delete [] bufferToDelete;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-11-19 23:13:41 +00:00
|
|
|
/**
|
|
|
|
* Replaceable API
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
UnicodeString::handleReplaceBetween(UTextOffset start,
|
|
|
|
UTextOffset limit,
|
|
|
|
const UnicodeString& text) {
|
|
|
|
replaceBetween(start, limit, text);
|
|
|
|
}
|
|
|
|
|
2000-05-20 04:41:12 +00:00
|
|
|
/**
|
|
|
|
* Replaceable API
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
UnicodeString::copy(int32_t start, int32_t limit, int32_t dest) {
|
|
|
|
UChar* text = new UChar[limit - start];
|
|
|
|
extractBetween(start, limit, text, 0);
|
|
|
|
insert(dest, text, 0, limit - start);
|
|
|
|
delete[] text;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::doReverse(UTextOffset start,
|
|
|
|
int32_t length)
|
|
|
|
{
|
|
|
|
// if we're bogus, do nothing
|
2000-03-29 01:13:49 +00:00
|
|
|
if(isBogus() || !cloneArrayIfNeeded()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return *this;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// pin the indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
UChar *left = getArrayStart() + start;
|
|
|
|
UChar *right = getArrayStart() + start + length;
|
|
|
|
UChar swap;
|
|
|
|
|
|
|
|
while(left < --right) {
|
|
|
|
swap = *left;
|
|
|
|
*left++ = *right;
|
|
|
|
*right = swap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2000-04-01 00:21:57 +00:00
|
|
|
UnicodeString::padLeading(int32_t targetLength,
|
|
|
|
UChar padChar)
|
|
|
|
{
|
|
|
|
if(isBogus() || fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
// move contents up by padding width
|
|
|
|
int32_t start = targetLength - fLength;
|
|
|
|
us_arrayCopy(fArray, 0, fArray, start, fLength);
|
|
|
|
|
|
|
|
// fill in padding character
|
|
|
|
while(--start >= 0) {
|
|
|
|
fArray[start] = padChar;
|
|
|
|
}
|
|
|
|
fLength = targetLength;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2000-04-01 00:21:57 +00:00
|
|
|
UnicodeString::padTrailing(int32_t targetLength,
|
|
|
|
UChar padChar)
|
|
|
|
{
|
|
|
|
if(isBogus() || fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
|
|
|
|
return FALSE;
|
|
|
|
} else {
|
|
|
|
// fill in padding character
|
|
|
|
int32_t length = targetLength;
|
|
|
|
while(--length >= fLength) {
|
|
|
|
fArray[length] = padChar;
|
|
|
|
}
|
|
|
|
fLength = targetLength;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
UnicodeString::trim()
|
|
|
|
{
|
|
|
|
if(isBogus()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32 c;
|
|
|
|
UTextOffset i = fLength, length;
|
|
|
|
|
|
|
|
// first cut off trailing white space
|
|
|
|
for(;;) {
|
|
|
|
length = i;
|
|
|
|
if(i <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
2000-04-12 19:36:07 +00:00
|
|
|
UTF_PREV_CHAR(fArray, 0, i, c);
|
2000-04-01 00:21:57 +00:00
|
|
|
if(!(c == 0x20 || Unicode::isWhitespace(c))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(length < fLength) {
|
|
|
|
fLength = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find leading white space
|
|
|
|
UTextOffset start;
|
|
|
|
i = 0;
|
|
|
|
for(;;) {
|
|
|
|
start = i;
|
|
|
|
if(i >= length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UTF_NEXT_CHAR(fArray, i, length, c);
|
|
|
|
if(!(c == 0x20 || Unicode::isWhitespace(c))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// move string forward over leading white space
|
|
|
|
if(start > 0) {
|
|
|
|
doReplace(0, start, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//========================================
|
|
|
|
// Hashing
|
|
|
|
//========================================
|
|
|
|
int32_t
|
2000-04-12 20:04:41 +00:00
|
|
|
UnicodeString::doHashCode() const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-31 23:50:49 +00:00
|
|
|
/* Delegate hash computation to uhash. This makes UnicodeString
|
|
|
|
* hashing consistent with UChar* hashing. */
|
2000-04-12 20:04:41 +00:00
|
|
|
int32_t hashCode = uhash_hashUCharsN(getArrayStart(), fLength);
|
|
|
|
if (hashCode == kInvalidHashCode) {
|
|
|
|
hashCode = kEmptyHashCode;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
2000-04-12 20:04:41 +00:00
|
|
|
return hashCode;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Codeset conversion
|
|
|
|
//========================================
|
|
|
|
int32_t
|
|
|
|
UnicodeString::extract(UTextOffset start,
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t length,
|
|
|
|
char *dst,
|
|
|
|
const char *codepage) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// if we're bogus or there's nothing to convert, do nothing
|
2000-03-29 01:13:49 +00:00
|
|
|
if(isBogus() || length <= 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// pin the indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
// set up the conversion parameters
|
|
|
|
const UChar *mySource = getArrayStart() + start;
|
|
|
|
const UChar *mySourceEnd = mySource + length;
|
|
|
|
char *myTarget = dst;
|
1999-10-25 22:20:12 +00:00
|
|
|
char *myTargetLimit;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t arraySize = 0x0FFFFFFF;
|
|
|
|
|
|
|
|
// create the converter
|
1999-12-01 17:51:53 +00:00
|
|
|
UConverter *converter;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if the codepage is the default, use our cache
|
1999-12-01 17:51:53 +00:00
|
|
|
if(codepage == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
converter = getDefaultConverter(status);
|
1999-12-01 17:51:53 +00:00
|
|
|
} else if(*codepage == 0) {
|
|
|
|
converter = 0;
|
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
converter = ucnv_open(codepage, &status);
|
1999-12-01 17:51:53 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if we failed, set the appropriate flags and return
|
1999-12-01 17:51:53 +00:00
|
|
|
// if it is an empty string, then use the "invariant character" conversion
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// close the converter
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepage == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
releaseDefaultConverter(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
ucnv_close(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-12-01 17:51:53 +00:00
|
|
|
// perform the conversion
|
|
|
|
if(converter == 0) {
|
|
|
|
// use the "invariant characters" conversion
|
|
|
|
if(length > fLength - start) {
|
|
|
|
length = fLength - start;
|
|
|
|
}
|
|
|
|
u_UCharsToChars(mySource, myTarget, length);
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// there is no loop here since we assume the buffer is large enough
|
1999-10-25 22:20:12 +00:00
|
|
|
myTargetLimit = myTarget + arraySize;
|
|
|
|
|
2000-02-18 21:05:24 +00:00
|
|
|
/* Pin the limit to U_MAX_PTR. NULL check is for AS/400. */
|
2000-03-29 01:13:49 +00:00
|
|
|
if((myTargetLimit < myTarget) || (myTargetLimit == NULL)) {
|
|
|
|
myTargetLimit = (char*)U_MAX_PTR;
|
|
|
|
}
|
1999-10-25 22:20:12 +00:00
|
|
|
|
1999-10-26 21:15:33 +00:00
|
|
|
ucnv_fromUnicode(converter, &myTarget, myTargetLimit,
|
2000-03-29 01:13:49 +00:00
|
|
|
&mySource, mySourceEnd, 0, TRUE, &status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// close the converter
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepage == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
releaseDefaultConverter(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
ucnv_close(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return (myTarget - dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeString::doCodepageCreate(const char *codepageData,
|
|
|
|
int32_t dataLength,
|
|
|
|
const char *codepage)
|
|
|
|
{
|
|
|
|
// if there's nothing to convert, do nothing
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepageData == 0 || dataLength <= 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// create the converter
|
|
|
|
// if the codepage is the default, use our cache
|
1999-12-01 17:51:53 +00:00
|
|
|
// if it is an empty string, then use the "invariant character" conversion
|
2000-03-29 01:13:49 +00:00
|
|
|
UConverter *converter = (codepage == 0 ?
|
|
|
|
getDefaultConverter(status) :
|
|
|
|
*codepage == 0 ?
|
|
|
|
0 :
|
|
|
|
ucnv_open(codepage, &status));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if we failed, set the appropriate flags and return
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// close the converter
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepage == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
releaseDefaultConverter(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
ucnv_close(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform the conversion
|
1999-12-01 17:51:53 +00:00
|
|
|
if(converter == 0) {
|
|
|
|
// use the "invariant characters" conversion
|
2000-03-29 01:13:49 +00:00
|
|
|
if(cloneArrayIfNeeded(dataLength, dataLength, FALSE)) {
|
1999-12-01 17:51:53 +00:00
|
|
|
u_charsToUChars(codepageData, getArrayStart(), dataLength);
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength = dataLength;
|
|
|
|
} else {
|
|
|
|
setToBogus();
|
1999-12-01 17:51:53 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// set up the conversion parameters
|
|
|
|
const char *mySource = codepageData;
|
|
|
|
const char *mySourceEnd = mySource + dataLength;
|
|
|
|
UChar *myTarget;
|
|
|
|
|
|
|
|
// estimate the size needed:
|
|
|
|
// 1.25 UChar's per source byte should cover most cases
|
|
|
|
int32_t arraySize = dataLength + (dataLength >> 2);
|
|
|
|
|
|
|
|
// we do not care about the current contents
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool doCopyArray = FALSE;
|
1999-12-01 17:51:53 +00:00
|
|
|
for(;;) {
|
2000-03-29 01:13:49 +00:00
|
|
|
if(!cloneArrayIfNeeded(arraySize, arraySize, doCopyArray)) {
|
|
|
|
setToBogus();
|
|
|
|
break;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// perform the conversion
|
2000-03-29 01:13:49 +00:00
|
|
|
myTarget = fArray + fLength;
|
|
|
|
ucnv_toUnicode(converter, &myTarget, fArray + fCapacity,
|
|
|
|
&mySource, mySourceEnd, 0, FALSE, &status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// update the conversion parameters
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength = myTarget - fArray;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// allocate more space and copy data, if needed
|
1999-12-01 17:51:53 +00:00
|
|
|
if(status == U_INDEX_OUTOFBOUNDS_ERROR) {
|
2000-03-29 01:13:49 +00:00
|
|
|
// reset the error code
|
|
|
|
status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// keep the previous conversion results
|
|
|
|
doCopyArray = TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// estimate the new size needed, larger than before
|
|
|
|
// try 2 UChar's per remaining source byte
|
|
|
|
arraySize = fLength + 2 * (mySourceEnd - mySource);
|
1999-12-01 17:51:53 +00:00
|
|
|
} else {
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the converter
|
2000-03-29 01:13:49 +00:00
|
|
|
if(codepage == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
releaseDefaultConverter(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
ucnv_close(converter);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// External Buffer
|
|
|
|
//========================================
|
2000-03-29 01:13:49 +00:00
|
|
|
// ### TODO:
|
|
|
|
// this is very, very dirty: we should not ever expose our array to the outside,
|
|
|
|
// and this also violates the const-ness of this object
|
|
|
|
// this must be removed when the resource bundle implementation does not need it any more!
|
1999-08-16 21:50:52 +00:00
|
|
|
const UChar*
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::getUChars() const {
|
1999-08-16 21:50:52 +00:00
|
|
|
// if we're bogus, do nothing
|
2000-03-29 01:13:49 +00:00
|
|
|
if(isBogus()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
if(fCapacity <= fLength || fArray[fLength] != 0) {
|
|
|
|
if(((UnicodeString &)*this).cloneArrayIfNeeded(fLength + 1)) {
|
|
|
|
fArray[fLength] = 0;
|
1999-12-01 21:28:18 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
return fArray;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Miscellaneous
|
|
|
|
//========================================
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::cloneArrayIfNeeded(int32_t newCapacity,
|
|
|
|
int32_t growCapacity,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool doCopyArray,
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t **pBufferToDelete) {
|
|
|
|
// default parameters need to be static, therefore
|
|
|
|
// the defaults are -1 to have convenience defaults
|
|
|
|
if(newCapacity == -1) {
|
|
|
|
newCapacity = fCapacity;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
/*
|
|
|
|
* We need to make a copy of the array if
|
|
|
|
* the buffer is read-only, or
|
|
|
|
* the buffer is refCounted (shared), and refCount>1, or
|
|
|
|
* the buffer is too small.
|
|
|
|
* Return FALSE if memory could not be allocated.
|
|
|
|
*/
|
|
|
|
if(fFlags & kBufferIsReadonly ||
|
|
|
|
fFlags & kRefCounted && refCount() > 1 ||
|
|
|
|
newCapacity > fCapacity
|
|
|
|
) {
|
|
|
|
// save old values
|
|
|
|
UChar *array = fArray;
|
|
|
|
uint16_t flags = fFlags;
|
|
|
|
|
|
|
|
// check growCapacity for default value and use of the stack buffer
|
|
|
|
if(growCapacity == -1) {
|
|
|
|
growCapacity = newCapacity;
|
|
|
|
} else if(newCapacity <= US_STACKBUF_SIZE && growCapacity > US_STACKBUF_SIZE) {
|
|
|
|
growCapacity = US_STACKBUF_SIZE;
|
1999-12-02 18:33:25 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// allocate a new array
|
|
|
|
if(allocate(growCapacity) ||
|
|
|
|
newCapacity < growCapacity && allocate(newCapacity)
|
|
|
|
) {
|
|
|
|
if(doCopyArray) {
|
|
|
|
// copy the contents
|
|
|
|
// do not copy more than what fits - it may be smaller than before
|
|
|
|
if(fCapacity < fLength) {
|
|
|
|
fLength = fCapacity;
|
|
|
|
}
|
|
|
|
us_arrayCopy(array, 0, fArray, 0, fLength);
|
|
|
|
} else {
|
|
|
|
fLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// release the old array
|
|
|
|
if(flags & kRefCounted) {
|
|
|
|
// the array is refCounted; decrement and release if 0
|
|
|
|
int32_t *pRefCount = ((int32_t *)array - 1);
|
|
|
|
if(--*pRefCount == 0) {
|
|
|
|
if(pBufferToDelete == 0) {
|
|
|
|
delete [] pRefCount;
|
|
|
|
} else {
|
|
|
|
// the caller requested to delete it himself
|
|
|
|
*pBufferToDelete = pRefCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// not enough memory for growCapacity and not even for the smaller newCapacity
|
|
|
|
// reset the old values for setToBogus() to release the array
|
|
|
|
fArray = array;
|
|
|
|
fFlags = flags;
|
|
|
|
setToBogus();
|
|
|
|
return FALSE;
|
1999-12-02 18:33:25 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// private function for C API
|
2000-03-29 01:13:49 +00:00
|
|
|
U_CFUNC int32_t
|
|
|
|
T_UnicodeString_length(const UnicodeString *s)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
return s->length();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-02 23:26:44 +00:00
|
|
|
// private function for C API
|
|
|
|
U_CFUNC int32_t
|
|
|
|
T_UnicodeString_extract(const UnicodeString *s, char *dst)
|
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
return s->extract(0, s->length(), dst, "");
|
1999-12-02 23:26:44 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Default converter caching
|
|
|
|
//========================================
|
|
|
|
|
|
|
|
UConverter*
|
|
|
|
UnicodeString::getDefaultConverter(UErrorCode &status)
|
|
|
|
{
|
|
|
|
UConverter *converter = 0;
|
|
|
|
|
|
|
|
if(fgDefaultConverter != 0) {
|
|
|
|
Mutex lock;
|
|
|
|
|
|
|
|
// need to check to make sure it wasn't taken out from under us
|
|
|
|
if(fgDefaultConverter != 0) {
|
|
|
|
converter = fgDefaultConverter;
|
|
|
|
fgDefaultConverter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the cache was empty, create a converter
|
|
|
|
if(converter == 0) {
|
|
|
|
converter = ucnv_open(0, &status);
|
2000-03-29 01:13:49 +00:00
|
|
|
if(U_FAILURE(status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return converter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeString::releaseDefaultConverter(UConverter *converter)
|
|
|
|
{
|
|
|
|
if(fgDefaultConverter == 0) {
|
2000-04-10 23:26:18 +00:00
|
|
|
if (converter != 0) {
|
|
|
|
ucnv_reset(converter);
|
|
|
|
}
|
2000-04-01 00:21:57 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
Mutex lock;
|
|
|
|
|
|
|
|
if(fgDefaultConverter == 0) {
|
|
|
|
fgDefaultConverter = converter;
|
|
|
|
converter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// it's safe to close a 0 converter
|
1999-08-16 21:50:52 +00:00
|
|
|
ucnv_close(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Streaming (to be removed)
|
|
|
|
//========================================
|
|
|
|
|
|
|
|
#include "unistrm.h"
|
|
|
|
#include "filestrm.h"
|
|
|
|
|
|
|
|
|
|
|
|
inline uint8_t
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_hibyte(uint16_t x)
|
1999-08-16 21:50:52 +00:00
|
|
|
{ return (uint8_t)(x >> 8); }
|
|
|
|
|
|
|
|
inline uint8_t
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_lobyte(uint16_t x)
|
1999-08-16 21:50:52 +00:00
|
|
|
{ return (uint8_t)(x & 0xff); }
|
|
|
|
|
|
|
|
inline uint16_t
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_hiword(uint32_t x)
|
1999-08-16 21:50:52 +00:00
|
|
|
{ return (uint16_t)(x >> 16); }
|
|
|
|
|
|
|
|
inline uint16_t
|
1999-12-28 23:39:02 +00:00
|
|
|
uprv_loword(uint32_t x)
|
1999-08-16 21:50:52 +00:00
|
|
|
{ return (uint16_t)(x & 0xffff); }
|
|
|
|
|
|
|
|
inline void
|
|
|
|
writeLong(FileStream *os,
|
|
|
|
int32_t x)
|
|
|
|
{
|
1999-12-28 23:39:02 +00:00
|
|
|
uint16_t word = uprv_hiword((uint32_t)x);
|
|
|
|
T_FileStream_putc(os, uprv_hibyte(word));
|
|
|
|
T_FileStream_putc(os, uprv_lobyte(word));
|
|
|
|
word = uprv_loword((uint32_t)x);
|
|
|
|
T_FileStream_putc(os, uprv_hibyte(word));
|
|
|
|
T_FileStream_putc(os, uprv_lobyte(word));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int32_t
|
|
|
|
readLong(FileStream *is)
|
|
|
|
{
|
|
|
|
int32_t x = 0;
|
|
|
|
uint16_t byte;
|
|
|
|
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
x |= byte;
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
x = (x << 8) | byte;
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
x = (x << 8) | byte;
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
x = (x << 8) | byte;
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
writeUChar(FileStream *os,
|
|
|
|
UChar c)
|
|
|
|
{
|
1999-12-28 23:39:02 +00:00
|
|
|
T_FileStream_putc(os, uprv_hibyte(c));
|
|
|
|
T_FileStream_putc(os, uprv_lobyte(c));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline UChar
|
|
|
|
readUChar(FileStream *is)
|
|
|
|
{
|
|
|
|
UChar c = 0;
|
|
|
|
uint16_t byte;
|
|
|
|
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
c |= byte;
|
|
|
|
byte = T_FileStream_getc(is);
|
|
|
|
c = (c << 8) | byte;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeStringStreamer::streamOut(const UnicodeString *s,
|
|
|
|
FileStream *os)
|
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(!T_FileStream_error(os)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
writeLong(os, s->fLength);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const UChar *c = s->getArrayStart();
|
|
|
|
const UChar *end = c + s->fLength;
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
while(c != end && ! T_FileStream_error(os)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
writeUChar(os, *c++);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeStringStreamer::streamIn(UnicodeString *s,
|
|
|
|
FileStream *is)
|
|
|
|
{
|
|
|
|
int32_t newSize;
|
|
|
|
|
|
|
|
// handle error conditions
|
|
|
|
if(T_FileStream_error(is) || T_FileStream_eof(is)) {
|
|
|
|
s->setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newSize = readLong(is);
|
|
|
|
if((newSize < 0) || T_FileStream_error(is)
|
|
|
|
|| ((newSize > 0) && T_FileStream_eof(is))) {
|
|
|
|
s->setToBogus(); //error condition
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone s's array, if needed
|
2000-03-29 01:13:49 +00:00
|
|
|
if(!s->cloneArrayIfNeeded(newSize, newSize, FALSE)) {
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UChar *c = s->getArrayStart();
|
|
|
|
UChar *end = c + newSize;
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
while(c < end && ! (T_FileStream_error(is) || T_FileStream_eof(is))) {
|
1999-08-16 21:50:52 +00:00
|
|
|
*c++ = readUChar(is);
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// couldn't read all chars
|
|
|
|
if(c < end) {
|
|
|
|
s->setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->fLength = newSize;
|
|
|
|
}
|
|
|
|
|
2000-05-22 19:50:23 +00:00
|
|
|
void
|
|
|
|
UnicodeStringStreamer::streamOut(const UnicodeString *s,
|
|
|
|
UMemoryStream *os)
|
|
|
|
{
|
|
|
|
if(!uprv_mstrm_error(os)) {
|
|
|
|
uprv_mstrm_write(os, (uint8_t*)&s->fLength, sizeof(s->fLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
const UChar *c = s->getArrayStart();
|
|
|
|
const UChar *end = c + s->fLength;
|
|
|
|
|
|
|
|
while(c != end && ! uprv_mstrm_error(os)) {
|
|
|
|
uprv_mstrm_write(os, (uint8_t*)c, sizeof(*c));
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeStringStreamer::streamIn(UnicodeString *s,
|
|
|
|
UMemoryStream *is)
|
|
|
|
{
|
|
|
|
int32_t newSize;
|
|
|
|
|
|
|
|
// handle error conditions
|
|
|
|
if(uprv_mstrm_error(is) || uprv_mstrm_eof(is)) {
|
|
|
|
s->setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uprv_mstrm_read(is, (uint8_t *)&newSize, sizeof(int32_t));
|
|
|
|
if((newSize < 0) || uprv_mstrm_error(is)
|
|
|
|
|| ((newSize > 0) && uprv_mstrm_eof(is))) {
|
|
|
|
s->setToBogus(); //error condition
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone s's array, if needed
|
|
|
|
if(!s->cloneArrayIfNeeded(newSize, newSize, FALSE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar *c = s->getArrayStart();
|
|
|
|
UChar *end = c + newSize;
|
|
|
|
|
|
|
|
while(c < end && ! (uprv_mstrm_error(is) || uprv_mstrm_eof(is))) {
|
|
|
|
uprv_mstrm_read(is, (uint8_t *)c, sizeof(*c));
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// couldn't read all chars
|
|
|
|
if(c < end) {
|
|
|
|
s->setToBogus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->fLength = newSize;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// console IO
|
|
|
|
|
2000-05-15 19:32:34 +00:00
|
|
|
#if U_IOSTREAM_SOURCE >= 198506
|
|
|
|
|
|
|
|
#if U_IOSTREAM_SOURCE >= 199711
|
|
|
|
|
|
|
|
U_COMMON_API std::ostream &
|
|
|
|
operator<<(std::ostream& stream, const UnicodeString& s)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
U_COMMON_API ostream &
|
|
|
|
operator<<(ostream& stream, const UnicodeString& s)
|
|
|
|
|
|
|
|
#endif
|
2000-04-25 21:33:15 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-03-29 01:13:49 +00:00
|
|
|
if(s.length() > 0) {
|
|
|
|
char buffer[200];
|
|
|
|
UConverter *converter;
|
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
// use the default converter to convert chunks of text
|
|
|
|
converter = UnicodeString::getDefaultConverter(errorCode);
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
|
|
|
const UChar *us = s.getArrayStart(), *uLimit = us + s.length();
|
|
|
|
char *s, *sLimit = buffer + sizeof(buffer);
|
|
|
|
do {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
|
|
|
s = buffer;
|
|
|
|
ucnv_fromUnicode(converter, &s, sLimit, &us, uLimit, 0, FALSE, &errorCode);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// write this chunk
|
|
|
|
if(s > buffer) {
|
|
|
|
stream.write(buffer, s - buffer);
|
|
|
|
}
|
|
|
|
} while(errorCode == U_INDEX_OUTOFBOUNDS_ERROR);
|
|
|
|
UnicodeString::releaseDefaultConverter(converter);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
stream.flush();
|
|
|
|
return stream;
|
|
|
|
}
|
2000-04-25 21:33:15 +00:00
|
|
|
|
|
|
|
#endif
|