]> granicus.if.org Git - icu/commitdiff
ICU-8697 C review fixes: correct UnicodeString constructor, use LocalPointer, use...
authorPeter Edberg <pedberg@unicode.org>
Fri, 24 Feb 2012 20:27:21 +0000 (20:27 +0000)
committerPeter Edberg <pedberg@unicode.org>
Fri, 24 Feb 2012 20:27:21 +0000 (20:27 +0000)
X-SVN-Rev: 31509

icu4c/source/i18n/decimfmt.cpp
icu4c/source/i18n/numfmt.cpp
icu4c/source/i18n/unum.cpp
icu4c/source/test/intltest/numfmtst.cpp

index 1b4b311b5dbb0c225ac048556367fc2671f642a2..2981c25b4e2f133d79ef9ce0f6672c754e7d9e27 100644 (file)
@@ -55,6 +55,7 @@
 #include "unicode/plurrule.h"
 #include "unicode/utf16.h"
 #include "unicode/numsys.h"
+#include "unicode/localpointer.h"
 #include "uresimp.h"
 #include "ucurrimp.h"
 #include "charstr.h"
@@ -1596,23 +1597,21 @@ DecimalFormat::parse(const UnicodeString& text,
 
 CurrencyAmount* DecimalFormat::parseCurrency(const UnicodeString& text,
                                              ParsePosition& pos) const {
-    CurrencyAmount* currAmt = NULL;
+    CurrencyAmount* currAmtToReturn = NULL;
     Formattable parseResult;
     int32_t start = pos.getIndex();
     UChar curbuf[4];
     parse(text, parseResult, pos, curbuf);
-    if (pos.getIndex() != start && pos.getErrorIndex() == -1) {
+    if (pos.getIndex() != start) {
         UErrorCode ec = U_ZERO_ERROR;
-        currAmt = new CurrencyAmount(parseResult, curbuf, ec);
-        if (U_FAILURE(ec) || currAmt == NULL) {
+        LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult, curbuf, ec));
+        if (U_FAILURE(ec)) {
             pos.setIndex(start); // indicate failure
-            if ( currAmt != NULL ) {
-                delete currAmt;
-                currAmt = NULL;
-            }
+        } else {
+            currAmtToReturn = currAmt.orphan();
         }
     }
-    return currAmt;
+    return currAmtToReturn;
 }
 
 /**
index 57ca29df5d01aa877416acfd74ac7858a94d4499..a877238308c88bef1c4ac5bfdc2f8f39f15fbe16 100644 (file)
@@ -35,6 +35,7 @@
 #include "unicode/curramt.h"
 #include "unicode/numsys.h"
 #include "unicode/rbnf.h"
+#include "unicode/localpointer.h"
 #include "charstr.h"
 #include "winnmfmt.h"
 #include "uresimp.h"
@@ -626,26 +627,24 @@ NumberFormat::parse(const UnicodeString& text,
 CurrencyAmount* NumberFormat::parseCurrency(const UnicodeString& text,
                                             ParsePosition& pos) const {
     // Default implementation only -- subclasses should override
-    CurrencyAmount* currAmt = NULL;
+    CurrencyAmount* currAmtToReturn = NULL;
     Formattable parseResult;
     int32_t start = pos.getIndex();
     parse(text, parseResult, pos);
-    if (pos.getIndex() != start && pos.getErrorIndex() == -1) {
+    if (pos.getIndex() != start) {
         UChar curr[4];
         UErrorCode ec = U_ZERO_ERROR;
         getEffectiveCurrency(curr, ec);
         if (U_SUCCESS(ec)) {
-            currAmt = new CurrencyAmount(parseResult, curr, ec);
-            if (U_FAILURE(ec) || currAmt == NULL) {
+            LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult, curr, ec));
+            if (U_FAILURE(ec)) {
                 pos.setIndex(start); // indicate failure
-                if ( currAmt != NULL ) {
-                    delete currAmt;
-                    currAmt = NULL;
-                }
+            } else {
+                currAmtToReturn = currAmt.orphan();
             }
         }
     }
-    return currAmt;
+    return currAmtToReturn;
 }
 
 // -------------------------------------
index a16d3890e976aa4166693b167bebd9525921821f..4fc3f5d4b8c4d138d542639dcb32f756ab3e517d 100644 (file)
@@ -24,6 +24,7 @@
 #include "unicode/fmtable.h"
 #include "unicode/dcfmtsym.h"
 #include "unicode/curramt.h"
+#include "unicode/localpointer.h"
 #include "uassert.h"
 #include "cpputils.h"
 #include "cstring.h"
@@ -323,8 +324,7 @@ parseRes(Formattable& res,
     if(U_FAILURE(*status))
         return;
     
-    int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
-    const UnicodeString src((UChar*)text, len, len);
+    const UnicodeString src((UBool)(textLength == -1), text, textLength);
     ParsePosition pp;
     
     if(parsePos != 0)
@@ -423,30 +423,25 @@ unum_parseDoubleCurrency(const UNumberFormat* fmt,
     if (U_FAILURE(*status)) {
         return doubleVal;
     }
-    int32_t len = (textLength == -1 ? u_strlen(text) : textLength);
-    const UnicodeString src((UChar*)text, len, len);
+    const UnicodeString src((UBool)(textLength == -1), text, textLength);
     ParsePosition pp;
     if (parsePos != NULL) {
         pp.setIndex(*parsePos);
     }
     *status = U_PARSE_ERROR; // assume failure, reset if succeed
-    CurrencyAmount* currAmt = ((const NumberFormat*)fmt)->parseCurrency(src, pp);
+    LocalPointer<CurrencyAmount> currAmt(((const NumberFormat*)fmt)->parseCurrency(src, pp));
     if (pp.getErrorIndex() != -1) {
         if (parsePos != NULL) {
             *parsePos = pp.getErrorIndex();
         }
-        if (currAmt != NULL) {
-            delete currAmt;
-        }
     } else {
         if (parsePos != NULL) {
             *parsePos = pp.getIndex();
         }
-        if (currAmt != NULL) {
+        if (pp.getIndex() > 0) {
             *status = U_ZERO_ERROR;
             u_strcpy(currency, currAmt->getISOCurrency());
             doubleVal = currAmt->getNumber().getDouble(*status);
-            delete currAmt;
         }
     }
     return doubleVal;
index 119d57e41fd51ac9778ec40e9904d685de634494..85028207da74a5f376c1fe43ec70d35d7ff85f93 100644 (file)
@@ -159,11 +159,10 @@ NumberFormatTest::TestAPI(void)
     }
 
     ParsePosition ppos;
-    CurrencyAmount* currAmt = test->parseCurrency("",ppos);
+    LocalPointer<CurrencyAmount> currAmt(test->parseCurrency("",ppos));
     // old test for (U_FAILURE(status)) was bogus here, method does not set status!
-    if (currAmt != NULL) {
+    if (ppos.getIndex()) {
         errln("Parsed empty string as currency");
-        delete currAmt;
     }
 
     delete test;
@@ -6018,13 +6017,12 @@ NumberFormatTest::TestParseCurrencyInUCurr() {
       NumberFormat* numFmt = NumberFormat::createInstance(locale, UNUM_CURRENCY, status);
       if (numFmt != NULL && U_SUCCESS(status)) {
           ParsePosition parsePos;
-          CurrencyAmount* currAmt = numFmt->parseCurrency(formatted, parsePos);
-          if (currAmt != NULL) {
+          LocalPointer<CurrencyAmount> currAmt(numFmt->parseCurrency(formatted, parsePos));
+          if (parsePos.getIndex() > 0) {
               double doubleVal = currAmt->getNumber().getDouble(status);
               if ( doubleVal != 1.0 ) {
                   errln("Parsed as currency value other than 1.0: " + formatted + " -> " + doubleVal);
               }
-              delete currAmt;
           } else {
               errln("Failed to parse as currency: " + formatted);
           }
@@ -6042,11 +6040,10 @@ NumberFormatTest::TestParseCurrencyInUCurr() {
       NumberFormat* numFmt = NumberFormat::createInstance(locale, UNUM_CURRENCY, status);
       if (numFmt != NULL && U_SUCCESS(status)) {
           ParsePosition parsePos;
-          CurrencyAmount* currAmt = numFmt->parseCurrency(formatted, parsePos);
-          if (currAmt != NULL) {
+          LocalPointer<CurrencyAmount> currAmt(numFmt->parseCurrency(formatted, parsePos));
+          if (parsePos.getIndex() > 0) {
               double doubleVal = currAmt->getNumber().getDouble(status);
               errln("Parsed as currency, should not have: " + formatted + " -> " + doubleVal);
-              delete currAmt;
           }
       } else {
           dataerrln("Unable to create NumberFormat. - %s", u_errorName(status));