diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-10-15 03:36:22 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-10-23 05:51:00 +0000 |
commit | 8515aa18716779985a5955292524fd683528c576 (patch) | |
tree | 35a1312c967a5ffcdc4e8f02c1f3afee5bb637a9 /src/corelib/io/qtextstream.cpp | |
parent | be926e412c9c4ec9ee77b49dbe370fbb5451f0e1 (diff) |
QTextStream: optimize streaming of QLatin1String and const char*
Instead of converting the QLatin1String to a QString at the first
opportunity, keep it around until it is appended to one of the
internal QStrings in write().
Avoids a memory allocation per QLatin1String / const char* streamed.
Change-Id: Id973a9b743e5a6696defbc4ef4ed2db1ef54e9cc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/io/qtextstream.cpp')
-rw-r--r-- | src/corelib/io/qtextstream.cpp | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 3c9385c625..a8fd2dd7ab 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -847,6 +847,21 @@ inline void QTextStreamPrivate::write(QChar ch) /*! \internal */ +void QTextStreamPrivate::write(QLatin1String data) +{ + if (string) { + // ### What about seek()?? + string->append(data); + } else { + writeBuffer += data; + if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE) + flushWriteBuffer(); + } +} + +/*! + \internal +*/ inline bool QTextStreamPrivate::getChar(QChar *ch) { if ((string && stringOffset == string->size()) @@ -959,6 +974,34 @@ void QTextStreamPrivate::putString(const QChar *data, int len, bool number) } /*! + \internal +*/ +void QTextStreamPrivate::putString(QLatin1String data, bool number) +{ + if (Q_UNLIKELY(params.fieldWidth > data.size())) { + + // handle padding + + const PaddingResult pad = padding(data.size()); + + if (params.fieldAlignment == QTextStream::AlignAccountingStyle && number) { + const QChar sign = data.size() > 0 ? QLatin1Char(*data.data()) : QChar(); + if (sign == locale.negativeSign() || sign == locale.positiveSign()) { + // write the sign before the padding, then skip it later + write(&sign, 1); + data = QLatin1String(data.data() + 1, data.size() - 1); + } + } + + write(pad.padding.constData(), pad.left); + write(data); + write(pad.padding.constData(), pad.right); + } else { + write(data); + } +} + +/*! Constructs a QTextStream. Before you can use it for reading or writing, you must assign a device or a string. @@ -2529,14 +2572,13 @@ QTextStream &QTextStream::operator<<(const QString &string) \overload Writes \a string to the stream, and returns a reference to the - QTextStream. The contents of \a string are converted with the - QString constructor that takes a QLatin1String as argument. + QTextStream. */ QTextStream &QTextStream::operator<<(QLatin1String string) { Q_D(QTextStream); CHECK_VALID_STREAM(*this); - d->putString(QString(string)); + d->putString(string); return *this; } |