]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/nfrule.cpp
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / i18n / nfrule.cpp
index c3561205b765c3746e42a182fb2dd1bfb4fed3fc..c2d6526413004a779a3d729cae7b231f5c1a0cd3 100644 (file)
@@ -1,6 +1,6 @@
 /*
 ******************************************************************************
-*   Copyright (C) 1997-2011, International Business Machines
+*   Copyright (C) 1997-2014, International Business Machines
 *   Corporation and others.  All Rights Reserved.
 ******************************************************************************
 *   file name:  nfrule.cpp
@@ -17,6 +17,7 @@
 
 #if U_HAVE_RBNF
 
+#include "unicode/localpointer.h"
 #include "unicode/rbnf.h"
 #include "unicode/tblcoll.h"
 #include "unicode/coleitr.h"
@@ -1148,16 +1149,17 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErr
         // isn't a RuleBasedCollator, because RuleBasedCollator defines
         // the CollationElementIterator protocol.  Hopefully, this
         // will change someday.)
-        RuleBasedCollator* collator = (RuleBasedCollator*)formatter->getCollator();
-        CollationElementIterator* strIter = collator->createCollationElementIterator(str);
-        CollationElementIterator* prefixIter = collator->createCollationElementIterator(prefix);
+        const RuleBasedCollator* collator = formatter->getCollator();
+        if (collator == NULL) {
+            status = U_MEMORY_ALLOCATION_ERROR;
+            return 0;
+        }
+        LocalPointer<CollationElementIterator> strIter(collator->createCollationElementIterator(str));
+        LocalPointer<CollationElementIterator> prefixIter(collator->createCollationElementIterator(prefix));
         // Check for memory allocation error.
-        if (collator == NULL || strIter == NULL || prefixIter == NULL) {
-               delete collator;
-               delete strIter;
-               delete prefixIter;
-               status = U_MEMORY_ALLOCATION_ERROR;
-               return 0;
+        if (strIter.isNull() || prefixIter.isNull()) {
+            status = U_MEMORY_ALLOCATION_ERROR;
+            return 0;
         }
 
         UErrorCode err = U_ZERO_ERROR;
@@ -1209,8 +1211,6 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErr
             // if skipping over ignorables brought us to the end
             // of the target string, we didn't match and return 0
             if (oStr == CollationElementIterator::NULLORDER) {
-                delete prefixIter;
-                delete strIter;
                 return 0;
             }
 
@@ -1219,8 +1219,6 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErr
             // get a mismatch, dump out and return 0
             if (CollationElementIterator::primaryOrder(oStr)
                 != CollationElementIterator::primaryOrder(oPrefix)) {
-                delete prefixIter;
-                delete strIter;
                 return 0;
 
                 // otherwise, advance to the next character in each string
@@ -1240,9 +1238,6 @@ NFRule::prefixLength(const UnicodeString& str, const UnicodeString& prefix, UErr
 #ifdef RBNF_DEBUG
         fprintf(stderr, "prefix length: %d\n", result);
 #endif
-        delete prefixIter;
-        delete strIter;
-
         return result;
 #if 0
         //----------------------------------------------------------------
@@ -1450,15 +1445,17 @@ NFRule::allIgnorable(const UnicodeString& str, UErrorCode& status) const
     // a collation element iterator and make sure each collation
     // element is 0 (ignorable) at the primary level
     if (formatter->isLenient()) {
-        RuleBasedCollator* collator = (RuleBasedCollator*)(formatter->getCollator());
-        CollationElementIterator* iter = collator->createCollationElementIterator(str);
-        
+        const RuleBasedCollator* collator = formatter->getCollator();
+        if (collator == NULL) {
+            status = U_MEMORY_ALLOCATION_ERROR;
+            return FALSE;
+        }
+        LocalPointer<CollationElementIterator> iter(collator->createCollationElementIterator(str));
+
         // Memory allocation error check.
-        if (collator == NULL || iter == NULL) {
-               delete collator;
-               delete iter;
-               status = U_MEMORY_ALLOCATION_ERROR;
-               return FALSE;
+        if (iter.isNull()) {
+            status = U_MEMORY_ALLOCATION_ERROR;
+            return FALSE;
         }
 
         UErrorCode err = U_ZERO_ERROR;
@@ -1468,7 +1465,6 @@ NFRule::allIgnorable(const UnicodeString& str, UErrorCode& status) const
             o = iter->next(err);
         }
 
-        delete iter;
         return o == CollationElementIterator::NULLORDER;
     }
 #endif
@@ -1482,5 +1478,3 @@ U_NAMESPACE_END
 
 /* U_HAVE_RBNF */
 #endif
-
-