1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
|
|
|
* Copyright (C) 1999-2001, 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.
|
2001-06-26 17:57:53 +00:00
|
|
|
* 06/25/01 grhoten Removed the dependency on iostream
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
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"
|
|
|
|
#include "unicode/unistr.h"
|
2002-03-19 03:01:28 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-01-02 19:23:27 +00:00
|
|
|
#include "unicode/ucnv.h"
|
2002-02-21 18:42:52 +00:00
|
|
|
#include "unicode/ubrk.h"
|
2000-03-31 23:50:49 +00:00
|
|
|
#include "uhash.h"
|
2001-02-02 20:41:18 +00:00
|
|
|
#include "ustr_imp.h"
|
2002-05-22 00:35:01 +00:00
|
|
|
#include "unormimp.h"
|
2002-03-09 00:36:54 +00:00
|
|
|
#include "umutex.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-06-26 17:57:53 +00:00
|
|
|
#if 0
|
|
|
|
|
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
|
|
|
//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
|
2001-10-22 05:29:14 +00:00
|
|
|
static
|
1999-08-16 21:50:52 +00:00
|
|
|
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
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char UnicodeString::fgClassID=0;
|
|
|
|
const char UCharReference::fgClassID=0;
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Reference Counting functions, put at top of file so that optimizing compilers
|
|
|
|
// have a chance to automatically inline.
|
|
|
|
//========================================
|
|
|
|
|
|
|
|
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
|
|
|
|
{ return *((int32_t *)fArray - 1); }
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
UnicodeString::setRefCount(int32_t count)
|
|
|
|
{ return (*((int32_t *)fArray - 1) = count); }
|
|
|
|
|
|
|
|
void
|
|
|
|
UnicodeString::releaseArray() {
|
|
|
|
if((fFlags & kRefCounted) && removeRef() == 0) {
|
|
|
|
uprv_free((int32_t *)fArray - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//========================================
|
|
|
|
// Constructors
|
|
|
|
//========================================
|
|
|
|
UnicodeString::UnicodeString()
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(0),
|
2000-03-29 01:13:49 +00:00
|
|
|
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
|
2001-10-18 22:04:22 +00:00
|
|
|
while(i < length) {
|
|
|
|
int32_t unitIdx = 0;
|
2001-10-22 22:08:44 +00:00
|
|
|
while(unitIdx < unitCount) {
|
2001-10-18 22:04:22 +00:00
|
|
|
fArray[i++]=units[unitIdx++];
|
2000-04-19 20:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fLength = length;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString::UnicodeString(UChar ch)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(1),
|
1999-08-16 21:50:52 +00:00
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(1),
|
2000-04-01 00:21:57 +00:00
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
2000-04-01 00:21:57 +00:00
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t i = 0;
|
2000-04-01 00:21:57 +00:00
|
|
|
UTF_APPEND_CHAR(fStackBuffer, i, US_STACKBUF_SIZE, ch);
|
|
|
|
fLength = i;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString::UnicodeString(const UChar *text)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(textLength),
|
2000-03-29 01:13:49 +00:00
|
|
|
fCapacity(isTerminated ? textLength + 1 : textLength),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray((UChar *)text),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kReadonlyAlias)
|
1999-12-01 17:51:53 +00:00
|
|
|
{
|
2002-06-08 19:42:51 +00:00
|
|
|
if( text == 0 || textLength < -1 ||
|
|
|
|
(textLength == -1 && !isTerminated) ||
|
|
|
|
(textLength >= 0 && isTerminated && text[textLength] != 0)
|
|
|
|
) {
|
2000-03-29 01:13:49 +00:00
|
|
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(bufLength),
|
2000-03-29 01:13:49 +00:00
|
|
|
fCapacity(buffCapacity),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(buff),
|
2001-09-20 01:14:37 +00:00
|
|
|
fFlags(kWritableAlias)
|
2000-03-29 01:13:49 +00:00
|
|
|
{
|
|
|
|
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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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) {
|
2001-05-23 17:55:35 +00:00
|
|
|
doCodepageCreate(codepageData, (int32_t)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)
|
2001-07-02 20:58:14 +00:00
|
|
|
: fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
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
|
|
|
}
|
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
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)) {
|
|
|
|
// check arguments
|
|
|
|
if(srcLength<-1 || (srcLength!=0 && src==0)) {
|
|
|
|
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
|
|
|
// get input length
|
|
|
|
if(srcLength==-1) {
|
2001-09-28 00:42:28 +00:00
|
|
|
srcLength=(int32_t)uprv_strlen(src);
|
2001-09-18 01:12:14 +00:00
|
|
|
}
|
|
|
|
if(srcLength>0) {
|
|
|
|
if(cnv!=0) {
|
|
|
|
// use the provided converter
|
|
|
|
ucnv_resetToUnicode(cnv);
|
|
|
|
doCodepageCreate(src, srcLength, cnv, errorCode);
|
|
|
|
} else {
|
|
|
|
// use the default converter
|
|
|
|
cnv=u_getDefaultConverter(&errorCode);
|
|
|
|
doCodepageCreate(src, srcLength, cnv, errorCode);
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
setToBogus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString::UnicodeString(const UnicodeString& that)
|
2001-08-18 01:09:24 +00:00
|
|
|
: Replaceable(),
|
|
|
|
fLength(0),
|
2000-03-29 01:13:49 +00:00
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
2001-02-14 18:02:44 +00:00
|
|
|
fArray(fStackBuffer),
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
|
|
|
*this = that;
|
|
|
|
}
|
|
|
|
|
2002-04-26 18:02:49 +00:00
|
|
|
UnicodeString::UnicodeString(const UnicodeString& that,
|
2002-07-03 11:59:21 +00:00
|
|
|
int32_t srcStart)
|
2002-04-26 18:02:49 +00:00
|
|
|
: Replaceable(),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
|
|
|
fArray(fStackBuffer),
|
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
|
|
|
setTo(that, srcStart);
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString::UnicodeString(const UnicodeString& that,
|
2002-07-03 11:59:21 +00:00
|
|
|
int32_t srcStart,
|
|
|
|
int32_t srcLength)
|
2002-04-26 18:02:49 +00:00
|
|
|
: Replaceable(),
|
|
|
|
fLength(0),
|
|
|
|
fCapacity(US_STACKBUF_SIZE),
|
|
|
|
fArray(fStackBuffer),
|
|
|
|
fFlags(kShortString)
|
|
|
|
{
|
|
|
|
setTo(that, srcStart, srcLength);
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
//========================================
|
|
|
|
// 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
|
2001-05-23 17:55:35 +00:00
|
|
|
int32_t words = (int32_t)(((sizeof(int32_t) + capacity * U_SIZEOF_UCHAR + 15) & ~15) >> 2);
|
2002-02-28 01:42:40 +00:00
|
|
|
int32_t *array = (int32_t*) uprv_malloc( sizeof(int32_t) * words );
|
2000-03-29 01:13:49 +00:00
|
|
|
if(array != 0) {
|
|
|
|
// set initial refCount and point behind the refCount
|
|
|
|
*array++ = 1;
|
|
|
|
|
|
|
|
// have fArray point to the first UChar
|
|
|
|
fArray = (UChar *)array;
|
2001-05-23 17:55:35 +00:00
|
|
|
fCapacity = (int32_t)((words - 1) * (sizeof(int32_t) / U_SIZEOF_UCHAR));
|
2000-03-29 01:13:49 +00:00
|
|
|
fFlags = kLongString;
|
|
|
|
} else {
|
2000-08-26 01:54:05 +00:00
|
|
|
fLength = 0;
|
2000-03-29 01:13:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
|
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();
|
|
|
|
|
2001-10-06 00:22:13 +00:00
|
|
|
// we always copy the length
|
2000-03-29 01:13:49 +00:00
|
|
|
fLength = src.fLength;
|
2001-09-20 01:14:37 +00:00
|
|
|
if(fLength == 0) {
|
|
|
|
// empty string - use the stack buffer
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
|
|
|
return *this;
|
|
|
|
}
|
2000-03-29 01:13:49 +00:00
|
|
|
|
2001-09-20 01:14:37 +00:00
|
|
|
// fLength>0 and not an "open" src.getBuffer(minCapacity)
|
2000-03-29 01:13:49 +00:00
|
|
|
switch(src.fFlags) {
|
|
|
|
case kShortString:
|
|
|
|
// short string using the stack buffer, do the same
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
2001-09-20 01:14:37 +00:00
|
|
|
uprv_memcpy(fStackBuffer, src.fArray, fLength * U_SIZEOF_UCHAR);
|
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;
|
2001-09-20 01:14:37 +00:00
|
|
|
case kWritableAlias:
|
|
|
|
// src is a writable alias; we make a copy of that instead
|
2000-03-29 01:13:49 +00:00
|
|
|
if(allocate(fLength)) {
|
2001-09-20 01:14:37 +00:00
|
|
|
uprv_memcpy(fArray, src.fArray, fLength * U_SIZEOF_UCHAR);
|
2000-03-29 01:13:49 +00:00
|
|
|
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
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::numDisplayCells( int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
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;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t limit = start + length;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
while(start < limit) {
|
2000-04-06 16:47:20 +00:00
|
|
|
UTF_NEXT_CHAR(fArray, start, limit, c);
|
2002-03-19 03:01:28 +00:00
|
|
|
switch(u_charCellWidth(c)) {
|
|
|
|
case U_ZERO_WIDTH:
|
2000-08-30 23:42:28 +00:00
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-03-19 03:01:28 +00:00
|
|
|
case U_HALF_WIDTH:
|
1999-08-16 21:50:52 +00:00
|
|
|
result += 1;
|
|
|
|
break;
|
|
|
|
|
2002-03-19 03:01:28 +00:00
|
|
|
case U_FULL_WIDTH:
|
1999-08-16 21:50:52 +00:00
|
|
|
result += 2;
|
|
|
|
break;
|
|
|
|
|
2002-03-19 03:01:28 +00:00
|
|
|
case U_NEUTRAL_WIDTH:
|
1999-08-16 21:50:52 +00:00
|
|
|
result += (asian ? 2 : 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCharReference
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::operator[] (int32_t pos)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
return UCharReference(this, pos);
|
|
|
|
}
|
|
|
|
|
2000-07-16 13:39:07 +00:00
|
|
|
UnicodeString UnicodeString::unescape() const {
|
|
|
|
UnicodeString result;
|
|
|
|
for (int32_t i=0; i<length(); ) {
|
2000-08-16 20:24:57 +00:00
|
|
|
UChar32 c = charAt(i++);
|
2000-07-16 13:39:07 +00:00
|
|
|
if (c == 0x005C /*'\\'*/) {
|
|
|
|
c = unescapeAt(i); // advances i
|
|
|
|
if (c == (UChar32)0xFFFFFFFF) {
|
2000-08-16 23:28:24 +00:00
|
|
|
result.remove(); // return empty string
|
2000-07-16 13:39:07 +00:00
|
|
|
break; // invalid escape sequence
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.append(c);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// u_unescapeAt() callback to get a UChar from a UnicodeString
|
2001-10-05 22:14:23 +00:00
|
|
|
U_CDECL_BEGIN
|
2001-10-25 21:55:45 +00:00
|
|
|
static UChar U_CALLCONV
|
|
|
|
UnicodeString_charAt(int32_t offset, void *context) {
|
2000-07-16 13:39:07 +00:00
|
|
|
return ((UnicodeString*) context)->charAt(offset);
|
|
|
|
}
|
2001-10-05 22:14:23 +00:00
|
|
|
U_CDECL_END
|
2000-07-16 13:39:07 +00:00
|
|
|
|
|
|
|
UChar32 UnicodeString::unescapeAt(int32_t &offset) const {
|
2001-10-05 22:14:23 +00:00
|
|
|
return u_unescapeAt(UnicodeString_charAt, &offset, length(), (void*)this);
|
2000-07-16 13:39:07 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//========================================
|
|
|
|
// Read-only implementation
|
|
|
|
//========================================
|
|
|
|
int8_t
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doCompare( int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
1999-08-16 21:50:52 +00:00
|
|
|
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-10-19 21:02:01 +00:00
|
|
|
chars += start;
|
|
|
|
srcChars += srcStart;
|
2000-03-29 01:13:49 +00:00
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t minLength;
|
1999-12-07 00:12:31 +00:00
|
|
|
int8_t lengthResult;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-11-13 19:27:19 +00:00
|
|
|
// get the srcLength if necessary
|
|
|
|
if(srcLength < 0) {
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2001-10-05 21:18:12 +00:00
|
|
|
if(minLength > 0 && chars != srcChars) {
|
1999-12-07 00:12:31 +00:00
|
|
|
int32_t result;
|
|
|
|
|
2000-08-21 19:55:59 +00:00
|
|
|
# if U_IS_BIG_ENDIAN
|
1999-12-07 00:12:31 +00:00
|
|
|
// big-endian: byte comparison works
|
2000-10-19 21:02:01 +00:00
|
|
|
result = uprv_memcmp(chars, srcChars, minLength * sizeof(UChar));
|
1999-12-07 00:12:31 +00:00
|
|
|
if(result != 0) {
|
|
|
|
return (int8_t)(result >> 15 | 1);
|
|
|
|
}
|
2000-08-21 19:55:59 +00:00
|
|
|
# else
|
1999-12-07 00:12:31 +00:00
|
|
|
// little-endian: compare UChar units
|
|
|
|
do {
|
2000-10-19 21:02:01 +00:00
|
|
|
result = ((int32_t)*(chars++) - (int32_t)*(srcChars++));
|
1999-12-07 00:12:31 +00:00
|
|
|
if(result != 0) {
|
|
|
|
return (int8_t)(result >> 15 | 1);
|
|
|
|
}
|
|
|
|
} while(--minLength > 0);
|
2000-08-21 19:55:59 +00:00
|
|
|
# endif
|
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
|
|
|
}
|
|
|
|
|
2001-01-24 02:37:58 +00:00
|
|
|
/* String compare in code point order - doCompare() compares in code unit order. */
|
|
|
|
int8_t
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doCompareCodePointOrder(int32_t start,
|
2001-01-24 02:37:58 +00:00
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
2001-01-24 02:37:58 +00:00
|
|
|
int32_t srcLength) const
|
|
|
|
{
|
|
|
|
// compare illegal string values
|
|
|
|
if(isBogus()) {
|
|
|
|
if(srcChars==0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(srcChars==0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
2002-05-24 17:05:31 +00:00
|
|
|
int32_t diff = uprv_strCompare(fArray + start, length, srcChars + srcStart, srcLength, FALSE, TRUE);
|
2002-05-13 23:03:09 +00:00
|
|
|
/* translate the 32-bit result into an 8-bit one */
|
|
|
|
if(diff!=0) {
|
|
|
|
return (int8_t)(diff >> 15 | 1);
|
2001-01-24 02:37:58 +00:00
|
|
|
} else {
|
2002-05-13 23:03:09 +00:00
|
|
|
return 0;
|
2001-01-24 02:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-14 01:10:10 +00:00
|
|
|
int8_t
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doCaseCompare(int32_t start,
|
2001-02-14 01:10:10 +00:00
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
2001-02-14 01:10:10 +00:00
|
|
|
int32_t srcLength,
|
|
|
|
uint32_t options) const
|
|
|
|
{
|
|
|
|
// compare illegal string values
|
|
|
|
if(isBogus()) {
|
|
|
|
if(srcChars==0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if(srcChars==0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
// get the correct pointer
|
|
|
|
const UChar *chars = getArrayStart();
|
|
|
|
|
|
|
|
chars += start;
|
|
|
|
srcChars += srcStart;
|
|
|
|
|
2001-10-05 21:18:12 +00:00
|
|
|
if(chars != srcChars) {
|
2002-05-22 00:35:01 +00:00
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
|
|
|
int32_t result=unorm_cmpEquivFold(chars, length, srcChars, srcLength,
|
|
|
|
options|U_COMPARE_IGNORE_CASE, &errorCode);
|
2001-10-05 21:18:12 +00:00
|
|
|
if(result!=0) {
|
|
|
|
return (int8_t)(result >> 24 | 1);
|
|
|
|
}
|
2002-05-22 00:35:01 +00:00
|
|
|
} else {
|
|
|
|
// get the srcLength if necessary
|
|
|
|
if(srcLength < 0) {
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
|
|
|
}
|
|
|
|
if(length != srcLength) {
|
|
|
|
return (int8_t)((length - srcLength) >> 24 | 1);
|
|
|
|
}
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
2001-10-05 21:18:12 +00:00
|
|
|
return 0;
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
2001-07-02 20:58:14 +00:00
|
|
|
int32_t
|
|
|
|
UnicodeString::getLength() const {
|
|
|
|
return length();
|
|
|
|
}
|
|
|
|
|
2001-02-14 18:02:44 +00:00
|
|
|
UChar
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::getCharAt(int32_t offset) const {
|
2001-02-14 18:02:44 +00:00
|
|
|
return charAt(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
UChar32
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::getChar32At(int32_t offset) const {
|
2001-02-14 18:02:44 +00:00
|
|
|
return char32At(offset);
|
|
|
|
}
|
|
|
|
|
2001-10-11 21:40:24 +00:00
|
|
|
int32_t
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::countChar32(int32_t start, int32_t length) const {
|
2001-10-11 21:40:24 +00:00
|
|
|
pinIndices(start, length);
|
|
|
|
// if(isBogus()) then fArray==0 and start==0 - u_countChar32() checks for NULL
|
|
|
|
return u_countChar32(fArray+start, length);
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
|
|
|
UnicodeString::moveIndex32(int32_t index, int32_t delta) const {
|
2001-09-17 22:05:21 +00:00
|
|
|
// pin index
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doExtract(int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length,
|
|
|
|
UChar *dst,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t dstStart) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-10-03 23:51:00 +00:00
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
// do not copy anything if we alias dst itself
|
|
|
|
if(fArray + start != dst + dstStart) {
|
|
|
|
us_arrayCopy(getArrayStart(), start, dst, dstStart, length);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-09-05 23:37:56 +00:00
|
|
|
int32_t
|
|
|
|
UnicodeString::extract(UChar *dest, int32_t destCapacity,
|
|
|
|
UErrorCode &errorCode) const {
|
|
|
|
if(U_SUCCESS(errorCode)) {
|
2001-09-18 01:12:14 +00:00
|
|
|
if(isBogus() || destCapacity<0 || (destCapacity>0 && dest==0)) {
|
2001-09-05 23:37:56 +00:00
|
|
|
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
2001-10-03 23:51:00 +00:00
|
|
|
if(fLength>0 && fLength<=destCapacity && fArray!=dest) {
|
|
|
|
uprv_memcpy(dest, fArray, fLength*U_SIZEOF_UCHAR);
|
2001-09-05 23:37:56 +00:00
|
|
|
}
|
|
|
|
return u_terminateUChars(dest, destCapacity, fLength, &errorCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fLength;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
1999-12-03 00:04:14 +00:00
|
|
|
UnicodeString::indexOf(const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
1999-12-03 00:04:14 +00:00
|
|
|
int32_t srcLength,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
1999-12-03 00:04:14 +00:00
|
|
|
int32_t length) const
|
|
|
|
{
|
2001-11-13 19:27:19 +00:00
|
|
|
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength == 0) {
|
1999-12-03 00:04:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-13 19:27:19 +00:00
|
|
|
// get the srcLength if necessary
|
|
|
|
if(srcLength < 0) {
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
|
|
|
if(srcLength == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
// 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();
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t limit = start + length;
|
1999-12-03 00:04:14 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString::doIndexOf(UChar c,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
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) {
|
2002-04-30 17:22:11 +00:00
|
|
|
return (int32_t)(begin - getArrayStart());
|
1999-12-03 00:04:14 +00:00
|
|
|
}
|
|
|
|
} while(++begin < limit);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
2002-02-22 02:00:42 +00:00
|
|
|
UnicodeString::doIndexOf(UChar32 c,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
2002-02-22 02:00:42 +00:00
|
|
|
int32_t length) const {
|
|
|
|
// pin indices
|
|
|
|
pinIndices(start, length);
|
|
|
|
if(length == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// c<0xd800 handled by inline function indexOf(UChar32 c, start, length)
|
|
|
|
if(c<=0xdfff) {
|
|
|
|
// surrogate code point
|
2002-03-18 23:25:30 +00:00
|
|
|
const UChar *t = uprv_strFindSurrogate(fArray + start, length, (UChar)c);
|
|
|
|
if(t != 0) {
|
2002-04-30 17:22:11 +00:00
|
|
|
return (int32_t)(t - fArray);
|
2002-03-18 23:25:30 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
2002-02-22 02:00:42 +00:00
|
|
|
}
|
|
|
|
} else if(c<=0xffff) {
|
|
|
|
// non-surrogate BMP code point
|
|
|
|
return doIndexOf((UChar)c, start, length);
|
|
|
|
} else {
|
|
|
|
// supplementary code point, search for string
|
|
|
|
UChar buffer[2] = { UTF16_LEAD(c), UTF16_TRAIL(c) };
|
|
|
|
return indexOf(buffer, 2, start, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
1999-12-03 00:04:14 +00:00
|
|
|
UnicodeString::lastIndexOf(const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
1999-12-03 00:04:14 +00:00
|
|
|
int32_t srcLength,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
1999-12-03 00:04:14 +00:00
|
|
|
int32_t length) const
|
|
|
|
{
|
2001-11-13 19:27:19 +00:00
|
|
|
if(isBogus() || srcChars == 0 || srcStart < 0 || srcLength == 0) {
|
1999-12-03 00:04:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-11-13 19:27:19 +00:00
|
|
|
// get the srcLength if necessary
|
|
|
|
if(srcLength < 0) {
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
|
|
|
if(srcLength == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-03 00:04:14 +00:00
|
|
|
// 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();
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t pos;
|
1999-12-03 00:04:14 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString::doLastIndexOf(UChar c,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
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) {
|
2002-04-30 17:22:11 +00:00
|
|
|
return (int32_t)(limit - getArrayStart());
|
1999-12-03 00:04:14 +00:00
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t
|
2002-02-22 02:00:42 +00:00
|
|
|
UnicodeString::doLastIndexOf(UChar32 c,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start,
|
2002-02-22 02:00:42 +00:00
|
|
|
int32_t length) const {
|
|
|
|
// pin indices
|
|
|
|
pinIndices(start, length);
|
|
|
|
if(length == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// c<0xd800 handled by inline function lastIndexOf(UChar32 c, start, length)
|
|
|
|
if(c<=0xdfff) {
|
|
|
|
// surrogate code point
|
2002-03-18 23:25:30 +00:00
|
|
|
const UChar *t = uprv_strFindLastSurrogate(fArray + start, length, (UChar)c);
|
|
|
|
if(t != 0) {
|
2002-04-30 17:22:11 +00:00
|
|
|
return (int32_t)(t - fArray);
|
2002-03-18 23:25:30 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
2002-02-22 02:00:42 +00:00
|
|
|
}
|
|
|
|
} else if(c<=0xffff) {
|
|
|
|
// non-surrogate BMP code point
|
|
|
|
return doLastIndexOf((UChar)c, start, length);
|
|
|
|
} else {
|
|
|
|
// supplementary code point, search for string
|
|
|
|
UChar buffer[2] = { UTF16_LEAD(c), UTF16_TRAIL(c) };
|
|
|
|
return lastIndexOf(buffer, 2, start, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-07 03:22:12 +00:00
|
|
|
UnicodeString&
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::findAndReplace(int32_t start,
|
1999-12-07 03:22:12 +00:00
|
|
|
int32_t length,
|
|
|
|
const UnicodeString& oldText,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t oldStart,
|
1999-12-07 03:22:12 +00:00
|
|
|
int32_t oldLength,
|
|
|
|
const UnicodeString& newText,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t newStart,
|
1999-12-07 03:22:12 +00:00
|
|
|
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) {
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t pos = indexOf(oldText, oldStart, oldLength, start, length);
|
1999-12-07 03:22:12 +00:00
|
|
|
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)
|
|
|
|
{
|
2001-09-20 22:19:51 +00:00
|
|
|
if(fFlags & kOpenGetBuffer) {
|
|
|
|
// do not modify a string that has an "open" getBuffer(minCapacity)
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2002-06-08 19:42:51 +00:00
|
|
|
if( text == 0 || textLength < -1 ||
|
|
|
|
(textLength == -1 && !isTerminated) ||
|
|
|
|
(textLength >= 0 && isTerminated && text[textLength] != 0)
|
|
|
|
) {
|
2000-03-29 01:13:49 +00:00
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
fArray = (UChar *)text;
|
|
|
|
if(textLength != -1) {
|
|
|
|
fLength = textLength;
|
2002-06-08 19:42:51 +00:00
|
|
|
fCapacity = isTerminated ? fLength + 1 : fLength;
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
|
|
|
// text is terminated, or else it would have failed the above test
|
|
|
|
fLength = u_strlen(text);
|
|
|
|
fCapacity = fLength + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fFlags = kReadonlyAlias;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2001-09-20 01:14:37 +00:00
|
|
|
// setTo() analogous to the writable-aliasing constructor with the same signature
|
2000-03-29 01:13:49 +00:00
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::setTo(UChar *buffer,
|
|
|
|
int32_t buffLength,
|
|
|
|
int32_t buffCapacity) {
|
2001-09-20 22:19:51 +00:00
|
|
|
if(fFlags & kOpenGetBuffer) {
|
|
|
|
// do not modify a string that has an "open" getBuffer(minCapacity)
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2000-03-29 01:13:49 +00:00
|
|
|
if(buffer == 0 || buffLength < 0 || buffLength > buffCapacity) {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
releaseArray();
|
|
|
|
|
|
|
|
fArray = buffer;
|
|
|
|
fLength = buffLength;
|
|
|
|
fCapacity = buffCapacity;
|
2001-09-20 01:14:37 +00:00
|
|
|
fFlags = kWritableAlias;
|
2000-03-29 01:13:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::setCharAt(int32_t offset,
|
1999-08-16 21:50:52 +00:00
|
|
|
UChar c)
|
|
|
|
{
|
2001-10-06 00:22:13 +00:00
|
|
|
if(cloneArrayIfNeeded() && fLength > 0) {
|
2000-03-29 01:13:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-02-14 01:10:10 +00:00
|
|
|
/*
|
|
|
|
* Implement argument checking and buffer handling
|
|
|
|
* for string case mapping as a common function.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
TO_LOWER,
|
|
|
|
TO_UPPER,
|
2002-02-21 18:42:52 +00:00
|
|
|
TO_TITLE,
|
2001-02-14 01:10:10 +00:00
|
|
|
FOLD_CASE
|
|
|
|
};
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-02-14 01:10:10 +00:00
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::toLower() {
|
2002-02-21 18:42:52 +00:00
|
|
|
return caseMap(0, Locale::getDefault(), 0, TO_LOWER);
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::toLower(const Locale &locale) {
|
2002-02-21 18:42:52 +00:00
|
|
|
return caseMap(0, locale, 0, TO_LOWER);
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::toUpper() {
|
2002-02-21 18:42:52 +00:00
|
|
|
return caseMap(0, Locale::getDefault(), 0, TO_UPPER);
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::toUpper(const Locale &locale) {
|
2002-02-21 18:42:52 +00:00
|
|
|
return caseMap(0, locale, 0, TO_UPPER);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::foldCase(uint32_t options) {
|
2002-02-21 18:42:52 +00:00
|
|
|
return caseMap(0, Locale::getDefault(), options, FOLD_CASE);
|
2001-02-14 01:10:10 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-02-14 01:10:10 +00:00
|
|
|
UnicodeString &
|
2002-02-21 18:42:52 +00:00
|
|
|
UnicodeString::caseMap(BreakIterator *titleIter,
|
|
|
|
const Locale& locale,
|
2001-02-14 01:10:10 +00:00
|
|
|
uint32_t options,
|
|
|
|
int32_t toWhichCase) {
|
2001-02-02 20:41:18 +00:00
|
|
|
if(fLength <= 0) {
|
|
|
|
// nothing to do
|
2000-03-29 01:13:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-02-02 20:41:18 +00:00
|
|
|
// We need to allocate a new buffer for the internal string case mapping function.
|
|
|
|
// This is very similar to how doReplace() below keeps the old array pointer
|
|
|
|
// and deletes the old array itself after it is done.
|
|
|
|
// In addition, we are forcing cloneArrayIfNeeded() to always allocate a new array.
|
|
|
|
UChar *oldArray = fArray;
|
|
|
|
int32_t oldLength = fLength;
|
|
|
|
int32_t *bufferToDelete = 0;
|
2001-02-02 21:07:43 +00:00
|
|
|
|
|
|
|
// Make sure that if the string is in fStackBuffer we do not overwrite it!
|
|
|
|
int32_t capacity;
|
|
|
|
if(fLength <= US_STACKBUF_SIZE) {
|
|
|
|
if(fArray == fStackBuffer) {
|
|
|
|
capacity = 2 * US_STACKBUF_SIZE; // make sure that cloneArrayIfNeeded() allocates a new buffer
|
|
|
|
} else {
|
|
|
|
capacity = US_STACKBUF_SIZE;
|
|
|
|
}
|
|
|
|
} else {
|
2002-02-20 02:09:42 +00:00
|
|
|
capacity = fLength + 20;
|
2001-02-02 21:07:43 +00:00
|
|
|
}
|
|
|
|
if(!cloneArrayIfNeeded(capacity, capacity, FALSE, &bufferToDelete, TRUE)) {
|
2001-02-02 20:41:18 +00:00
|
|
|
return *this;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2002-02-21 18:42:52 +00:00
|
|
|
// set up the titlecasing break iterator
|
|
|
|
UBreakIterator *cTitleIter = 0;
|
2002-02-20 02:09:42 +00:00
|
|
|
UErrorCode errorCode;
|
2002-02-21 18:42:52 +00:00
|
|
|
|
|
|
|
if(toWhichCase == TO_TITLE) {
|
|
|
|
if(titleIter != 0) {
|
|
|
|
cTitleIter = (UBreakIterator *)titleIter;
|
|
|
|
} else {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
2002-03-13 19:19:42 +00:00
|
|
|
cTitleIter = ubrk_open(UBRK_TITLE, locale.getName(),
|
2002-02-21 18:42:52 +00:00
|
|
|
oldArray, oldLength,
|
|
|
|
&errorCode);
|
|
|
|
if(U_FAILURE(errorCode)) {
|
2002-07-16 01:55:55 +00:00
|
|
|
uprv_free(bufferToDelete);
|
2002-02-21 18:42:52 +00:00
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case-map, and if the result is too long, then reallocate and repeat.
|
2002-02-20 02:09:42 +00:00
|
|
|
do {
|
|
|
|
errorCode = U_ZERO_ERROR;
|
|
|
|
if(toWhichCase==TO_LOWER) {
|
|
|
|
fLength = u_internalStrToLower(fArray, fCapacity,
|
|
|
|
oldArray, oldLength,
|
2002-02-21 18:42:52 +00:00
|
|
|
0, oldLength,
|
2002-02-20 02:09:42 +00:00
|
|
|
locale.getName(),
|
|
|
|
&errorCode);
|
|
|
|
} else if(toWhichCase==TO_UPPER) {
|
|
|
|
fLength = u_internalStrToUpper(fArray, fCapacity,
|
|
|
|
oldArray, oldLength,
|
|
|
|
locale.getName(),
|
|
|
|
&errorCode);
|
2002-02-21 18:42:52 +00:00
|
|
|
} else if(toWhichCase==TO_TITLE) {
|
|
|
|
fLength = u_internalStrToTitle(fArray, fCapacity,
|
|
|
|
oldArray, oldLength,
|
|
|
|
cTitleIter, locale.getName(),
|
|
|
|
&errorCode);
|
2002-02-20 02:09:42 +00:00
|
|
|
} else {
|
|
|
|
fLength = u_internalStrFoldCase(fArray, fCapacity,
|
|
|
|
oldArray, oldLength,
|
|
|
|
options,
|
|
|
|
&errorCode);
|
|
|
|
}
|
|
|
|
} while(errorCode==U_BUFFER_OVERFLOW_ERROR && cloneArrayIfNeeded(fLength, fLength, FALSE));
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-02-21 18:42:52 +00:00
|
|
|
if(cTitleIter != 0 && titleIter == 0) {
|
|
|
|
ubrk_close(cTitleIter);
|
|
|
|
}
|
|
|
|
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(bufferToDelete);
|
2001-02-02 20:41:18 +00:00
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
setToBogus();
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doReplace( int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length,
|
|
|
|
const UnicodeString& src,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
1999-08-16 21:50:52 +00:00
|
|
|
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&
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doReplace(int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t srcStart,
|
1999-08-16 21:50:52 +00:00
|
|
|
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;
|
2001-11-13 19:27:19 +00:00
|
|
|
} else if(srcLength < 0) {
|
|
|
|
// get the srcLength if necessary
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
1999-12-07 03:22:12 +00:00
|
|
|
}
|
|
|
|
|
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
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(bufferToDelete);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-11-19 23:13:41 +00:00
|
|
|
/**
|
|
|
|
* Replaceable API
|
|
|
|
*/
|
|
|
|
void
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::handleReplaceBetween(int32_t start,
|
|
|
|
int32_t limit,
|
1999-11-19 23:13:41 +00:00
|
|
|
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) {
|
2002-03-18 21:25:01 +00:00
|
|
|
if (limit <= start) {
|
|
|
|
return; // Nothing to do; avoid bogus malloc call
|
|
|
|
}
|
2002-02-28 01:42:40 +00:00
|
|
|
UChar* text = (UChar*) uprv_malloc( sizeof(UChar) * (limit - start) );
|
2000-05-20 04:41:12 +00:00
|
|
|
extractBetween(start, limit, text, 0);
|
|
|
|
insert(dest, text, 0, limit - start);
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(text);
|
2000-05-20 04:41:12 +00:00
|
|
|
}
|
|
|
|
|
2002-07-03 01:03:46 +00:00
|
|
|
/**
|
|
|
|
* Replaceable API
|
|
|
|
*
|
|
|
|
* NOTE: This is for the Replaceable class. There is no rep.cpp,
|
|
|
|
* so we implement this function here.
|
|
|
|
*/
|
|
|
|
UBool Replaceable::hasMetaData() const {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaceable API
|
|
|
|
*/
|
|
|
|
UBool UnicodeString::hasMetaData() const {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doReverse(int32_t start,
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length)
|
|
|
|
{
|
2001-10-06 00:22:13 +00:00
|
|
|
if(fLength <= 1 || !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;
|
2001-09-18 22:28:10 +00:00
|
|
|
UBool hasSupplementary = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
while(left < --right) {
|
2001-09-18 22:28:10 +00:00
|
|
|
hasSupplementary |= (UBool)UTF_IS_LEAD(swap = *left);
|
|
|
|
hasSupplementary |= (UBool)UTF_IS_LEAD(*left++ = *right);
|
1999-08-16 21:50:52 +00:00
|
|
|
*right = swap;
|
|
|
|
}
|
|
|
|
|
2001-09-18 22:28:10 +00:00
|
|
|
/* if there are supplementary code points in the reversed range, then re-swap their surrogates */
|
|
|
|
if(hasSupplementary) {
|
|
|
|
UChar swap2;
|
|
|
|
|
|
|
|
left = getArrayStart() + start;
|
|
|
|
right = getArrayStart() + start + length - 1; // -1 so that we can look at *(left+1) if left<right
|
|
|
|
while(left < right) {
|
|
|
|
if(UTF_IS_TRAIL(swap = *left) && UTF_IS_LEAD(swap2 = *(left + 1))) {
|
|
|
|
*left++ = swap2;
|
|
|
|
*left++ = swap;
|
|
|
|
} else {
|
|
|
|
++left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
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)
|
|
|
|
{
|
2001-10-06 00:22:13 +00:00
|
|
|
if(fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
|
2000-04-01 00:21:57 +00:00
|
|
|
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)
|
|
|
|
{
|
2001-10-06 00:22:13 +00:00
|
|
|
if(fLength >= targetLength || !cloneArrayIfNeeded(targetLength)) {
|
2000-04-01 00:21:57 +00:00
|
|
|
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;
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t i = fLength, length;
|
2000-04-01 00:21:57 +00:00
|
|
|
|
|
|
|
// 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);
|
2002-03-19 03:01:28 +00:00
|
|
|
if(!(c == 0x20 || u_isWhitespace(c))) {
|
2000-04-01 00:21:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(length < fLength) {
|
|
|
|
fLength = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find leading white space
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t start;
|
2000-04-01 00:21:57 +00:00
|
|
|
i = 0;
|
|
|
|
for(;;) {
|
|
|
|
start = i;
|
|
|
|
if(i >= length) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UTF_NEXT_CHAR(fArray, i, length, c);
|
2002-03-19 03:01:28 +00:00
|
|
|
if(!(c == 0x20 || u_isWhitespace(c))) {
|
2000-04-01 00:21:57 +00:00
|
|
|
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
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::extract(int32_t start,
|
2000-03-29 01:13:49 +00:00
|
|
|
int32_t length,
|
2000-08-23 02:10:51 +00:00
|
|
|
char *target,
|
|
|
|
uint32_t dstSize,
|
2000-03-29 01:13:49 +00:00
|
|
|
const char *codepage) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-18 01:12:14 +00:00
|
|
|
// if the arguments are illegal, then do nothing
|
2001-09-28 00:42:28 +00:00
|
|
|
if(/*dstSize < 0 || */(dstSize > 0 && target == 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);
|
|
|
|
|
|
|
|
// create the converter
|
1999-12-01 17:51:53 +00:00
|
|
|
UConverter *converter;
|
2001-03-17 22:14:30 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
// just write the NUL if the string length is 0
|
|
|
|
if(length == 0) {
|
|
|
|
return u_terminateChars(target, dstSize, 0, &status);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// if the codepage is the default, use our cache
|
2001-03-17 22:14:30 +00:00
|
|
|
// if it is an empty string, then use the "invariant character" conversion
|
2000-08-23 02:10:51 +00:00
|
|
|
if (codepage == 0) {
|
2001-08-11 00:29:58 +00:00
|
|
|
converter = u_getDefaultConverter(&status);
|
2000-08-23 02:10:51 +00:00
|
|
|
} else if (*codepage == 0) {
|
2001-03-17 22:14:30 +00:00
|
|
|
// use the "invariant characters" conversion
|
2001-09-18 01:12:14 +00:00
|
|
|
int32_t destLength;
|
|
|
|
// careful: dstSize is unsigned! (0xffffffff means "unlimited")
|
|
|
|
if(dstSize >= 0x80000000) {
|
|
|
|
destLength = length;
|
|
|
|
// make sure that the NUL-termination works (takes int32_t)
|
|
|
|
dstSize=0x7fffffff;
|
|
|
|
} else if(length <= (int32_t)dstSize) {
|
|
|
|
destLength = length;
|
|
|
|
} else {
|
|
|
|
destLength = (int32_t)dstSize;
|
2001-03-17 22:14:30 +00:00
|
|
|
}
|
2001-09-18 01:12:14 +00:00
|
|
|
u_UCharsToChars(getArrayStart() + start, target, destLength);
|
|
|
|
return u_terminateChars(target, (int32_t)dstSize, length, &status);
|
1999-12-01 17:51:53 +00:00
|
|
|
} 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
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
length = doExtract(start, length, target, (int32_t)dstSize, converter, status);
|
|
|
|
|
|
|
|
// close the converter
|
|
|
|
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)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
if(isBogus() || destCapacity<0 || (destCapacity>0 && dest==0)) {
|
|
|
|
errorCode=U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
1999-10-25 22:20:12 +00:00
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
// nothing to do?
|
|
|
|
if(fLength<=0) {
|
|
|
|
return u_terminateChars(dest, destCapacity, 0, &errorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the converter
|
|
|
|
UBool isDefaultConverter;
|
|
|
|
if(cnv==0) {
|
|
|
|
isDefaultConverter=TRUE;
|
|
|
|
cnv=u_getDefaultConverter(&errorCode);
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2000-08-23 02:10:51 +00:00
|
|
|
} else {
|
2001-09-18 01:12:14 +00:00
|
|
|
isDefaultConverter=FALSE;
|
|
|
|
ucnv_resetFromUnicode(cnv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert
|
|
|
|
int32_t length=doExtract(0, fLength, dest, destCapacity, cnv, errorCode);
|
|
|
|
|
|
|
|
// release the converter
|
|
|
|
if(isDefaultConverter) {
|
|
|
|
u_releaseDefaultConverter(cnv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2002-02-26 17:50:59 +00:00
|
|
|
void
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::extractBetween(int32_t start,
|
|
|
|
int32_t limit,
|
2002-02-26 17:50:59 +00:00
|
|
|
UnicodeString& target) const
|
|
|
|
{ doExtract(start, limit - start, target); }
|
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
int32_t
|
2002-03-12 01:32:42 +00:00
|
|
|
UnicodeString::doExtract(int32_t start, int32_t length,
|
2001-09-18 01:12:14 +00:00
|
|
|
char *dest, int32_t destCapacity,
|
|
|
|
UConverter *cnv,
|
|
|
|
UErrorCode &errorCode) const {
|
|
|
|
if(U_FAILURE(errorCode)) {
|
|
|
|
if(destCapacity!=0) {
|
|
|
|
*dest=0;
|
2000-08-23 02:10:51 +00:00
|
|
|
}
|
2001-09-18 01:12:14 +00:00
|
|
|
return 0;
|
2000-08-23 02:10:51 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
const UChar *src=fArray+start, *srcLimit=src+length;
|
|
|
|
char *originalDest=dest;
|
|
|
|
const char *destLimit;
|
|
|
|
|
|
|
|
if(destCapacity==0) {
|
|
|
|
destLimit=dest=0;
|
2001-09-28 00:42:28 +00:00
|
|
|
} else if(destCapacity==-1) {
|
2001-09-18 01:12:14 +00:00
|
|
|
// Pin the limit to U_MAX_PTR if the "magic" destCapacity is used.
|
|
|
|
destLimit=(char*)U_MAX_PTR(dest);
|
|
|
|
// for NUL-termination, translate into highest int32_t
|
|
|
|
destCapacity=0x7fffffff;
|
2000-03-29 01:13:49 +00:00
|
|
|
} else {
|
2001-09-18 01:12:14 +00:00
|
|
|
destLimit=dest+destCapacity;
|
2000-03-29 01:13:49 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
// perform the conversion
|
|
|
|
ucnv_fromUnicode(cnv, &dest, destLimit, &src, srcLimit, 0, TRUE, &errorCode);
|
|
|
|
length=(int32_t)(dest-originalDest);
|
|
|
|
|
|
|
|
// if an overflow occurs, then get the preflighting length
|
|
|
|
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);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ?
|
2001-08-11 00:29:58 +00:00
|
|
|
u_getDefaultConverter(&status) :
|
2000-03-29 01:13:49 +00:00
|
|
|
*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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-09-18 01:12:14 +00:00
|
|
|
// convert using the real converter
|
|
|
|
doCodepageCreate(codepageData, dataLength, converter, status);
|
|
|
|
if(U_FAILURE(status)) {
|
|
|
|
setToBogus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the converter
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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,
|
2001-09-18 22:28:10 +00:00
|
|
|
&mySource, mySourceEnd, 0, TRUE, &status);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// update the conversion parameters
|
2002-04-30 17:22:11 +00:00
|
|
|
fLength = (int32_t)(myTarget - fArray);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// allocate more space and copy data, if needed
|
2000-08-11 19:23:50 +00:00
|
|
|
if(status == U_BUFFER_OVERFLOW_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
|
2002-04-30 17:22:11 +00:00
|
|
|
arraySize = (int32_t)(fLength + 2 * (mySourceEnd - mySource));
|
1999-12-01 17:51:53 +00:00
|
|
|
} else {
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// External Buffer
|
|
|
|
//========================================
|
2001-09-20 01:14:37 +00:00
|
|
|
|
|
|
|
UChar *
|
|
|
|
UnicodeString::getBuffer(int32_t minCapacity) {
|
|
|
|
if(minCapacity>=-1 && cloneArrayIfNeeded(minCapacity)) {
|
|
|
|
fFlags|=kOpenGetBuffer;
|
|
|
|
fLength=0;
|
|
|
|
return fArray;
|
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-09-20 01:14:37 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-20 01:14:37 +00:00
|
|
|
void
|
|
|
|
UnicodeString::releaseBuffer(int32_t newLength) {
|
|
|
|
if(fFlags&kOpenGetBuffer && newLength>=-1) {
|
|
|
|
// set the new fLength
|
|
|
|
if(newLength==-1) {
|
|
|
|
// the new length is the string length, capped by fCapacity
|
|
|
|
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;
|
1999-12-01 21:28:18 +00:00
|
|
|
}
|
2001-09-20 01:14:37 +00:00
|
|
|
fFlags&=~kOpenGetBuffer;
|
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,
|
2001-02-02 20:41:18 +00:00
|
|
|
int32_t **pBufferToDelete,
|
|
|
|
UBool forceClone) {
|
2000-03-29 01:13:49 +00:00
|
|
|
// 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
|
|
|
|
2001-09-20 01:14:37 +00:00
|
|
|
// while a getBuffer(minCapacity) is "open",
|
|
|
|
// prevent any modifications of the string by returning FALSE here
|
|
|
|
if(fFlags & kOpenGetBuffer) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-10-06 00:22:13 +00:00
|
|
|
// if we're bogus, set us to empty first
|
|
|
|
if(fFlags & kIsBogus) {
|
|
|
|
fArray = fStackBuffer;
|
|
|
|
fLength = 0;
|
|
|
|
fCapacity = US_STACKBUF_SIZE;
|
|
|
|
fFlags = kShortString;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2001-02-02 20:41:18 +00:00
|
|
|
if(forceClone ||
|
|
|
|
fFlags & kBufferIsReadonly ||
|
2000-03-29 01:13:49 +00:00
|
|
|
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);
|
2002-03-09 00:36:54 +00:00
|
|
|
if(umtx_atomic_dec(pRefCount) == 0) {
|
2000-03-29 01:13:49 +00:00
|
|
|
if(pBufferToDelete == 0) {
|
2002-02-28 01:42:40 +00:00
|
|
|
uprv_free(pRefCount);
|
2000-03-29 01:13:49 +00:00
|
|
|
} 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
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|