| 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 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | |
| 50 | #ifndef Patternist_AtomicComparators_H |
| 51 | #define Patternist_AtomicComparators_H |
| 52 | |
| 53 | #include <private/qabstractfloat_p.h> |
| 54 | #include <private/qatomiccomparator_p.h> |
| 55 | #include <private/qprimitives_p.h> |
| 56 | |
| 57 | /** |
| 58 | * @file |
| 59 | * @short Contains all the classes implementing comparisons between atomic values. |
| 60 | */ |
| 61 | |
| 62 | QT_BEGIN_NAMESPACE |
| 63 | |
| 64 | namespace QPatternist |
| 65 | { |
| 66 | /** |
| 67 | * @short Performs case @em sensitive string comparison |
| 68 | * between @c xs:anyUri, @c xs:string, and @c xs:untypedAtomic. |
| 69 | * |
| 70 | * @ingroup Patternist_xdm |
| 71 | * @author Frans Englich <frans.englich@nokia.com> |
| 72 | */ |
| 73 | class StringComparator : public AtomicComparator |
| 74 | { |
| 75 | public: |
| 76 | /** |
| 77 | * Compares two strings, and returns the appropriate AtomicComparator::ComparisonResult enum. This |
| 78 | * is a bit simplified version of string comparison as defined in the XPath specifications, |
| 79 | * since this does not take any string collations into account(which an implementation neither |
| 80 | * is required to do). |
| 81 | * |
| 82 | * @see <a href="http://www.w3.org/TR/xpath-functions/#string-compare">XQuery 1.0 and XPath |
| 83 | * 2.0 Functions and Operators, 7.3 ValueComparison::Equality and Comparison of Strings</a> |
| 84 | */ |
| 85 | virtual ComparisonResult compare(const Item &op1, |
| 86 | const AtomicComparator::Operator op, |
| 87 | const Item &op2) const; |
| 88 | |
| 89 | /** |
| 90 | * Compares two strings, and returns @c true if they are considered equal as per |
| 91 | * an ordinary string comparison. In other words, this is an implementation with |
| 92 | * the Unicode code point collation. |
| 93 | * |
| 94 | * @see <a href="http://www.w3.org/TR/xpath-functions/#string-compare">XQuery 1.0 and XPath |
| 95 | * 2.0 Functions and Operators, 7.3 ValueComparison::Equality and Comparison of Strings</a> |
| 96 | */ |
| 97 | virtual bool equals(const Item &op1, |
| 98 | const Item &op2) const; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * @short Performs case @em insensitive string comparison |
| 103 | * between @c xs:anyUri, @c xs:string, and @c xs:untypedAtomic. |
| 104 | * |
| 105 | * @ingroup Patternist_xdm |
| 106 | * @author Frans Englich <frans.englich@nokia.com> |
| 107 | */ |
| 108 | class CaseInsensitiveStringComparator : public AtomicComparator |
| 109 | { |
| 110 | public: |
| 111 | /** |
| 112 | * Converts both string values to upper case and afterwards compare them. |
| 113 | */ |
| 114 | virtual ComparisonResult compare(const Item &op1, |
| 115 | const AtomicComparator::Operator op, |
| 116 | const Item &op2) const; |
| 117 | |
| 118 | /** |
| 119 | * Converts both string values case insensitively. |
| 120 | */ |
| 121 | virtual bool equals(const Item &op1, |
| 122 | const Item &op2) const; |
| 123 | }; |
| 124 | |
| 125 | /** |
| 126 | * @short Compares @c xs:base64Binary and @c xs:hexBinary values. |
| 127 | * |
| 128 | * @author Frans Englich <frans.englich@nokia.com> |
| 129 | */ |
| 130 | class BinaryDataComparator : public AtomicComparator |
| 131 | { |
| 132 | public: |
| 133 | virtual bool equals(const Item &op1, |
| 134 | const Item &op2) const; |
| 135 | }; |
| 136 | |
| 137 | /** |
| 138 | * @short Compares @c xs:boolean values. |
| 139 | * |
| 140 | * This is done via the object's Boolean::evaluteEBV() function. |
| 141 | * |
| 142 | * @author Frans Englich <frans.englich@nokia.com> |
| 143 | */ |
| 144 | class BooleanComparator : public AtomicComparator |
| 145 | { |
| 146 | public: |
| 147 | virtual ComparisonResult compare(const Item &op1, |
| 148 | const AtomicComparator::Operator op, |
| 149 | const Item &op2) const; |
| 150 | |
| 151 | virtual bool equals(const Item &op1, |
| 152 | const Item &op2) const; |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * @short Compares @c xs:double values. |
| 157 | * |
| 158 | * @todo Add docs about numeric promotion |
| 159 | * |
| 160 | * @author Frans Englich <frans.englich@nokia.com> |
| 161 | */ |
| 162 | class AbstractFloatComparator : public AtomicComparator |
| 163 | { |
| 164 | public: |
| 165 | virtual ComparisonResult compare(const Item &op1, |
| 166 | const AtomicComparator::Operator op, |
| 167 | const Item &op2) const; |
| 168 | |
| 169 | virtual bool equals(const Item &op1, |
| 170 | const Item &op2) const; |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * @short Compares @c xs:double values for the purpose of sorting. |
| 175 | * |
| 176 | * @todo Add docs about numeric promotion |
| 177 | * |
| 178 | * @author Frans Englich <frans.englich@nokia.com> |
| 179 | */ |
| 180 | template<const AtomicComparator::Operator t_op> |
| 181 | class AbstractFloatSortComparator : public AbstractFloatComparator |
| 182 | { |
| 183 | public: |
| 184 | virtual ComparisonResult compare(const Item &o1, |
| 185 | const AtomicComparator::Operator op, |
| 186 | const Item &o2) const |
| 187 | { |
| 188 | Q_ASSERT_X(t_op == OperatorLessThanNaNLeast || t_op == OperatorLessThanNaNGreatest, Q_FUNC_INFO, |
| 189 | "Can only be instantiated with those two." ); |
| 190 | Q_ASSERT(op == t_op); |
| 191 | Q_UNUSED(op); /* Needed when building in release mode. */ |
| 192 | |
| 193 | const xsDouble v1 = o1.template as<Numeric>()->toDouble(); |
| 194 | const xsDouble v2 = o2.template as<Numeric>()->toDouble(); |
| 195 | |
| 196 | if(qIsNaN(d: v1) && !qIsNaN(d: v2)) |
| 197 | return t_op == OperatorLessThanNaNLeast ? LessThan : GreaterThan; |
| 198 | if(!qIsNaN(d: v1) && qIsNaN(d: v2)) |
| 199 | return t_op == OperatorLessThanNaNLeast ? GreaterThan : LessThan; |
| 200 | |
| 201 | if(Double::isEqual(a: v1, b: v2)) |
| 202 | return Equal; |
| 203 | else if(v1 < v2) |
| 204 | return LessThan; |
| 205 | else |
| 206 | return GreaterThan; |
| 207 | } |
| 208 | |
| 209 | }; |
| 210 | |
| 211 | /** |
| 212 | * @short Compares @c xs:decimal values. |
| 213 | * |
| 214 | * @author Frans Englich <frans.englich@nokia.com> |
| 215 | */ |
| 216 | class DecimalComparator : public AtomicComparator |
| 217 | { |
| 218 | public: |
| 219 | virtual ComparisonResult compare(const Item &op1, |
| 220 | const AtomicComparator::Operator op, |
| 221 | const Item &op2) const; |
| 222 | |
| 223 | virtual bool equals(const Item &op1, |
| 224 | const Item &op2) const; |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * @short Compares @c xs:integer values. |
| 229 | * |
| 230 | * @author Frans Englich <frans.englich@nokia.com> |
| 231 | */ |
| 232 | class IntegerComparator : public AtomicComparator |
| 233 | { |
| 234 | public: |
| 235 | virtual ComparisonResult compare(const Item &op1, |
| 236 | const AtomicComparator::Operator op, |
| 237 | const Item &op2) const; |
| 238 | |
| 239 | virtual bool equals(const Item &op1, |
| 240 | const Item &op2) const; |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * @short Compares @c xs:QName values. |
| 245 | * |
| 246 | * @author Frans Englich <frans.englich@nokia.com> |
| 247 | */ |
| 248 | class QNameComparator : public AtomicComparator |
| 249 | { |
| 250 | public: |
| 251 | virtual bool equals(const Item &op1, |
| 252 | const Item &op2) const; |
| 253 | }; |
| 254 | |
| 255 | /** |
| 256 | * @short Compares sub-classes of AbstractDateTime. |
| 257 | * |
| 258 | * @author Frans Englich <frans.englich@nokia.com> |
| 259 | */ |
| 260 | class AbstractDateTimeComparator : public AtomicComparator |
| 261 | { |
| 262 | public: |
| 263 | virtual ComparisonResult compare(const Item &op1, |
| 264 | const AtomicComparator::Operator op, |
| 265 | const Item &op2) const; |
| 266 | virtual bool equals(const Item &op1, |
| 267 | const Item &op2) const; |
| 268 | }; |
| 269 | |
| 270 | /** |
| 271 | * @short Compares sub-classes of AbstractDuration. |
| 272 | * |
| 273 | * @author Frans Englich <frans.englich@nokia.com> |
| 274 | */ |
| 275 | class AbstractDurationComparator : public AtomicComparator |
| 276 | { |
| 277 | public: |
| 278 | virtual ComparisonResult compare(const Item &op1, |
| 279 | const AtomicComparator::Operator op, |
| 280 | const Item &op2) const; |
| 281 | virtual bool equals(const Item &op1, |
| 282 | const Item &op2) const; |
| 283 | |
| 284 | private: |
| 285 | static inline QDateTime addDurationToDateTime(const QDateTime &dateTime, |
| 286 | const AbstractDuration *const duration); |
| 287 | }; |
| 288 | } |
| 289 | |
| 290 | QT_END_NAMESPACE |
| 291 | |
| 292 | #endif |
| 293 | |