diff --git a/icu4c/source/common/unicode/rep.h b/icu4c/source/common/unicode/rep.h index f6d5539fb7..057a3e575c 100644 --- a/icu4c/source/common/unicode/rep.h +++ b/icu4c/source/common/unicode/rep.h @@ -160,39 +160,28 @@ protected: Replaceable(); /** - * Constructor with initial length. - */ - Replaceable(int32_t initialLength); + * Virtual version of length(). + */ + virtual int32_t getLength() const = 0; /** * Virtual version of charAt(). - * This allows UnicodeString::charAt() to be inline again (see jitterbug 709). */ virtual UChar getCharAt(UTextOffset offset) const = 0; /** * Virtual version of char32At(). - * This allows UnicodeString::char32At() to be inline again (see jitterbug 709). */ virtual UChar32 getChar32At(UTextOffset offset) const = 0; - - /** - * This field must always reflect the number of UChars in the text - * object. A subclass must keep this up to date. - * Moved here from UnicodeString so that length() can be inline (see jitterbug 709). - */ - int32_t fLength; // number characters in fArray }; -inline Replaceable::Replaceable() : fLength(0) {} - -inline Replaceable::Replaceable(int32_t initialLength) : fLength(initialLength) {} +inline Replaceable::Replaceable() {} inline Replaceable::~Replaceable() {} inline int32_t Replaceable::length() const { - return fLength; + return getLength(); } inline UChar diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index 08179587d4..6e9c9dde5d 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -2203,6 +2203,11 @@ public: //======================================== protected: + /** + * Implement Replaceable::getLength() (see jitterbug 1027). + */ + virtual int32_t getLength() const; + /** * The change in Replaceable to use virtual getCharAt() allows * UnicodeString::charAt() to be inline again (see jitterbug 709). @@ -2422,8 +2427,7 @@ private: * 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 fLength; // number of characters in fArray int32_t fCapacity; // sizeof fArray UChar *fArray; // the Unicode data uint16_t fFlags; // bit flags: see constants above diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index f2a14d4df9..51a1154c0f 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -94,13 +94,15 @@ UConverter* UnicodeString::fgDefaultConverter = 0; // Constructors //======================================== UnicodeString::UnicodeString() - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) {} UnicodeString::UnicodeString(int32_t capacity, UChar32 c, int32_t count) - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(0), fFlags(0) { @@ -164,7 +166,7 @@ UnicodeString::UnicodeString(int32_t capacity, UChar32 c, int32_t count) } UnicodeString::UnicodeString(UChar ch) - : Replaceable(1), + : fLength(1), fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) @@ -173,7 +175,7 @@ UnicodeString::UnicodeString(UChar ch) } UnicodeString::UnicodeString(UChar32 ch) - : Replaceable(1), + : fLength(1), fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) @@ -184,7 +186,8 @@ UnicodeString::UnicodeString(UChar32 ch) } UnicodeString::UnicodeString(const UChar *text) - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) { @@ -193,7 +196,8 @@ UnicodeString::UnicodeString(const UChar *text) UnicodeString::UnicodeString(const UChar *text, int32_t textLength) - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) { @@ -203,7 +207,7 @@ UnicodeString::UnicodeString(const UChar *text, UnicodeString::UnicodeString(UBool isTerminated, const UChar *text, int32_t textLength) - : Replaceable(textLength), + : fLength(textLength), fCapacity(isTerminated ? textLength + 1 : textLength), fArray((UChar *)text), fFlags(kReadonlyAlias) @@ -220,7 +224,7 @@ UnicodeString::UnicodeString(UBool isTerminated, UnicodeString::UnicodeString(UChar *buff, int32_t bufLength, int32_t buffCapacity) - : Replaceable(bufLength), + : fLength(bufLength), fCapacity(buffCapacity), fArray(buff), fFlags(kWriteableAlias) @@ -232,7 +236,8 @@ UnicodeString::UnicodeString(UChar *buff, UnicodeString::UnicodeString(const char *codepageData, const char *codepage) - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) { @@ -245,7 +250,8 @@ UnicodeString::UnicodeString(const char *codepageData, UnicodeString::UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage) - : fCapacity(US_STACKBUF_SIZE), + : fLength(0), + fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) { @@ -255,7 +261,7 @@ UnicodeString::UnicodeString(const char *codepageData, } UnicodeString::UnicodeString(const UnicodeString& that) - : Replaceable(), + : fLength(0), fCapacity(US_STACKBUF_SIZE), fArray(fStackBuffer), fFlags(kShortString) @@ -618,6 +624,11 @@ UnicodeString::doCaseCompare(UTextOffset start, } } +int32_t +UnicodeString::getLength() const { + return length(); +} + UChar UnicodeString::getCharAt(UTextOffset offset) const { return charAt(offset); diff --git a/icu4c/source/i18n/utrans.cpp b/icu4c/source/i18n/utrans.cpp index 6dbad35f0a..7dd388d474 100644 --- a/icu4c/source/i18n/utrans.cpp +++ b/icu4c/source/i18n/utrans.cpp @@ -52,6 +52,8 @@ public: protected: + virtual int32_t getLength() const; + virtual UChar getCharAt(UTextOffset offset) const; virtual UChar32 getChar32At(UTextOffset offset) const; @@ -60,7 +62,7 @@ protected: ReplaceableGlue::ReplaceableGlue(UReplaceable *replaceable, UReplaceableCallbacks *funcCallback) - : Replaceable((*funcCallback->length)(replaceable)) + : Replaceable() { this->rep = replaceable; this->func = funcCallback; @@ -72,6 +74,10 @@ ReplaceableGlue::~ReplaceableGlue() { delete[] buf; } +int32_t ReplaceableGlue::getLength() const { + return (*func->length)(rep); +} + UChar ReplaceableGlue::getCharAt(UTextOffset offset) const { return (*func->charAt)(rep, offset); } @@ -91,12 +97,10 @@ void ReplaceableGlue::handleReplaceBetween(UTextOffset start, } text.extract(0, len, buf); (*func->replace)(rep, start, limit, buf, len); - fLength = (*func->length)(rep); } void ReplaceableGlue::copy(int32_t start, int32_t limit, int32_t dest) { (*func->copy)(rep, start, limit, dest); - fLength = (*func->length)(rep); } /********************************************************************