]>
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() 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
)) {
72 U_ASSERT(!quantity
.bogus
);
73 if (quantity
.isZero() && quantity
.isNegative()) {
77 if (quantity
.fitsInLong()) {
78 return static_cast<double>(quantity
.toLong());
80 return quantity
.toDouble();
84 void ParsedNumber::populateFormattable(Formattable
& output
, parse_flags_t parseFlags
) const {
85 bool sawNaN
= 0 != (flags
& FLAG_NAN
);
86 bool sawInfinity
= 0 != (flags
& FLAG_INFINITY
);
87 bool integerOnly
= 0 != (parseFlags
& PARSE_FLAG_INTEGER_ONLY
);
89 // Check for NaN, infinity, and -0.0
91 // Can't use NAN or std::nan because the byte pattern is platform-dependent;
92 // MSVC sets the sign bit, but Clang and GCC do not
93 output
.setDouble(uprv_getNaN());
97 if (0 != (flags
& FLAG_NEGATIVE
)) {
98 output
.setDouble(-INFINITY
);
101 output
.setDouble(INFINITY
);
105 U_ASSERT(!quantity
.bogus
);
106 if (quantity
.isZero() && quantity
.isNegative() && !integerOnly
) {
107 output
.setDouble(-0.0);
112 output
.adoptDecimalQuantity(new DecimalQuantity(quantity
));
115 bool ParsedNumber::isBetterThan(const ParsedNumber
& other
) {
116 // Favor results with strictly more characters consumed.
117 return charEnd
> other
.charEnd
;
122 #endif /* #if !UCONFIG_NO_FORMATTING */