1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include <math.h> |
41 | |
42 | #include <private/qlocale_tools_p.h> |
43 | |
44 | #include "qabstractfloat_p.h" |
45 | #include "qatomictype_p.h" |
46 | #include "qbuiltintypes_p.h" |
47 | #include "qvalidationerror_p.h" |
48 | |
49 | #include "qdecimal_p.h" |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | using namespace QPatternist; |
54 | |
55 | Decimal::Decimal(const xsDecimal num) : m_value(num) |
56 | { |
57 | } |
58 | |
59 | Decimal::Ptr Decimal::fromValue(const xsDecimal num) |
60 | { |
61 | return Decimal::Ptr(new Decimal(num)); |
62 | } |
63 | |
64 | AtomicValue::Ptr Decimal::fromLexical(const QString &strNumeric) |
65 | { |
66 | /* QString::toDouble() handles the whitespace facet. */ |
67 | const QString strNumericTrimmed(strNumeric.trimmed()); |
68 | |
69 | /* Block these out, as QString::toDouble() supports them. */ |
70 | if(strNumericTrimmed.compare(other: QLatin1String("-INF" ), cs: Qt::CaseInsensitive) == 0 |
71 | || strNumericTrimmed.compare(other: QLatin1String("INF" ), cs: Qt::CaseInsensitive) == 0 |
72 | || strNumericTrimmed.compare(other: QLatin1String("+INF" ), cs: Qt::CaseInsensitive) == 0 |
73 | || strNumericTrimmed.compare(other: QLatin1String("nan" ), cs: Qt::CaseInsensitive) == 0 |
74 | || strNumericTrimmed.contains(c: QLatin1Char('e')) |
75 | || strNumericTrimmed.contains(c: QLatin1Char('E'))) |
76 | { |
77 | return ValidationError::createError(); |
78 | } |
79 | |
80 | bool conversionOk = false; |
81 | const xsDecimal num = strNumericTrimmed.toDouble(ok: &conversionOk); |
82 | |
83 | if(conversionOk) |
84 | return AtomicValue::Ptr(new Decimal(num)); |
85 | else |
86 | return ValidationError::createError(); |
87 | } |
88 | |
89 | bool Decimal::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const |
90 | { |
91 | return !Double::isEqual(a: m_value, b: 0.0); |
92 | } |
93 | |
94 | QString Decimal::stringValue() const |
95 | { |
96 | return toString(value: m_value); |
97 | } |
98 | |
99 | QString Decimal::toString(const xsDecimal value) |
100 | { |
101 | /* |
102 | * If SV is in the value space of xs:integer, that is, if there are no |
103 | * significant digits after the decimal point, then the value is converted |
104 | * from an xs:decimal to an xs:integer and the resulting xs:integer is |
105 | * converted to an xs:string using the rule above. |
106 | */ |
107 | if(Double::isEqual(a: ::floor(x: value), b: value)) |
108 | { |
109 | /* The static_cast is identical to Integer::toInteger(). */ |
110 | return QString::number(static_cast<xsInteger>(value)); |
111 | } |
112 | else |
113 | { |
114 | int sign; |
115 | int decimalPoint; |
116 | const QString qret = qdtoa(d: value, decpt: &decimalPoint, sign: &sign); |
117 | QString valueAsString; |
118 | |
119 | if(sign) |
120 | valueAsString += QLatin1Char('-'); |
121 | |
122 | if(0 < decimalPoint) |
123 | { |
124 | valueAsString += qret.left(n: decimalPoint); |
125 | valueAsString += QLatin1Char('.'); |
126 | if (qret.size() <= decimalPoint) |
127 | valueAsString += QLatin1Char('0'); |
128 | else |
129 | valueAsString += qret.mid(position: decimalPoint); |
130 | } |
131 | else |
132 | { |
133 | valueAsString += QLatin1Char('0'); |
134 | valueAsString += QLatin1Char('.'); |
135 | |
136 | for(int d = decimalPoint; d < 0; d++) |
137 | valueAsString += QLatin1Char('0'); |
138 | |
139 | valueAsString += qret; |
140 | } |
141 | |
142 | return valueAsString; |
143 | } |
144 | } |
145 | |
146 | ItemType::Ptr Decimal::type() const |
147 | { |
148 | return BuiltinTypes::xsDecimal; |
149 | } |
150 | |
151 | xsDouble Decimal::toDouble() const |
152 | { |
153 | return static_cast<xsDouble>(m_value); |
154 | } |
155 | |
156 | xsInteger Decimal::toInteger() const |
157 | { |
158 | return static_cast<xsInteger>(m_value); |
159 | } |
160 | |
161 | xsFloat Decimal::toFloat() const |
162 | { |
163 | return static_cast<xsFloat>(m_value); |
164 | } |
165 | |
166 | xsDecimal Decimal::toDecimal() const |
167 | { |
168 | return m_value; |
169 | } |
170 | |
171 | qulonglong Decimal::toUnsignedInteger() const |
172 | { |
173 | Q_ASSERT_X(false, Q_FUNC_INFO, |
174 | "It makes no sense to call this function, see Numeric::toUnsignedInteger()." ); |
175 | return 0; |
176 | } |
177 | |
178 | Numeric::Ptr Decimal::round() const |
179 | { |
180 | return Numeric::Ptr(new Decimal(roundFloat(val: m_value))); |
181 | } |
182 | |
183 | Numeric::Ptr Decimal::roundHalfToEven(const xsInteger /*scale*/) const |
184 | { |
185 | return Numeric::Ptr(); |
186 | } |
187 | |
188 | Numeric::Ptr Decimal::floor() const |
189 | { |
190 | return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(::floor(x: m_value)))); |
191 | } |
192 | |
193 | Numeric::Ptr Decimal::ceiling() const |
194 | { |
195 | return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(ceil(x: m_value)))); |
196 | } |
197 | |
198 | Numeric::Ptr Decimal::abs() const |
199 | { |
200 | return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(fabs(x: m_value)))); |
201 | } |
202 | |
203 | bool Decimal::isNaN() const |
204 | { |
205 | return false; |
206 | } |
207 | |
208 | bool Decimal::isInf() const |
209 | { |
210 | return false; |
211 | } |
212 | |
213 | Item Decimal::toNegated() const |
214 | { |
215 | if(AbstractFloat<true>::isEqual(a: m_value, b: 0.0)) |
216 | return fromValue(num: 0).data(); |
217 | else |
218 | return fromValue(num: -m_value).data(); |
219 | } |
220 | |
221 | bool Decimal::isSigned() const |
222 | { |
223 | Q_ASSERT_X(false, Q_FUNC_INFO, |
224 | "It makes no sense to call this function, see Numeric::isSigned()." ); |
225 | return false; |
226 | } |
227 | |
228 | QT_END_NAMESPACE |
229 | |