/src/icu/source/i18n/numparse_parsednumber.cpp
Line | Count | Source (jump to first uncovered line) |
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 "string_segment.h" |
15 | | #include "putilimp.h" |
16 | | #include <cmath> |
17 | | |
18 | | using namespace icu; |
19 | | using namespace icu::number; |
20 | | using namespace icu::number::impl; |
21 | | using namespace icu::numparse; |
22 | | using namespace icu::numparse::impl; |
23 | | |
24 | | |
25 | 0 | ParsedNumber::ParsedNumber() { |
26 | 0 | clear(); |
27 | 0 | } |
28 | | |
29 | 0 | void ParsedNumber::clear() { |
30 | 0 | quantity.bogus = true; |
31 | 0 | charEnd = 0; |
32 | 0 | flags = 0; |
33 | 0 | prefix.setToBogus(); |
34 | 0 | suffix.setToBogus(); |
35 | 0 | currencyCode[0] = 0; |
36 | 0 | } |
37 | | |
38 | 0 | void ParsedNumber::setCharsConsumed(const StringSegment& segment) { |
39 | 0 | charEnd = segment.getOffset(); |
40 | 0 | } |
41 | | |
42 | 0 | void ParsedNumber::postProcess() { |
43 | 0 | if (!quantity.bogus && 0 != (flags & FLAG_NEGATIVE)) { |
44 | 0 | quantity.negate(); |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | 0 | bool ParsedNumber::success() const { |
49 | 0 | return charEnd > 0 && 0 == (flags & FLAG_FAIL); |
50 | 0 | } |
51 | | |
52 | 0 | bool ParsedNumber::seenNumber() const { |
53 | 0 | return !quantity.bogus || 0 != (flags & FLAG_NAN) || 0 != (flags & FLAG_INFINITY); |
54 | 0 | } |
55 | | |
56 | 0 | double ParsedNumber::getDouble(UErrorCode& status) const { |
57 | 0 | bool sawNaN = 0 != (flags & FLAG_NAN); |
58 | 0 | bool sawInfinity = 0 != (flags & FLAG_INFINITY); |
59 | | |
60 | | // Check for NaN, infinity, and -0.0 |
61 | 0 | if (sawNaN) { |
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 |
64 | 0 | return uprv_getNaN(); |
65 | 0 | } |
66 | 0 | if (sawInfinity) { |
67 | 0 | if (0 != (flags & FLAG_NEGATIVE)) { |
68 | 0 | return -INFINITY; |
69 | 0 | } else { |
70 | 0 | return INFINITY; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | if (quantity.bogus) { |
74 | 0 | status = U_INVALID_STATE_ERROR; |
75 | 0 | return 0.0; |
76 | 0 | } |
77 | 0 | if (quantity.isZeroish() && quantity.isNegative()) { |
78 | 0 | return -0.0; |
79 | 0 | } |
80 | | |
81 | 0 | if (quantity.fitsInLong()) { |
82 | 0 | return static_cast<double>(quantity.toLong()); |
83 | 0 | } else { |
84 | 0 | return quantity.toDouble(); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | void ParsedNumber::populateFormattable(Formattable& output, parse_flags_t parseFlags) const { |
89 | 0 | bool sawNaN = 0 != (flags & FLAG_NAN); |
90 | 0 | bool sawInfinity = 0 != (flags & FLAG_INFINITY); |
91 | 0 | bool integerOnly = 0 != (parseFlags & PARSE_FLAG_INTEGER_ONLY); |
92 | | |
93 | | // Check for NaN, infinity, and -0.0 |
94 | 0 | if (sawNaN) { |
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 | 0 | output.setDouble(uprv_getNaN()); |
98 | 0 | return; |
99 | 0 | } |
100 | 0 | if (sawInfinity) { |
101 | 0 | if (0 != (flags & FLAG_NEGATIVE)) { |
102 | 0 | output.setDouble(-INFINITY); |
103 | 0 | return; |
104 | 0 | } else { |
105 | 0 | output.setDouble(INFINITY); |
106 | 0 | return; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | U_ASSERT(!quantity.bogus); |
110 | 0 | if (quantity.isZeroish() && quantity.isNegative() && !integerOnly) { |
111 | 0 | output.setDouble(-0.0); |
112 | 0 | return; |
113 | 0 | } |
114 | | |
115 | | // All other numbers |
116 | 0 | output.adoptDecimalQuantity(new DecimalQuantity(quantity)); |
117 | 0 | } |
118 | | |
119 | 0 | bool ParsedNumber::isBetterThan(const ParsedNumber& other) { |
120 | | // Favor results with strictly more characters consumed. |
121 | 0 | return charEnd > other.charEnd; |
122 | 0 | } |
123 | | |
124 | | |
125 | | |
126 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |