From d631c31235dc3ab682ba5d4cbb466680d47b36f2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 20 Oct 2011 12:47:04 +0200 Subject: Make QStringLiteral and QByteArrayLiteral always return the real types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Up until now, the macros would return an internal type that contained the pointer to the data. This breaks code that tried to use the macros with operators, like QStringBuilder but also when writing: QStringList() << QStringLiteral("a") << QStringLiteral("b"); This change seems to work fine now and I can also verify that this works: const auto str = QStringLiteral("Hello"); Even though it creates a QString, which is non-POD and non-constexpr. Change-Id: Iaf82af9bea4245513a1128ea54f9d2d3d785fb09 Reviewed-by: Olivier Goffart Reviewed-by: Jędrzej Nowacki --- src/corelib/tools/qstring.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/corelib/tools/qstring.h') diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index edb140b682..a96046d837 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -113,13 +113,14 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, # if defined(Q_COMPILER_LAMBDA) # define QStringLiteral(str) \ - ([]() -> QStringDataPtr { \ + ([]() -> QString { \ enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \ static const QStaticStringData qstring_literal = { \ Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ QT_UNICODE_LITERAL(str) }; \ QStringDataPtr holder = { qstring_literal.data_ptr() }; \ - return holder; \ + const QString s(holder); \ + return s; \ }()) \ /**/ @@ -129,14 +130,14 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, // To do that, we need the __extension__ {( )} trick which only GCC supports # define QStringLiteral(str) \ - __extension__ ({ \ + QString(__extension__ ({ \ enum { Size = sizeof(QT_UNICODE_LITERAL(str))/2 - 1 }; \ static const QStaticStringData qstring_literal = { \ Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ QT_UNICODE_LITERAL(str) }; \ QStringDataPtr holder = { qstring_literal.data_ptr() }; \ holder; \ - }) \ + })) \ /**/ # endif @@ -144,9 +145,10 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, #ifndef QStringLiteral // no lambdas, not GCC, or GCC in C++98 mode with 4-byte wchar_t -// fallback, uses QLatin1String as next best options +// fallback, return a temporary QString +// source code is assumed to be encoded in UTF-8 -# define QStringLiteral(str) QLatin1String(str) +# define QStringLiteral(str) QString::fromUtf8(str, sizeof(str) - 1) #endif #define Q_STATIC_STRING_DATA_HEADER_INITIALIZER_WITH_OFFSET(size, offset) \ -- cgit v1.2.3 From e92e5fda44602d7595aca329a3133ecb9991a6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Sun, 1 Apr 2012 01:18:27 +0200 Subject: Migrate QString over to QArrayData MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ieadc60523a2bef61a088920576c65c720b11bfb9 Reviewed-by: Jędrzej Nowacki Reviewed-by: Thiago Macieira --- src/corelib/tools/qstring.h | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'src/corelib/tools/qstring.h') diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index a96046d837..9fcd203a3f 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -69,17 +69,7 @@ class QLatin1String; class QStringRef; template class QVector; -struct QStringData { - QtPrivate::RefCount ref; - int size; - uint alloc : 31; - uint capacityReserved : 1; - - qptrdiff offset; - - inline ushort *data() { return reinterpret_cast(reinterpret_cast(this) + offset); } - inline const ushort *data() const { return reinterpret_cast(reinterpret_cast(this) + offset); } -}; +typedef QTypedArrayData QStringData; #if defined(Q_COMPILER_UNICODE_STRINGS) @@ -162,13 +152,13 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, template struct QStaticStringData { - QStringData str; + QArrayData str; qunicodechar data[N + 1]; QStringData *data_ptr() const { Q_ASSERT(str.ref.isStatic()); - return const_cast(&str); + return const_cast(static_cast(&str)); } }; @@ -621,9 +611,9 @@ public: // compatibility struct Null { }; static const Null null; - inline QString(const Null &): d(shared_null.data_ptr()) {} + inline QString(const Null &): d(Data::sharedNull()) {} inline QString &operator=(const Null &) { *this = QString(); return *this; } - inline bool isNull() const { return d == &shared_null.str; } + inline bool isNull() const { return d == Data::sharedNull(); } bool isSimpleText() const; @@ -642,11 +632,8 @@ private: QString &operator=(const QByteArray &a); #endif - static const QStaticStringData<1> shared_null; - static const QStaticStringData<1> shared_empty; Data *d; - static void free(Data *); void reallocData(uint alloc, bool grow = false); void expand(int i); void updateProperties() const; @@ -903,8 +890,8 @@ inline void QCharRef::setRow(uchar arow) { QChar(*this).setRow(arow); } inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); } -inline QString::QString() : d(shared_null.data_ptr()) {} -inline QString::~QString() { if (!d->ref.deref()) free(d); } +inline QString::QString() : d(Data::sharedNull()) {} +inline QString::~QString() { if (!d->ref.deref()) Data::deallocate(d); } inline void QString::reserve(int asize) { @@ -1179,7 +1166,7 @@ public: inline const QChar *unicode() const { if (!m_string) - return reinterpret_cast(QString::shared_null.str.data()); + return reinterpret_cast(QString::Data::sharedNull()->data()); return m_string->unicode() + m_position; } inline const QChar *data() const { return unicode(); } -- cgit v1.2.3