#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"
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;
}
/**
#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"
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;
}
// -------------------------------------
#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"
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)
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;
}
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;
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);
}
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));