Allow brace initialization for some of QLatin1StringView constructors

Removed "explicit" keyword from constructors taking
(const char *, qsizetype) and (const char *, const char *).

Switched to using brace initialization for creating QLatin1StringView
in QtCore.

[ChangeLog][QtCore][QLatin1StringView] The (const char *, qsizetype)
and (const char *, const char *) constructors are no longer explicit.

Change-Id: I4f6760692e4df60fe4231e86a25f6ea03cd1bf82
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Sona Kurazyan 2022-03-15 10:37:23 +01:00
parent 678548afe8
commit a3f7dd5260
6 changed files with 27 additions and 22 deletions

View File

@ -166,7 +166,7 @@ static inline QLatin1StringView stringDataView(const QMetaObject *mo, int index)
uint offset = mo->d.stringdata[2*index];
uint length = mo->d.stringdata[2*index + 1];
const char *string = reinterpret_cast<const char *>(mo->d.stringdata) + offset;
return QLatin1StringView(string, length);
return {string, qsizetype(length)};
}
static inline QByteArray stringData(const QMetaObject *mo, int index)

View File

@ -350,7 +350,7 @@ static QLatin1StringView findSectionName(const IMAGE_SECTION_HEADER *section, QB
n = qstrnlen(ptr, stringTable.size() - offset);
}
return QLatin1StringView(ptr, n);
return {ptr, n};
}
QLibraryScanResult QCoffPeParser::parse(QByteArrayView data, QString *errMsg)

View File

@ -115,7 +115,7 @@ struct ByteData
QString toUtf8String() const { return QString::fromUtf8(byte(), len); }
QByteArray asByteArrayView() const { return QByteArray::fromRawData(byte(), len); }
QLatin1StringView asLatin1() const { return QLatin1StringView(byte(), len); }
QLatin1StringView asLatin1() const { return {byte(), len}; }
QUtf8StringView asUtf8StringView() const { return QUtf8StringView(byte(), len); }
QStringView asStringView() const{ return QStringView(utf16(), len / 2); }
QString asQStringRaw() const { return QString::fromRawData(utf16(), len / 2); }

View File

