]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/alphaindex.cpp
ICU-66108.tar.gz
[apple/icu.git] / icuSources / i18n / alphaindex.cpp
index 19fc92fdc8c9259d2af6e61e93bd95681c8a7c38..9c312bd8e674e54cf6e3e6b8893ab5594fab30fe 100644 (file)
@@ -1,3 +1,5 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
 *******************************************************************************
 * Copyright (C) 2009-2014, International Business Machines Corporation and
 /*
 *******************************************************************************
 * Copyright (C) 2009-2014, International Business Machines Corporation and
@@ -30,8 +32,6 @@
 //#include <string>
 //#include <iostream>
 
 //#include <string>
 //#include <iostream>
 
-#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
-
 U_NAMESPACE_BEGIN
 
 namespace {
 U_NAMESPACE_BEGIN
 
 namespace {
@@ -260,8 +260,7 @@ AlphabeticIndex::ImmutableIndex *AlphabeticIndex::buildImmutableIndex(UErrorCode
     // but that would be worth it only if this method is called multiple times,
     // or called after using the old-style bucket iterator API.
     LocalPointer<BucketList> immutableBucketList(createBucketList(errorCode));
     // but that would be worth it only if this method is called multiple times,
     // or called after using the old-style bucket iterator API.
     LocalPointer<BucketList> immutableBucketList(createBucketList(errorCode));
-    LocalPointer<RuleBasedCollator> coll(
-        static_cast<RuleBasedCollator *>(collatorPrimaryOnly_->clone()));
+    LocalPointer<RuleBasedCollator> coll(collatorPrimaryOnly_->clone());
     if (immutableBucketList.isNull() || coll.isNull()) {
         errorCode = U_MEMORY_ALLOCATION_ERROR;
         return NULL;
     if (immutableBucketList.isNull() || coll.isNull()) {
         errorCode = U_MEMORY_ALLOCATION_ERROR;
         return NULL;
@@ -351,7 +350,7 @@ void AlphabeticIndex::initLabels(UVector &indexCharacters, UErrorCode &errorCode
     }
     if (U_FAILURE(errorCode)) { return; }
 
     }
     if (U_FAILURE(errorCode)) { return; }
 
-    // if the result is still too large, cut down to maxCount elements, by removing every nth element
+    // if the result is still too large, cut down to maxLabelCount_ elements, by removing every nth element
 
     int32_t size = indexCharacters.size() - 1;
     if (size > maxLabelCount_) {
 
     int32_t size = indexCharacters.size() - 1;
     if (size > maxLabelCount_) {
@@ -444,9 +443,8 @@ BucketList *AlphabeticIndex::createBucketList(UErrorCode &errorCode) const {
     };
     UBool hasPinyin = FALSE;
 
     };
     UBool hasPinyin = FALSE;
 
-    LocalPointer<UVector> bucketList(new UVector(errorCode));
-    if (bucketList.isNull()) {
-        errorCode = U_MEMORY_ALLOCATION_ERROR;
+    LocalPointer<UVector> bucketList(new UVector(errorCode), errorCode);
+    if (U_FAILURE(errorCode)) {
         return NULL;
     }
     bucketList->setDeleter(uprv_deleteUObject);
         return NULL;
     }
     bucketList->setDeleter(uprv_deleteUObject);
@@ -512,8 +510,8 @@ BucketList *AlphabeticIndex::createBucketList(UErrorCode &errorCode) const {
                                           ces, errorCode) &&
                 current.charAt(current.length() - 1) != 0xFFFF /* !current.endsWith("\uffff") */) {
             // "AE-ligature" or "Sch" etc.
                                           ces, errorCode) &&
                 current.charAt(current.length() - 1) != 0xFFFF /* !current.endsWith("\uffff") */) {
             // "AE-ligature" or "Sch" etc.
-            for (int32_t i = bucketList->size() - 2;; --i) {
-                Bucket *singleBucket = getBucket(*bucketList, i);
+            for (int32_t j = bucketList->size() - 2;; --j) {
+                Bucket *singleBucket = getBucket(*bucketList, j);
                 if (singleBucket->labelType_ != U_ALPHAINDEX_NORMAL) {
                     // There is no single-character bucket since the last
                     // underflow or inflow label.
                 if (singleBucket->labelType_ != U_ALPHAINDEX_NORMAL) {
                     // There is no single-character bucket since the last
                     // underflow or inflow label.
@@ -603,15 +601,14 @@ BucketList *AlphabeticIndex::createBucketList(UErrorCode &errorCode) const {
         nextBucket = bucket;
     }
 
         nextBucket = bucket;
     }
 
-    LocalPointer<UVector> publicBucketList(new UVector(errorCode));
-    if (bucketList.isNull()) {
-        errorCode = U_MEMORY_ALLOCATION_ERROR;
+    LocalPointer<UVector> publicBucketList(new UVector(errorCode), errorCode);
+    if (U_FAILURE(errorCode)) {
         return NULL;
     }
     // Do not call publicBucketList->setDeleter():
     // This vector shares its objects with the bucketList.
         return NULL;
     }
     // Do not call publicBucketList->setDeleter():
     // This vector shares its objects with the bucketList.
-    for (int32_t i = 0; i < bucketList->size(); ++i) {
-        bucket = getBucket(*bucketList, i);
+    for (int32_t j = 0; j < bucketList->size(); ++j) {
+        bucket = getBucket(*bucketList, j);
         if (bucket->displayBucket_ == NULL) {
             publicBucketList->addElement(bucket, errorCode);
         }
         if (bucket->displayBucket_ == NULL) {
             publicBucketList->addElement(bucket, errorCode);
         }
@@ -727,7 +724,7 @@ void AlphabeticIndex::addIndexExemplars(const Locale &locale, UErrorCode &status
     }
 
     // question: should we add auxiliary exemplars?
     }
 
     // question: should we add auxiliary exemplars?
-    if (exemplars.containsSome(0x61, 0x7A) /* a-z */ || exemplars.size() == 0) {
+    if (exemplars.containsSome(0x61, 0x7A) /* a-z */ || exemplars.isEmpty()) {
         exemplars.add(0x61, 0x7A);
     }
     if (exemplars.containsSome(0xAC00, 0xD7A3)) {  // Hangul syllables
         exemplars.add(0x61, 0x7A);
     }
     if (exemplars.containsSome(0xAC00, 0xD7A3)) {  // Hangul syllables
@@ -742,14 +739,9 @@ void AlphabeticIndex::addIndexExemplars(const Locale &locale, UErrorCode &status
         // cut down to small list
         // make use of the fact that Ethiopic is allocated in 8's, where
         // the base is 0 mod 8.
         // cut down to small list
         // make use of the fact that Ethiopic is allocated in 8's, where
         // the base is 0 mod 8.
-        UnicodeSet ethiopic(
-            UNICODE_STRING_SIMPLE("[[:Block=Ethiopic:]&[:Script=Ethiopic:]]"), status);
-        UnicodeSetIterator it(ethiopic);
-        while (it.next() && !it.isString()) {
-            if ((it.getCodepoint() & 0x7) != 0) {
-                exemplars.remove(it.getCodepoint());
-            }
-        }
+        UnicodeSet ethiopic(UnicodeString(u"[ሀለሐመሠረሰሸቀቈቐቘበቨተቸኀኈነኘአከኰኸዀወዐዘዠየደዸጀገጐጘጠጨጰጸፀፈፐፘ]"), status);
+        ethiopic.retainAll(exemplars);
+        exemplars.remove(u'ሀ', 0x137F).addAll(ethiopic);
     }
 
     // Upper-case any that aren't already so.
     }
 
     // Upper-case any that aren't already so.
@@ -914,7 +906,7 @@ void AlphabeticIndex::init(const Locale *locale, UErrorCode &status) {
             return;
         }
     }
             return;
         }
     }
-    collatorPrimaryOnly_ = static_cast<RuleBasedCollator *>(collator_->clone());
+    collatorPrimaryOnly_ = collator_->clone();
     if (collatorPrimaryOnly_ == NULL) {
         status = U_MEMORY_ALLOCATION_ERROR;
         return;
     if (collatorPrimaryOnly_ == NULL) {
         status = U_MEMORY_ALLOCATION_ERROR;
         return;
@@ -965,7 +957,7 @@ collatorComparator(const void *context, const void *left, const void *right) {
     }
     if (leftString == NULL) {
         return 1;
     }
     if (leftString == NULL) {
         return 1;
-    };
+    }
     if (rightString == NULL) {
         return -1;
     }
     if (rightString == NULL) {
         return -1;
     }
@@ -992,9 +984,8 @@ UVector *AlphabeticIndex::firstStringsInScript(UErrorCode &status) {
     if (U_FAILURE(status)) {
         return NULL;
     }
     if (U_FAILURE(status)) {
         return NULL;
     }
-    LocalPointer<UVector> dest(new UVector(status));
-    if (dest.isNull()) {
-        status = U_MEMORY_ALLOCATION_ERROR;
+    LocalPointer<UVector> dest(new UVector(status), status);
+    if (U_FAILURE(status)) {
         return NULL;
     }
     dest->setDeleter(uprv_deleteUObject);
         return NULL;
     }
     dest->setDeleter(uprv_deleteUObject);