]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/numparse_parsednumber.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
12 #include "numparse_types.h"
13 #include "number_decimalquantity.h"
18 using namespace icu::number
;
19 using namespace icu::number::impl
;
20 using namespace icu::numparse
;
21 using namespace icu::numparse::impl
;
24 ParsedNumber::ParsedNumber() {
28 void ParsedNumber::clear() {
29 quantity
.bogus
= true;
37 void ParsedNumber::setCharsConsumed(const StringSegment
& segment
) {
38 charEnd
= segment
.getOffset();
41 void ParsedNumber::postProcess() {
42 if (!quantity
.bogus
&& 0 != (flags
& FLAG_NEGATIVE
)) {
47 bool ParsedNumber::success() const {
48 return charEnd
> 0 && 0 == (flags
& FLAG_FAIL
);
51 bool ParsedNumber::seenNumber() const {
52 return !quantity
.bogus
|| 0 != (flags
& FLAG_NAN
) || 0 != (flags
& FLAG_INFINITY
);
55 double ParsedNumber::getDouble(UErrorCode
& status
) const {
56 bool sawNaN
= 0 != (flags
& FLAG_NAN
);
57 bool sawInfinity
= 0 != (flags
& FLAG_INFINITY
);
59 // Check for NaN, infinity, and -0.0
61 // Can't use NAN or std::nan because the byte pattern is platform-dependent;
62 // MSVC sets the sign bit, but Clang and GCC do not
66 if (0 != (flags
& FLAG_NEGATIVE
)) {
73 status
= U_INVALID_STATE_ERROR
;
76 if (quantity
.isZero() && quantity
.isNegative()) {
80 if (quantity
.fitsInLong()) {
81 return static_cast<double>(quantity
.toLong());
83 return quantity
.toDouble();
87 void ParsedNumber::populateFormattable(Formattable
& output
, parse_flags_t parseFlags
) const {
88 bool sawNaN
= 0 != (flags
& FLAG_NAN
);
89 bool sawInfinity
= 0 != (flags
& FLAG_INFINITY
);
90 bool integerOnly
= 0 != (parseFlags
& PARSE_FLAG_INTEGER_ONLY
);
92 // Check for NaN, infinity, and -0.0
94 // Can't use NAN or std::nan because the byte pattern is platform-dependent;
95 // MSVC sets the sign bit, but Clang and GCC do not
96 output
.setDouble(uprv_getNaN());
100 if (0 != (flags
& FLAG_NEGATIVE
)) {
101 output
.setDouble(-INFINITY
);
104 output
.setDouble(INFINITY
);
108 U_ASSERT(!quantity
.bogus
);
109 if (quantity
.isZero() && quantity
.isNegative() && !integerOnly
) {
110 output
.setDouble(-0.0);
115 output
.adoptDecimalQuantity(new DecimalQuantity(quantity
));
118 bool ParsedNumber::isBetterThan(const ParsedNumber
& other
) {
119 // Favor results with strictly more characters consumed.
120 return charEnd
> other
.charEnd
;
125 #endif /* #if !UCONFIG_NO_FORMATTING */