1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QCOLLATOR_H
6#define QCOLLATOR_H
7
8#include <QtCore/qstring.h>
9#include <QtCore/qstringlist.h>
10#include <QtCore/qlocale.h>
11
12QT_BEGIN_NAMESPACE
13
14class QCollatorPrivate;
15class QCollatorSortKeyPrivate;
16
17class Q_CORE_EXPORT QCollatorSortKey
18{
19 friend class QCollator;
20public:
21 QCollatorSortKey(const QCollatorSortKey &other);
22 ~QCollatorSortKey();
23 QCollatorSortKey &operator=(const QCollatorSortKey &other);
24 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QCollatorSortKey)
25 void swap(QCollatorSortKey &other) noexcept
26 { d.swap(other&: other.d); }
27
28 int compare(const QCollatorSortKey &key) const;
29 friend bool operator<(const QCollatorSortKey &lhs, const QCollatorSortKey &rhs)
30 { return lhs.compare(key: rhs) < 0; }
31
32protected:
33 QCollatorSortKey(QCollatorSortKeyPrivate*);
34
35 QExplicitlySharedDataPointer<QCollatorSortKeyPrivate> d;
36
37private:
38 QCollatorSortKey();
39};
40
41class Q_CORE_EXPORT QCollator
42{
43public:
44 QCollator();
45 explicit QCollator(const QLocale &locale);
46 QCollator(const QCollator &);
47 ~QCollator();
48 QCollator &operator=(const QCollator &);
49 QCollator(QCollator &&other) noexcept
50 : d(other.d) { other.d = nullptr; }
51 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCollator)
52
53 void swap(QCollator &other) noexcept
54 { qt_ptr_swap(lhs&: d, rhs&: other.d); }
55
56 void setLocale(const QLocale &locale);
57 QLocale locale() const;
58
59 Qt::CaseSensitivity caseSensitivity() const;
60 void setCaseSensitivity(Qt::CaseSensitivity cs);
61
62 void setNumericMode(bool on);
63 bool numericMode() const;
64
65 void setIgnorePunctuation(bool on);
66 bool ignorePunctuation() const;
67
68 int compare(const QString &s1, const QString &s2) const
69 { return compare(s1: QStringView(s1), s2: QStringView(s2)); }
70#if QT_CORE_REMOVED_SINCE(6, 4) && QT_POINTER_SIZE != 4
71 int compare(const QChar *s1, int len1, const QChar *s2, int len2) const
72 { return compare(QStringView(s1, len1), QStringView(s2, len2)); }
73#endif
74 int compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
75 { return compare(s1: QStringView(s1, len1), s2: QStringView(s2, len2)); }
76
77 bool operator()(const QString &s1, const QString &s2) const
78 { return compare(s1, s2) < 0; }
79 int compare(QStringView s1, QStringView s2) const;
80
81 bool operator()(QStringView s1, QStringView s2) const
82 { return compare(s1, s2) < 0; }
83
84 QCollatorSortKey sortKey(const QString &string) const;
85
86 static int defaultCompare(QStringView s1, QStringView s2);
87 static QCollatorSortKey defaultSortKey(QStringView key);
88
89private:
90 QCollatorPrivate *d;
91
92 void detach();
93};
94
95Q_DECLARE_SHARED(QCollatorSortKey)
96Q_DECLARE_SHARED(QCollator)
97
98QT_END_NAMESPACE
99
100#endif // QCOLLATOR_P_H
101

source code of qtbase/src/corelib/text/qcollator.h