diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-05-03 15:43:10 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-03 16:11:58 +0200 |
commit | 6dac3f18941bd2d66218ec43f08e4fb9be869c63 (patch) | |
tree | f2664c9f31d5046f38a1403f46eb0fc162d5242c /src/corelib/tools | |
parent | b4dabb9e50ce26469b0b7d71e168463abf727334 (diff) | |
parent | 1e49914fee099c4c0d634743326b50ad02e6c8f1 (diff) |
Merge "Merge remote-tracking branch 'origin/api_changes'" into refs/staging/master
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qbytearray.h | 13 | ||||
-rw-r--r-- | src/corelib/tools/qstring.cpp | 85 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 43 | ||||
-rw-r--r-- | src/corelib/tools/qstringbuilder.h | 31 |
4 files changed, 47 insertions, 125 deletions
diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 45be63aa9b..0f5ebcfb18 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -169,13 +169,14 @@ struct QByteArrayDataPtr #if defined(Q_COMPILER_LAMBDA) # define QByteArrayLiteral(str) \ - ([]() -> QByteArrayDataPtr { \ + ([]() -> QByteArray { \ enum { Size = sizeof(str) - 1 }; \ static const QStaticByteArrayData<Size> qbytearray_literal = { \ Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(Size), \ str }; \ QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \ - return holder; \ + const QByteArray ba(holder); \ + return ba; \ }()) \ /**/ @@ -185,22 +186,22 @@ struct QByteArrayDataPtr // To do that, we need the __extension__ {( )} trick which only GCC supports # define QByteArrayLiteral(str) \ - __extension__ ({ \ + QByteArray(__extension__ ({ \ enum { Size = sizeof(str) - 1 }; \ static const QStaticByteArrayData<Size> qbytearray_literal = { \ Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(Size), \ str }; \ QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; \ holder; \ - }) \ + })) \ /**/ #endif #ifndef QByteArrayLiteral -// no lambdas, not GCC, use const char * instead +// no lambdas, not GCC, just return a temporary QByteArray -# define QByteArrayLiteral(str) (str) +# define QByteArrayLiteral(str) QByteArray(str, sizeof(str) - 1) #endif class Q_CORE_EXPORT QByteArray diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index d913606f5b..fd0d1286f6 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -794,9 +794,6 @@ const QString::Null QString::null = { }; \sa split() */ -const QStaticStringData<1> QString::shared_null = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } }; -const QStaticStringData<1> QString::shared_empty = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } }; - /*! \typedef QString::ConstIterator Qt-style synonym for QString::const_iterator. @@ -1037,7 +1034,7 @@ int QString::toUcs4_helper(const ushort *uc, int length, uint *out) QString::QString(const QChar *unicode, int size) { if (!unicode) { - d = shared_null.data_ptr(); + d = Data::sharedNull(); } else { if (size < 0) { size = 0; @@ -1045,15 +1042,11 @@ QString::QString(const QChar *unicode, int size) ++size; } if (!size) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); memcpy(d->data(), unicode, size * sizeof(QChar)); d->data()[size] = '\0'; } @@ -1069,15 +1062,11 @@ QString::QString(const QChar *unicode, int size) QString::QString(int size, QChar ch) { if (size <= 0) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; ushort *i = d->data() + size; ushort *b = d->data(); @@ -1095,13 +1084,9 @@ QString::QString(int size, QChar ch) */ QString::QString(int size, Qt::Initialization) { - d = (Data*) ::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar)); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; } @@ -1117,13 +1102,9 @@ QString::QString(int size, Qt::Initialization) */ QString::QString(QChar ch) { - d = (Data *) ::malloc(sizeof(Data) + 2*sizeof(QChar)); + d = Data::allocate(2); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = 1; - d->alloc = 2u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[0] = ch.unicode(); d->data()[1] = '\0'; } @@ -1180,12 +1161,6 @@ QString::QString(QChar ch) \internal */ -// ### Qt 5: rename freeData() to avoid confusion. See task 197625. -void QString::free(Data *d) -{ - ::free(d); -} - /*! Sets the size of the string to \a size characters. @@ -1227,9 +1202,9 @@ void QString::resize(int size) } if (size == 0 && !d->capacityReserved) { - Data *x = shared_empty.data_ptr(); + Data *x = Data::allocate(0); if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = x; } else { if (d->ref.isShared() || uint(size) + 1u > d->alloc @@ -1299,17 +1274,14 @@ void QString::reallocData(uint alloc, bool grow) alloc = qAllocMore(alloc * sizeof(QChar), sizeof(Data)) / sizeof(QChar); if (d->ref.isShared() || IS_RAW_DATA(d)) { - Data *x = static_cast<Data *>(::malloc(sizeof(Data) + alloc * sizeof(QChar))); + Data::AllocationOptions allocOptions(d->capacityReserved ? Data::CapacityReserved : 0); + Data *x = Data::allocate(alloc, allocOptions); Q_CHECK_PTR(x); - x->ref.initializeOwned(); x->size = qMin(int(alloc) - 1, d->size); - x->alloc = alloc; - x->capacityReserved = d->capacityReserved; - x->offset = sizeof(QStringData); ::memcpy(x->data(), d->data(), x->size * sizeof(QChar)); x->data()[x->size] = 0; if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = x; } else { Data *p = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc * sizeof(QChar))); @@ -1349,7 +1321,7 @@ QString &QString::operator=(const QString &other) { other.d->ref.ref(); if (!d->ref.deref()) - QString::free(d); + Data::deallocate(d); d = other.d; return *this; } @@ -1520,8 +1492,8 @@ QString& QString::insert(int i, QChar ch) */ QString &QString::append(const QString &str) { - if (str.d != &shared_null.str) { - if (d == &shared_null.str) { + if (str.d != Data::sharedNull()) { + if (d == Data::sharedNull()) { operator=(str); } else { if (d->ref.isShared() || uint(d->size + str.d->size) + 1u > d->alloc) @@ -4048,19 +4020,15 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size) { Data *d; if (!str) { - d = shared_null.data_ptr(); + d = Data::sharedNull(); } else if (size == 0 || (!*str && size < 0)) { - d = shared_empty.data_ptr(); + d = Data::allocate(0); } else { if (size < 0) size = qstrlen(str); - d = static_cast<Data *>(::malloc(sizeof(Data) + (uint(size) + 1u) * sizeof(QChar))); + d = Data::allocate(size + 1); Q_CHECK_PTR(d); - d->ref.initializeOwned(); d->size = size; - d->alloc = uint(size) + 1u; - d->capacityReserved = false; - d->offset = sizeof(QStringData); d->data()[size] = '\0'; ushort *dst = d->data(); /* SIMD: @@ -4126,7 +4094,7 @@ QString QString::fromLocal8Bit_helper(const char *str, int size) if (!str) return QString(); if (size == 0 || (!*str && size < 0)) { - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } #if !defined(QT_NO_TEXTCODEC) @@ -4295,7 +4263,7 @@ QString QString::simplified() const break; if (++from == fromEnd) { // All-whitespace string - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } } @@ -4390,7 +4358,7 @@ QString QString::trimmed() const } int l = end - start + 1; if (l <= 0) { - QStringDataPtr empty = { shared_empty.data_ptr() }; + QStringDataPtr empty = { Data::allocate(0) }; return QString(empty); } return QString(s + start, l); @@ -7424,17 +7392,12 @@ QString QString::fromRawData(const QChar *unicode, int size) { Data *x; if (!unicode) { - x = shared_null.data_ptr(); + x = Data::sharedNull(); } else if (!size) { - x = shared_empty.data_ptr(); + x = Data::allocate(0); } else { - x = static_cast<Data *>(::malloc(sizeof(Data))); + x = Data::fromRawData(reinterpret_cast<const ushort *>(unicode), size); Q_CHECK_PTR(x); - x->ref.initializeOwned(); - x->size = size; - x->alloc = 0; - x->capacityReserved = false; - x->offset = reinterpret_cast<const char *>(unicode) - reinterpret_cast<char *>(x); } QStringDataPtr dataPtr = { x }; return QString(dataPtr); diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 3d0de55ed7..bf2928346c 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -78,17 +78,7 @@ class QLatin1String; class QStringRef; template <typename T> class QVector; -struct QStringData { - QtPrivate::RefCount ref; - int size; - uint alloc : 31; - uint capacityReserved : 1; - - qptrdiff offset; - - inline ushort *data() { return reinterpret_cast<ushort *>(reinterpret_cast<char *>(this) + offset); } - inline const ushort *data() const { return reinterpret_cast<const ushort *>(reinterpret_cast<const char *>(this) + offset); } -}; +typedef QTypedArrayData<ushort> QStringData; #if defined(Q_COMPILER_UNICODE_STRINGS) @@ -122,13 +112,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<Size> 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; \ }()) \ /**/ @@ -138,14 +129,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<Size> qstring_literal = { \ Q_STATIC_STRING_DATA_HEADER_INITIALIZER(Size), \ QT_UNICODE_LITERAL(str) }; \ QStringDataPtr holder = { qstring_literal.data_ptr() }; \ holder; \ - }) \ + })) \ /**/ # endif @@ -153,9 +144,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) \ @@ -169,13 +161,13 @@ Q_STATIC_ASSERT_X(sizeof(qunicodechar) == 2, template <int N> struct QStaticStringData { - QStringData str; + QArrayData str; qunicodechar data[N + 1]; QStringData *data_ptr() const { Q_ASSERT(str.ref.isStatic()); - return const_cast<QStringData *>(&str); + return const_cast<QStringData *>(static_cast<const QStringData*>(&str)); } }; @@ -632,9 +624,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; @@ -653,11 +645,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; @@ -927,8 +916,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) { @@ -1203,7 +1192,7 @@ public: inline const QChar *unicode() const { if (!m_string) - return reinterpret_cast<const QChar *>(QString::shared_null.str.data()); + return reinterpret_cast<const QChar *>(QString::Data::sharedNull()->data()); return m_string->unicode() + m_position; } inline const QChar *data() const { return unicode(); } diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 9a1fd6949b..9b1cd1ee7e 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -253,19 +253,6 @@ template <> struct QConcatenable<QString> : private QAbstractConcatenable } }; -template <> struct QConcatenable<QStringDataPtr> : private QAbstractConcatenable -{ - typedef QStringDataPtr type; - typedef QString ConvertTo; - enum { ExactSize = true }; - static int size(const type &a) { return a.ptr->size; } - static inline void appendTo(const type &a, QChar *&out) - { - memcpy(out, reinterpret_cast<const char*>(a.ptr->data()), sizeof(QChar) * a.ptr->size); - out += a.ptr->size; - } -}; - template <> struct QConcatenable<QStringRef> : private QAbstractConcatenable { typedef QStringRef type; @@ -358,24 +345,6 @@ template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable } }; -template <> struct QConcatenable<QByteArrayDataPtr> : private QAbstractConcatenable -{ - typedef QByteArrayDataPtr type; - typedef QByteArray ConvertTo; - enum { ExactSize = false }; - static int size(const type &ba) { return ba.ptr->size; } -#ifndef QT_NO_CAST_FROM_ASCII - static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out) - { - QAbstractConcatenable::convertFromAscii(a.ptr->data(), a.ptr->size, out); - } -#endif - static inline void appendTo(const type &ba, char *&out) - { - ::memcpy(out, ba.ptr->data(), ba.ptr->size); - out += ba.ptr->size; - } -}; template <typename A, typename B> struct QConcatenable< QStringBuilder<A, B> > |