Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use QCollator for sort library. #1943

Merged
merged 4 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/library/basetrackcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ int BaseTrackCache::compareColumnValues(int sortColumn, Qt::SortOrder sortOrder,
result = 0;
}
} else {
result = val1.toString().localeAwareCompare(val2.toString());
result = m_collator.compare(val1.toString(), val2.toString());
}

// If we're in descending order, flip the comparison.
Expand Down
3 changes: 3 additions & 0 deletions src/library/basetrackcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "track/track.h"
#include "util/class.h"
#include "util/memory.h"
#include "util/string.h"

class SearchQueryParser;
class QueryNode;
Expand Down Expand Up @@ -122,6 +123,8 @@ class BaseTrackCache : public QObject {

const ColumnCache m_columnCache;

const StringCollator m_collator;

QStringList m_searchColumns;
QVector<int> m_searchColumnIndices;

Expand Down
20 changes: 7 additions & 13 deletions src/util/db/dbconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ void removeDatabase(
QSqlDatabase::removeDatabase(connectionName);
}

// The default comparison of strings for sorting.
inline int compareLocaleAwareCaseInsensitive(
const QString& first, const QString& second) {
return QString::localeAwareCompare(first.toLower(), second.toLower());
}

void makeLatinLow(QChar* c, int count) {
for (int i = 0; i < count; ++i) {
if (c[i].decompositionTag() != QChar::NoDecomposition) {
Expand Down Expand Up @@ -181,13 +175,13 @@ int likeCompareInner(
int sqliteStringCompareUTF16(void* pArg,
int len1, const void* data1,
int len2, const void* data2) {
Q_UNUSED(pArg);
StringCollator* pCollator = static_cast<StringCollator*>(pArg);
// Construct a QString without copy
QString string1 = QString::fromRawData(reinterpret_cast<const QChar*>(data1),
QString string1 = QString::fromRawData(static_cast<const QChar*>(data1),
len1 / sizeof(QChar));
QString string2 = QString::fromRawData(reinterpret_cast<const QChar*>(data2),
QString string2 = QString::fromRawData(static_cast<const QChar*>(data2),
len2 / sizeof(QChar));
return compareLocaleAwareCaseInsensitive(string1, string2);
return pCollator->compare(string1, string2);
}

const char* const kLexicographicalCollationFunc = "mixxxLexicographicalCollationFunc";
Expand Down Expand Up @@ -234,7 +228,7 @@ void sqliteLike(sqlite3_context *context,

#endif // __SQLITE3__

bool initDatabase(QSqlDatabase database) {
bool initDatabase(QSqlDatabase database, StringCollator* pCollator) {
DEBUG_ASSERT(database.isOpen());
#ifdef __SQLITE3__
QVariant v = database.driver()->handle();
Expand All @@ -260,7 +254,7 @@ bool initDatabase(QSqlDatabase database) {
handle,
kLexicographicalCollationFunc,
SQLITE_UTF16,
nullptr,
pCollator,
sqliteStringCompareUTF16);
VERIFY_OR_DEBUG_ASSERT(result == SQLITE_OK) {
kLogger.warning()
Expand Down Expand Up @@ -331,7 +325,7 @@ bool DbConnection::open() {
<< m_sqlDatabase.lastError();
return false; // abort
}
if (!initDatabase(m_sqlDatabase)) {
if (!initDatabase(m_sqlDatabase, &m_collator)) {
kLogger.warning()
<< "Failed to initialize database connection"
<< *this;
Expand Down
3 changes: 2 additions & 1 deletion src/util/db/dbconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@


#include <QSqlDatabase>

#include <QtDebug>

#include "util/string.h"

namespace mixxx {

Expand Down Expand Up @@ -63,6 +63,7 @@ class DbConnection final {
DbConnection(const DbConnection&&) = delete;

QSqlDatabase m_sqlDatabase;
StringCollator m_collator;
};

} // namespace mixxx
Expand Down
25 changes: 21 additions & 4 deletions src/util/string.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
#ifndef MIXXX_STRING_H
#define MIXXX_STRING_H

#include <QLocale>
#include <QCollator>
#include <QString>
#include <QStringRef>

// The default comparison of strings for sorting.
inline int compareLocaleAwareCaseInsensitive(
const QString& first, const QString& second) {
return QString::localeAwareCompare(first.toLower(), second.toLower());
}
class StringCollator {
public:
explicit StringCollator(QLocale locale = QLocale())
: m_collator(std::move(locale)) {
m_collator.setCaseSensitivity(Qt::CaseInsensitive);
}

int compare(const QString& s1, const QString& s2) const {
return m_collator.compare(s1, s2);
}

int compare(const QStringRef& s1, const QStringRef& s2) const {
return m_collator.compare(s1, s2);
}

private:
QCollator m_collator;
};

#endif // MIXXX_STRING_H