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"
|
2019-04-05 23:43:19 +00:00
|
|
|
#include "string_segment.h"
|
2018-04-26 01:33:59 +00:00
|
|
|
#include "putilimp.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);
|
|
|
|
}
|
|
|
|
|
2019-02-06 22:38:36 +00:00
|
|
|
double ParsedNumber::getDouble(UErrorCode& status) const {
|
2018-02-06 09:43:37 +00:00
|
|
|
bool sawNaN = 0 != (flags & FLAG_NAN);
|
|
|
|
bool sawInfinity = 0 != (flags & FLAG_INFINITY);
|
|
|
|
|
|
|
|
// Check for NaN, infinity, and -0.0
|
|
|
|
if (sawNaN) {
|
2018-04-26 01:33:59 +00:00
|
|
|
// Can't use NAN or std::nan because the byte pattern is platform-dependent;
|
|
|
|
// MSVC sets the sign bit, but Clang and GCC do not
|
|
|
|
return uprv_getNaN();
|
2018-02-06 09:43:37 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 22:38:36 +00:00
|
|
|
if (quantity.bogus) {
|
|
|
|
status = U_INVALID_STATE_ERROR;
|
|
|
|
return 0.0;
|
|
|
|
}
|
2019-07-23 04:41:43 +00:00
|
|
|
if (quantity.isZeroish() && quantity.isNegative()) {
|
2018-02-06 09:43:37 +00:00
|
|
|
return -0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quantity.fitsInLong()) {
|
2018-04-25 00:39:22 +00:00
|
|
|
return static_cast<double>(quantity.toLong());
|
2018-03-31 03:10:44 +00:00
|
|
|
} 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) {
|
2018-04-26 01:33:59 +00:00
|
|
|
// Can't use NAN or std::nan because the byte pattern is platform-dependent;
|
|
|
|
// MSVC sets the sign bit, but Clang and GCC do not
|
|
|
|
output.setDouble(uprv_getNaN());
|
2018-03-21 09:48:55 +00:00
|
|
|
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);
|
2019-07-23 04:41:43 +00:00
|
|
|
if (quantity.isZeroish() && 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 */
|