]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/tblcoll.cpp
ICU-511.32.tar.gz
[apple/icu.git] / icuSources / i18n / tblcoll.cpp
index 720107ee032a597492567d5a91e16f83644736ec..cbe0394930c723d5c97b026a5dcb74b3c542c160 100644 (file)
@@ -1,7 +1,7 @@
 /*
  ******************************************************************************
- * Copyright (C) 1996-2008, International Business Machines Corporation and   *
- * others. All Rights Reserved.                                               *
+ * Copyright (C) 1996-2012, International Business Machines Corporation and
+ * others. All Rights Reserved.
  ******************************************************************************
  */
 
@@ -68,6 +68,7 @@
 #include "cmemory.h"
 #include "cstring.h"
 #include "putilimp.h"
+#include "ustr_imp.h"
 
 /* public RuleBasedCollator constructor ---------------------------------- */
 
@@ -100,7 +101,7 @@ RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
                                      UErrorCode& status) : dataIsOwned(FALSE)
 {
     construct(rules,
-        getUCollationStrength(collationStrength),
+        (UColAttributeValue)collationStrength,
         UCOL_DEFAULT,
         status);
 }
@@ -122,12 +123,12 @@ RuleBasedCollator::RuleBasedCollator(const UnicodeString& rules,
                                      UErrorCode& status) : dataIsOwned(FALSE)
 {
     construct(rules,
-        getUCollationStrength(collationStrength),
+        (UColAttributeValue)collationStrength,
         decompositionMode,
         status);
 }
-RuleBasedCollator::RuleBasedCollator(const uint8_t *bin, int32_t length, 
-                    const RuleBasedCollator *base, 
+RuleBasedCollator::RuleBasedCollator(const uint8_t *bin, int32_t length,
+                    const RuleBasedCollator *base,
                     UErrorCode &status) :
 dataIsOwned(TRUE),
 isWriteThroughAlias(FALSE)
@@ -190,63 +191,51 @@ RuleBasedCollator::~RuleBasedCollator()
 UBool RuleBasedCollator::operator==(const Collator& that) const
 {
   /* only checks for address equals here */
-  if (Collator::operator==(that))
+  if (this == &that) {
     return TRUE;
-
-  if (getDynamicClassID() != that.getDynamicClassID())
+  }
+  if (!Collator::operator==(that)) {
     return FALSE;  /* not the same class */
+  }
 
   RuleBasedCollator& thatAlias = (RuleBasedCollator&)that;
 
-  // weiv: use C function, commented code below is wrong
   return ucol_equals(this->ucollator, thatAlias.ucollator);
-  /*
-  synwee : orginal code does not check for data compatibility
-  */
-  /*
-  if (ucollator != thatAlias.ucollator)
-    return FALSE;
-
-  return TRUE;
-  */
-}
-
-UBool RuleBasedCollator::operator!=(const Collator& other) const
-{
-    return !(*this == other);
 }
 
 // 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;
 }
 
+
 CollationElementIterator* RuleBasedCollator::createCollationElementIterator
                                            (const UnicodeString& source) const
 {
@@ -303,7 +292,7 @@ void RuleBasedCollator::getRules(UColRuleOption delta, UnicodeString &buffer)
             ucol_getRulesEx(ucollator, delta, rules, rulesize);
             buffer.setTo(rules, rulesize);
             uprv_free(rules);
-        } else { // couldn't allocate 
+        } else { // couldn't allocate
             buffer.remove();
         }
     }
@@ -329,15 +318,9 @@ void RuleBasedCollator::getVersion(UVersionInfo versionInfo) const
     }
 }
 
