qlibraryinfo.cpp: use qOffsetStringArray for qtConfEntries
Beats a manual array with too wide strings. I thought even to simply replace this with a switch (loc)... it's not like this is performance-critical code, given it uses QString. Change-Id: I2bbf422288924c198645fffd16a977778ff8d52d Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
parent
95cc652e1a
commit
2c2c6de85a
@ -304,35 +304,6 @@ QVersionNumber QLibraryInfo::version() noexcept
|
|||||||
return QVersionNumber(QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH);
|
return QVersionNumber(QT_VERSION_MAJOR, QT_VERSION_MINOR, QT_VERSION_PATCH);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* To add a new entry in QLibraryInfo::LibraryPath, add it to the enum
|
|
||||||
* in qtbase/src/corelib/global/qlibraryinfo.h and:
|
|
||||||
* - add its relative path in the qtConfEntries[] array below
|
|
||||||
* (the key is what appears in a qt.conf file)
|
|
||||||
*/
|
|
||||||
|
|
||||||
static const struct {
|
|
||||||
char key[19], value[13];
|
|
||||||
} qtConfEntries[] = {
|
|
||||||
{ "Prefix", "." },
|
|
||||||
{ "Documentation", "doc" }, // should be ${Data}/doc
|
|
||||||
{ "Headers", "include" },
|
|
||||||
{ "Libraries", "lib" },
|
|
||||||
#ifdef Q_OS_WIN
|
|
||||||
{ "LibraryExecutables", "bin" },
|
|
||||||
#else
|
|
||||||
{ "LibraryExecutables", "libexec" }, // should be ${ArchData}/libexec
|
|
||||||
#endif
|
|
||||||
{ "Binaries", "bin" },
|
|
||||||
{ "Plugins", "plugins" }, // should be ${ArchData}/plugins
|
|
||||||
{ "Qml2Imports", "qml" }, // should be ${ArchData}/qml
|
|
||||||
{ "ArchData", "." },
|
|
||||||
{ "Data", "." },
|
|
||||||
{ "Translations", "translations" }, // should be ${Data}/translations
|
|
||||||
{ "Examples", "examples" },
|
|
||||||
{ "Tests", "tests" },
|
|
||||||
};
|
|
||||||
|
|
||||||
static QString prefixFromAppDirHelper()
|
static QString prefixFromAppDirHelper()
|
||||||
{
|
{
|
||||||
QString appDir;
|
QString appDir;
|
||||||
@ -515,17 +486,44 @@ static QString getPrefix()
|
|||||||
void QLibraryInfoPrivate::keyAndDefault(QLibraryInfo::LibraryPath loc, QString *key,
|
void QLibraryInfoPrivate::keyAndDefault(QLibraryInfo::LibraryPath loc, QString *key,
|
||||||
QString *value)
|
QString *value)
|
||||||
{
|
{
|
||||||
if (unsigned(loc) < sizeof(qtConfEntries)/sizeof(qtConfEntries[0])) {
|
/*
|
||||||
*key = QLatin1String(qtConfEntries[loc].key);
|
* To add a new entry in QLibraryInfo::LibraryPath, add it to the enum
|
||||||
*value = QLatin1String(qtConfEntries[loc].value);
|
* in qtbase/src/corelib/global/qlibraryinfo.h and:
|
||||||
}
|
* - add its relative path in the qtConfEntries[] array below
|
||||||
#ifndef Q_OS_WIN // On Windows we use the registry
|
* (the key is what appears in a qt.conf file)
|
||||||
else if (loc == QLibraryInfo::SettingsPath) {
|
*/
|
||||||
*key = QLatin1String("Settings");
|
static constexpr auto qtConfEntries = qOffsetStringArray(
|
||||||
*value = QLatin1String(".");
|
"Prefix", ".",
|
||||||
}
|
"Documentation", "doc", // should be ${Data}/doc
|
||||||
|
"Headers", "include",
|
||||||
|
"Libraries", "lib",
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
"LibraryExecutables", "bin",
|
||||||
|
#else
|
||||||
|
"LibraryExecutables", "libexec", // should be ${ArchData}/libexec
|
||||||
#endif
|
#endif
|
||||||
else {
|
"Binaries", "bin",
|
||||||
|
"Plugins", "plugins", // should be ${ArchData}/plugins
|
||||||
|
"Qml2Imports", "qml", // should be ${ArchData}/qml
|
||||||
|
"ArchData", ".",
|
||||||
|
"Data", ".",
|
||||||
|
"Translations", "translations", // should be ${Data}/translations
|
||||||
|
"Examples", "examples",
|
||||||
|
"Tests", "tests"
|
||||||
|
);
|
||||||
|
static constexpr QByteArrayView dot = qtConfEntries.viewAt(1);
|
||||||
|
static_assert(dot.size() == 1);
|
||||||
|
static_assert(dot[0] == '.');
|
||||||
|
|
||||||
|
if (int(loc) < qtConfEntries.count()) {
|
||||||
|
*key = QLatin1String(qtConfEntries.viewAt(loc * 2));
|
||||||
|
*value = QLatin1String(qtConfEntries.viewAt(loc * 2 + 1));
|
||||||
|
#ifndef Q_OS_WIN // On Windows we use the registry
|
||||||
|
} else if (loc == QLibraryInfo::SettingsPath) {
|
||||||
|
*key = QLatin1String("Settings");
|
||||||
|
*value = QLatin1String(dot);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
key->clear();
|
key->clear();
|
||||||
value->clear();
|
value->clear();
|
||||||
}
|
}
|
||||||
|
@ -54,9 +54,11 @@
|
|||||||
|
|
||||||
#include "private/qglobal_p.h"
|
#include "private/qglobal_p.h"
|
||||||
|
|
||||||
|
#include <QByteArrayView>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
class tst_QOffsetStringArray;
|
class tst_QOffsetStringArray;
|
||||||
@ -80,7 +82,7 @@ public:
|
|||||||
|
|
||||||
constexpr const char *operator[](const int index) const noexcept
|
constexpr const char *operator[](const int index) const noexcept
|
||||||
{
|
{
|
||||||
return m_string.data() + m_offsets[qBound(int(0), index, count() - 1)];
|
return m_string.data() + m_offsets[qBound(int(0), index, count())];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const char *at(const int index) const noexcept
|
constexpr const char *at(const int index) const noexcept
|
||||||
@ -88,7 +90,13 @@ public:
|
|||||||
return m_string.data() + m_offsets[index];
|
return m_string.data() + m_offsets[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr int count() const { return int(m_offsets.size()); }
|
constexpr QByteArrayView viewAt(qsizetype index) const noexcept
|
||||||
|
{
|
||||||
|
return { m_string.data() + m_offsets[index],
|
||||||
|
qsizetype(m_offsets[index + 1]) - qsizetype(m_offsets[index]) - 1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr int count() const { return int(m_offsets.size()) - 1; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StaticString m_string;
|
StaticString m_string;
|
||||||
@ -162,9 +170,9 @@ constexpr auto qOffsetStringArray(StringExtractor extractString, const T &... en
|
|||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
std::array fullOffsetList = { offset += sizeof(extractString(T{}))... };
|
std::array fullOffsetList = { offset += sizeof(extractString(T{}))... };
|
||||||
|
|
||||||
// prepend zero, drop last element
|
// prepend zero
|
||||||
std::array<MinifiedOffsetType, Count> minifiedOffsetList = {};
|
std::array<MinifiedOffsetType, Count + 1> minifiedOffsetList = {};
|
||||||
QtPrivate::copyData(fullOffsetList.begin(), Count - 1, minifiedOffsetList.begin() + 1);
|
QtPrivate::copyData(fullOffsetList.begin(), Count, minifiedOffsetList.begin() + 1);
|
||||||
|
|
||||||
std::array staticString = QtPrivate::makeStaticString<StringLength>(extractString, entries...);
|
std::array staticString = QtPrivate::makeStaticString<StringLength>(extractString, entries...);
|
||||||
return QOffsetStringArray(staticString, minifiedOffsetList);
|
return QOffsetStringArray(staticString, minifiedOffsetList);
|
||||||
|
@ -46,8 +46,7 @@ constexpr const auto messages = qOffsetStringArray(
|
|||||||
"level - 1",
|
"level - 1",
|
||||||
"level - 2",
|
"level - 2",
|
||||||
"level - 3",
|
"level - 3",
|
||||||
"level - 4",
|
"level - 4"
|
||||||
""
|
|
||||||
);
|
);
|
||||||
|
|
||||||
constexpr const auto messages257 = qOffsetStringArray(
|
constexpr const auto messages257 = qOffsetStringArray(
|
||||||
@ -90,15 +89,15 @@ constexpr const auto messagesBigOffsets = qOffsetStringArray(
|
|||||||
|
|
||||||
void tst_QOffsetStringArray::init()
|
void tst_QOffsetStringArray::init()
|
||||||
{
|
{
|
||||||
static_assert(messages.m_string.size() == 51);
|
static_assert(messages.m_string.size() == 50);
|
||||||
static_assert(messages.m_offsets.size() == 6);
|
static_assert(messages.m_offsets.size() == 6);
|
||||||
static_assert(std::is_same_v<decltype(messages.m_offsets)::value_type, quint8>);
|
static_assert(std::is_same_v<decltype(messages.m_offsets)::value_type, quint8>);
|
||||||
|
|
||||||
static_assert(messages257.m_offsets.size() == 257);
|
static_assert(messages257.m_offsets.size() == 258);
|
||||||
static_assert(messages257.m_string.size() == 260);
|
static_assert(messages257.m_string.size() == 260);
|
||||||
static_assert(std::is_same_v<decltype(messages257.m_offsets)::value_type, quint16>);
|
static_assert(std::is_same_v<decltype(messages257.m_offsets)::value_type, quint16>);
|
||||||
|
|
||||||
static_assert(messagesBigOffsets.m_offsets.size() == 4);
|
static_assert(messagesBigOffsets.m_offsets.size() == 5);
|
||||||
static_assert(messagesBigOffsets.m_string.size() == 364);
|
static_assert(messagesBigOffsets.m_string.size() == 364);
|
||||||
static_assert(std::is_same_v<decltype(messagesBigOffsets.m_offsets)::value_type, quint16>);
|
static_assert(std::is_same_v<decltype(messagesBigOffsets.m_offsets)::value_type, quint16>);
|
||||||
}
|
}
|
||||||
@ -110,6 +109,7 @@ void tst_QOffsetStringArray::access()
|
|||||||
QCOMPARE(messages[2], "level - 2");
|
QCOMPARE(messages[2], "level - 2");
|
||||||
QCOMPARE(messages[3], "level - 3");
|
QCOMPARE(messages[3], "level - 3");
|
||||||
QCOMPARE(messages[4], "level - 4");
|
QCOMPARE(messages[4], "level - 4");
|
||||||
|
// out of bounds returns empty strings:
|
||||||
QCOMPARE(messages[5], "");
|
QCOMPARE(messages[5], "");
|
||||||
QCOMPARE(messages[6], "");
|
QCOMPARE(messages[6], "");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user