QSslSocket: use function objects and algorithms when sorting library paths

The first change is to make libGreaterThan a function object.
This allows all compilers to inline the comparison into the
sort implementation.

The second change is to use QString::splitRef instead of
QString::split to extract the components.

The third is to extract the element comparison into a
function object and replace the rest of libGreaterThan
with a suitable call to std::lexicographical_compare,
rendering most code comments (present or missing) moot.

Change-Id: I3a761d721aa7cf5fa727dcc4ddca4b922f413899
Reviewed-by: Richard J. Moore <rich@kde.org>
This commit is contained in:
Marc Mutz 2014-08-17 16:42:04 +02:00
parent 35eb5c8678
commit ed7f5375e1

View File

@ -443,39 +443,41 @@ bool q_resolveOpenSslSymbols()
#else
# ifdef Q_OS_UNIX
static bool libGreaterThan(const QString &lhs, const QString &rhs)
struct NumericallyLess
{
QStringList lhsparts = lhs.split(QLatin1Char('.'));
QStringList rhsparts = rhs.split(QLatin1Char('.'));
Q_ASSERT(lhsparts.count() > 1 && rhsparts.count() > 1);
for (int i = 1; i < rhsparts.count(); ++i) {
if (lhsparts.count() <= i)
// left hand side is shorter, so it's less than rhs
return false;
typedef bool result_type;
result_type operator()(const QStringRef &lhs, const QStringRef &rhs) const
{
bool ok = false;
int b = 0;
int a = lhsparts.at(i).toInt(&ok);
int a = lhs.toInt(&ok);
if (ok)
b = rhsparts.at(i).toInt(&ok);
b = rhs.toInt(&ok);
if (ok) {
// both toInt succeeded
if (a == b)
continue;
return a > b;
return a < b;
} else {
// compare as strings;
if (lhsparts.at(i) == rhsparts.at(i))
continue;
return lhsparts.at(i) > rhsparts.at(i);
return lhs < rhs;
}
}
};
// they compared strictly equally so far
// lhs cannot be less than rhs
return true;
}
struct LibGreaterThan
{
typedef bool result_type;
result_type operator()(const QString &lhs, const QString &rhs) const
{
const QVector<QStringRef> lhsparts = lhs.splitRef(QLatin1Char('.'));
const QVector<QStringRef> rhsparts = rhs.splitRef(QLatin1Char('.'));
Q_ASSERT(lhsparts.count() > 1 && rhsparts.count() > 1);
// note: checking rhs < lhs, the same as lhs > rhs
return std::lexicographical_compare(rhsparts.begin() + 1, rhsparts.end(),
lhsparts.begin() + 1, lhsparts.end(),
NumericallyLess());
}
};
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
static int dlIterateCallback(struct dl_phdr_info *info, size_t size, void *data)
@ -539,7 +541,7 @@ static QStringList findAllLibSsl()
QDir dir(path);
QStringList entryList = dir.entryList(QStringList() << QLatin1String("libssl.*"), QDir::Files);
std::sort(entryList.begin(), entryList.end(), libGreaterThan);
std::sort(entryList.begin(), entryList.end(), LibGreaterThan());
foreach (const QString &entry, entryList)
foundSsls << path + QLatin1Char('/') + entry;
}
@ -556,7 +558,7 @@ static QStringList findAllLibCrypto()
QDir dir(path);
QStringList entryList = dir.entryList(QStringList() << QLatin1String("libcrypto.*"), QDir::Files);
std::sort(entryList.begin(), entryList.end(), libGreaterThan);
std::sort(entryList.begin(), entryList.end(), LibGreaterThan());
foreach (const QString &entry, entryList)
foundCryptos << path + QLatin1Char('/') + entry;
}