/*
******************************************************************************
-* Copyright (C) 1996-2004, International Business Machines Corporation and *
+* Copyright (C) 1996-2006, International Business Machines Corporation and *
* others. All Rights Reserved. *
******************************************************************************
*/
: Collator(that)
, dataIsOwned(FALSE)
, isWriteThroughAlias(FALSE)
-, ucollator(that.ucollator)
-, urulestring(that.urulestring)
+, ucollator(NULL)
{
+ RuleBasedCollator::operator=(that);
}
RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
decompositionMode,
status);
}
+RuleBasedCollator::RuleBasedCollator(const uint8_t *bin, int32_t length,
+ const RuleBasedCollator *base,
+ UErrorCode &status) :
+dataIsOwned(TRUE),
+isWriteThroughAlias(FALSE)
+{
+ ucollator = ucol_openBinary(bin, length, base->ucollator, &status);
+}
void
-RuleBasedCollator::setRuleStringFromCollator(UErrorCode& status)
+RuleBasedCollator::setRuleStringFromCollator()
{
- urulestring = NULL;
- if (U_SUCCESS(status))
- {
- int32_t length;
- const UChar *r = ucol_getRules(ucollator, &length);
+ int32_t length;
+ const UChar *r = ucol_getRules(ucollator, &length);
- if (length > 0) {
- // alias the rules string
- urulestring = new UnicodeString(TRUE, r, length);
- }
- else {
- urulestring = new UnicodeString();
- }
- /* test for NULL */
- if (urulestring == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
+ if (r && length > 0) {
+ // alias the rules string
+ urulestring.setTo(TRUE, r, length);
+ }
+ else {
+ urulestring.truncate(0); // Clear string.
}
}
UColAttributeValue decompositionMode,
UErrorCode& status)
{
- urulestring = 0;
ucollator = ucol_openRules(rules.getBuffer(), rules.length(),
decompositionMode, collationStrength,
NULL, &status);
dataIsOwned = TRUE; // since we own a collator now, we need to get rid of it
isWriteThroughAlias = FALSE;
- setRuleStringFromCollator(status);
+ if(ucollator == NULL) {
+ if(U_SUCCESS(status)) {
+ status = U_MEMORY_ALLOCATION_ERROR;
+ }
+ return; // Failure
+ }
+
+ setRuleStringFromCollator();
}
/* RuleBasedCollator public destructor ----------------------------------- */
if (dataIsOwned)
{
ucol_close(ucollator);
- delete urulestring;
}
ucollator = 0;
- urulestring = 0;
}
/* RuleBaseCollator public methods --------------------------------------- */
if (dataIsOwned)
{
ucol_close(ucollator);
- ucollator = NULL;
- delete urulestring;
}
- dataIsOwned = FALSE;
+ urulestring.truncate(0); // empty the rule string
+ dataIsOwned = TRUE;
isWriteThroughAlias = FALSE;
- ucollator = that.ucollator;
- urulestring = that.urulestring;
+
+ 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();
+ }
}
return *this;
}
// aliasing, not write-through
Collator* RuleBasedCollator::clone() const
{
- return new RuleBasedCollator(*this);
+ return new RuleBasedCollator(*this);
}
CollationElementIterator* RuleBasedCollator::createCollationElementIterator
*/
const UnicodeString& RuleBasedCollator::getRules() const
{
- return (*urulestring);
+ return urulestring;
}
void RuleBasedCollator::getRules(UColRuleOption delta, UnicodeString &buffer)
return ucol_cloneRuleData(ucollator, &length, &status);
}
+
+int32_t RuleBasedCollator::cloneBinary(uint8_t *buffer, int32_t capacity, UErrorCode &status)
+{
+ return ucol_cloneBinary(ucollator, buffer, capacity, &status);
+}
+
void RuleBasedCollator::setAttribute(UColAttribute attr,
UColAttributeValue value,
UErrorCode &status)
return NULL;
}
- UnicodeString *r = new UnicodeString(*urulestring);
- RuleBasedCollator *result = new RuleBasedCollator(ucol, r);
+ RuleBasedCollator *result = new RuleBasedCollator();
+ result->ucollator = ucol;
result->dataIsOwned = TRUE;
result->isWriteThroughAlias = FALSE;
+ setRuleStringFromCollator();
return result;
}
// RuleBaseCollatorNew private constructor ----------------------------------
RuleBasedCollator::RuleBasedCollator()
- : dataIsOwned(FALSE), isWriteThroughAlias(FALSE), ucollator(0), urulestring(0)
-{
-}
-
-RuleBasedCollator::RuleBasedCollator(UCollator *collator,
- UnicodeString *rule)
- : dataIsOwned(FALSE), isWriteThroughAlias(FALSE), urulestring(0)
+ : dataIsOwned(FALSE), isWriteThroughAlias(FALSE), ucollator(NULL)
{
- ucollator = collator;
- urulestring = rule;
}
RuleBasedCollator::RuleBasedCollator(const Locale& desiredLocale,
- UErrorCode& status) :
- dataIsOwned(FALSE), ucollator(0), urulestring(0)
+ UErrorCode& status)
+ : dataIsOwned(FALSE), isWriteThroughAlias(FALSE), ucollator(NULL)
{
if (U_FAILURE(status))
return;
if (U_SUCCESS(status))
{
- int32_t length;
- const UChar *r = ucol_getRules(ucollator, &length);
- if (length > 0) {
- // alias the rules string
- urulestring = new UnicodeString(TRUE, r, length);
- }
- else {
- urulestring = new UnicodeString();
- }
- /* test for NULL */
- if (urulestring == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- dataIsOwned = TRUE;
- isWriteThroughAlias = FALSE;
+ setRuleStringFromCollator();
}
}
if (!(dataIsOwned || isWriteThroughAlias)) {
UErrorCode status = U_ZERO_ERROR;
ucollator = ucol_safeClone(ucollator, NULL, NULL, &status);
- setRuleStringFromCollator(status);
+ setRuleStringFromCollator();
dataIsOwned = TRUE;
isWriteThroughAlias = FALSE;
}