// aliasing, not write-through
RuleBasedCollator& RuleBasedCollator::operator=(const RuleBasedCollator& that)
{
- if (this != &that)
- {
- if (dataIsOwned)
- {
- ucol_close(ucollator);
- }
+ if (this == &that) { return *this; }
- urulestring.truncate(0); // empty the rule string
- dataIsOwned = TRUE;
- isWriteThroughAlias = FALSE;
+ UErrorCode intStatus = U_ZERO_ERROR;
+ int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
+ UCollator *ucol = ucol_safeClone(that.ucollator, NULL, &buffersize, &intStatus);
+ if (U_FAILURE(intStatus)) { return *this; }
- UErrorCode intStatus = U_ZERO_ERROR;
- int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
- ucollator = ucol_safeClone(that.ucollator, NULL, &buffersize,
- &intStatus);
- if (U_SUCCESS(intStatus)) {
- setRuleStringFromCollator();
- }
+ if (dataIsOwned) {
+ ucol_close(ucollator);
}
+ ucollator = ucol;
+ dataIsOwned = TRUE;
+ isWriteThroughAlias = FALSE;
+ setRuleStringFromCollator();
return *this;
}
// aliasing, not write-through
Collator* RuleBasedCollator::clone() const
{
- return new RuleBasedCollator(*this);
+ RuleBasedCollator* coll = new RuleBasedCollator(*this);
+ // There is a small chance that the internal ucol_safeClone() call fails.
+ if (coll != NULL && coll->ucollator == NULL) {
+ delete coll;
+ return NULL;
+ }
+ return coll;
}
return ucol_getVariableTop(ucollator, &status);
}
-Collator* RuleBasedCollator::safeClone(void) const
-{
- UErrorCode intStatus = U_ZERO_ERROR;
- int32_t buffersize = U_COL_SAFECLONE_BUFFERSIZE;
- UCollator *ucol = ucol_safeClone(ucollator, NULL, &buffersize,
- &intStatus);
- if (U_FAILURE(intStatus)) {
- return NULL;
- }
-
- RuleBasedCollator *result = new RuleBasedCollator();
- // Null pointer check
- if (result != NULL) {
- result->ucollator = ucol;
- result->dataIsOwned = TRUE;
- result->isWriteThroughAlias = FALSE;
- result->setRuleStringFromCollator();
- }
-
- return result;
-}
-
-
int32_t RuleBasedCollator::getSortKey(const UnicodeString& source,
uint8_t *result, int32_t resultLength)
const
virtual UBool operator!=(const Collator& other) const;
/**
- * Makes a shallow copy of the current object.
- * @return a copy of this object
+ * Makes a copy of this object.
+ * @return a copy of this object, owned by the caller
* @stable ICU 2.0
*/
virtual Collator* clone(void) const = 0;
*/
virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
-
/**
- * Thread safe cloning operation
- * @return pointer to the new clone, user should remove it.
- * @stable ICU 2.2
+ * Same as clone().
+ * The base class implementation simply calls clone().
+ * @return a copy of this object, owned by the caller
+ * @see clone()
+ * @deprecated ICU 50 no need to have two methods for cloning
*/
- virtual Collator* safeClone(void) const = 0;
+ virtual Collator* safeClone(void) const;
/**
* Get the sort key as an array of bytes from an UnicodeString.
virtual UBool operator==(const Collator& other) const;
/**
- * Makes a deep copy of the object.
- * The caller owns the returned object.
- * @return the cloned object.
+ * Makes a copy of this object.
+ * @return a copy of this object, owned by the caller
* @stable ICU 2.0
*/
virtual Collator* clone(void) const;
*/
virtual UnicodeSet *getTailoredSet(UErrorCode &status) const;
- /**
- * Thread safe cloning operation.
- * @return pointer to the new clone, user should remove it.
- * @stable ICU 2.2
- */
- virtual Collator* safeClone(void) const;
-
/**
* Get the sort key as an array of bytes from an UnicodeString.
* @param source string to be processed.