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 <thiago.macieira@intel.com>
This commit is contained in:
parent
d206d04b7c
commit
a903ddd8dc
@ -893,27 +893,53 @@ 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<QChar, PreallocatedPadding> pad(padSize);
|
||||
std::fill_n(pad.begin(), padSize, params.padChar);
|
||||
if (Q_UNLIKELY(params.fieldWidth > len)) {
|
||||
|
||||
int padLeft = 0, padRight = 0;
|
||||
// handle padding:
|
||||
|
||||
const PaddingResult pad = padding(len);
|
||||
|
||||
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()) {
|
||||
@ -923,15 +949,10 @@ void QTextStreamPrivate::putString(const QChar *data, int len, bool number)
|
||||
--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);
|
||||
}
|
||||
|
@ -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<QChar, PreallocatedPadding> padding;
|
||||
};
|
||||
PaddingResult padding(int len) const;
|
||||
|
||||
// buffers
|
||||
bool fillReadBuffer(qint64 maxBytes = -1);
|
||||
void resetReadBuffer();
|
||||
|
Loading…
Reference in New Issue
Block a user