1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include "qabstractfloat_p.h"
45 #include "qatomictype_p.h"
46 #include "qbuiltintypes_p.h"
47 #include "qvalidationerror_p.h"
49 #include "qdecimal_p.h"
53 using namespace QPatternist;
55 Decimal::Decimal(const xsDecimal num) : m_value(num)
59 Decimal::Ptr Decimal::fromValue(const xsDecimal num)
61 return Decimal::Ptr(new Decimal(num));
64 AtomicValue::Ptr Decimal::fromLexical(const QString &strNumeric)
66 /* QString::toDouble() handles the whitespace facet. */
67 const QString strNumericTrimmed(strNumeric.trimmed());
69 /* Block these out, as QString::toDouble() supports them. */
70 if(strNumericTrimmed.compare(QLatin1String("-INF"), Qt::CaseInsensitive) == 0
71 || strNumericTrimmed.compare(QLatin1String("INF"), Qt::CaseInsensitive) == 0
72 || strNumericTrimmed.compare(QLatin1String("+INF"), Qt::CaseInsensitive) == 0
73 || strNumericTrimmed.compare(QLatin1String("nan"), Qt::CaseInsensitive) == 0
74 || strNumericTrimmed.contains(QLatin1Char('e'))
75 || strNumericTrimmed.contains(QLatin1Char('E')))
77 return ValidationError::createError();
80 bool conversionOk = false;
81 const xsDecimal num = strNumericTrimmed.toDouble(&conversionOk);
84 return AtomicValue::Ptr(new Decimal(num));
86 return ValidationError::createError();
89 bool Decimal::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const
91 return !Double::isEqual(m_value, 0.0);
94 QString Decimal::stringValue() const
96 return toString(m_value);
99 QString Decimal::toString(const xsDecimal value)
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.
107 if(Double::isEqual(::floor(value), value))
109 /* The static_cast is identical to Integer::toInteger(). */
110 return QString::number(static_cast<xsInteger>(value));
117 static_cast<void>(qdtoa(value, 0, 0, &decimalPoint, &sign, 0, &result));
118 /* If the copy constructor is used instead of QString::operator=(),
119 * it doesn't compile. I have no idea why. */
120 const QString qret(QString::fromLatin1(result));
123 QString valueAsString;
126 valueAsString += QLatin1Char('-');
130 valueAsString += qret.left(decimalPoint);
131 valueAsString += QLatin1Char('.');
132 if (qret.size() <= decimalPoint)
133 valueAsString += QLatin1Char('0');
135 valueAsString += qret.mid(decimalPoint);
139 valueAsString += QLatin1Char('0');
140 valueAsString += QLatin1Char('.');
142 for(int d = decimalPoint; d < 0; d++)
143 valueAsString += QLatin1Char('0');
145 valueAsString += qret;
148 return valueAsString;
152 ItemType::Ptr Decimal::type() const
154 return BuiltinTypes::xsDecimal;
157 xsDouble Decimal::toDouble() const
159 return static_cast<xsDouble>(m_value);
162 xsInteger Decimal::toInteger() const
164 return static_cast<xsInteger>(m_value);
167 xsFloat Decimal::toFloat() const
169 return static_cast<xsFloat>(m_value);
172 xsDecimal Decimal::toDecimal() const
177 qulonglong Decimal::toUnsignedInteger() const
179 Q_ASSERT_X(false, Q_FUNC_INFO,
180 "It makes no sense to call this function, see Numeric::toUnsignedInteger().");
184 Numeric::Ptr Decimal::round() const
186 return Numeric::Ptr(new Decimal(roundFloat(m_value)));
189 Numeric::Ptr Decimal::roundHalfToEven(const xsInteger /*scale*/) const
191 return Numeric::Ptr();
194 Numeric::Ptr Decimal::floor() const
196 return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(::floor(m_value))));
199 Numeric::Ptr Decimal::ceiling() const
201 return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(ceil(m_value))));
204 Numeric::Ptr Decimal::abs() const
206 return Numeric::Ptr(new Decimal(static_cast<xsDecimal>(fabs(m_value))));
209 bool Decimal::isNaN() const
214 bool Decimal::isInf() const
219 Item Decimal::toNegated() const
221 if(AbstractFloat<true>::isEqual(m_value, 0.0))
222 return fromValue(0).data();
224 return fromValue(-m_value).data();
227 bool Decimal::isSigned() const
229 Q_ASSERT_X(false, Q_FUNC_INFO,
230 "It makes no sense to call this function, see Numeric::isSigned().");