]>
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"
14 #include "string_segment.h"
19 using namespace icu::number
;
20 using namespace icu::number::impl
;
21 using namespace icu::numparse
;
22 using namespace icu::numparse::impl
;
25 ParsedNumber::ParsedNumber() {
29 void ParsedNumber::clear() {
30 quantity
.bogus
= true;
38 void ParsedNumber::setCharsConsumed(const StringSegment
& segment
) {
39 charEnd
= segment
.getOffset();
42 void ParsedNumber::postProcess() {
43 if (!quantity
.bogus
&& 0 != (flags
& FLAG_NEGATIVE
)) {
48 bool ParsedNumber::success() const {
49 return charEnd
> 0 && 0 == (flags
& FLAG_FAIL
);
52 bool ParsedNumber::seenNumber() const {
53 return !quantity
.bogus
|| 0 != (flags
& FLAG_NAN
) || 0 != (flags
& FLAG_INFINITY
);
56 double ParsedNumber::getDouble(UErrorCode
& status
) const {
57 bool sawNaN
= 0 != (flags
& FLAG_NAN
);
58 bool sawInfinity
= 0 != (flags
& FLAG_INFINITY
);
60 // Check for NaN, infinity, and -0.0
62 // Can't use NAN or std::nan because the byte pattern is platform-dependent;
63 // MSVC sets the sign bit, but Clang and GCC do not
67 if (0 != (flags
& FLAG_NEGATIVE
)) {
74 status
= U_INVALID_STATE_ERROR
;
77 if (quantity
.isZeroish() && quantity
.isNegative()) {
81 if (quantity
.fitsInLong()) {
82 return static_cast<double>(quantity
.toLong());
84 return quantity
.toDouble();
88 void ParsedNumber::populateFormattable(Formattable
& output
, parse_flags_t parseFlags
) const {
89 bool sawNaN
= 0 != (flags
& FLAG_NAN
);
90 bool sawInfinity
= 0 != (flags
& FLAG_INFINITY
);
91 bool integerOnly
= 0 != (parseFlags
& PARSE_FLAG_INTEGER_ONLY
);
93 // Check for NaN, infinity, and -0.0
95 // Can't use NAN or std::nan because the byte pattern is platform-dependent;
96 // MSVC sets the sign bit, but Clang and GCC do not
97 output
.setDouble(uprv_getNaN());
101 if (0 != (flags
& FLAG_NEGATIVE
)) {
102 output
.setDouble(-INFINITY
);
105 output
.setDouble(INFINITY
);
109 U_ASSERT(!quantity
.bogus
);
110 if (quantity
.isZeroish() && quantity
.isNegative() && !integerOnly
) {
111 output
.setDouble(-0.0);
116 output
.adoptDecimalQuantity(new DecimalQuantity(quantity
));
119 bool ParsedNumber::isBetterThan(const ParsedNumber
& other
) {
120 // Favor results with strictly more characters consumed.
121 return charEnd
> other
.charEnd
;
126 #endif /* #if !UCONFIG_NO_FORMATTING */