source: trunk/src/xmlpatterns/data/qdecimal.cpp@ 780

Last change on this file since 780 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 6.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <math.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
51QT_BEGIN_NAMESPACE
52
53using namespace QPatternist;
54
55Decimal::Decimal(const xsDecimal num) : m_value(num)
56{
57}
58
59Decimal::Ptr Decimal::fromValue(const xsDecimal num)
60{
61 return Decimal::Ptr(new Decimal(num));
62}
63
64AtomicValue::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(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')))
76 {
77 return ValidationError::createError();
78 }
79
80 bool conversionOk = false;
81 const xsDecimal num = strNumericTrimmed.toDouble(&conversionOk);
82
83 if(conversionOk)
84 return AtomicValue::Ptr(new Decimal(num));
85 else
86 return ValidationError::createError();
87}
88
89bool Decimal::evaluateEBV(const QExplicitlySharedDataPointer<DynamicContext> &) const
90{
91 return !Double::isEqual(m_value, 0.0);
92}
93
94QString Decimal::stringValue() const
95{
96 return toString(m_value);
97}
98
99QString 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(::floor(value), 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 char *result = 0;
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));
121 delete result;
122
123 QString valueAsString;
124
125 if(sign)
126 valueAsString += QLatin1Char('-');
127
128 if(0 < decimalPoint)
129 {