From a903ddd8dc806b51c21c84965611880a25037562 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 15 Oct 2015 10:45:11 +0200 Subject: [PATCH] QTextStream: Extract Method padding() from putString() This is in preparation of adding a putString(QLatin1String) overload. To keep the change simple, I kept the construction of the padChar chain per putString() call. It probably makes sense to have a QString::resize(int, QChar) to perform the padding operation in-place. Let's leave that for another changeset. Change-Id: I9ef66b8df38117e1669fd2bece6ee180a2ce3369 Reviewed-by: Thiago Macieira --- src/corelib/io/qtextstream.cpp | 79 +++++++++++++++++++++------------- src/corelib/io/qtextstream_p.h | 8 ++++ 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 64222c2212..3c9385c625 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -893,45 +893,66 @@ inline void QTextStreamPrivate::putChar(QChar ch) write(ch); } + +/*! + \internal +*/ +QTextStreamPrivate::PaddingResult QTextStreamPrivate::padding(int len) const +{ + Q_ASSERT(params.fieldWidth > len); // calling padding() when no padding is needed is an error + + // Do NOT break NRVO in this function or kittens will die! + + PaddingResult result; + + const int padSize = params.fieldWidth - len; + + result.padding.resize(padSize); + std::fill_n(result.padding.begin(), padSize, params.padChar); + + switch (params.fieldAlignment) { + case QTextStream::AlignLeft: + result.left = 0; + result.right = padSize; + break; + case QTextStream::AlignRight: + case QTextStream::AlignAccountingStyle: + result.left = padSize; + result.right = 0; + break; + case QTextStream::AlignCenter: + result.left = padSize/2; + result.right = padSize - padSize/2; + break; + } + + return result; +} + /*! \internal */ void QTextStreamPrivate::putString(const QChar *data, int len, bool number) { - int padSize = params.fieldWidth - len; - if (Q_UNLIKELY(padSize > 0)) { - // handle padding - static const int PreallocatedPadding = 80; // typical line length - QVarLengthArray pad(padSize); - std::fill_n(pad.begin(), padSize, params.padChar); + if (Q_UNLIKELY(params.fieldWidth > len)) { - int padLeft = 0, padRight = 0; + // handle padding: - switch (params.fieldAlignment) { - case QTextStream::AlignLeft: - padRight = padSize; - break; - case QTextStream::AlignRight: - case QTextStream::AlignAccountingStyle: - padLeft = padSize; - if (params.fieldAlignment == QTextStream::AlignAccountingStyle && number) { - const QChar sign = len > 0 ? data[0] : QChar(); - if (sign == locale.negativeSign() || sign == locale.positiveSign()) { - // write the sign before the padding, then skip it later - write(&sign, 1); - ++data; - --len; - } + const PaddingResult pad = padding(len); + + if (params.fieldAlignment == QTextStream::AlignAccountingStyle && number) { + const QChar sign = len > 0 ? data[0] : QChar(); + if (sign == locale.negativeSign() || sign == locale.positiveSign()) { + // write the sign before the padding, then skip it later + write(&sign, 1); + ++data; + --len; } - break; - case QTextStream::AlignCenter: - padLeft = padSize/2; - padRight = padSize - padSize/2; - break; } - write(pad.constData(), padLeft); + + write(pad.padding.constData(), pad.left); write(data, len); - write(pad.constData(), padRight); + write(pad.padding.constData(), pad.right); } else { write(data, len); } diff --git a/src/corelib/io/qtextstream_p.h b/src/corelib/io/qtextstream_p.h index 01289d7dc6..c44ae5dada 100644 --- a/src/corelib/io/qtextstream_p.h +++ b/src/corelib/io/qtextstream_p.h @@ -173,6 +173,14 @@ public: inline void putChar(QChar ch); void putNumber(qulonglong number, bool negative); + struct PaddingResult { + enum { PreallocatedPadding = 80 }; // typical line length + + int right, left; + QVarLengthArray padding; + }; + PaddingResult padding(int len) const; + // buffers bool fillReadBuffer(qint64 maxBytes = -1); void resetReadBuffer();