summaryrefslogtreecommitdiff
path: root/deps/icu-small/source/i18n/numparse_parsednumber.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'deps/icu-small/source/i18n/numparse_parsednumber.cpp')
-rw-r--r--deps/icu-small/source/i18n/numparse_parsednumber.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/deps/icu-small/source/i18n/numparse_parsednumber.cpp b/deps/icu-small/source/i18n/numparse_parsednumber.cpp
new file mode 100644
index 0000000000..98da4e8319
--- /dev/null
+++ b/deps/icu-small/source/i18n/numparse_parsednumber.cpp
@@ -0,0 +1,122 @@
+// © 2018 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
+
+#include "unicode/utypes.h"
+
+#if !UCONFIG_NO_FORMATTING
+
+// Allow implicit conversion from char16_t* to UnicodeString for this file:
+// Helpful in toString methods and elsewhere.
+#define UNISTR_FROM_STRING_EXPLICIT
+
+#include "numparse_types.h"
+#include "number_decimalquantity.h"
+#include "putilimp.h"
+#include <cmath>
+
+using namespace icu;
+using namespace icu::number;
+using namespace icu::number::impl;
+using namespace icu::numparse;
+using namespace icu::numparse::impl;
+
+
+ParsedNumber::ParsedNumber() {
+ clear();
+}
+
+void ParsedNumber::clear() {
+ quantity.bogus = true;
+ charEnd = 0;
+ flags = 0;
+ prefix.setToBogus();
+ suffix.setToBogus();
+ currencyCode[0] = 0;
+}
+
+void ParsedNumber::setCharsConsumed(const StringSegment& segment) {
+ charEnd = segment.getOffset();
+}
+
+void ParsedNumber::postProcess() {
+ if (!quantity.bogus && 0 != (flags & FLAG_NEGATIVE)) {
+ quantity.negate();
+ }
+}
+
+bool ParsedNumber::success() const {
+ return charEnd > 0 && 0 == (flags & FLAG_FAIL);
+}
+
+bool ParsedNumber::seenNumber() const {
+ return !quantity.bogus || 0 != (flags & FLAG_NAN) || 0 != (flags & FLAG_INFINITY);
+}
+
+double ParsedNumber::getDouble() const {
+ bool sawNaN = 0 != (flags & FLAG_NAN);
+ bool sawInfinity = 0 != (flags & FLAG_INFINITY);
+
+ // Check for NaN, infinity, and -0.0
+ if (sawNaN) {
+ // Can't use NAN or std::nan because the byte pattern is platform-dependent;
+ // MSVC sets the sign bit, but Clang and GCC do not
+ return uprv_getNaN();
+ }
+ if (sawInfinity) {
+ if (0 != (flags & FLAG_NEGATIVE)) {
+ return -INFINITY;
+ } else {
+ return INFINITY;
+ }
+ }
+ U_ASSERT(!quantity.bogus);
+ if (quantity.isZero() && quantity.isNegative()) {
+ return -0.0;
+ }
+
+ if (quantity.fitsInLong()) {
+ return static_cast<double>(quantity.toLong());
+ } else {
+ return quantity.toDouble();
+ }
+}
+
+void ParsedNumber::populateFormattable(Formattable& output, parse_flags_t parseFlags) const {
+ bool sawNaN = 0 != (flags & FLAG_NAN);
+ bool sawInfinity = 0 != (flags & FLAG_INFINITY);
+ bool integerOnly = 0 != (parseFlags & PARSE_FLAG_INTEGER_ONLY);
+
+ // Check for NaN, infinity, and -0.0
+ if (sawNaN) {
+ // Can't use NAN or std::nan because the byte pattern is platform-dependent;
+ // MSVC sets the sign bit, but Clang and GCC do not
+ output.setDouble(uprv_getNaN());
+ return;
+ }
+ if (sawInfinity) {
+ if (0 != (flags & FLAG_NEGATIVE)) {
+ output.setDouble(-INFINITY);
+ return;
+ } else {
+ output.setDouble(INFINITY);
+ return;
+ }
+ }
+ U_ASSERT(!quantity.bogus);
+ if (quantity.isZero() && quantity.isNegative() && !integerOnly) {
+ output.setDouble(-0.0);
+ return;
+ }
+
+ // All other numbers
+ output.adoptDecimalQuantity(new DecimalQuantity(quantity));
+}
+
+bool ParsedNumber::isBetterThan(const ParsedNumber& other) {
+ // Favor results with strictly more characters consumed.
+ return charEnd > other.charEnd;
+}
+
+
+
+#endif /* #if !UCONFIG_NO_FORMATTING */