-Collator::EComparisonResult RuleBasedCollator::compare(
-                                               const UnicodeString& source,
-                                               const UnicodeString& target,
-                                               int32_t length) const
-{
-    UErrorCode status = U_ZERO_ERROR;
-    return getEComparisonResult(compare(source.getBuffer(), uprv_min(length,source.length()), target.getBuffer(), uprv_min(length,target.length()), status));
-}
-
+/**
+* Compare two strings using this collator
+*/
 UCollationResult RuleBasedCollator::compare(
                                                const UnicodeString& source,
                                                const UnicodeString& target,
@@ -347,16 +330,6 @@ UCollationResult RuleBasedCollator::compare(
     return compare(source.getBuffer(), uprv_min(length,source.length()), target.getBuffer(), uprv_min(length,target.length()), status);
 }
 
-Collator::EComparisonResult RuleBasedCollator::compare(const UChar* source,
-                                                       int32_t sourceLength,
-                                                       const UChar* target,
-                                                       int32_t targetLength)
-                                                       const
-{
-    return  getEComparisonResult(ucol_strcoll(ucollator, source, sourceLength,
-                                                         target, targetLength));
-}
-
 UCollationResult RuleBasedCollator::compare(const UChar* source,
                                                        int32_t sourceLength,
                                                        const UChar* target,
@@ -370,17 +343,6 @@ UCollationResult RuleBasedCollator::compare(const UChar* source,
     }
 }
 
-/**
-* Compare two strings using this collator
-*/
-Collator::EComparisonResult RuleBasedCollator::compare(
-                                             const UnicodeString& source,
-                                             const UnicodeString& target) const
-{
-    return getEComparisonResult(ucol_strcoll(ucollator, source.getBuffer(), source.length(),
-                                                        target.getBuffer(), target.length()));
-}
-
 UCollationResult RuleBasedCollator::compare(
                                              const UnicodeString& source,
                                              const UnicodeString& target,
@@ -394,6 +356,16 @@ UCollationResult RuleBasedCollator::compare(
     }
 }
 
+UCollationResult RuleBasedCollator::compare(UCharIterator &sIter,
+                                            UCharIterator &tIter,
+                                            UErrorCode &status) const {
+    if(U_SUCCESS(status)) {
+        return ucol_strcollIter(ucollator, &sIter, &tIter, &status);
+    } else {
+        return UCOL_EQUAL;
+    }
+}
+
 /**
 * Retrieve a collation key for the specified string. The key can be compared
 * with other collation keys using a bitwise comparison (e.g. memcmp) to find
@@ -440,21 +412,28 @@ CollationKey& RuleBasedCollator::getCollationKey(const UChar* source,
                                                     CollationKey& sortkey,
                                                     UErrorCode& status) const
 {
-    if (U_FAILURE(status))
-    {
+    if (U_FAILURE(status)) {
+        return sortkey.setToBogus();
+    }
+    if (sourceLen < -1 || (source == NULL && sourceLen != 0)) {
+        status = U_ILLEGAL_ARGUMENT_ERROR;
         return sortkey.setToBogus();
     }
 
-    if ((!source) || (sourceLen == 0)) {
+    if (sourceLen < 0) {
+        sourceLen = u_strlen(source);
+    }
+    if (sourceLen == 0) {
         return sortkey.reset();
     }
 
-    uint8_t *result;
-    int32_t resultLen = ucol_getSortKeyWithAllocation(ucollator,
-                                                      source, sourceLen,
-                                                      &result,
-                                                      &status);
-    sortkey.adopt(result, resultLen);
+    int32_t resultLen = ucol_getCollationKey(ucollator, source, sourceLen, sortkey, status);
+
+    if (U_SUCCESS(status)) {
+        sortkey.setLength(resultLen);
+    } else {
+        sortkey.setToBogus();
+    }
     return sortkey;
 }
 
@@ -497,7 +476,7 @@ void RuleBasedCollator::setAttribute(UColAttribute attr,
 }
 
 UColAttributeValue RuleBasedCollator::getAttribute(UColAttribute attr,
-                                                      UErrorCode &status)
+                                                      UErrorCode &status) const
 {
     if (U_FAILURE(status))
         return UCOL_DEFAULT;
@@ -509,12 +488,12 @@ uint32_t RuleBasedCollator::setVariableTop(const UChar *varTop, int32_t len, UEr
     return ucol_setVariableTop(ucollator, varTop, len, &status);
 }
 
-uint32_t RuleBasedCollator::setVariableTop(const UnicodeString varTop, UErrorCode &status) {
+uint32_t RuleBasedCollator::setVariableTop(const UnicodeString &varTop, UErrorCode &status) {
     checkOwned();
     return ucol_setVariableTop(ucollator, varTop.getBuffer(), varTop.length(), &status);
 }
 
-void RuleBasedCollator::setVariableTop(const uint32_t varTop, UErrorCode &status) {
+void RuleBasedCollator::setVariableTop(uint32_t varTop, UErrorCode &status) {
     checkOwned();
     ucol_restoreVariableTop(ucollator, varTop, &status);
 }
@@ -523,29 +502,6 @@ uint32_t RuleBasedCollator::getVariableTop(UErrorCode &status) const {
   return ucol_getVariableTop(ucollator, &status);
 }
 
-Collator* RuleBasedCollator::safeClone(void)
-{
-    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;
-           setRuleStringFromCollator();
-    }
-
-    return result;
-}
-
-
 int32_t RuleBasedCollator::getSortKey(const UnicodeString& source,
                                          uint8_t *result, int32_t resultLength)
                                          const
@@ -560,19 +516,27 @@ int32_t RuleBasedCollator::getSortKey(const UChar *source,
     return ucol_getSortKey(ucollator, source, sourceLength, result, resultLength);
 }
 
-Collator::ECollationStrength RuleBasedCollator::getStrength(void) const
+int32_t RuleBasedCollator::getReorderCodes(int32_t *dest,
+                                          int32_t destCapacity,
+                                          UErrorCode& status) const
 {
-    UErrorCode intStatus = U_ZERO_ERROR;
-    return getECollationStrength(ucol_getAttribute(ucollator, UCOL_STRENGTH,
-                                &intStatus));
+    return ucol_getReorderCodes(ucollator, dest, destCapacity, &status);
 }
 
-void RuleBasedCollator::setStrength(ECollationStrength newStrength)
+void RuleBasedCollator::setReorderCodes(const int32_t *reorderCodes,
+                                       int32_t reorderCodesLength,
+                                       UErrorCode& status)
 {
     checkOwned();
-    UErrorCode intStatus = U_ZERO_ERROR;
-    UCollationStrength strength = getUCollationStrength(newStrength);
-    ucol_setAttribute(ucollator, UCOL_STRENGTH, strength, &intStatus);
+    ucol_setReorderCodes(ucollator, reorderCodes, reorderCodesLength, &status);
+}
+
+int32_t RuleBasedCollator::getEquivalentReorderCodes(int32_t reorderCode,
+                                int32_t* dest,
+                                int32_t destCapacity,
+                                UErrorCode& status)
+{
+    return ucol_getEquivalentReorderCodes(reorderCode, dest, destCapacity, &status);
 }
 
 /**
@@ -583,14 +547,14 @@ int32_t RuleBasedCollator::hashCode() const
 {
     int32_t length;
     const UChar *rules = ucol_getRules(ucollator, &length);
-    return uhash_hashUCharsN(rules, length);
+    return ustr_hashUCharsN(rules, length);
 }
 
 /**
 * return the locale of this collator
 */
-const Locale RuleBasedCollator::getLocale(ULocDataLocaleType type, UErrorCode &status) const {
-    const char *result = ucol_getLocale(ucollator, type, &status);
+Locale RuleBasedCollator::getLocale(ULocDataLocaleType type, UErrorCode &status) const {
+    const char *result = ucol_getLocaleByType(ucollator, type, &status);
     if(result == NULL) {
         Locale res("");
         res.setToBogus();
@@ -673,12 +637,13 @@ RuleBasedCollator::RuleBasedCollator(const Locale& desiredLocale,
     }
 }
 
-void 
+void
 RuleBasedCollator::setUCollator(const char *locale,
                                 UErrorCode &status)
 {
-    if (U_FAILURE(status))
+    if (U_FAILURE(status)) {
         return;
+    }
     if (ucollator && dataIsOwned)
         ucol_close(ucollator);
     ucollator = ucol_open_internal(locale, &status);
@@ -698,6 +663,16 @@ RuleBasedCollator::checkOwned() {
     }
 }
 
+
+int32_t RuleBasedCollator::internalGetShortDefinitionString(const char *locale,
+                                                                      char *buffer,
+                                                                      int32_t capacity,
+                                                                      UErrorCode &status) const {
+  /* simply delegate */
+  return ucol_getShortDefinitionString(ucollator, locale, buffer, capacity, &status);
+}
+
+
 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RuleBasedCollator)
 
 U_NAMESPACE_END