Fix QLatin1String(const char *) constructor to be constexpr

strlen() is not constexpr, use the QByteArrayView's
lengthHelperPointer() instead.

Change-Id: Ie49236edba3306e951402e6b776c15068cac0332
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Sona Kurazyan 2022-02-24 15:37:59 +01:00
parent 736213bf66
commit 62c7145390
2 changed files with 11 additions and 8 deletions

View File

@ -100,6 +100,13 @@ struct IsContainerCompatibleWithQByteArrayView<T, std::enable_if_t<
// Don't make an accidental copy constructor // Don't make an accidental copy constructor
std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {}; std::negation<std::is_same<std::decay_t<T>, QByteArrayView>>>>> : std::true_type {};
// Used by QLatin1String too
template <typename Char>
static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
{
return qsizetype(std::char_traits<Char>::length(data));
}
} // namespace QtPrivate } // namespace QtPrivate
class Q_CORE_EXPORT QByteArrayView class Q_CORE_EXPORT QByteArrayView
@ -138,12 +145,6 @@ private:
typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value, typename std::enable_if_t<QtPrivate::IsContainerCompatibleWithQByteArrayView<T>::value,
bool>; bool>;
template <typename Char>
static constexpr qsizetype lengthHelperPointer(const Char *data) noexcept
{
return qsizetype(std::char_traits<Char>::length(data));
}
template <typename Container> template <typename Container>
static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
{ {
@ -185,7 +186,7 @@ public:
template <typename Pointer, if_compatible_pointer<Pointer> = true> template <typename Pointer, if_compatible_pointer<Pointer> = true>
constexpr QByteArrayView(const Pointer &data) noexcept constexpr QByteArrayView(const Pointer &data) noexcept
: QByteArrayView( : QByteArrayView(
data, data ? lengthHelperPointer(data) : 0) {} data, data ? QtPrivate::lengthHelperPointer(data) : 0) {}
#endif #endif
#ifdef Q_QDOC #ifdef Q_QDOC

View File

@ -49,6 +49,7 @@
#include <QtCore/qchar.h> #include <QtCore/qchar.h>
#include <QtCore/qbytearray.h> #include <QtCore/qbytearray.h>
#include <QtCore/qbytearrayview.h>
#include <QtCore/qarraydata.h> #include <QtCore/qarraydata.h>
#include <QtCore/qnamespace.h> #include <QtCore/qnamespace.h>
#include <QtCore/qstringliteral.h> #include <QtCore/qstringliteral.h>
@ -85,7 +86,8 @@ class QLatin1String
public: public:
constexpr inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {} constexpr inline QLatin1String() noexcept : m_size(0), m_data(nullptr) {}
constexpr QLatin1String(std::nullptr_t) noexcept : QLatin1String() {} constexpr QLatin1String(std::nullptr_t) noexcept : QLatin1String() {}
constexpr inline explicit QLatin1String(const char *s) noexcept : m_size(s ? qsizetype(strlen(s)) : 0), m_data(s) {} 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 explicit QLatin1String(const char *f, const char *l)
: QLatin1String(f, qsizetype(l - f)) {} : QLatin1String(f, qsizetype(l - f)) {}
constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {} constexpr inline explicit QLatin1String(const char *s, qsizetype sz) noexcept : m_size(sz), m_data(s) {}