diff options
Diffstat (limited to 'src/corelib/tools/qcollator.cpp')
-rw-r--r-- | src/corelib/tools/qcollator.cpp | 136 |
1 files changed, 90 insertions, 46 deletions
diff --git a/src/corelib/tools/qcollator.cpp b/src/corelib/tools/qcollator.cpp index 1cf223aae6..6e85027462 100644 --- a/src/corelib/tools/qcollator.cpp +++ b/src/corelib/tools/qcollator.cpp @@ -46,7 +46,6 @@ QT_BEGIN_NAMESPACE - /*! \class QCollator \inmodule QtCore @@ -59,27 +58,38 @@ QT_BEGIN_NAMESPACE \ingroup string-processing \ingroup shared - QCollator is initialized with a QLocale and an optional collation strategy. It tries to - initialize the collator with the specified values. The collator can then be used to compare - and sort strings in a locale dependent fashion. + QCollator is initialized with a QLocale and an optional collation strategy. + It tries to initialize the collator with the specified values. The collator + can then be used to compare and sort strings in a locale dependent fashion. + + A QCollator object can be used together with template based sorting + algorithms such as std::sort to sort a list of QStrings. + + In addition to the locale and collation strategy, several optional flags can + be set that influence the result of the collation. +*/ + +/*! + \since 5.13 - A QCollator object can be used together with template based sorting algorithms such as std::sort - to sort a list of QStrings. + Constructs a QCollator using the system's default collation locale. - In addition to the locale and collation strategy, several optional flags can be set that influence - the result of the collation. + \sa setLocale(), QLocale::collation() */ +QCollator::QCollator() + : d(new QCollatorPrivate(QLocale::system().collation())) +{ + d->init(); +} /*! - Constructs a QCollator from \a locale. If \a locale is not specified - the system's default locale is used. + Constructs a QCollator from \a locale. \sa setLocale() */ QCollator::QCollator(const QLocale &locale) : d(new QCollatorPrivate(locale)) { - d->init(); } /*! @@ -129,9 +139,9 @@ QCollator &QCollator::operator=(const QCollator &other) Move constructor. Moves from \a other into this collator. - Note that a moved-from QCollator can only be destroyed or assigned - to. The effect of calling other functions than the destructor or - one of the assignment operators is undefined. + Note that a moved-from QCollator can only be destroyed or assigned to. + The effect of calling other functions than the destructor or one of the + assignment operators is undefined. */ /*! @@ -139,9 +149,9 @@ QCollator &QCollator::operator=(const QCollator &other) Move-assigns from \a other to this collator. - Note that a moved-from QCollator can only be destroyed or assigned - to. The effect of calling other functions than the destructor or - one of the assignment operators is undefined. + Note that a moved-from QCollator can only be destroyed or assigned to. + The effect of calling other functions than the destructor or one of the + assignment operators is undefined. */ /*! @@ -219,15 +229,11 @@ Qt::CaseSensitivity QCollator::caseSensitivity() const Enables numeric sorting mode when \a on is set to true. - This will enable proper sorting of numeric digits, so that e.g. 100 sorts after 99. + This will enable proper sorting of numeric digits, so that e.g. 100 sorts + after 99. By default this mode is off. - \note On Windows, this functionality makes use of the \l{ICU} library. If Qt was - compiled without ICU support, it falls back to code using native Windows API, - which only works from Windows 7 onwards. On older versions of Windows, it will not work - and a warning will be emitted at runtime. - \sa numericMode() */ void QCollator::setNumericMode(bool on) @@ -254,11 +260,13 @@ bool QCollator::numericMode() const /*! \fn void QCollator::setIgnorePunctuation(bool on) - If \a on is set to true, punctuation characters and symbols are ignored when determining sort order. + If \a on is set to true, punctuation characters and symbols are ignored when + determining sort order. The default is locale dependent. - \note This method is not currently supported if Qt is configured to not use ICU on Linux. + \note This method is not currently supported if Qt is configured to not use + ICU on Linux. \sa ignorePunctuation() */ @@ -274,7 +282,8 @@ void QCollator::setIgnorePunctuation(bool on) /*! \fn bool QCollator::ignorePunctuation() const - Returns \c true if punctuation characters and symbols are ignored when determining sort order. + Returns \c true if punctuation characters and symbols are ignored when + determining sort order. \sa setIgnorePunctuation() */ @@ -284,35 +293,66 @@ bool QCollator::ignorePunctuation() const } /*! - \fn int QCollator::compare(const QString &s1, const QString &s2) const + \since 5.13 + \fn bool QCollator::operator()(QStringView s1, QStringView s2) const + \internal +*/ + +/*! + \since 5.13 + \fn int QCollator::compare(QStringView s1, QStringView s2) const - Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero - depending on whether \a s1 is smaller, equal or larger than \a s2. - */ + Compares \a s1 with \a s2. + Returns an integer less than, equal to, or greater than zero depending on + whether \a s1 sorts before, with or after \a s2. +*/ +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QCollator::operator()(const QString &s1, const QString &s2) const \internal */ /*! - \fn int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const \overload - Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero - depending on whether \a s1 is smaller, equal or larger than \a s2. + Compares \a s1 with \a s2. + + Returns an integer less than, equal to, or greater than zero depending on + whether \a s1 sorts before, with or after \a s2. +*/ +int QCollator::compare(const QString &s1, const QString &s2) const +{ + return compare(QStringView(s1), QStringView(s2)); +} + +/*! + \overload + + Compares \a s1 with \a s2. + + Returns an integer less than, equal to, or greater than zero depending on + whether \a s1 sorts before, with or after \a s2. */ +int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const +{ + return compare(QStringView(s1), QStringView(s2)); +} /*! - \fn int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const \overload - Compares \a s1 with \a s2. \a len1 and \a len2 specify the length of the - QChar arrays pointer to by \a s1 and \a s2. + Compares \a s1 with \a s2. \a len1 and \a len2 specify the lengths of the + QChar arrays pointed to by \a s1 and \a s2. - Returns an integer less than, equal to, or greater than zero - depending on whether \a s1 is smaller, equal or larger than \a s2. + Returns an integer less than, equal to, or greater than zero depending on + whether \a s1 sorts before, with or after \a s2. */ +int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const +{ + return compare(QStringView(s1, len1), QStringView(s2, len2)); +} +#endif // QT_STRINGVIEW_LEVEL < 2 /*! \fn QCollatorSortKey QCollator::sortKey(const QString &string) const @@ -320,9 +360,11 @@ bool QCollator::ignorePunctuation() const Returns a sortKey for \a string. Creating the sort key is usually somewhat slower, than using the compare() - methods directly. But if the string is compared repeatedly (e.g. when sorting - a whole list of strings), it's usually faster to create the sort keys for each - string and then sort using the keys. + methods directly. But if the string is compared repeatedly (e.g. when + sorting a whole list of strings), it's usually faster to create the sort + keys for each string and then sort using the keys. + + \note Not supported with the C (a.k.a. POSIX) locale on Darwin. */ /*! @@ -332,8 +374,8 @@ bool QCollator::ignorePunctuation() const \since 5.2 - The QCollatorSortKey class is always created by QCollator::sortKey() - and is used for fast strings collation, for example when collating many strings. + The QCollatorSortKey class is always created by QCollator::sortKey() and is + used for fast strings collation, for example when collating many strings. \reentrant \ingroup i18n @@ -402,9 +444,11 @@ QCollatorSortKey& QCollatorSortKey::operator=(const QCollatorSortKey &other) /*! \fn int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const - Compares the key to \a otherKey. Returns a negative value if the key - is less than \a otherKey, 0 if the key is equal to \a otherKey or a - positive value if the key is greater than \a otherKey. + Compares this key to \a otherKey. + + Returns a negative value if the key is less than \a otherKey, 0 if the key + is equal to \a otherKey or a positive value if the key is greater than \a + otherKey. \sa operator<() */ |