]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/tblcoll.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / i18n / tblcoll.cpp
index 14f7ad1776f688aeee786cc17de8a9050f433ea5..779cf4d1c9710554feb0652c50c0f3df8fecc6f0 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ******************************************************************************
-* Copyright (C) 1996-2004, International Business Machines Corporation and   *
+* Copyright (C) 1996-2006, International Business Machines Corporation and   *
 * others. All Rights Reserved.                                               *
 ******************************************************************************
 */
@@ -80,9 +80,9 @@ RuleBasedCollator::RuleBasedCollator(const RuleBasedCollator& that)
 : Collator(that)
 , dataIsOwned(FALSE)
 , isWriteThroughAlias(FALSE)
-, ucollator(that.ucollator)
-, urulestring(that.urulestring)
+, ucollator(NULL)
 {
+    RuleBasedCollator::operator=(that);
 }
 
 RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
@@ -126,28 +126,27 @@ 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.
     }
 }
 
@@ -158,7 +157,6 @@ RuleBasedCollator::construct(const UnicodeString& rules,
                              UColAttributeValue decompositionMode,
                              UErrorCode& status)
 {
-    urulestring = 0;
     ucollator = ucol_openRules(rules.getBuffer(), rules.length(),
         decompositionMode, collationStrength,
         NULL, &status);
@@ -166,7 +164,14 @@ RuleBasedCollator::construct(const UnicodeString& rules,
     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 ----------------------------------- */
@@ -176,10 +181,8 @@ RuleBasedCollator::~RuleBasedCollator()
     if (dataIsOwned)
     {
         ucol_close(ucollator);
-        delete urulestring;
     }
     ucollator = 0;
-    urulestring = 0;
 }
 
 /* RuleBaseCollator public methods --------------------------------------- */
@@ -221,14 +224,19 @@ RuleBasedCollator& RuleBasedCollator::operator=(const RuleBasedCollator& that)
         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;
 }
@@ -236,7 +244,7 @@ RuleBasedCollator& RuleBasedCollator::operator=(const RuleBasedCollator& that)
 // aliasing, not write-through
 Collator* RuleBasedCollator::clone() const
 {
-  return new RuleBasedCollator(*this);
+    return new RuleBasedCollator(*this);
 }
 
 CollationElementIterator* RuleBasedCollator::createCollationElementIterator
@@ -282,7 +290,7 @@ CollationElementIterator* RuleBasedCollator::createCollationElementIterator
 */
 const UnicodeString& RuleBasedCollator::getRules() const
 {
-    return (*urulestring);
+    return urulestring;
 }
 
 void RuleBasedCollator::getRules(UColRuleOption delta, UnicodeString &buffer)
@@ -472,6 +480,12 @@ uint8_t* RuleBasedCollator::cloneRuleData(int32_t &length,
     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)
@@ -519,10 +533,11 @@ Collator* RuleBasedCollator::safeClone(void)
         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;
 }
@@ -603,21 +618,13 @@ RuleBasedCollator::setLocales(const Locale& requestedLocale, const Locale& valid
 // 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;
@@ -659,22 +666,7 @@ RuleBasedCollator::RuleBasedCollator(const Locale& desiredLocale,
 
     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();
     }
 }
 
@@ -697,7 +689,7 @@ RuleBasedCollator::checkOwned() {
     if (!(dataIsOwned || isWriteThroughAlias)) {
         UErrorCode status = U_ZERO_ERROR;
         ucollator = ucol_safeClone(ucollator, NULL, NULL, &status);
-        setRuleStringFromCollator(status);
+        setRuleStringFromCollator();
         dataIsOwned = TRUE;
         isWriteThroughAlias = FALSE;
     }