}
FormattedNumber LocalizedNumberFormatter::formatInt(int64_t value, UErrorCode &status) const {
- if (U_FAILURE(status)) { return FormattedNumber(); }
+ if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
auto results = new NumberFormatterResults();
if (results == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
- return FormattedNumber();
+ return FormattedNumber(status);
}
results->quantity.setToLong(value);
return formatImpl(results, status);
}
FormattedNumber LocalizedNumberFormatter::formatDouble(double value, UErrorCode &status) const {
- if (U_FAILURE(status)) { return FormattedNumber(); }
+ if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
auto results = new NumberFormatterResults();
if (results == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
- return FormattedNumber();
+ return FormattedNumber(status);
}
results->quantity.setToDouble(value);
return formatImpl(results, status);
}
FormattedNumber LocalizedNumberFormatter::formatDecimal(StringPiece value, UErrorCode &status) const {
- if (U_FAILURE(status)) { return FormattedNumber(); }
+ if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
auto results = new NumberFormatterResults();
if (results == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
- return FormattedNumber();
+ return FormattedNumber(status);
}
results->quantity.setToDecNumber(value);
return formatImpl(results, status);
NumberFormatterImpl::applyStatic(fMacros, results->quantity, results->string, status);
}
- return FormattedNumber(results);
+ // Do not save the results object if we encountered a failure.
+ if (U_SUCCESS(status)) {
+ return FormattedNumber(results);
+ } else {
+ delete results;
+ return FormattedNumber(status);
+ }
}
UnicodeString FormattedNumber::toString() const {
- if (fResults == nullptr) { return {}; }
+ if (fResults == nullptr) {
+ // TODO: http://bugs.icu-project.org/trac/ticket/13437
+ return {};
+ }
return fResults->string.toUnicodeString();
}
Appendable &FormattedNumber::appendTo(Appendable &appendable) {
- if (fResults == nullptr) { return appendable; }
+ if (fResults == nullptr) {
+ // TODO: http://bugs.icu-project.org/trac/ticket/13437
+ return appendable;
+ }
appendable.appendString(fResults->string.chars(), fResults->string.length());
return appendable;
}
void FormattedNumber::populateFieldPosition(FieldPosition &fieldPosition, UErrorCode &status) {
- if (fResults == nullptr) { return; }
+ if (U_FAILURE(status)) { return; }
+ if (fResults == nullptr) {
+ status = fErrorCode;
+ return;
+ }
fResults->string.populateFieldPosition(fieldPosition, 0, status);
}
void
FormattedNumber::populateFieldPositionIterator(FieldPositionIterator &iterator, UErrorCode &status) {
- if (fResults == nullptr) { return; }
+ if (U_FAILURE(status)) { return; }
+ if (fResults == nullptr) {
+ status = fErrorCode;
+ return;
+ }
fResults->string.populateFieldPositionIterator(iterator, status);
}
if (minInt >= 0 && minInt <= kMaxIntFracSig) {
return {static_cast<int8_t>(minInt), -1};
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (maxInt >= 0 && maxInt <= kMaxIntFracSig) {
return {fUnion.minMaxInt.fMinInt, static_cast<int8_t>(maxInt)};
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
NotationUnion union_ = {settings};
return {NTN_SCIENTIFIC, union_};
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (targetWidth >= 0) {
return {cp, targetWidth, position};
} else {
- return {U_NUMBER_PADDING_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_PADDING_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minMaxFractionPlaces >= 0 && minMaxFractionPlaces <= kMaxIntFracSig) {
return constructFraction(minMaxFractionPlaces, minMaxFractionPlaces);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minFractionPlaces >= 0 && minFractionPlaces <= kMaxIntFracSig) {
return constructFraction(minFractionPlaces, -1);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (maxFractionPlaces >= 0 && maxFractionPlaces <= kMaxIntFracSig) {
return constructFraction(0, maxFractionPlaces);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
minFractionPlaces <= maxFractionPlaces) {
return constructFraction(minFractionPlaces, maxFractionPlaces);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minMaxSignificantDigits >= 0 && minMaxSignificantDigits <= kMaxIntFracSig) {
return constructSignificant(minMaxSignificantDigits, minMaxSignificantDigits);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minSignificantDigits >= 0 && minSignificantDigits <= kMaxIntFracSig) {
return constructSignificant(minSignificantDigits, -1);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (maxSignificantDigits >= 0 && maxSignificantDigits <= kMaxIntFracSig) {
return constructSignificant(0, maxSignificantDigits);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
minSignificantDigits <= maxSignificantDigits) {
return constructSignificant(minSignificantDigits, maxSignificantDigits);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (roundingIncrement > 0.0) {
return constructIncrement(roundingIncrement, 0);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minSignificantDigits >= 0 && minSignificantDigits <= kMaxIntFracSig) {
return constructFractionSignificant(*this, minSignificantDigits, -1);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (maxSignificantDigits >= 0 && maxSignificantDigits <= kMaxIntFracSig) {
return constructFractionSignificant(*this, -1, maxSignificantDigits);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
if (minFrac >= 0 && minFrac <= kMaxIntFracSig) {
return constructIncrement(fUnion.increment.fIncrement, minFrac);
} else {
- return {U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR};
+ return {U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR};
}
}
static constexpr char16_t kDefaultCurrency[] = u"XXX";
// FIXME: New error codes:
-static constexpr UErrorCode U_NUMBER_DIGIT_WIDTH_OUT_OF_RANGE_ERROR = U_ILLEGAL_ARGUMENT_ERROR;
-static constexpr UErrorCode U_NUMBER_PADDING_WIDTH_OUT_OF_RANGE_ERROR = U_ILLEGAL_ARGUMENT_ERROR;
+static constexpr UErrorCode U_NUMBER_DIGIT_WIDTH_OUTOFBOUNDS_ERROR = U_ILLEGAL_ARGUMENT_ERROR;
+static constexpr UErrorCode U_NUMBER_PADDING_WIDTH_OUTOFBOUNDS_ERROR = U_ILLEGAL_ARGUMENT_ERROR;
// Forward declarations:
* <p>
* This function is very hot, being called in every call to the number formatting pipeline.
*
- * @param fq
- * The quantity to be formatted.
+ * @param results
+ * The results object. This method takes ownership.
* @return The formatted number result.
*/
FormattedNumber formatImpl(impl::NumberFormatterResults *results, UErrorCode &status) const;
// Can't use LocalPointer because NumberFormatterResults is forward-declared
const impl::NumberFormatterResults *fResults;
- // Default constructor for error states
- FormattedNumber() : fResults(nullptr) {}
+ // Error code for the terminal methods
+ UErrorCode fErrorCode;
- explicit FormattedNumber(impl::NumberFormatterResults *results) : fResults(results) {}
+ explicit FormattedNumber(impl::NumberFormatterResults *results)
+ : fResults(results), fErrorCode(U_ZERO_ERROR) {};
+
+ explicit FormattedNumber(UErrorCode errorCode)
+ : fResults(nullptr), fErrorCode(errorCode) {};
// To give LocalizedNumberFormatter format methods access to this class's constructor:
friend class LocalizedNumberFormatter;
-1));
{
- UErrorCode status = U_ZERO_ERROR;
- lnf.formatInt(1, status);
+ UErrorCode status1 = U_ZERO_ERROR;
+ UErrorCode status2 = U_ZERO_ERROR;
+ FormattedNumber fn = lnf.formatInt(1, status1);
assertEquals(
"Should fail with U_ILLEGAL_ARGUMENT_ERROR since rounder is not legal",
U_ILLEGAL_ARGUMENT_ERROR,
- status);
+ status1);
+ FieldPosition fp;
+ fn.populateFieldPosition(fp, status2);
+ assertEquals(
+ "Should fail with U_ILLEGAL_ARGUMENT_ERROR on terminal method",
+ U_ILLEGAL_ARGUMENT_ERROR,
+ status2);
}
{