2018-02-06 09:43:37 +00:00
|
|
|
// © 2018 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2018-04-23 23:02:26 +00:00
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2018-02-06 09:43:37 +00:00
|
|
|
|
2018-02-13 02:23:52 +00:00
|
|
|
// Allow implicit conversion from char16_t* to UnicodeString for this file:
|
|
|
|
// Helpful in toString methods and elsewhere.
|
|
|
|
#define UNISTR_FROM_STRING_EXPLICIT
|
|
|
|
|
2018-02-06 09:43:37 +00:00
|
|
|
#include "numparse_types.h"
|
2018-03-21 09:48:55 +00:00
|
|
|
#include "number_decimalquantity.h"
|
2018-02-06 09:43:37 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace icu;
|
2018-03-21 09:48:55 +00:00
|
|
|
using namespace icu::number;
|
|
|
|
using namespace icu::number::impl;
|
2018-02-06 09:43:37 +00:00
|
|
|
using namespace icu::numparse;
|
|
|
|
using namespace icu::numparse::impl;
|
|
|
|
|
|
|
|
|
|
|
|
ParsedNumber::ParsedNumber() {
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParsedNumber::clear() {
|
|
|
|
quantity.bogus = true;
|
|
|
|
charEnd = 0;
|
|
|
|
flags = 0;
|
|
|
|
prefix.setToBogus();
|
|
|
|
suffix.setToBogus();
|
2018-02-10 02:59:49 +00:00
|
|
|
currencyCode[0] = 0;
|
2018-02-06 09:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParsedNumber::setCharsConsumed(const StringSegment& segment) {
|
|
|
|
charEnd = segment.getOffset();
|
|
|
|
}
|
|
|
|
|
2018-03-31 03:10:44 +00:00
|
|
|
void ParsedNumber::postProcess() {
|
|
|
|
if (!quantity.bogus && 0 != (flags & FLAG_NEGATIVE)) {
|
|
|
|
quantity.negate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 09:43:37 +00:00
|
|
|
bool ParsedNumber::success() const {
|
|
|
|
return charEnd > 0 && 0 == (flags & FLAG_FAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParsedNumber::seenNumber() const {
|
|
|
|
return !quantity.bogus || 0 != (flags & FLAG_NAN) || 0 != (flags & FLAG_INFINITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
double ParsedNumber::getDouble() const {
|
|
|
|
bool sawNaN = 0 != (flags & FLAG_NAN);
|
|
|
|
bool sawInfinity = 0 != (flags & FLAG_INFINITY);
|
|
|
|
|
|
|
|
// Check for NaN, infinity, and -0.0
|
|
|
|
if (sawNaN) {
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
if (sawInfinity) {
|
2018-03-31 03:10:44 +00:00
|
|
|
if (0 != (flags & FLAG_NEGATIVE)) {
|
2018-02-06 09:43:37 +00:00
|
|
|
return -INFINITY;
|
|
|
|
} else {
|
|
|
|
return INFINITY;
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 03:10:44 +00:00
|
|
|
U_ASSERT(!quantity.bogus);
|
|
|
|
if (quantity.isZero() && quantity.isNegative()) {
|
2018-02-06 09:43:37 +00:00
|
|
|
return -0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quantity.fitsInLong()) {
|
2018-03-31 03:10:44 +00:00
|
|
|
return quantity.toLong();
|
|
|
|
} else {
|
|
|
|
return quantity.toDouble();
|
2018-02-13 02:23:52 +00:00
|
|
|
}
|
2018-02-06 09:43:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:05:20 +00:00
|
|
|
void ParsedNumber::populateFormattable(Formattable& output, parse_flags_t parseFlags) const {
|
2018-03-21 09:48:55 +00:00
|
|
|
bool sawNaN = 0 != (flags & FLAG_NAN);
|
|
|
|
bool sawInfinity = 0 != (flags & FLAG_INFINITY);
|
2018-04-17 08:05:20 +00:00
|
|
|
bool integerOnly = 0 != (parseFlags & PARSE_FLAG_INTEGER_ONLY);
|
2018-03-21 09:48:55 +00:00
|
|
|
|
|
|
|
// Check for NaN, infinity, and -0.0
|
|
|
|
if (sawNaN) {
|
|
|
|
output.setDouble(NAN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sawInfinity) {
|
2018-03-31 03:10:44 +00:00
|
|
|
if (0 != (flags & FLAG_NEGATIVE)) {
|
2018-03-21 09:48:55 +00:00
|
|
|
output.setDouble(-INFINITY);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
output.setDouble(INFINITY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 03:10:44 +00:00
|
|
|
U_ASSERT(!quantity.bogus);
|
2018-04-17 08:05:20 +00:00
|
|
|
if (quantity.isZero() && quantity.isNegative() && !integerOnly) {
|
2018-03-21 09:48:55 +00:00
|
|
|
output.setDouble(-0.0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All other numbers
|
2018-03-31 03:10:44 +00:00
|
|
|
output.adoptDecimalQuantity(new DecimalQuantity(quantity));
|
2018-03-21 09:48:55 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:59:35 +00:00
|
|
|
bool ParsedNumber::isBetterThan(const ParsedNumber& other) {
|
|
|
|
// Favor results with strictly more characters consumed.
|
|
|
|
return charEnd > other.charEnd;
|
|
|
|
}
|
|
|
|
|
2018-02-06 09:43:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|