Skip to content

Commit

Permalink
Merge pull request #1943 from daschuer/qcollator
Browse files Browse the repository at this point in the history
Use QCollator for sort library.
  • Loading branch information
daschuer authored Dec 12, 2018
2 parents 457ad4a + c225a27 commit 0cab853
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
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

0 comments on commit 0cab853

Please sign in to comment.