@ -228,16 +228,16 @@ QLatin1StringView QLocalePrivate::languageToCode(QLocale::Language language,
const LanguageCodeEntry &i = languageCodeList[language];
if (codeTypes.testFlag(QLocale::ISO639Part1) && i.part1.isValid())
return QLatin1StringView(i.part1.code, 2);
return {i.part1.code, 2};
if (codeTypes.testFlag(QLocale::ISO639Part2B) && i.part2B.isValid())
return QLatin1StringView(i.part2B.code, 3);
return {i.part2B.code, 3};
if (codeTypes.testFlag(QLocale::ISO639Part2T) && i.part2T.isValid())
return QLatin1StringView(i.part2T.code, 3);
return {i.part2T.code, 3};
if (codeTypes.testFlag(QLocale::ISO639Part3))
return QLatin1StringView(i.part3.code, 3);
return {i.part3.code, 3};
return {};
}
@ -247,7 +247,7 @@ QLatin1StringView QLocalePrivate::scriptToCode(QLocale::Script script)
if (script == QLocale::AnyScript || script > QLocale::LastScript)
return {};
const unsigned char *c = script_code_list + 4 * script;
return QLatin1StringView(reinterpret_cast<const char *>(c), 4);
return {reinterpret_cast<const char *>(c), 4};
}
QLatin1StringView QLocalePrivate::territoryToCode(QLocale::Territory territory)
@ -256,7 +256,7 @@ QLatin1StringView QLocalePrivate::territoryToCode(QLocale::Territory territory)
return {};
const unsigned char *c = territory_code_list + 3 * territory;
return QLatin1StringView(reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3);
return {reinterpret_cast<const char*>(c), c[2] == 0 ? 2 : 3};
}
namespace {

View File

@ -100,9 +100,9 @@ public:
constexpr QLatin1StringView(std::nullptr_t) noexcept : QLatin1StringView() {}
constexpr inline explicit QLatin1StringView(const char *s) noexcept
: m_size(s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0), m_data(s) {}
constexpr explicit QLatin1StringView(const char *f, const char *l)
constexpr QLatin1StringView(const char *f, const char *l)
: QLatin1StringView(f, qsizetype(l - f)) {}
constexpr inline explicit QLatin1StringView(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
constexpr inline QLatin1StringView(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
explicit QLatin1StringView(const QByteArray &s) noexcept : m_size(s.size()), m_data(s.constData()) {}
constexpr explicit QLatin1StringView(QByteArrayView s) noexcept : m_size(s.size()), m_data(s.data()) {}
#else
@ -111,9 +111,9 @@ public:
constexpr QLatin1String(std::nullptr_t) noexcept : QLatin1String() {}
constexpr inline explicit QLatin1String(const char *s) noexcept
: m_size(s ? qsizetype(QtPrivate::lengthHelperPointer(s)) : 0), m_data(s) {}
constexpr explicit QLatin1String(const char *f, const char *l)
constexpr QLatin1String(const char *f, const char *l)
: QLatin1String(f, qsizetype(l - f)) {}
constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
constexpr inline QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}
explicit QLatin1String(const QByteArray &s) noexcept : m_size(s.size()), m_data(s.constData()) {}
constexpr explicit QLatin1String(QByteArrayView s) noexcept : m_size(s.size()), m_data(s.data()) {}
#endif // !Q_L1S_VIEW_IS_PRIMARY
@ -271,25 +271,25 @@ public:
{
if (size_t(n) >= size_t(size()))
n = size();
return QLatin1StringView(m_data, n);
return {m_data, n};
}
[[nodiscard]] constexpr QLatin1StringView right(qsizetype n) const
{
if (size_t(n) >= size_t(size()))
n = size();
return QLatin1StringView(m_data + m_size - n, n);
return {m_data + m_size - n, n};
}
[[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos) const
{ verify(pos); return QLatin1StringView(m_data + pos, m_size - pos); }
{ verify(pos); return {m_data + pos, m_size - pos}; }
[[nodiscard]] constexpr QLatin1StringView sliced(qsizetype pos, qsizetype n) const
{ verify(pos, n); return QLatin1StringView(m_data + pos, n); }
{ verify(pos, n); return {m_data + pos, n}; }
[[nodiscard]] constexpr QLatin1StringView first(qsizetype n) const
{ verify(n); return QLatin1StringView(m_data, n); }
{ verify(n); return {m_data, n}; }
[[nodiscard]] constexpr QLatin1StringView last(qsizetype n) const
{ verify(n); return QLatin1StringView(m_data + size() - n, n); }
{ verify(n); return {m_data + size() - n, n}; }
[[nodiscard]] constexpr QLatin1StringView chopped(qsizetype n) const
{ verify(n); return QLatin1StringView(m_data, size() - n); }
{ verify(n); return {m_data, size() - n}; }
constexpr void chop(qsizetype n)
{ verify(n); m_size -= n; }
@ -440,7 +440,7 @@ constexpr QAnyStringView::QAnyStringView(QLatin1StringView str) noexcept
constexpr QLatin1StringView QAnyStringView::asLatin1StringView() const
{
Q_ASSERT(isLatin1());
return QLatin1StringView{m_data_utf8, int(size())};
return {m_data_utf8, int(size())};
}
template <typename Visitor>
@ -1671,7 +1671,7 @@ inline namespace StringLiterals {
constexpr inline QLatin1StringView operator"" _L1(const char *str, size_t size) noexcept
{
return QLatin1StringView(str, qsizetype(size));
return {str, qsizetype(size)};
}
} // StringLiterals

View File

@ -126,6 +126,11 @@ void tst_QLatin1StringView::construction()
QCOMPARE(l1s.latin1(), reinterpret_cast<const void *>(&str[0]));
QCOMPARE(l1s.latin1(), "hello");
QLatin1StringView s1 = {str, 5};
QCOMPARE(s1, l1s);
QLatin1StringView s2 = {str, str + 5};
QCOMPARE(s2, l1s);
QByteArrayView helloView(str);
helloView = helloView.first(4);
l1s = QLatin1StringView(helloView);