Qt 4.8
qatomiccomparators.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qabstractduration_p.h"
43 #include "qabstractdatetime_p.h"
44 #include "qbase64binary_p.h"
45 #include "qboolean_p.h"
46 #include "qdynamiccontext_p.h"
47 #include "qqnamevalue_p.h"
48 
49 #include "qatomiccomparators_p.h"
50 
52 
53 using namespace QPatternist;
54 
55 /* -------------------------------------------------- */
59  const Item &o2) const
60 {
61  const int result = QString::compare(o1.stringValue(), o2.stringValue());
62 
63  if(result > 0)
64  return GreaterThan;
65  else if(result < 0)
66  return LessThan;
67  else
68  {
69  Q_ASSERT(result == 0);
70  return Equal;
71  }
72 }
73 
75  const Item &o2) const
76 {
77  return o1.stringValue() == o2.stringValue();
78 }
79 /* -------------------------------------------------- */
80 
81 /* -------------------------------------------------- */
85  const Item &o2) const
86 {
87  const QString i1(o1.stringValue().toLower());
88  const QString i2(o2.stringValue().toLower());
89  const int result = QString::compare(i1, i2);
90 
91  if(result > 0)
92  return GreaterThan;
93  else if(result < 0)
94  return LessThan;
95  else
96  {
97  Q_ASSERT(result == 0);
98  return Equal;
99  }
100 }
101 
103  const Item &o2) const
104 {
105  const QString s1(o1.stringValue());
106  const QString s2(o2.stringValue());
107 
108  return s1.length() == s2.length() &&
109  s1.startsWith(s2, Qt::CaseInsensitive);
110 }
111 /* -------------------------------------------------- */
112 
113 /* -------------------------------------------------- */
115  const Item &o2) const
116 {
117  return o1.as<Base64Binary>()->asByteArray() ==
118  o2.as<Base64Binary>()->asByteArray();
119 }
120 /* -------------------------------------------------- */
121 
122 /* -------------------------------------------------- */
126  const Item &o2) const
127 {
128  /* We know Boolean::evaluateEBV doesn't use the DynamicContext. */
129  const bool v1 = o1.as<AtomicValue>()->evaluateEBV(QExplicitlySharedDataPointer<DynamicContext>());
130  const bool v2 = o2.as<AtomicValue>()->evaluateEBV(QExplicitlySharedDataPointer<DynamicContext>());
131 
132  if(v1 == v2)
133  return Equal;
134  else if(v1 == false)
135  {
136  Q_ASSERT(v2 == true);
137  return LessThan;
138  }
139  else
140  {
141  Q_ASSERT(v1 == true && v2 == false);
142  return GreaterThan;
143  }
144 }
145 
147  const Item &o2) const
148 {
149  /* Boolean is an atomic class. */
150  return o1.as<AtomicValue>() == o2.as<AtomicValue>();
151 }
152 /* -------------------------------------------------- */
153 
154 /* -------------------------------------------------- */
158  const Item &o2) const
159 {
160  const xsDouble v1 = o1.as<Numeric>()->toDouble();
161  const xsDouble v2 = o2.as<Numeric>()->toDouble();
162 
163  if(Double::isEqual(v1, v2))
164  return Equal;
165  else if(v1 < v2)
166  return LessThan;
167  else if(v1 > v2)
168  return GreaterThan;
169  else
170  {
171  /* We have NaN values. Make sure we don't return a result which would
172  * signify success for the operator in question. */
173  if((op & OperatorGreaterThan) == OperatorGreaterThan)
174  return LessThan;
175  else
176  {
177  Q_ASSERT((op & OperatorLessThan) == OperatorLessThan);
178  return GreaterThan;
179  }
180  }
181 }
182 
184  const Item &o2) const
185 {
186  return Double::isEqual(o1.as<Numeric>()->toDouble(), o2.as<Numeric>()->toDouble());
187 }
188 /* -------------------------------------------------- */
189 
190 /* -------------------------------------------------- */
194  const Item &o2) const
195 {
196  const xsDecimal v1 = o1.as<Numeric>()->toDecimal();
197  const xsDecimal v2 = o2.as<Numeric>()->toDecimal();
198 
199  if(Double::isEqual(v1, v2))
200  return Equal;
201  else if(v1 < v2)
202  return LessThan;
203  else
204  return GreaterThan;
205 }
206 
208  const Item &o2) const
209 {
210  return Double::isEqual(o1.as<Numeric>()->toDecimal(), o2.as<Numeric>()->toDecimal());
211 }
212 /* -------------------------------------------------- */
213 
214 /* -------------------------------------------------- */
218  const Item &o2) const
219 {
220  const Numeric *const num1 = o1.as<Numeric>();
221  const Numeric *const num2 = o1.as<Numeric>();
222 
235  if(num1->isSigned() || num2->isSigned())
236  {
237  const xsInteger v1 = o1.as<Numeric>()->toInteger();
238  const xsInteger v2 = o2.as<Numeric>()->toInteger();
239 
240  if(v1 == v2)
241  return Equal;
242  else if(v1 < v2)
243  return LessThan;
244  else
245  return GreaterThan;
246  }
247  else
248  {
249  const qulonglong v1 = o1.as<Numeric>()->toUnsignedInteger();
250  const qulonglong v2 = o2.as<Numeric>()->toUnsignedInteger();
251 
252  if(v1 == v2)
253  return Equal;
254  else if(v1 < v2)
255  return LessThan;
256  else
257  return GreaterThan;
258  }
259 }
260 
262  const Item &o2) const
263 {
264  return o1.as<Numeric>()->toInteger() == o2.as<Numeric>()->toInteger();
265 }
266 
267 /* -------------------------------------------------- */
268 
269 /* -------------------------------------------------- */
271  const Item &o2) const
272 {
273  return o1.as<QNameValue>()->m_qName ==
274  o2.as<QNameValue>()->m_qName;
275 }
276 /* -------------------------------------------------- */
277 
278 /* -------------------------------------------------- */
280  const Item &o2) const
281 {
282  const QDateTime dt1(o1.as<AbstractDateTime>()->toDateTime());
283  const QDateTime dt2(o2.as<AbstractDateTime>()->toDateTime());
284 
285  /*
286  pDebug() << "COMPARING:"
287  << o1->as<AbstractDateTime>()->toDateTime().toString()
288  << o2->as<AbstractDateTime>()->toDateTime().toString();
289  pDebug() << "DATE ONLY:"
290  << o1->as<AbstractDateTime>()->toDateTime().isDateOnly()
291  << o2->as<AbstractDateTime>()->toDateTime().isDateOnly();
292  */
293  return dt1 == dt2 &&
294  dt1.timeSpec() == dt2.timeSpec();
295 }
296 
300  const Item &operand2) const
301 {
302  const QDateTime &dt1 = operand1.as<AbstractDateTime>()->toDateTime();
303  const QDateTime &dt2 = operand2.as<AbstractDateTime>()->toDateTime();
304 
305  if(dt1 == dt2)
306  return Equal;
307  else if(dt1 < dt2)
308  return LessThan;
309  else
310  return GreaterThan;
311 }
312 /* -------------------------------------------------- */
313 
314 /* -------------------------------------------------- */
316  const Item &o2) const
317 {
318  /* We use AbstractDuration::operator==() */
319  return *o1.as<AbstractDuration>() ==
320  *o2.as<AbstractDuration>();
321 }
322 
324  const AbstractDuration *const duration)
325 {
326  QDateTime result(dateTime);
327  qint64 seconds = 0;
328 
329  const qint8 signMultiplier = (duration->isPositive() ? 1 : -1);
330 
331  result = result.addYears(signMultiplier * duration->years());
332  result = result.addMonths(signMultiplier * duration->months());
333  result = result.addDays(signMultiplier * duration->days());
334 
335  seconds = 60 * 60 * duration->hours();
336  seconds += 60 * duration->minutes();
337  seconds += duration->seconds();
338 
339  result = result.addSecs(signMultiplier * seconds);
340  result = result.addMSecs(signMultiplier * duration->mseconds());
341 
342  return result;
343 }
344 
348  const Item &o2) const
349 {
350  const AbstractDuration *const duration = o1.as<AbstractDuration>();
351  const AbstractDuration *const otherDuration = o2.as<AbstractDuration>();
352 
353  const QDateTime dateTime1(QDate(1696, 9, 1), QTime(0, 0, 0), Qt::UTC);
354  const QDateTime dateTime2(QDate(1697, 2, 1), QTime(0, 0, 0), Qt::UTC);
355  const QDateTime dateTime3(QDate(1903, 3, 1), QTime(0, 0, 0), Qt::UTC);
356  const QDateTime dateTime4(QDate(1903, 7, 1), QTime(0, 0, 0), Qt::UTC);
357 
358  const QDateTime durationDateTime1 = addDurationToDateTime(dateTime1, duration);
359  const QDateTime durationDateTime2 = addDurationToDateTime(dateTime2, duration);
360  const QDateTime durationDateTime3 = addDurationToDateTime(dateTime3, duration);
361  const QDateTime durationDateTime4 = addDurationToDateTime(dateTime4, duration);
362 
363  const QDateTime otherDurationDateTime1 = addDurationToDateTime(dateTime1, otherDuration);
364  const QDateTime otherDurationDateTime2 = addDurationToDateTime(dateTime2, otherDuration);
365  const QDateTime otherDurationDateTime3 = addDurationToDateTime(dateTime3, otherDuration);
366  const QDateTime otherDurationDateTime4 = addDurationToDateTime(dateTime4, otherDuration);
367 
368  if (durationDateTime1 > otherDurationDateTime1 &&
369  durationDateTime2 > otherDurationDateTime2 &&
370  durationDateTime3 > otherDurationDateTime3 &&
371  durationDateTime4 > otherDurationDateTime4) {
372  return GreaterThan;
373  } else if (durationDateTime1 < otherDurationDateTime1 &&
374  durationDateTime2 < otherDurationDateTime2 &&
375  durationDateTime3 < otherDurationDateTime3 &&
376  durationDateTime4 < otherDurationDateTime4) {
377  return LessThan;
378  } else if (*duration == *otherDuration) {
379  return Equal;
380  } else {
381  return Incomparable;
382  }
383 }
384 
385 /* -------------------------------------------------- */
QDateTime addSecs(int secs) const
Returns a QDateTime object containing a datetime s seconds later than the datetime of this object (or...
Definition: qdatetime.cpp:2869
virtual bool equals(const Item &op1, const Item &op2) const
virtual SecondProperty seconds() const =0
virtual bool equals(const Item &op1, const Item &op2) const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
Base class for classes implementing durations.
Base class for classes implementing values related to time, date or both.
virtual xsFloat toDecimal() const =0
virtual MinuteProperty minutes() const =0
qint64 xsInteger
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
virtual bool equals(const Item &op1, const Item &op2) const
The QDate class provides date functions.
Definition: qdatetime.h:55
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual bool equals(const Item &op1, const Item &op2) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
xsDouble xsDecimal
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
virtual bool isSigned() const =0
Returns true if this value is signed. If false is returned, the value is unsigned.
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
virtual MonthProperty months() const =0
virtual bool equals(const Item &op1, const Item &op2) const
QDateTime addYears(int years) const
Returns a QDateTime object containing a datetime nyears years later than the datetime of this object ...
Definition: qdatetime.cpp:2809
signed char qint8
Definition: qglobal.h:933
The QTime class provides clock time functions.
Definition: qdatetime.h:148
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
virtual DayCountProperty days() const =0
Base class for all classes representing atomic values.
Definition: qitem_p.h:118
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
The namespace for the internal API of QtXmlPatterns.
QString stringValue() const
Returns the string value of this Item.
Definition: qitem_p.h:302
QDateTime addMonths(int months) const
Returns a QDateTime object containing a datetime nmonths months later than the datetime of this objec...
Definition: qdatetime.cpp:2796
const QDateTime & toDateTime() const
virtual HourProperty hours() const =0
__int64 qint64
Definition: qglobal.h:942
QDateTime addMSecs(qint64 msecs) const
Returns a QDateTime object containing a datetime msecs miliseconds later than the datetime of this ob...
Definition: qdatetime.cpp:2881
virtual bool equals(const Item &op1, const Item &op2) const
virtual YearProperty years() const =0
virtual bool equals(const Item &op1, const Item &op2) const
const QXmlName m_qName
QDateTime addDays(int days) const
Returns a QDateTime object containing a datetime ndays days later than the datetime of this object (o...
Definition: qdatetime.cpp:2783
int compare(const QString &s) const
Definition: qstring.cpp:5037
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
static bool isEqual(const xsDouble a, const xsDouble b)
virtual bool equals(const Item &op1, const Item &op2) const
Base class for all numeric values.
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
Qt::TimeSpec timeSpec() const
Returns the time specification of the datetime.
Definition: qdatetime.cpp:2379
Implements the value instance of the xs:QName type.
Definition: qqnamevalue_p.h:73
virtual xsDouble toDouble() const =0
virtual bool equals(const Item &op1, const Item &op2) const
static qreal toDouble(const QChar *&str)
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
quint64 qulonglong
Definition: qglobal.h:952
virtual bool equals(const Item &op1, const Item &op2) const
TCastTarget * as() const
Definition: qitem_p.h:278
static QDateTime addDurationToDateTime(const QDateTime &dateTime, const AbstractDuration *const duration)
Implements the value instance of the xs:base64Binary type.
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
virtual ComparisonResult compare(const Item &op1, const AtomicComparator::Operator op, const Item &op2) const
Contains all the classes implementing comparisons between atomic values.
virtual MSecondProperty mseconds() const =0