Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/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 "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
0
ParsedNumber::ParsedNumber() {
25
0
    clear();
26
0
}
27
28
0
void ParsedNumber::clear() {
29
0
    quantity.bogus = true;
30
0
    charEnd = 0;
31
0
    flags = 0;
32
0
    prefix.setToBogus();
33
0
    suffix.setToBogus();
34
0
    currencyCode[0] = 0;
35
0
}
36
37
0
void ParsedNumber::setCharsConsumed(const StringSegment& segment) {
38
0
    charEnd = segment.getOffset();
39
0
}
40
41
0
void ParsedNumber::postProcess() {
42
0
    if (!quantity.bogus && 0 != (flags & FLAG_NEGATIVE)) {
43
0
        quantity.negate();
44
0
    }
45
0
}
46
47
0
bool ParsedNumber::success() const {
48
0
    return charEnd > 0 && 0 == (flags & FLAG_FAIL);
49
0
}
50
51
0
bool ParsedNumber::seenNumber() const {
52
0
    return !quantity.bogus || 0 != (flags & FLAG_NAN) || 0 != (flags & FLAG_INFINITY);
53
0
}
54
55
0
double ParsedNumber::getDouble() const {
56
0
    bool sawNaN = 0 != (flags & FLAG_NAN);
57
0
    bool sawInfinity = 0 != (flags & FLAG_INFINITY);
58
0
59
0
    // Check for NaN, infinity, and -0.0
60
0
    if (sawNaN) {
61
0
        // Can't use NAN or std::nan because the byte pattern is platform-dependent;
62
0
        // MSVC sets the sign bit, but Clang and GCC do not
63
0
        return uprv_getNaN();
64
0
    }
65
0
    if (sawInfinity) {
66
0
        if (0 != (flags & FLAG_NEGATIVE)) {
67
0
            return -INFINITY;
68
0
        } else {
69
0
            return INFINITY;
70
0
        }
71
0
    }
72
0
    U_ASSERT(!quantity.bogus);
73
0
    if (quantity.isZero() && quantity.isNegative()) {
74
0
        return -0.0;
75
0
    }
76
0
77
0
    if (quantity.fitsInLong()) {
78
0
        return static_cast<double>(quantity.toLong());
79
0
    } else {
80
0
        return quantity.toDouble();
81
0
    }
82
0
}
83
84
0
void ParsedNumber::populateFormattable(Formattable& output, parse_flags_t parseFlags) const {
85
0
    bool sawNaN = 0 != (flags & FLAG_NAN);
86
0
    bool sawInfinity = 0 != (flags & FLAG_INFINITY);
87
0
    bool integerOnly = 0 != (parseFlags & PARSE_FLAG_INTEGER_ONLY);
88
0
89
0
    // Check for NaN, infinity, and -0.0
90
0
    if (sawNaN) {
91
0
        // Can't use NAN or std::nan because the byte pattern is platform-dependent;
92
0
        // MSVC sets the sign bit, but Clang and GCC do not
93
0
        output.setDouble(uprv_getNaN());
94
0
        return;
95
0
    }
96
0
    if (sawInfinity) {
97
0
        if (0 != (flags & FLAG_NEGATIVE)) {
98
0
            output.setDouble(-INFINITY);
99
0
            return;
100
0
        } else {
101
0
            output.setDouble(INFINITY);
102
0
            return;
103
0
        }
104
0
    }
105
0
    U_ASSERT(!quantity.bogus);
106
0
    if (quantity.isZero() && quantity.isNegative() && !integerOnly) {
107
0
        output.setDouble(-0.0);
108
0
        return;
109
0
    }
110
0
111
0
    // All other numbers
112
0
    output.adoptDecimalQuantity(new DecimalQuantity(quantity));
113
0
}
114
115
0
bool ParsedNumber::isBetterThan(const ParsedNumber& other) {
116
0
    // Favor results with strictly more characters consumed.
117
0
    return charEnd > other.charEnd;
118
0
}
119
120
121
122
#endif /* #if !UCONFIG_NO_FORMATTING */