]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/numparse_parsednumber.cpp
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / numparse_parsednumber.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7
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
11
12 #include "numparse_types.h"
13 #include "number_decimalquantity.h"
14 #include "putilimp.h"
15 #include <cmath>
16
17 using namespace icu;
18 using namespace icu::number;
19 using namespace icu::number::impl;
20 using namespace icu::numparse;
21 using namespace icu::numparse::impl;
22
23
24 ParsedNumber::ParsedNumber() {
25 clear();
26 }
27
28 void ParsedNumber::clear() {
29 quantity.bogus = true;
30 charEnd = 0;
31 flags = 0;
32 prefix.setToBogus();
33 suffix.setToBogus();
34 currencyCode[0] = 0;
35 }
36
37 void ParsedNumber::setCharsConsumed(const StringSegment& segment) {
38 charEnd = segment.getOffset();
39 }
40
41 void ParsedNumber::postProcess() {
42 if (!quantity.bogus && 0 != (flags & FLAG_NEGATIVE)) {
43 quantity.negate();
44 }
45 }
46
47 bool ParsedNumber::success() const {
48 return charEnd > 0 && 0 == (flags & FLAG_FAIL);
49 }
50
51 bool ParsedNumber::seenNumber() const {
52 return !quantity.bogus || 0 != (flags & FLAG_NAN) || 0 != (flags & FLAG_INFINITY);
53 }
54
55 double ParsedNumber::getDouble(UErrorCode& status) const {
56 bool sawNaN = 0 != (flags & FLAG_NAN);
57 bool sawInfinity = 0 != (flags & FLAG_INFINITY);
58
59 // Check for NaN, infinity, and -0.0
60 if (sawNaN) {
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
63 return uprv_getNaN();
64 }
65 if (sawInfinity) {
66 if (0 != (flags & FLAG_NEGATIVE)) {
67 return -INFINITY;
68 } else {
69 return INFINITY;
70 }
71 }
72 if (quantity.bogus) {
73 status = U_INVALID_STATE_ERROR;
74 return 0.0;
75 }
76 if (quantity.isZero() && quantity.isNegative()) {
77 return -0.0;
78 }
79
80 if (quantity.fitsInLong()) {
81 return static_cast<double>(quantity.toLong());
82 } else {
83 return quantity.toDouble();
84 }
85 }
86
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);
91
92 // Check for NaN, infinity, and -0.0
93 if (sawNaN) {
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());
97 return;
98 }
99 if (sawInfinity) {
100 if (0 != (flags & FLAG_NEGATIVE)) {
101 output.setDouble(-INFINITY);
102 return;
103 } else {
104 output.setDouble(INFINITY);
105 return;
106 }
107 }
108 U_ASSERT(!quantity.bogus);
109 if (quantity.isZero() && quantity.isNegative() && !integerOnly) {
110 output.setDouble(-0.0);
111 return;
112 }
113
114 // All other numbers
115 output.adoptDecimalQuantity(new DecimalQuantity(quantity));
116 }
117
118 bool ParsedNumber::isBetterThan(const ParsedNumber& other) {
119 // Favor results with strictly more characters consumed.
120 return charEnd > other.charEnd;
121 }
122
123
124
125 #endif /* #if !UCONFIG_NO_FORMATTING */