| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcollator_icu.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||
| 2 | ** | - | ||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
| 4 | ** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org> | - | ||||||
| 5 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
| 6 | ** | - | ||||||
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||
| 8 | ** | - | ||||||
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
| 10 | ** Commercial License Usage | - | ||||||
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
| 12 | ** accordance with the commercial license agreement provided with the | - | ||||||
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
| 17 | ** | - | ||||||
| 18 | ** GNU Lesser General Public License Usage | - | ||||||
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
| 20 | ** General Public License version 3 as published by the Free Software | - | ||||||
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
| 22 | ** packaging of this file. Please review the following information to | - | ||||||
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
| 25 | ** | - | ||||||
| 26 | ** GNU General Public License Usage | - | ||||||
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
| 32 | ** included in the packaging of this file. Please review the following | - | ||||||
| 33 | ** information to ensure the GNU General Public License requirements will | - | ||||||
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
| 36 | ** | - | ||||||
| 37 | ** $QT_END_LICENSE$ | - | ||||||
| 38 | ** | - | ||||||
| 39 | ****************************************************************************/ | - | ||||||
| 40 | - | |||||||
| 41 | #include "qcollator_p.h" | - | ||||||
| 42 | #include "qstringlist.h" | - | ||||||
| 43 | #include "qstring.h" | - | ||||||
| 44 | - | |||||||
| 45 | #include <unicode/utypes.h> | - | ||||||
| 46 | #include <unicode/ucol.h> | - | ||||||
| 47 | #include <unicode/ustring.h> | - | ||||||
| 48 | #include <unicode/ures.h> | - | ||||||
| 49 | - | |||||||
| 50 | #include "qdebug.h" | - | ||||||
| 51 | - | |||||||
| 52 | QT_BEGIN_NAMESPACE | - | ||||||
| 53 | - | |||||||
| 54 | void QCollatorPrivate::init() | - | ||||||
| 55 | { | - | ||||||
| 56 | cleanup(); | - | ||||||
| 57 | - | |||||||
| 58 | UErrorCode status = U_ZERO_ERROR; | - | ||||||
| 59 | QByteArray name = locale.bcp47Name().replace(QLatin1Char('-'), QLatin1Char('_')).toLatin1(); | - | ||||||
| 60 | collator = ucol_open(name.constData(), &status); | - | ||||||
| 61 | if (U_FAILURE(status)) {
| 0-336 | ||||||
| 62 | qWarning("Could not create collator: %d", status); | - | ||||||
| 63 | collator = 0; | - | ||||||
| 64 | dirty = false; | - | ||||||
| 65 | return; never executed: return; | 0 | ||||||
| 66 | } | - | ||||||
| 67 | - | |||||||
| 68 | // enable normalization by default | - | ||||||
| 69 | ucol_setAttribute(collator, UCOL_NORMALIZATION_MODE, UCOL_ON, &status); | - | ||||||
| 70 | - | |||||||
| 71 | // The strength attribute in ICU is rather badly documented. Basically UCOL_PRIMARY | - | ||||||
| 72 | // ignores differences between base characters and accented characters as well as case. | - | ||||||
| 73 | // So A and A-umlaut would compare equal. | - | ||||||
| 74 | // UCOL_SECONDARY ignores case differences. UCOL_TERTIARY is the default in most languages | - | ||||||
| 75 | // and does case sensitive comparison. | - | ||||||
| 76 | // UCOL_QUATERNARY is used as default in a few languages such as Japanese to take care of some | - | ||||||
| 77 | // additional differences in those languages. | - | ||||||
| 78 | UColAttributeValue val = (caseSensitivity == Qt::CaseSensitive) ? UCOL_DEFAULT_STRENGTH : UCOL_SECONDARY;
| 138-198 | ||||||
| 79 | - | |||||||
| 80 | status = U_ZERO_ERROR; | - | ||||||
| 81 | ucol_setAttribute(collator, UCOL_STRENGTH, val, &status); | - | ||||||
| 82 | if (U_FAILURE(status))
| 0-336 | ||||||
| 83 | qWarning("ucol_setAttribute: Case First failed: %d", status); never executed: QMessageLogger(__FILE__, 83, __PRETTY_FUNCTION__).warning("ucol_setAttribute: Case First failed: %d", status); | 0 | ||||||
| 84 | - | |||||||
| 85 | status = U_ZERO_ERROR; | - | ||||||
| 86 | ucol_setAttribute(collator, UCOL_NUMERIC_COLLATION, numericMode ? UCOL_ON : UCOL_OFF, &status); | - | ||||||
| 87 | if (U_FAILURE(status))
| 0-336 | ||||||
| 88 | qWarning("ucol_setAttribute: numeric collation failed: %d", status); never executed: QMessageLogger(__FILE__, 88, __PRETTY_FUNCTION__).warning("ucol_setAttribute: numeric collation failed: %d", status); | 0 | ||||||
| 89 | - | |||||||
| 90 | status = U_ZERO_ERROR; | - | ||||||
| 91 | ucol_setAttribute(collator, UCOL_ALTERNATE_HANDLING, ignorePunctuation ? UCOL_SHIFTED : UCOL_NON_IGNORABLE, &status); | - | ||||||
| 92 | if (U_FAILURE(status))
| 0-336 | ||||||
| 93 | qWarning("ucol_setAttribute: Alternate handling failed: %d", status); never executed: QMessageLogger(__FILE__, 93, __PRETTY_FUNCTION__).warning("ucol_setAttribute: Alternate handling failed: %d", status); | 0 | ||||||
| 94 | - | |||||||
| 95 | dirty = false; | - | ||||||
| 96 | } executed 336 times by 13 tests: end of blockExecuted by:
| 336 | ||||||
| 97 | - | |||||||
| 98 | void QCollatorPrivate::cleanup() | - | ||||||
| 99 | { | - | ||||||
| 100 | if (collator)
| 336-386 | ||||||
| 101 | ucol_close(collator); executed 336 times by 13 tests: ucol_close_52(collator);Executed by:
| 336 | ||||||
| 102 | collator = 0; | - | ||||||
| 103 | } executed 722 times by 19 tests: end of blockExecuted by:
| 722 | ||||||
| 104 | - | |||||||
| 105 | int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const | - | ||||||
| 106 | { | - | ||||||
| 107 | if (d->dirty)
| 143-18877 | ||||||
| 108 | d->init(); executed 143 times by 6 tests: d->init();Executed by:
| 143 | ||||||
| 109 | - | |||||||
| 110 | if (d->collator)
| 0-19020 | ||||||
| 111 | return ucol_strcoll(d->collator, (const UChar *)s1, len1, (const UChar *)s2, len2); executed 19020 times by 12 tests: return ucol_strcoll_52(d->collator, (const UChar *)s1, len1, (const UChar *)s2, len2);Executed by:
| 19020 | ||||||
| 112 | - | |||||||
| 113 | return QString::compare(QString(s1, len1), QString(s2, len2), d->caseSensitivity); never executed: return QString::compare(QString(s1, len1), QString(s2, len2), d->caseSensitivity); | 0 | ||||||
| 114 | } | - | ||||||
| 115 | - | |||||||
| 116 | int QCollator::compare(const QString &s1, const QString &s2) const | - | ||||||
| 117 | { | - | ||||||
| 118 | if (d->collator)
| 0-7625 | ||||||
| 119 | return compare(s1.constData(), s1.size(), s2.constData(), s2.size()); executed 7625 times by 6 tests: return compare(s1.constData(), s1.size(), s2.constData(), s2.size());Executed by:
| 7625 | ||||||
| 120 | - | |||||||
| 121 | return QString::compare(s1, s2, d->caseSensitivity); never executed: return QString::compare(s1, s2, d->caseSensitivity); | 0 | ||||||
| 122 | } | - | ||||||
| 123 | - | |||||||
| 124 | int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const | - | ||||||
| 125 | { | - | ||||||
| 126 | if (d->collator)
| 0 | ||||||
| 127 | return compare(s1.constData(), s1.size(), s2.constData(), s2.size()); never executed: return compare(s1.constData(), s1.size(), s2.constData(), s2.size()); | 0 | ||||||
| 128 | - | |||||||
| 129 | return QStringRef::compare(s1, s2, d->caseSensitivity); never executed: return QStringRef::compare(s1, s2, d->caseSensitivity); | 0 | ||||||
| 130 | } | - | ||||||
| 131 | - | |||||||
| 132 | QCollatorSortKey QCollator::sortKey(const QString &string) const | - | ||||||
| 133 | { | - | ||||||
| 134 | if (d->dirty)
| 0 | ||||||
| 135 | d->init(); never executed: d->init(); | 0 | ||||||
| 136 | - | |||||||
| 137 | if (d->collator) {
| 0 | ||||||
| 138 | QByteArray result(16 + string.size() + (string.size() >> 2), Qt::Uninitialized); | - | ||||||
| 139 | int size = ucol_getSortKey(d->collator, (const UChar *)string.constData(), | - | ||||||
| 140 | string.size(), (uint8_t *)result.data(), result.size()); | - | ||||||
| 141 | if (size > result.size()) {
| 0 | ||||||
| 142 | result.resize(size); | - | ||||||
| 143 | size = ucol_getSortKey(d->collator, (const UChar *)string.constData(), | - | ||||||
| 144 | string.size(), (uint8_t *)result.data(), result.size()); | - | ||||||
| 145 | } never executed: end of block | 0 | ||||||
| 146 | result.truncate(size); | - | ||||||
| 147 | return QCollatorSortKey(new QCollatorSortKeyPrivate(result)); never executed: return QCollatorSortKey(new QCollatorSortKeyPrivate(result)); | 0 | ||||||
| 148 | } | - | ||||||
| 149 | - | |||||||
| 150 | return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray())); never executed: return QCollatorSortKey(new QCollatorSortKeyPrivate(QByteArray())); | 0 | ||||||
| 151 | } | - | ||||||
| 152 | - | |||||||
| 153 | int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const | - | ||||||
| 154 | { | - | ||||||
| 155 | return qstrcmp(d->m_key, otherKey.d->m_key); never executed: return qstrcmp(d->m_key, otherKey.d->m_key); | 0 | ||||||
| 156 | } | - | ||||||
| 157 | - | |||||||
| 158 | QT_END_NAMESPACE | - | ||||||
| Source code | Switch to Preprocessed file |