/*
**********************************************************************
* Copyright (C) 1998-2001, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
* File unistr.h
*
* Modification History:
*
* Date Name Description
* 09/25/98 stephen Creation.
* 11/11/98 stephen Changed per 11/9 code review.
* 04/20/99 stephen Overhauled per 4/16 code review.
* 11/18/99 aliu Made to inherit from Replaceable. Added method
* handleReplaceBetween(); other methods unchanged.
******************************************************************************
*/
#ifndef UNISTR_H
#define UNISTR_H
#include "unicode/utypes.h"
#include "unicode/rep.h"
#include "unicode/uchar.h"
class Locale; // unicode/locid.h
class UCharReference;
class UnicodeStringStreamer;// unicode/unistrm.h
class UnicodeConverter; // unicode/convert.h
struct UConverter; // unicode/ucnv.h
// for unistrm.h
/**
* Write the contents of a UnicodeString to an ostream. This functions writes
* the characters in a UnicodeString to an ostream. The UChars in the
* UnicodeString are truncated to char, leading to undefined results with
* anything not in the Latin1 character set.
*/
#if U_IOSTREAM_SOURCE >= 199711
#include In ICU, strings are stored and used as UTF-16.
* This means that a string internally consists of 16-bit Unicode code units. Indexes and offsets into and lengths of strings always count code units, not code points.
* This is the same as with multi-byte char* strings in traditional string handling.
* Operations on partial strings typically do not test for code point boundaries.
* If necessary, the user needs to take care of such boundaries by testing for the code unit
* values or by using functions like UnicodeString::getCharStart() and UnicodeString::getCharLimit()
* (or, in C, the equivalent macros UTF_SET_CHAR_START() and UTF_SET_CHAR_LIMIT(), see utf.h). UnicodeString uses four storage models:
* NUL
, must be specified as a constant.
* The U_STRING_DECL macro should be invoked exactly once for one
* such string variable before it is used.
*/
#if U_SIZEOF_WCHAR_T==U_SIZEOF_UCHAR && U_CHARSET_FAMILY==U_ASCII_FAMILY
# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (UChar *)L ## cs, length)
#elif U_SIZEOF_UCHAR==1 && U_CHARSET_FAMILY==U_ASCII_FAMILY
# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (UChar *)cs, length)
#else
# define UNICODE_STRING(cs, length) UnicodeString(cs, length, "")
#endif
/**
* UnicodeString is a string class that stores Unicode characters directly and provides
* similar functionality as the Java String class.
* It is a concrete implementation of the abstract class Replaceable (for transliteration).
*
*
* UTF-16 is a variable-length encoding: A Unicode character may be stored with either
* one code unit — which is the most common case — or with a matched pair of
* special code units ("surrogates").
* The data type for code units is UChar.
* For single-character handling, a Unicode character code point is a scalar value
* in the range 0..0x10ffff. ICU uses the UChar32 type for code points.
*
dst
itself as an external buffer,
* then extract() will not copy the contents.
*
* @param start offset of first character which will be copied into the array
* @param length the number of characters to extract
* @param dst array in which to copy characters. The length of dst
* must be at least (dstStart + length).
* @param dstStart the offset in dst where the first character
* will be extracted
* @stable
*/
inline void extract(UTextOffset start,
int32_t length,
UChar *dst,
UTextOffset dstStart = 0) const;
/**
* Copy the characters in the range
* [start, start + length) into the UnicodeString
* target.
* @param start offset of first character which will be copied
* @param length the number of characters to extract
* @param target UnicodeString into which to copy characters.
* @return A reference to target
* @stable
*/
inline void extract(UTextOffset start,
int32_t length,
UnicodeString& target) const;
/**
* Copy the characters in the range [start, limit)
* into the array dst, beginning at dstStart.
* @param start offset of first character which will be copied into the array
* @param limit offset immediately following the last character to be copied
* @param dst array in which to copy characters. The length of dst
* must be at least (dstStart + (limit - start)).
* @param dstStart the offset in dst where the first character
* will be extracted
* @stable
*/
inline void extractBetween(UTextOffset start,
UTextOffset limit,
UChar *dst,
UTextOffset dstStart = 0) const;
/**
* Copy the characters in the range [start, limit)
* into the UnicodeString target.
* @param start offset of first character which will be copied
* @param limit offset immediately following the last character to be copied
* @param target UnicodeString into which to copy characters.
* @return A reference to target
* @stable
*/
inline void extractBetween(UTextOffset start,
UTextOffset limit,
UnicodeString& target) const;
/**
* Copy the characters in the range
* [start, start + length) into an array of characters
* in a specified codepage.
* @param start offset of first character which will be copied
* @param startLength the number of characters to extract
* @param target the target buffer for extraction
* @param codepage the desired codepage for the characters. 0 has
* the special meaning of the default codepage
* If codepage
is an empty string (""
),
* then a simple conversion is performed on the codepage-invariant
* subset ("invariant characters") of the platform encoding. See utypes.h.
* If target is NULL, then the number of bytes required for
* target is returned. It is assumed that the target is big enough
* to fit all of the characters.
* @return the number of characters written to target
* @stable
*/
inline int32_t extract(UTextOffset start,
int32_t startLength,
char *target,
const char *codepage = 0) const;
/**
* Copy the characters in the range
* [start, start + length) into an array of characters
* in a specified codepage.
* @param start offset of first character which will be copied
* @param startLength the number of characters to extract
* @param target the target buffer for extraction
* @param targetLength the length of the target buffer
* @param codepage the desired codepage for the characters. 0 has
* the special meaning of the default codepage
* If codepage
is an empty string (""
),
* then a simple conversion is performed on the codepage-invariant
* subset ("invariant characters") of the platform encoding. See utypes.h.
* If target is NULL, then the number of bytes required for
* target is returned.
* @return the number of characters written to target
* @stable
*/
int32_t extract(UTextOffset start,
int32_t startLength,
char *target,
uint32_t targetLength,
const char *codepage = 0) const;
/* Length operations */
/**
* Return the length of the UnicodeString object.
* The length is the number of characters in the text.
* @returns the length of the UnicodeString object
* @stable
*/
inline int32_t length(void) const;
/**
* Determine if this string is empty.
* @return TRUE if this string contains 0 characters, FALSE otherwise.
* @stable
*/
inline UBool empty(void) const;
/* Other operations */
/**
* Generate a hash code for this object.
* @return The hash code of this UnicodeString.
* @stable
*/
inline int32_t hashCode(void) const;
/**
* Determine if this string is still valid.
* @return TRUE if the string is valid, FALSE otherwise
* @draft
*/
inline UBool isBogus(void) const;
//========================================
// Write operations
//========================================
/* Assignment operations */
/**
* Assignment operator. Replace the characters in this UnicodeString
* with the characters from srcText.
* @param srcText The text containing the characters to replace
* @return a reference to this
* @stable
*/
UnicodeString& operator= (const UnicodeString& srcText);
/**
* Assignment operator. Replace the characters in this UnicodeString
* with the code unit ch.
* @param ch the code unit to replace
* @return a reference to this
* @draft
*/
inline UnicodeString& operator= (UChar ch);
/**
* Assignment operator. Replace the characters in this UnicodeString
* with the code point ch.
* @param ch the code point to replace
* @return a reference to this
* @draft
*/
inline UnicodeString& operator= (UChar32 ch);
/**
* Set the text in the UnicodeString object to the characters
* in srcText in the range
* [srcStart, srcStart + srcLength).
* srcText is not modified.
* @param srcText the source for the new characters
* @param srcStart the offset into srcText where new characters
* will be obtained
* @param srcLength the number of characters in srcText in the
* replace string.
* @return a reference to this
* @stable
*/
inline UnicodeString& setTo(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength);
/**
* Set the text in the UnicodeString object to the characters in
* srcText.
* srcText is not modified.
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
inline UnicodeString& setTo(const UnicodeString& srcText);
/**
* Set the characters in the UnicodeString object to the characters
* in srcChars. srcChars is not modified.
* @param srcChars the source for the new characters
* @param srcLength the number of Unicode characters in srcChars.
* @return a reference to this
* @stable
*/
inline UnicodeString& setTo(const UChar *srcChars,
int32_t srcLength);
/**
* Set the characters in the UnicodeString object to the code unit
* srcChar.
* @param srcChar the code unit which becomes the UnicodeString's character
* content
* @return a reference to this
* @draft
*/
UnicodeString& setTo(UChar srcChar);
/**
* Set the characters in the UnicodeString object to the code point
* srcChar.
* @param srcChar the code point which becomes the UnicodeString's character
* content
* @return a reference to this
* @draft
*/
UnicodeString& setTo(UChar32 srcChar);
/**
* Aliasing setTo() function, analogous to the readonly-aliasing UChar* constructor.
* The text will be used for the UnicodeString object, but
* it will not be released when the UnicodeString is destroyed.
* This has copy-on-write semantics:
* When the string is modified, then the buffer is first copied into
* newly allocated memory.
* The aliased buffer is never modified.
* In an assignment to another UnicodeString, the text will be aliased again,
* so that both strings then alias the same readonly-text.
*
* @param isTerminated specifies if text
is NUL
-terminated.
* This must be true if textLength==-1
.
* @param text The characters to alias for the UnicodeString.
* @param textLength The number of Unicode characters in text
to alias.
* If -1, then this constructor will determine the length
* by calling u_strlen()
.
* @draft
*/
UnicodeString &setTo(UBool isTerminated,
const UChar *text,
int32_t textLength);
/**
* Aliasing setTo() function, analogous to the writeable-aliasing UChar* constructor.
* The text will be used for the UnicodeString object, but
* it will not be released when the UnicodeString is destroyed.
* This has write-through semantics:
* For as long as the capacity of the buffer is sufficient, write operations
* will directly affect the buffer. When more capacity is necessary, then
* a new buffer will be allocated and the contents copied as with regularly
* constructed strings.
* In an assignment to another UnicodeString, the buffer will be copied.
* The extract(UChar *dst) function detects whether the dst pointer is the same
* as the string buffer itself and will in this case not copy the contents.
*
* @param buffer The characters to alias for the UnicodeString.
* @param buffLength The number of Unicode characters in buffer
to alias.
* @param buffCapacity The size of buffer
in UChars.
* @draft
*/
UnicodeString &setTo(UChar *buffer,
int32_t buffLength,
int32_t buffCapacity);
/**
* Set the character at the specified offset to the specified character.
* @param offset A valid offset into the text of the character to set
* @param ch The new character
* @return A reference to this
* @draft
*/
UnicodeString& setCharAt(UTextOffset offset,
UChar ch);
/* Append operations */
/**
* Append operator. Append the code unit ch to the UnicodeString
* object.
* @param ch the code unit to be appended
* @return a reference to this
* @draft
*/
inline UnicodeString& operator+= (UChar ch);
/**
* Append operator. Append the code point ch to the UnicodeString
* object.
* @param ch the code point to be appended
* @return a reference to this
* @draft
*/
inline UnicodeString& operator+= (UChar32 ch);
/**
* Append operator. Append the characters in srcText to the
* UnicodeString object at offset start. srcText is
* not modified.
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
inline UnicodeString& operator+= (const UnicodeString& srcText);
/**
* Append the characters
* in srcText in the range
* [srcStart, srcStart + srcLength) to the
* UnicodeString object at offset start. srcText
* is not modified.
* @param srcText the source for the new characters
* @param srcStart the offset into srcText where new characters
* will be obtained
* @param srcLength the number of characters in srcText in
* the append string
* @return a reference to this
* @stable
*/
inline UnicodeString& append(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength);
/**
* Append the characters in srcText to the UnicodeString object at
* offset start. srcText is not modified.
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
inline UnicodeString& append(const UnicodeString& srcText);
/**
* Append the characters in srcChars in the range
* [srcStart, srcStart + srcLength) to the UnicodeString
* object at offset
* start. srcChars is not modified.
* @param srcChars the source for the new characters
* @param srcStart the offset into srcChars where new characters
* will be obtained
* @param srcLength the number of characters in srcChars in
* the append string
* @return a reference to this
* @stable
*/
inline UnicodeString& append(const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength);
/**
* Append the characters in srcChars to the UnicodeString object
* at offset start. srcChars is not modified.
* @param srcChars the source for the new characters
* @param srcLength the number of Unicode characters in srcChars
* @return a reference to this
* @stable
*/
inline UnicodeString& append(const UChar *srcChars,
int32_t srcLength);
/**
* Append the code unit srcChar to the UnicodeString object.
* @param srcChar the code unit to append
* @return a reference to this
* @draft
*/
inline UnicodeString& append(UChar srcChar);
/**
* Append the code point srcChar to the UnicodeString object.
* @param srcChar the code point to append
* @return a reference to this
* @draft
*/
inline UnicodeString& append(UChar32 srcChar);
/* Insert operations */
/**
* Insert the characters in srcText in the range
* [srcStart, srcStart + srcLength) into the UnicodeString
* object at offset start. srcText is not modified.
* @param start the offset where the insertion begins
* @param srcText the source for the new characters
* @param srcStart the offset into srcText where new characters
* will be obtained
* @param srcLength the number of characters in srcText in
* the insert string
* @return a reference to this
* @stable
*/
inline UnicodeString& insert(UTextOffset start,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength);
/**
* Insert the characters in srcText into the UnicodeString object
* at offset start. srcText is not modified.
* @param start the offset where the insertion begins
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
inline UnicodeString& insert(UTextOffset start,
const UnicodeString& srcText);
/**
* Insert the characters in srcChars in the range
* [srcStart, srcStart + srcLength) into the UnicodeString
* object at offset start. srcChars is not modified.
* @param start the offset at which the insertion begins
* @param srcChars the source for the new characters
* @param srcStart the offset into srcChars where new characters
* will be obtained
* @param srcLength the number of characters in srcChars
* in the insert string
* @return a reference to this
* @stable
*/
inline UnicodeString& insert(UTextOffset start,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength);
/**
* Insert the characters in srcChars into the UnicodeString object
* at offset start. srcChars is not modified.
* @param start the offset where the insertion begins
* @param srcChars the source for the new characters
* @param srcLength the number of Unicode characters in srcChars.
* @return a reference to this
* @stable
*/
inline UnicodeString& insert(UTextOffset start,
const UChar *srcChars,
int32_t srcLength);
/**
* Insert the code unit srcChar into the UnicodeString object at
* offset start.
* @param start the offset at which the insertion occurs
* @param srcChar the code unit to insert
* @return a reference to this
* @draft
*/
inline UnicodeString& insert(UTextOffset start,
UChar srcChar);
/**
* Insert the code point srcChar into the UnicodeString object at
* offset start.
* @param start the offset at which the insertion occurs
* @param srcChar the code point to insert
* @return a reference to this
* @draft
*/
inline UnicodeString& insert(UTextOffset start,
UChar32 srcChar);
/* Replace operations */
/**
* Replace the characters in the range
* [start, start + length) with the characters in
* srcText in the range
* [srcStart, srcStart + srcLength).
* srcText is not modified.
* @param start the offset at which the replace operation begins
* @param length the number of characters to replace. The character at
* start + length is not modified.
* @param srcText the source for the new characters
* @param srcStart the offset into srcText where new characters
* will be obtained
* @param srcLength the number of characters in srcText in
* the replace string
* @return a reference to this
* @stable
*/
UnicodeString& replace(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength);
/**
* Replace the characters in the range
* [start, start + length)
* with the characters in srcText. srcText is
* not modified.
* @param start the offset at which the replace operation begins
* @param length the number of characters to replace. The character at
* start + length is not modified.
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
UnicodeString& replace(UTextOffset start,
int32_t length,
const UnicodeString& srcText);
/**
* Replace the characters in the range
* [start, start + length) with the characters in
* srcChars in the range
* [srcStart, srcStart + srcLength). srcChars
* is not modified.
* @param start the offset at which the replace operation begins
* @param length the number of characters to replace. The character at
* start + length is not modified.
* @param srcChars the source for the new characters
* @param srcStart the offset into srcChars where new characters
* will be obtained
* @param srcLength the number of characters in srcChars
* in the replace string
* @return a reference to this
* @stable
*/
UnicodeString& replace(UTextOffset start,
int32_t length,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength);
/**
* Replace the characters in the range
* [start, start + length) with the characters in
* srcChars. srcChars is not modified.
* @param start the offset at which the replace operation begins
* @param length number of characters to replace. The character at
* start + length is not modified.
* @param srcChars the source for the new characters
* @param srcLength the number of Unicode characters in srcChars
* @return a reference to this
* @stable
*/
inline UnicodeString& replace(UTextOffset start,
int32_t length,
const UChar *srcChars,
int32_t srcLength);
/**
* Replace the characters in the range
* [start, start + length) with the code unit
* srcChar.
* @param start the offset at which the replace operation begins
* @param length the number of characters to replace. The character at
* start + length is not modified.
* @param srcChar the new code unit
* @return a reference to this
* @draft
*/
inline UnicodeString& replace(UTextOffset start,
int32_t length,
UChar srcChar);
/**
* Replace the characters in the range
* [start, start + length) with the code point
* srcChar.
* @param start the offset at which the replace operation begins
* @param length the number of characters to replace. The character at
* start + length is not modified.
* @param srcChar the new code point
* @return a reference to this
* @draft
*/
inline UnicodeString& replace(UTextOffset start,
int32_t length,
UChar32 srcChar);
/**
* Replace the characters in the range [start, limit)
* with the characters in srcText. srcText is not modified.
* @param start the offset at which the replace operation begins
* @param limit the offset immediately following the replace range
* @param srcText the source for the new characters
* @return a reference to this
* @stable
*/
inline UnicodeString& replaceBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText);
/**
* Replace the characters in the range [start, limit)
* with the characters in srcText in the range
* [srcStart, srcLimit). srcText is not modified.
* @param start the offset at which the replace operation begins
* @param limit the offset immediately following the replace range
* @param srcText the source for the new characters
* @param srcStart the offset into srcChars where new characters
* will be obtained
* @param srcLimit the offset immediately following the range to copy
* in srcText
* @return a reference to this
* @stable
*/
inline UnicodeString& replaceBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText,
UTextOffset srcStart,
UTextOffset srcLimit);
/**
* Replace a substring of this object with the given text.
* @param start the beginning index, inclusive; 0 <= start
* <= limit
.
* @param limit the ending index, exclusive; start <= limit
* <= length()
.
* @param text the text to replace characters start
* to limit - 1
* @stable
*/
virtual void handleReplaceBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& text);
/**
* Copy a substring of this object, retaining attribute (out-of-band)
* information. This method is used to duplicate or reorder substrings.
* The destination index must not overlap the source range.
*
* @param start the beginning index, inclusive; 0 <= start <=
* limit
.
* @param limit the ending index, exclusive; start <= limit <=
* length()
.
* @param dest the destination index. The characters from
* start..limit-1
will be copied to dest
.
* Implementations of this method may assume that dest <= start ||
* dest >= limit
.
*/
virtual void copy(int32_t start, int32_t limit, int32_t dest);
/* Search and replace operations */
/**
* Replace all occurrences of characters in oldText with the characters
* in newText
* @param oldText the text containing the search text
* @param newText the text containing the replacement text
* @return a reference to this
* @stable
*/
inline UnicodeString& findAndReplace(const UnicodeString& oldText,
const UnicodeString& newText);
/**
* Replace all occurrences of characters in oldText with characters
* in newText
* in the range [start, start + length).
* @param start the start of the range in which replace will performed
* @param length the length of the range in which replace will be performed
* @param oldText the text containing the search text
* @param newText the text containing the replacement text
* @return a reference to this
* @stable
*/
inline UnicodeString& findAndReplace(UTextOffset start,
int32_t length,
const UnicodeString& oldText,
const UnicodeString& newText);
/**
* Replace all occurrences of characters in oldText in the range
* [oldStart, oldStart + oldLength) with the characters
* in newText in the range
* [newStart, newStart + newLength)
* in the range [start, start + length).
* @param start the start of the range in which replace will performed
* @param length the length of the range in which replace will be performed
* @param oldText the text containing the search text
* @param oldStart the start of the search range in oldText
* @param oldLength the length of the search range in oldText
* @param newText the text containing the replacement text
* @param newStart the start of the replacement range in newText
* @param newLength the length of the replacement range in newText
* @return a reference to this
* @stable
*/
UnicodeString& findAndReplace(UTextOffset start,
int32_t length,
const UnicodeString& oldText,
UTextOffset oldStart,
int32_t oldLength,
const UnicodeString& newText,
UTextOffset newStart,
int32_t newLength);
/* Remove operations */
/**
* Remove all characters from the UnicodeString object.
* @return a reference to this
* @stable
*/
inline UnicodeString& remove(void);
/**
* Remove the characters in the range
* [start, start + length) from the UnicodeString object.
* @param start the offset of the first character to remove
* @param length the number of characters to remove
* @return a reference to this
* @stable
*/
inline UnicodeString& remove(UTextOffset start,
int32_t length = INT32_MAX);
/**
* Remove the characters in the range
* [start, limit) from the UnicodeString object.
* @param start the offset of the first character to remove
* @param limit the offset immediately following the range to remove
* @return a reference to this
* @stable
*/
inline UnicodeString& removeBetween(UTextOffset start,
UTextOffset limit = INT32_MAX);
/* Length operations */
/**
* Pad the start of this UnicodeString with the character padChar.
* If the length of this UnicodeString is less than targetLength,
* length() - targetLength copies of padChar will be added to the
* beginning of this UnicodeString.
* @param targetLength the desired length of the string
* @param padChar the character to use for padding. Defaults to
* space (U+0020)
* @return TRUE if the text was padded, FALSE otherwise.
* @draft
*/
UBool padLeading(int32_t targetLength,
UChar padChar = 0x0020);
/**
* Pad the end of this UnicodeString with the character padChar.
* If the length of this UnicodeString is less than targetLength,
* length() - targetLength copies of padChar will be added to the
* end of this UnicodeString.
* @param targetLength the desired length of the string
* @param padChar the character to use for padding. Defaults to
* space (U+0020)
* @return TRUE if the text was padded, FALSE otherwise.
* @draft
*/
UBool padTrailing(int32_t targetLength,
UChar padChar = 0x0020);
/**
* Truncate this UnicodeString to the targetLength.
* @param targetLength the desired length of this UnicodeString.
* @return TRUE if the text was truncated, FALSE otherwise
* @stable
*/
inline UBool truncate(int32_t targetLength);
/**
* Trims leading and trailing whitespace from this UnicodeString.
* @return a reference to this
* @stable
*/
UnicodeString& trim(void);
/* Miscellaneous operations */
/**
* Reverse this UnicodeString in place.
* @return a reference to this
* @stable
*/
inline UnicodeString& reverse(void);
/**
* Reverse the range [start, start + length) in
* this UnicodeString.
* @param start the start of the range to reverse
* @param length the number of characters to to reverse
* @return a reference to this
* @stable
*/
inline UnicodeString& reverse(UTextOffset start,
int32_t length);
/**
* Convert the characters in this to UPPER CASE following the conventions of
* the default locale.
* @return A reference to this.
* @stable
*/
UnicodeString& toUpper(void);
/**
* Convert the characters in this to UPPER CASE following the conventions of
* a specific locale.
* @param locale The locale containing the conventions to use.
* @return A reference to this.
* @stable
*/
UnicodeString& toUpper(const Locale& locale);
/**
* Convert the characters in this to lower case following the conventions of
* the default locale.
* @return A reference to this.
* @stable
*/
UnicodeString& toLower(void);
/**
* Convert the characters in this to lower case following the conventions of
* a specific locale.
* @param locale The locale containing the conventions to use.
* @return A reference to this.
* @stable
*/
UnicodeString& toLower(const Locale& locale);
/**
* Case-fold the characters in this string.
* Case-folding is locale-independent and not context-sensitive,
* but there is an option for whether to include or exclude mappings for dotted I
* and dotless i that are marked with 'I' in CaseFolding.txt.
* The result may be longer or shorter than the original.
*
* @param options Either U_FOLD_CASE_DEFAULT or U_FOLD_CASE_EXCLUDE_SPECIAL_I
* @return A reference to this.
* @draft
*/
UnicodeString &foldCase(uint32_t options=U_FOLD_CASE_DEFAULT);
//========================================
// Constructors
//========================================
/** Construct an empty UnicodeString.
* @stable
*/
UnicodeString();
/**
* Construct a UnicodeString with capacity to hold capacity UChars
* @param capacity the number of UChars this UnicodeString should hold
* before a resize is necessary; if count is greater than 0 and count
* code points c take up more space than capacity, then capacity is adjusted
* accordingly.
* @param c is used to initially fill the string
* @param count specifies how many code points c are to be written in the
* string
* @draft
*/
UnicodeString(int32_t capacity, UChar32 c, int32_t count);
/**
* Single UChar (code unit) constructor.
* @param ch the character to place in the UnicodeString
* @draft
*/
UnicodeString(UChar ch);
/**
* Single UChar32 (code point) constructor.
* @param ch the character to place in the UnicodeString
* @draft
*/
UnicodeString(UChar32 ch);
/**
* UChar* constructor.
* @param text The characters to place in the UnicodeString. text
* must be NULL (U+0000) terminated.
* @stable
*/
UnicodeString(const UChar *text);
/**
* UChar* constructor.
* @param text The characters to place in the UnicodeString.
* @param textLength The number of Unicode characters in text
* to copy.
* @stable
*/
UnicodeString(const UChar *text,
int32_t textLength);
/**
* Readonly-aliasing UChar* constructor.
* The text will be used for the UnicodeString object, but
* it will not be released when the UnicodeString is destroyed.
* This has copy-on-write semantics:
* When the string is modified, then the buffer is first copied into
* newly allocated memory.
* The aliased buffer is never modified.
* In an assignment to another UnicodeString, the text will be aliased again,
* so that both strings then alias the same readonly-text.
*
* @param isTerminated specifies if text
is NUL
-terminated.
* This must be true if textLength==-1
.
* @param text The characters to alias for the UnicodeString.
* @param textLength The number of Unicode characters in text
to alias.
* If -1, then this constructor will determine the length
* by calling u_strlen()
.
* @stable
*/
UnicodeString(UBool isTerminated,
const UChar *text,
int32_t textLength);
/**
* Writeable-aliasing UChar* constructor.
* The text will be used for the UnicodeString object, but
* it will not be released when the UnicodeString is destroyed.
* This has write-through semantics:
* For as long as the capacity of the buffer is sufficient, write operations
* will directly affect the buffer. When more capacity is necessary, then
* a new buffer will be allocated and the contents copied as with regularly
* constructed strings.
* In an assignment to another UnicodeString, the buffer will be copied.
* The extract(UChar *dst) function detects whether the dst pointer is the same
* as the string buffer itself and will in this case not copy the contents.
*
* @param buffer The characters to alias for the UnicodeString.
* @param buffLength The number of Unicode characters in buffer
to alias.
* @param buffCapacity The size of buffer
in UChars.
* @draft
*/
UnicodeString(UChar *buffer, int32_t buffLength, int32_t buffCapacity);
/**
* char* constructor.
* @param codepageData an array of bytes, null-terminated
* @param codepage the encoding of codepageData. The special
* value 0 for codepage indicates that the text is in the
* platform's default codepage.
* If codepage
is an empty string (""
),
* then a simple conversion is performed on the codepage-invariant
* subset ("invariant characters") of the platform encoding. See utypes.h.
* @stable
*/
UnicodeString(const char *codepageData,
const char *codepage = 0);
/**
* char* constructor.
* @param codepageData an array of bytes.
* @param dataLength The number of bytes in codepageData.
* @param codepage the encoding of codepageData. The special
* value 0 for codepage indicates that the text is in the
* platform's default codepage.
* If codepage
is an empty string (""
),
* then a simple conversion is performed on the codepage-invariant
* subset ("invariant characters") of the platform encoding. See utypes.h.
* @stable
*/
UnicodeString(const char *codepageData,
int32_t dataLength,
const char *codepage = 0);
/**
* Copy constructor.
* @param that The UnicodeString object to copy.
* @stable
*/
UnicodeString(const UnicodeString& that);
/** Destructor.
* @stable
*/
~UnicodeString();
/* Miscellaneous operations */
/**
* Returns the number of display cells occupied by the range
* [start, length).
* This function is designed for Asian text and properly takes into account
* halfwidth and fullwidth variants of various CJK characters and the
* combining behavior of the Hangul Jamo characters (with some limitations;
* see documentation for Unicode::getCellWidth()).codepage==0
, then the default converter
* is used for the platform encoding.
* If codepage
is an empty string (""
),
* then a simple conversion is performed on the codepage-invariant
* subset ("invariant characters") of the platform encoding. See utypes.h.
*/
void doCodepageCreate(const char *codepageData,
int32_t dataLength,
const char *codepage);
/*
* This function is called when write access to the array
* is necessary.
*
* 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.
*/
UBool cloneArrayIfNeeded(int32_t newCapacity = -1,
int32_t growCapacity = -1,
UBool doCopyArray = TRUE,
int32_t **pBufferToDelete = 0,
UBool forceClone = FALSE);
// GrowBuffer function for string case mapping
static UBool U_CALLCONV
growBuffer(void *context,
UChar **buffer, int32_t *pCapacity, int32_t reqCapacity,
int32_t length);
// common function for case mappings
UnicodeString &
caseMap(const Locale& locale,
uint32_t options,
int32_t toWhichCase);
// ref counting
inline int32_t addRef(void);
inline int32_t removeRef(void);
inline int32_t refCount(void) const;
inline int32_t setRefCount(int32_t count);
// constants
enum {
#if UTF_SIZE==8
US_STACKBUF_SIZE=14, // Size of stack buffer for small strings
#elif UTF_SIZE==16
US_STACKBUF_SIZE=7, // Size of stack buffer for small strings
#else // UTF_SIZE==32
US_STACKBUF_SIZE=3, // Size of stack buffer for small strings
#endif
kInvalidUChar=0xffff, // invalid UChar index
kGrowSize=128, // grow size for this buffer
kInvalidHashCode=0, // invalid hash code
kEmptyHashCode=1, // hash code for empty string
// bit flag values for fFlags
kIsBogus=1, // this string is bogus, i.e., not valid
kUsingStackBuffer=2, // fArray==fStackBuffer
kRefCounted=4, // there is a refCount field before the characters in fArray
kBufferIsReadonly=8, // do not write to this buffer
// combined values for convenience
kShortString=kUsingStackBuffer,
kLongString=kRefCounted,
kReadonlyAlias=kBufferIsReadonly,
kWriteableAlias=0
};
// statics
// default converter cache
static UConverter* getDefaultConverter(UErrorCode& status);
static void releaseDefaultConverter(UConverter *converter);
static UConverter *fgDefaultConverter;
friend class UnicodeStringStreamer;
friend class UnicodeConverter;
#if U_IOSTREAM_SOURCE >= 199711
friend U_COMMON_API std::ostream &operator<<(std::ostream& stream, const UnicodeString& s);
#elif U_IOSTREAM_SOURCE >= 198506
friend U_COMMON_API ostream &operator<<(ostream& stream, const UnicodeString& s);
#endif
friend class StringCharacterIterator;
/*
* The following are all the class fields that are stored
* in each UnicodeString object.
* Note that UnicodeString has virtual functions,
* therefore there is an implicit vtable pointer
* as the first real field.
* The fields should be aligned such that no padding is
* necessary, mostly by having larger types first.
* On 32-bit machines, the size should be 32 bytes,
* on 64-bit machines (8-byte pointers), it should be 40 bytes.
*/
// (implicit) *vtable;
// fLength is moved into the superclass Replaceable (jitterbug 709)
// int32_t fLength; // number characters in fArray
int32_t fCapacity; // sizeof fArray
UChar *fArray; // the Unicode data
uint16_t fFlags; // bit flags: see constants above
#if UTF_SIZE==32
uint16_t fPadding; // padding to align the fStackBuffer for UTF-32
#endif
UChar fStackBuffer [ US_STACKBUF_SIZE ]; // buffer for small strings
public:
#ifdef ICU_UNICODESTRING_USE_DEPRECATES
//========================================
// Deprecated API
//========================================
/* size() -> length()
* @deprecated Remove after 2000-dec-31. Use length() instead. */
inline int32_t size(void) const;
// parameters reordered for consistency
/* @deprecated To be removed in first release in 2001. Use the other versions of this function */
inline UnicodeString& findAndReplace(const UnicodeString& oldText,
const UnicodeString& newText,
UTextOffset start,
int32_t length);
/* @deprecated Remove after 2000-dec-31. There is no replacement. */
inline void* operator new(size_t size);
/* @deprecated Remove after 2000-dec-31. There is no replacement. */
inline void* operator new(size_t size, void *location);
/* @deprecated Remove after 2000-dec-31. There is no replacement. */
inline void operator delete(void *location);
#endif
//========================================
// Non-public API - will be removed!
//========================================
/* @deprecated Remove after 2000-dec-31. There is no public replacement. */
const UChar* getUChars() const;
};
//========================================
// Array copying
//========================================
// Copy an array of UnicodeString OBJECTS (not pointers).
inline void
uprv_arrayCopy(const UnicodeString *src, UnicodeString *dst, int32_t count)
{ while(count-- > 0) *dst++ = *src++; }
inline void
uprv_arrayCopy(const UnicodeString *src, int32_t srcStart,
UnicodeString *dst, int32_t dstStart, int32_t count)
{ uprv_arrayCopy(src+srcStart, dst+dstStart, count); }
//========================================
// Inline members
//========================================
//========================================
// Read-only alias methods
//========================================
inline UBool
UnicodeString::operator== (const UnicodeString& text) const
{
if(isBogus()) {
return text.isBogus();
} else {
return
!text.isBogus() &&
fLength == text.fLength &&
doCompare(0, fLength, text, 0, text.fLength) == 0;
}
}
inline UBool
UnicodeString::operator!= (const UnicodeString& text) const
{ return (! operator==(text)); }
inline UBool
UnicodeString::operator> (const UnicodeString& text) const
{ return doCompare(0, fLength, text, 0, text.fLength) == 1; }
inline UBool
UnicodeString::operator< (const UnicodeString& text) const
{ return doCompare(0, fLength, text, 0, text.fLength) == -1; }
inline UBool
UnicodeString::operator>= (const UnicodeString& text) const
{ return doCompare(0, fLength, text, 0, text.fLength) != -1; }
inline UBool
UnicodeString::operator<= (const UnicodeString& text) const
{ return doCompare(0, fLength, text, 0, text.fLength) != 1; }
inline int8_t
UnicodeString::compare(const UnicodeString& text) const
{ return doCompare(0, fLength, text, 0, text.fLength); }
inline int8_t
UnicodeString::compare(UTextOffset start,
int32_t length,
const UnicodeString& srcText) const
{ return doCompare(start, length, srcText, 0, srcText.fLength); }
inline int8_t
UnicodeString::compare(const UChar *srcChars,
int32_t srcLength) const
{ return doCompare(0, fLength, srcChars, 0, srcLength); }
inline int8_t
UnicodeString::compare(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(start, length, srcText, srcStart, srcLength); }
inline int8_t
UnicodeString::compare(UTextOffset start,
int32_t length,
const UChar *srcChars) const
{ return doCompare(start, length, srcChars, 0, length); }
inline int8_t
UnicodeString::compare(UTextOffset start,
int32_t length,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(start, length, srcChars, srcStart, srcLength); }
inline int8_t
UnicodeString::compareBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText,
UTextOffset srcStart,
UTextOffset srcLimit) const
{ return doCompare(start, limit - start,
srcText, srcStart, srcLimit - srcStart); }
inline int8_t
UnicodeString::doCompare(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{
const UChar *srcChars;
if(!srcText.isBogus()) {
srcText.pinIndices(srcStart, srcLength);
srcChars=srcText.getArrayStart();
} else {
srcChars=0;
}
return doCompare(start, length, srcChars, srcStart, srcLength);
}
inline int8_t
UnicodeString::compareCodePointOrder(const UnicodeString& text) const
{ return doCompareCodePointOrder(0, fLength, text, 0, text.fLength); }
inline int8_t
UnicodeString::compareCodePointOrder(UTextOffset start,
int32_t length,
const UnicodeString& srcText) const
{ return doCompareCodePointOrder(start, length, srcText, 0, srcText.fLength); }
inline int8_t
UnicodeString::compareCodePointOrder(const UChar *srcChars,
int32_t srcLength) const
{ return doCompareCodePointOrder(0, fLength, srcChars, 0, srcLength); }
inline int8_t
UnicodeString::compareCodePointOrder(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompareCodePointOrder(start, length, srcText, srcStart, srcLength); }
inline int8_t
UnicodeString::compareCodePointOrder(UTextOffset start,
int32_t length,
const UChar *srcChars) const
{ return doCompareCodePointOrder(start, length, srcChars, 0, length); }
inline int8_t
UnicodeString::compareCodePointOrder(UTextOffset start,
int32_t length,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompareCodePointOrder(start, length, srcChars, srcStart, srcLength); }
inline int8_t
UnicodeString::compareCodePointOrderBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText,
UTextOffset srcStart,
UTextOffset srcLimit) const
{ return doCompareCodePointOrder(start, limit - start,
srcText, srcStart, srcLimit - srcStart); }
inline int8_t
UnicodeString::doCompareCodePointOrder(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{
const UChar *srcChars;
if(!srcText.isBogus()) {
srcText.pinIndices(srcStart, srcLength);
srcChars=srcText.getArrayStart();
} else {
srcChars=0;
}
return doCompareCodePointOrder(start, length, srcChars, srcStart, srcLength);
}
inline int8_t
UnicodeString::caseCompare(const UnicodeString &text, uint32_t options) const {
return doCaseCompare(0, fLength, text, 0, text.fLength, options);
}
inline int8_t
UnicodeString::caseCompare(UTextOffset start,
int32_t length,
const UnicodeString &srcText,
uint32_t options) const {
return doCaseCompare(start, length, srcText, 0, srcText.fLength, options);
}
inline int8_t
UnicodeString::caseCompare(const UChar *srcChars,
int32_t srcLength,
uint32_t options) const {
return doCaseCompare(0, fLength, srcChars, 0, srcLength, options);
}
inline int8_t
UnicodeString::caseCompare(UTextOffset start,
int32_t length,
const UnicodeString &srcText,
UTextOffset srcStart,
int32_t srcLength,
uint32_t options) const {
return doCaseCompare(start, length, srcText, srcStart, srcLength, options);
}
inline int8_t
UnicodeString::caseCompare(UTextOffset start,
int32_t length,
const UChar *srcChars,
uint32_t options) const {
return doCaseCompare(start, length, srcChars, 0, length, options);
}
inline int8_t
UnicodeString::caseCompare(UTextOffset start,
int32_t length,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength,
uint32_t options) const {
return doCaseCompare(start, length, srcChars, srcStart, srcLength, options);
}
inline int8_t
UnicodeString::caseCompareBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString &srcText,
UTextOffset srcStart,
UTextOffset srcLimit,
uint32_t options) const {
return doCaseCompare(start, limit - start, srcText, srcStart, srcLimit - srcStart, options);
}
inline int8_t
UnicodeString::doCaseCompare(UTextOffset start,
int32_t length,
const UnicodeString &srcText,
UTextOffset srcStart,
int32_t srcLength,
uint32_t options) const
{
const UChar *srcChars;
if(!srcText.isBogus()) {
srcText.pinIndices(srcStart, srcLength);
srcChars=srcText.getArrayStart();
} else {
srcChars=0;
}
return doCaseCompare(start, length, srcChars, srcStart, srcLength, options);
}
inline UTextOffset
UnicodeString::indexOf(const UnicodeString& text) const
{ return indexOf(text, 0, text.fLength, 0, fLength); }
inline UTextOffset
UnicodeString::indexOf(const UnicodeString& text,
UTextOffset start) const
{ return indexOf(text, 0, text.fLength, start, fLength - start); }
inline UTextOffset
UnicodeString::indexOf(const UnicodeString& text,
UTextOffset start,
int32_t length) const
{ return indexOf(text, 0, text.fLength, start, length); }
inline UTextOffset
UnicodeString::indexOf(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength,
UTextOffset start,
int32_t length) const
{
if(!srcText.isBogus()) {
srcText.pinIndices(srcStart, srcLength);
if(srcLength > 0) {
return indexOf(srcText.getArrayStart(), srcStart, srcLength, start, length);
}
}
return -1;
}
inline UTextOffset
UnicodeString::indexOf(const UChar *srcChars,
int32_t srcLength,
UTextOffset start) const
{ return indexOf(srcChars, 0, srcLength, start, fLength - start); }
inline UTextOffset
UnicodeString::indexOf(const UChar *srcChars,
int32_t srcLength,
UTextOffset start,
int32_t length) const
{ return indexOf(srcChars, 0, srcLength, start, length); }
inline UTextOffset
UnicodeString::indexOf(UChar c) const
{ return doIndexOf(c, 0, fLength); }
inline UTextOffset
UnicodeString::indexOf(UChar32 c) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doIndexOf((UChar)c, 0, fLength);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t length = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, length, c);
return indexOf(buffer, length, 0);
}
}
inline UTextOffset
UnicodeString::indexOf(UChar c,
UTextOffset start) const
{ return doIndexOf(c, start, fLength - start); }
inline UTextOffset
UnicodeString::indexOf(UChar32 c,
UTextOffset start) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doIndexOf((UChar)c, start, fLength - start);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t length = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, length, c);
return indexOf(buffer, length, start);
}
}
inline UTextOffset
UnicodeString::indexOf(UChar c,
UTextOffset start,
int32_t length) const
{ return doIndexOf(c, start, length); }
inline UTextOffset
UnicodeString::indexOf(UChar32 c,
UTextOffset start,
int32_t length) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doIndexOf((UChar)c, start, length);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t cLength = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, cLength, c);
return indexOf(buffer, cLength, start, length);
}
}
inline UTextOffset
UnicodeString::lastIndexOf(const UnicodeString& text) const
{ return lastIndexOf(text, 0, text.fLength, 0, fLength); }
inline UTextOffset
UnicodeString::lastIndexOf(const UnicodeString& text,
UTextOffset start) const
{ return lastIndexOf(text, 0, text.fLength, start, fLength - start); }
inline UTextOffset
UnicodeString::lastIndexOf(const UnicodeString& text,
UTextOffset start,
int32_t length) const
{ return lastIndexOf(text, 0, text.fLength, start, length); }
inline UTextOffset
UnicodeString::lastIndexOf(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength,
UTextOffset start,
int32_t length) const
{
if(!srcText.isBogus()) {
srcText.pinIndices(srcStart, srcLength);
if(srcLength > 0) {
return lastIndexOf(srcText.getArrayStart(), srcStart, srcLength, start, length);
}
}
return -1;
}
inline UTextOffset
UnicodeString::lastIndexOf(const UChar *srcChars,
int32_t srcLength,
UTextOffset start) const
{ return lastIndexOf(srcChars, 0, srcLength, start, fLength - start); }
inline UTextOffset
UnicodeString::lastIndexOf(const UChar *srcChars,
int32_t srcLength,
UTextOffset start,
int32_t length) const
{ return lastIndexOf(srcChars, 0, srcLength, start, length); }
inline UTextOffset
UnicodeString::lastIndexOf(UChar c) const
{ return doLastIndexOf(c, 0, fLength); }
inline UTextOffset
UnicodeString::lastIndexOf(UChar32 c) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doLastIndexOf((UChar)c, 0, fLength);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t count = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, count, c);
return lastIndexOf(buffer, count, 0);
}
}
inline UTextOffset
UnicodeString::lastIndexOf(UChar c,
UTextOffset start) const
{ return doLastIndexOf(c, start, fLength - start); }
inline UTextOffset
UnicodeString::lastIndexOf(UChar32 c,
UTextOffset start) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doLastIndexOf((UChar)c, start, fLength - start);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t count = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, count, c);
return lastIndexOf(buffer, count, start);
}
}
inline UTextOffset
UnicodeString::lastIndexOf(UChar c,
UTextOffset start,
int32_t length) const
{ return doLastIndexOf(c, start, length); }
inline UTextOffset
UnicodeString::lastIndexOf(UChar32 c,
UTextOffset start,
int32_t length) const {
if(!UTF_NEED_MULTIPLE_UCHAR(c)) {
return doLastIndexOf((UChar)c, start, length);
} else {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t count = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, count, c);
return lastIndexOf(buffer, count, start, length);
}
}
inline UBool
UnicodeString::startsWith(const UnicodeString& text) const
{ return compare(0, text.fLength, text, 0, text.fLength) == 0; }
inline UBool
UnicodeString::startsWith(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(0, srcLength, srcText, srcStart, srcLength) == 0; }
inline UBool
UnicodeString::startsWith(const UChar *srcChars,
int32_t srcLength) const
{ return doCompare(0, srcLength, srcChars, 0, srcLength) == 0; }
inline UBool
UnicodeString::startsWith(const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;}
inline UBool
UnicodeString::endsWith(const UnicodeString& text) const
{ return doCompare(fLength - text.fLength, text.fLength,
text, 0, text.fLength) == 0; }
inline UBool
UnicodeString::endsWith(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(fLength - srcLength, srcLength,
srcText, srcStart, srcLength) == 0; }
inline UBool
UnicodeString::endsWith(const UChar *srcChars,
int32_t srcLength) const
{ return doCompare(fLength - srcLength, srcLength,
srcChars, 0, srcLength) == 0; }
inline UBool
UnicodeString::endsWith(const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength) const
{ return doCompare(fLength - srcLength, srcLength,
srcChars, srcStart, srcLength) == 0;}
//========================================
// replace
//========================================
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
const UnicodeString& srcText)
{ return doReplace(start, length, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(start, length, srcText, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
const UChar *srcChars,
int32_t srcLength)
{ return doReplace(start, length, srcChars, 0, srcLength); }
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(start, length, srcChars, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
UChar srcChar)
{ return doReplace(start, length, &srcChar, 0, 1); }
inline UnicodeString&
UnicodeString::replace(UTextOffset start,
int32_t length,
UChar32 srcChar) {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t count = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, count, srcChar);
return doReplace(start, length, buffer, 0, count);
}
inline UnicodeString&
UnicodeString::replaceBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText)
{ return doReplace(start, limit - start, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::replaceBetween(UTextOffset start,
UTextOffset limit,
const UnicodeString& srcText,
UTextOffset srcStart,
UTextOffset srcLimit)
{ return doReplace(start, limit - start, srcText, srcStart, srcLimit - srcStart); }
inline UnicodeString&
UnicodeString::findAndReplace(const UnicodeString& oldText,
const UnicodeString& newText)
{ return findAndReplace(0, fLength, oldText, 0, oldText.fLength,
newText, 0, newText.fLength); }
inline UnicodeString&
UnicodeString::findAndReplace(UTextOffset start,
int32_t length,
const UnicodeString& oldText,
const UnicodeString& newText)
{ return findAndReplace(start, length, oldText, 0, oldText.fLength,
newText, 0, newText.fLength); }
// ============================
// extract
// ============================
inline void
UnicodeString::doExtract(UTextOffset start,
int32_t length,
UnicodeString& target) const
{ target.replace(0, target.fLength, *this, start, length); }
inline void
UnicodeString::extract(UTextOffset start,
int32_t length,
UChar *target,
UTextOffset targetStart) const
{ doExtract(start, length, target, targetStart); }
inline void
UnicodeString::extract(UTextOffset start,
int32_t length,
UnicodeString& target) const
{ doExtract(start, length, target); }
inline int32_t
UnicodeString::extract(UTextOffset start,
int32_t length,
char *dst,
const char *codepage) const
// This dstSize value will be checked explicitly
{return extract(start, length, dst, 0xffffffff, codepage);}
inline void
UnicodeString::extractBetween(UTextOffset start,
UTextOffset limit,
UChar *dst,
UTextOffset dstStart) const
{ doExtract(start, limit - start, dst, dstStart); }
inline void
UnicodeString::extractBetween(UTextOffset start,
UTextOffset limit,
UnicodeString& target) const
{ doExtract(start, limit - start, target); }
inline UChar
UnicodeString::doCharAt(UTextOffset offset) const
{
if((uint32_t)offset < (uint32_t)fLength) {
return fArray[offset];
} else {
return kInvalidUChar;
}
}
inline UChar
UnicodeString::charAt(UTextOffset offset) const
{ return doCharAt(offset); }
inline UChar
UnicodeString::operator[] (UTextOffset offset) const
{ return doCharAt(offset); }
inline UChar32
UnicodeString::char32At(UTextOffset offset) const
{
if((uint32_t)offset < (uint32_t)fLength) {
UChar32 c;
UTF_GET_CHAR(fArray, 0, offset, fLength, c);
return c;
} else {
return kInvalidUChar;
}
}
inline UTextOffset
UnicodeString::getCharStart(UTextOffset offset) {
if((uint32_t)offset < (uint32_t)fLength) {
UTF_SET_CHAR_START(fArray, 0, offset);
return offset;
} else {
return 0;
}
}
inline UTextOffset
UnicodeString::getCharLimit(UTextOffset offset) {
if((uint32_t)offset < (uint32_t)fLength) {
UTF_SET_CHAR_LIMIT(fArray, 0, offset, fLength);
return offset;
} else {
return fLength;
}
}
inline UBool
UnicodeString::empty() const
{ return fLength == 0; }
//========================================
// Read-only implementation methods
//========================================
inline int32_t
UnicodeString::length() const
{ return fLength; }
inline int32_t
UnicodeString::hashCode() const
{ return doHashCode(); }
//========================================
// Write alias methods
//========================================
inline UnicodeString&
UnicodeString::operator= (UChar ch)
{ return doReplace(0, fLength, &ch, 0, 1); }
inline UnicodeString&
UnicodeString::operator= (UChar32 ch)
{ return replace(0, fLength, ch); }
inline UnicodeString&
UnicodeString::setTo(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(0, fLength, srcText, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::setTo(const UnicodeString& srcText)
{ return doReplace(0, fLength, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::setTo(const UChar *srcChars,
int32_t srcLength)
{ return doReplace(0, fLength, srcChars, 0, srcLength); }
inline UnicodeString&
UnicodeString::setTo(UChar srcChar)
{ return doReplace(0, fLength, &srcChar, 0, 1); }
inline UnicodeString&
UnicodeString::setTo(UChar32 srcChar)
{ return replace(0, fLength, srcChar); }
inline UnicodeString&
UnicodeString::operator+= (UChar ch)
{ return doReplace(fLength, 0, &ch, 0, 1); }
inline UnicodeString&
UnicodeString::operator+= (UChar32 ch) {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t length = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, length, ch);
return doReplace(fLength, 0, buffer, 0, length);
}
inline UnicodeString&
UnicodeString::operator+= (const UnicodeString& srcText)
{ return doReplace(fLength, 0, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::append(const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(fLength, 0, srcText, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::append(const UnicodeString& srcText)
{ return doReplace(fLength, 0, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::append(const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(fLength, 0, srcChars, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::append(const UChar *srcChars,
int32_t srcLength)
{ return doReplace(fLength, 0, srcChars, 0, srcLength); }
inline UnicodeString&
UnicodeString::append(UChar srcChar)
{ return doReplace(fLength, 0, &srcChar, 0, 1); }
inline UnicodeString&
UnicodeString::append(UChar32 srcChar) {
UChar buffer[UTF_MAX_CHAR_LENGTH];
int32_t length = 0;
UTF_APPEND_CHAR_UNSAFE(buffer, length, srcChar);
return doReplace(fLength, 0, buffer, 0, length);
}
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
const UnicodeString& srcText,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(start, 0, srcText, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
const UnicodeString& srcText)
{ return doReplace(start, 0, srcText, 0, srcText.fLength); }
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
const UChar *srcChars,
UTextOffset srcStart,
int32_t srcLength)
{ return doReplace(start, 0, srcChars, srcStart, srcLength); }
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
const UChar *srcChars,
int32_t srcLength)
{ return doReplace(start, 0, srcChars, 0, srcLength); }
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
UChar srcChar)
{ return doReplace(start, 0, &srcChar, 0, 1); }
inline UnicodeString&
UnicodeString::insert(UTextOffset start,
UChar32 srcChar)
{ return replace(start, 0, srcChar); }
inline UnicodeString&
UnicodeString::remove(UTextOffset start,
int32_t length)
{ return doReplace(start, length, NULL, 0, 0); }
inline UnicodeString&
UnicodeString::remove()
{ return doReplace(0, fLength, 0, 0, 0); }
inline UnicodeString&
UnicodeString::removeBetween(UTextOffset start,
UTextOffset limit)
{ return doReplace(start, limit - start, NULL, 0, 0); }
inline UBool
UnicodeString::truncate(int32_t targetLength)
{
if((uint32_t)targetLength < (uint32_t)fLength) {
fLength = targetLength;
return TRUE;
} else {
return FALSE;
}
}
inline UnicodeString&
UnicodeString::reverse()
{ return doReverse(0, fLength); }
inline UnicodeString&
UnicodeString::reverse(UTextOffset start,
int32_t length)
{ return doReverse(start, length); }
//========================================
// Write implementation methods
//========================================
inline UBool
UnicodeString::isBogus() const
{ return (UBool)(fFlags & kIsBogus); }
//========================================
// Privates
//========================================
inline void
UnicodeString::pinIndices(UTextOffset& start,
int32_t& length) const
{
// pin indices
if(start < 0) {
start = 0;
} else if(start > fLength) {
start = fLength;
}
if(length < 0) {
length = 0;
} else if(length > (fLength - start)) {
length = (fLength - start);
}
}
inline UChar*
UnicodeString::getArrayStart()
{ return fArray; }
inline const UChar*
UnicodeString::getArrayStart() const
{ return fArray; }
inline int32_t
UnicodeString::getCapacity() const
{ return fCapacity; }
inline void
UnicodeString::releaseArray() {
if((fFlags & kRefCounted) && removeRef() == 0) {
delete [] ((int32_t *)fArray - 1);
}
}
inline int32_t
UnicodeString::addRef()
{ return ++*((int32_t *)fArray - 1); }
inline int32_t
UnicodeString::removeRef()
{ return --*((int32_t *)fArray - 1); }
inline int32_t
UnicodeString::refCount() const
{ return *((int32_t *)fArray - 1); }
inline int32_t
UnicodeString::setRefCount(int32_t count)
{ return (*((int32_t *)fArray - 1) = count); }
#ifdef ICU_UNICODESTRING_USE_DEPRECATES
// deprecated API - remove later
inline int32_t
UnicodeString::size() const
{ return fLength; }
inline UnicodeString&
UnicodeString::findAndReplace(const UnicodeString& oldText,
const UnicodeString& newText,
UTextOffset start,
int32_t length)
{ return findAndReplace(start, length, oldText, newText); }
inline void*
UnicodeString::operator new(size_t size)
{ return ::operator new(size); }
inline void*
UnicodeString::operator new(size_t,
void *location)
{ return location; }
inline void
UnicodeString::operator delete(void *location)
{ ::operator delete(location); }
#endif
//========================================
// Static members
//========================================
//========================================
// class UCharReference
//========================================
class UCharReference
{
public:
UCharReference();
inline UCharReference(UnicodeString *string,
UTextOffset pos);
inline UCharReference(const UCharReference& that);
~UCharReference();
inline UCharReference& operator= (const UCharReference& that);
inline UCharReference& operator= (UChar c);
inline operator UChar();
private:
UnicodeString *fString;
UTextOffset fPos;
};
//========================================
// Inline members
//========================================
inline
UCharReference::UCharReference(UnicodeString *string,
UTextOffset pos)
: fString(string), fPos(pos)
{}
inline
UCharReference::UCharReference(const UCharReference& that)
{ this->operator=(that); }
inline
UCharReference::~UCharReference()
{}
inline UCharReference&
UCharReference::operator= (const UCharReference& that)
{ fString->setCharAt(fPos, that.fString->charAt(that.fPos)); return *this; }
inline UCharReference&
UCharReference::operator= (UChar c)
{ fString->setCharAt(fPos, c); return *this; }
inline
UCharReference::operator UChar()
{ return fString->charAt(fPos); }
#endif