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:
parent
35eb5c8678
commit
ed7f5375e1
@ -443,39 +443,41 @@ bool q_resolveOpenSslSymbols()
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
# ifdef Q_OS_UNIX
|
# ifdef Q_OS_UNIX
|
||||||
static bool libGreaterThan(const QString &lhs, const QString &rhs)
|
struct NumericallyLess
|
||||||
{
|
{
|
||||||
QStringList lhsparts = lhs.split(QLatin1Char('.'));
|
typedef bool result_type;
|
||||||
QStringList rhsparts = rhs.split(QLatin1Char('.'));
|
result_type operator()(const QStringRef &lhs, const QStringRef &rhs) const
|
||||||
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;
|
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
int b = 0;
|
int b = 0;
|
||||||
int a = lhsparts.at(i).toInt(&ok);
|
int a = lhs.toInt(&ok);
|
||||||
if (ok)
|
if (ok)
|
||||||
b = rhsparts.at(i).toInt(&ok);
|
b = rhs.toInt(&ok);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
// both toInt succeeded
|
// both toInt succeeded
|
||||||
if (a == b)
|
return a < b;
|
||||||
continue;
|
|
||||||
return a > b;
|
|
||||||
} else {
|
} else {
|
||||||
// compare as strings;
|
// compare as strings;
|
||||||
if (lhsparts.at(i) == rhsparts.at(i))
|
return lhs < rhs;
|
||||||
continue;
|
|
||||||
return lhsparts.at(i) > rhsparts.at(i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// they compared strictly equally so far
|
struct LibGreaterThan
|
||||||
// lhs cannot be less than rhs
|
{
|
||||||
return true;
|
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)
|
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
|
||||||
static int dlIterateCallback(struct dl_phdr_info *info, size_t size, void *data)
|
static int dlIterateCallback(struct dl_phdr_info *info, size_t size, void *data)
|
||||||
@ -539,7 +541,7 @@ static QStringList findAllLibSsl()
|
|||||||
QDir dir(path);
|
QDir dir(path);
|
||||||
QStringList entryList = dir.entryList(QStringList() << QLatin1String("libssl.*"), QDir::Files);
|
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)
|
foreach (const QString &entry, entryList)
|
||||||
foundSsls << path + QLatin1Char('/') + entry;
|
foundSsls << path + QLatin1Char('/') + entry;
|
||||||
}
|
}
|
||||||
@ -556,7 +558,7 @@ static QStringList findAllLibCrypto()
|
|||||||
QDir dir(path);
|
QDir dir(path);
|
||||||
QStringList entryList = dir.entryList(QStringList() << QLatin1String("libcrypto.*"), QDir::Files);
|
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)
|
foreach (const QString &entry, entryList)
|
||||||
foundCryptos << path + QLatin1Char('/') + entry;
|
foundCryptos << path + QLatin1Char('/') + entry;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user