10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
137
138
139
140
141
142
143
144
145
146
149
150
151
152
153
154
155
156
157
158
159
160
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
180
181
182
183
184
185
186
187
188
189
190
193#include "private/qtextstream_p.h"
198#include <private/qdebug_p.h>
199#include <private/qnumeric_p.h>
200#include <private/qtools_p.h>
203#include "private/qlocale_p.h"
204#include "private/qstringconverter_p.h"
212#define CHECK_VALID_STREAM(x) do {
213 if (!d->string && !d->device) {
214 qWarning("QTextStream: No device");
219#define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(type) do {
223 switch (d->getNumber(&tmp)) {
224 case QTextStreamPrivate::npsOk:
227 case QTextStreamPrivate::npsMissingDigit:
228 case QTextStreamPrivate::npsInvalidPrefix:
230 setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData);
233 return *this; } while (0
)
235#define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(type) do {
239 if (d->getReal(&tmp)) {
243 setStatus(atEnd() ? QTextStream::ReadPastEnd : QTextStream::ReadCorruptData);
245 return *this; } while (0
)
249using namespace Qt::StringLiterals;
250using namespace QtMiscUtils;
257
258
259QTextStreamPrivate::QTextStreamPrivate(QTextStream *q_ptr)
260 : readConverterSavedStateOffset(0),
268
269
270QTextStreamPrivate::~QTextStreamPrivate()
272 disconnectFromDevice();
275 device->blockSignals(
true);
281void QTextStreamPrivate::Params::reset()
283 realNumberPrecision = 6;
287 fieldAlignment = QTextStream::AlignRight;
288 realNumberNotation = QTextStream::SmartNotation;
293
294
295void QTextStreamPrivate::reset()
300 deleteDevice =
false;
303 stringOpenMode = QTextStream::NotOpen;
305 readBufferOffset = 0;
306 readBufferStartDevicePos = 0;
309 hasWrittenData =
false;
311 encoding = QStringConverter::Utf8;
312 toUtf16 = QStringDecoder(encoding);
313 fromUtf16 = QStringEncoder(encoding);
314 autoDetectUnicode =
true;
316 status = QTextStream::Ok;
319void QTextStreamPrivate::setupDevice(QIODevice *device)
321 disconnectFromDevice();
328 aboutToCloseConnection = QObject::connect(
329 device, &QIODevice::aboutToClose, device, [
this] { flushWriteBuffer(); },
330 Qt::DirectConnection);
337void QTextStreamPrivate::disconnectFromDevice()
340 QObject::disconnect(aboutToCloseConnection);
341 aboutToCloseConnection = {};
346
347
348bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
356 bool textModeEnabled = device->isTextModeEnabled();
358 device->setTextModeEnabled(
false);
361 char buf[QTEXTSTREAM_BUFFERSIZE];
362 qint64 bytesRead = 0;
369 if (device->isSequential()
370#if !defined(QT_NO_QOBJECT)
371 && (file = qobject_cast<QFile *>(device)) && file->handle() == 0
375 bytesRead = device->readLine(buf, qMin<qint64>(
sizeof(buf), maxBytes));
377 bytesRead = device->readLine(buf,
sizeof(buf));
382 bytesRead = device->read(buf, qMin<qint64>(
sizeof(buf), maxBytes));
384 bytesRead = device->read(buf,
sizeof(buf));
389 device->setTextModeEnabled(
true);
394#ifndef QT_BOOTSTRAPPED
395 if (autoDetectUnicode) {
396 autoDetectUnicode =
false;
398 auto e = QStringConverter::encodingForData(QByteArrayView(buf, bytesRead));
402 toUtf16 = QStringDecoder(encoding);
403 fromUtf16 = QStringEncoder(encoding);
406#if defined (QTEXTSTREAM_DEBUG)
407 qDebug(
"QTextStreamPrivate::fillReadBuffer(), using %s encoding", QStringConverter::nameForEncoding(encoding));
411#if defined (QTEXTSTREAM_DEBUG)
412 qDebug(
"QTextStreamPrivate::fillReadBuffer(), device->read(\"%s\", %d) == %d",
413 QtDebugUtils::toPrintable(buf, bytesRead, 32).constData(),
414 int(
sizeof(buf)),
int(bytesRead));
417 qsizetype oldReadBufferSize = readBuffer.size();
418 readBuffer += toUtf16(QByteArrayView(buf, bytesRead));
421 if (readBuffer.size() > oldReadBufferSize && textModeEnabled) {
423 QChar *writePtr = readBuffer.data() + oldReadBufferSize;
424 QChar *readPtr = readBuffer.data() + oldReadBufferSize;
425 QChar *endPtr = readBuffer.data() + readBuffer.size();
427 qsizetype n = oldReadBufferSize;
428 if (readPtr < endPtr) {
430 while (*readPtr++ != CR) {
432 if (++writePtr == endPtr)
436 while (readPtr < endPtr) {
437 QChar ch = *readPtr++;
441 if (n < readBufferOffset)
447 readBuffer.resize(writePtr - readBuffer.data());
450#if defined (QTEXTSTREAM_DEBUG)
451 qDebug(
"QTextStreamPrivate::fillReadBuffer() read %d bytes from device. readBuffer = [%s]",
453 QtDebugUtils::toPrintable(readBuffer.toLatin1(), readBuffer.size(),
454 readBuffer.size()).constData());
460
461
462void QTextStreamPrivate::resetReadBuffer()
465 readBufferOffset = 0;
466 readBufferStartDevicePos = (device ? device->pos() : 0);
470
471
472void QTextStreamPrivate::flushWriteBuffer()
476 if (string || !device)
481 if (status != QTextStream::Ok)
484 if (writeBuffer.isEmpty())
487#if defined (Q_OS_WIN)
489 bool textModeEnabled = device->isTextModeEnabled();
490 if (textModeEnabled) {
491 device->setTextModeEnabled(
false);
492 writeBuffer.replace(u'\n',
"\r\n"_L1);
496 QByteArray data = fromUtf16(writeBuffer);
498 hasWrittenData =
true;
501 qint64 bytesWritten = device->write(data);
502#if defined (QTEXTSTREAM_DEBUG)
503 qDebug(
"QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d",
504 QtDebugUtils::toPrintable(data.constData(), data.size(), 32).constData(),
508#if defined (Q_OS_WIN)
511 device->setTextModeEnabled(
true);
514 if (bytesWritten <= 0) {
515 status = QTextStream::WriteFailed;
521 QFileDevice *file = qobject_cast<QFileDevice *>(device);
522 bool flushed = !file || file->flush();
527#if defined (QTEXTSTREAM_DEBUG)
528 qDebug(
"QTextStreamPrivate::flushWriteBuffer() wrote %d bytes",
int(bytesWritten));
530 if (!flushed || bytesWritten != qint64(data.size()))
531 status = QTextStream::WriteFailed;
534QString QTextStreamPrivate::read(qsizetype maxlen)
538 lastTokenSize = qMin(maxlen, string->size() - stringOffset);
539 ret = string->mid(stringOffset, lastTokenSize);
541 while (readBuffer.size() - readBufferOffset < maxlen && fillReadBuffer()) {}
542 lastTokenSize = qMin(maxlen, readBuffer.size() - readBufferOffset);
543 ret = readBuffer.mid(readBufferOffset, lastTokenSize);
547#if defined (QTEXTSTREAM_DEBUG)
548 qDebug(
"QTextStreamPrivate::read() maxlen = %d, token length = %d",
549 int(maxlen),
int(ret.length()));
555
556
557
558
559
560
561bool QTextStreamPrivate::scan(
const QChar **ptr, qsizetype *length, qsizetype maxlen,
562 TokenDelimiter delimiter)
564 qsizetype totalSize = 0;
565 qsizetype delimSize = 0;
566 bool consumeDelimiter =
false;
567 bool foundToken =
false;
568 qsizetype startOffset = device ? readBufferOffset : stringOffset;
575 chPtr = readBuffer.constData();
576 endOffset = readBuffer.size();
578 chPtr = string->constData();
579 endOffset = string->size();
581 chPtr += startOffset;
583 for (; !foundToken && startOffset < endOffset && (!maxlen || totalSize < maxlen); ++startOffset) {
584 const QChar ch = *chPtr++;
603 delimSize = (lastChar == u'\r') ? 2 : 1;
604 consumeDelimiter =
true;
611 && (!maxlen || totalSize < maxlen)
612 && device && fillReadBuffer());
614 if (totalSize == 0) {
615#if defined (QTEXTSTREAM_DEBUG)
616 qDebug(
"QTextStreamPrivate::scan() reached the end of input.");
623 if (delimiter == EndOfLine && totalSize > 0 && !foundToken) {
624 if (((string && stringOffset + totalSize == string->size()) || (device && device->atEnd()))
625 && lastChar == u'\r') {
626 consumeDelimiter =
true;
633 *length = totalSize - delimSize;
639 lastTokenSize = totalSize;
640 if (!consumeDelimiter)
641 lastTokenSize -= delimSize;
643#if defined (QTEXTSTREAM_DEBUG)
644 qDebug(
"QTextStreamPrivate::scan(%p, %p, %d, %x) token length = %d, delimiter = %d",
645 ptr, length,
int(maxlen), uint(delimiter),
int(totalSize - delimSize),
int(delimSize));
651
652
653inline const QChar *QTextStreamPrivate::readPtr()
const
655 Q_ASSERT(readBufferOffset <= readBuffer.size());
657 return string->constData() + stringOffset;
658 return readBuffer.constData() + readBufferOffset;
662
663
664inline void QTextStreamPrivate::consumeLastToken()
667 consume(lastTokenSize);
672
673
674inline void QTextStreamPrivate::consume(qsizetype size)
676#if defined (QTEXTSTREAM_DEBUG)
677 qDebug(
"QTextStreamPrivate::consume(%d)",
int(size));
680 stringOffset += size;
681 if (stringOffset > string->size())
682 stringOffset = string->size();
684 readBufferOffset += size;
685 if (readBufferOffset >= readBuffer.size()) {
686 readBufferOffset = 0;
688 saveConverterState(device->pos());
689 }
else if (readBufferOffset > QTEXTSTREAM_BUFFERSIZE) {
690 readBuffer = readBuffer.remove(0,readBufferOffset);
691 readConverterSavedStateOffset += readBufferOffset;
692 readBufferOffset = 0;
698
699
700inline void QTextStreamPrivate::saveConverterState(qint64 newPos)
703 memcpy((
void *)&savedToUtf16, (
void *)&toUtf16,
sizeof(QStringDecoder));
704 readBufferStartDevicePos = newPos;
705 readConverterSavedStateOffset = 0;
709
710
711inline void QTextStreamPrivate::restoreToSavedConverterState()
713 if (savedToUtf16.isValid())
714 memcpy((
void *)&toUtf16, (
void *)&savedToUtf16,
sizeof(QStringDecoder));
716 toUtf16.resetState();
717 savedToUtf16 = QStringDecoder();
721
722
723template <
typename Appendable>
724void QTextStreamPrivate::writeImpl(Appendable s)
730 writeBuffer.append(s);
731 if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE)
737
738
739void QTextStreamPrivate::write(QStringView s)
745
746
747void QTextStreamPrivate::write(QChar ch)
753
754
755void QTextStreamPrivate::write(QLatin1StringView data)
761
762
763void QTextStreamPrivate::writePadding(qsizetype len)
767 string->resize(string->size() + len, params.padChar);
769 writeBuffer.resize(writeBuffer.size() + len, params.padChar);
770 if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE)
776
777
778inline bool QTextStreamPrivate::getChar(QChar *ch)
780 if ((string && stringOffset == string->size())
781 || (device && readBuffer.isEmpty() && !fillReadBuffer())) {
793
794
795inline void QTextStreamPrivate::ungetChar(QChar ch)
798 if (stringOffset == 0)
801 (*string)[--stringOffset] = ch;
805 if (readBufferOffset == 0) {
806 readBuffer.prepend(ch);
810 readBuffer[--readBufferOffset] = ch;
814
815
816inline void QTextStreamPrivate::putChar(QChar ch)
818 if (params.fieldWidth > 0)
819 putString(QStringView{&ch, 1});
826
827
828QTextStreamPrivate::PaddingResult QTextStreamPrivate::padding(qsizetype len)
const
830 Q_ASSERT(params.fieldWidth > len);
832 qsizetype left = 0, right = 0;
834 const qsizetype padSize = params.fieldWidth - len;
836 switch (params.fieldAlignment) {
837 case QTextStream::AlignLeft:
840 case QTextStream::AlignRight:
841 case QTextStream::AlignAccountingStyle:
844 case QTextStream::AlignCenter:
846 right = padSize - padSize/2;
849 return { left, right };
853template <
typename StringView>
854auto parseSign(StringView data,
const QLocale &loc)
857 StringView sign, rest;
858 explicit operator
bool()
const noexcept {
return !sign.isEmpty(); }
862 if (
const QString sign = loc.negativeSign(); data.startsWith(sign))
863 return R{data.first(sign.size()), data.sliced(sign.size())};
864 if (
const QString sign = loc.positiveSign(); data.startsWith(sign))
865 return R{data.first(sign.size()), data.sliced(sign.size())};
866 return R{
nullptr, data};
871
872
873template <
typename StringView>
874void QTextStreamPrivate::putStringImpl(StringView data, PutStringMode mode)
876 const bool number = mode == PutStringMode::Number;
877 if (Q_UNLIKELY(params.fieldWidth > data.size())) {
881 const PaddingResult pad = padding(data.size());
883 if (params.fieldAlignment == QTextStream::AlignAccountingStyle && number) {
884 if (
const auto r = parseSign(data, locale)) {
891 writePadding(pad.left);
893 writePadding(pad.right);
900
901
902void QTextStreamPrivate::putString(QLatin1StringView data, PutStringMode mode)
904 putStringImpl(data, mode);
908
909
910void QTextStreamPrivate::putString(QStringView data, PutStringMode mode)
912 putStringImpl(data, mode);
916
917
918void QTextStreamPrivate::putString(QUtf8StringView data, PutStringMode mode)
920 putString(data.toString(), mode);
924
925
926
927
928
929QTextStream::QTextStream()
930 : d_ptr(
new QTextStreamPrivate(
this))
932#if defined (QTEXTSTREAM_DEBUG)
933 qDebug(
"QTextStream::QTextStream()");
938
939
940QTextStream::QTextStream(QIODevice *device)
941 : d_ptr(
new QTextStreamPrivate(
this))
943#if defined (QTEXTSTREAM_DEBUG)
944 qDebug(
"QTextStream::QTextStream(QIODevice *device == *%p)",
949 d->setupDevice(device);
953
954
955
956QTextStream::QTextStream(QString *string, OpenMode openMode)
957 : d_ptr(
new QTextStreamPrivate(
this))
959#if defined (QTEXTSTREAM_DEBUG)
960 qDebug(
"QTextStream::QTextStream(QString *string == *%p, openMode = %d)",
961 string,
int(openMode.toInt()));
965 d->stringOpenMode = openMode;
968#ifndef QT_BOOTSTRAPPED
970
971
972
973
974QTextStream::QTextStream(QByteArray *array, OpenMode openMode)
975 : d_ptr(
new QTextStreamPrivate(
this))
977#if defined (QTEXTSTREAM_DEBUG)
978 qDebug(
"QTextStream::QTextStream(QByteArray *array == *%p, openMode = %d)",
979 array,
int(openMode.toInt()));
982 d->device =
new QBuffer(array);
983 d->device->open(openMode);
984 d->deleteDevice =
true;
985 d->setupDevice(d->device);
989
990
991
992
993
994
995
996
997
998QTextStream::QTextStream(
const QByteArray &array, OpenMode openMode)
999 : d_ptr(
new QTextStreamPrivate(
this))
1001#if defined (QTEXTSTREAM_DEBUG)
1002 qDebug(
"QTextStream::QTextStream(const QByteArray &array == *(%p), openMode = %d)",
1003 &array,
int(openMode.toInt()));
1005 QBuffer *buffer =
new QBuffer;
1006 buffer->setData(array);
1007 buffer->open(openMode);
1011 d->deleteDevice =
true;
1012 d->setupDevice(d->device);
1017
1018
1019
1020
1021
1022
1023
1024
1025
1027QTextStream::QTextStream(FILE *fileHandle, OpenMode openMode)
1028 : d_ptr(
new QTextStreamPrivate(
this))
1030#if defined (QTEXTSTREAM_DEBUG)
1031 qDebug(
"QTextStream::QTextStream(FILE *fileHandle = %p, openMode = %d)",
1032 fileHandle,
int(openMode.toInt()));
1034 QFile *file =
new QFile;
1038 (
void)file->open(fileHandle, openMode);
1042 d->deleteDevice =
true;
1043 d->setupDevice(d->device);
1047
1048
1049
1050
1051
1052QTextStream::~QTextStream()
1055#if defined (QTEXTSTREAM_DEBUG)
1056 qDebug(
"QTextStream::~QTextStream()");
1058 if (!d->writeBuffer.isEmpty())
1059 d->flushWriteBuffer();
1063
1064
1065
1066
1067void QTextStream::reset()
1075
1076
1077
1078
1079void QTextStream::flush()
1082 d->flushWriteBuffer();
1086
1087
1088
1089bool QTextStream::seek(qint64 pos)
1092 d->lastTokenSize = 0;
1096 d->flushWriteBuffer();
1097 if (!d->device->seek(pos))
1099 d->resetReadBuffer();
1101 d->toUtf16.resetState();
1102 d->fromUtf16.resetState();
1107 if (d->string && pos <= d->string->size()) {
1108 d->stringOffset = pos;
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128qint64 QTextStream::pos()
const
1130 Q_D(
const QTextStream);
1133 if (d->readBuffer.isEmpty())
1134 return d->device->pos();
1135 if (d->device->isSequential())
1139 if (!d->device->seek(d->readBufferStartDevicePos))
1143 QTextStreamPrivate *thatd =
const_cast<QTextStreamPrivate *>(d);
1144 thatd->readBuffer.clear();
1146 thatd->restoreToSavedConverterState();
1147 if (d->readBufferStartDevicePos == 0)
1148 thatd->autoDetectUnicode =
true;
1152 qsizetype oldReadBufferOffset = d->readBufferOffset + d->readConverterSavedStateOffset;
1153 while (d->readBuffer.size() < oldReadBufferOffset) {
1154 if (!thatd->fillReadBuffer(1))
1157 thatd->readBufferOffset = oldReadBufferOffset;
1158 thatd->readConverterSavedStateOffset = 0;
1161 return d->device->pos();
1165 return d->stringOffset;
1167 qWarning(
"QTextStream::pos: no device");
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182void QTextStream::skipWhiteSpace()
1186 d->scan(
nullptr,
nullptr, 0, QTextStreamPrivate::NotSpace);
1187 d->consumeLastToken();
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200void QTextStream::setDevice(QIODevice *device)
1204 if (d->deleteDevice) {
1205 d->disconnectFromDevice();
1207 d->deleteDevice =
false;
1212 d->resetReadBuffer();
1213 d->setupDevice(d->device);
1217
1218
1219
1220
1221
1222QIODevice *QTextStream::device()
const
1224 Q_D(
const QTextStream);
1229
1230
1231
1232
1233
1234
1235void QTextStream::setString(QString *string, OpenMode openMode)
1239 if (d->deleteDevice) {
1240#ifndef QT_NO_QOBJECT
1241 d->setupDevice(d->device);
1242 d->device->blockSignals(
true);
1245 d->deleteDevice =
false;
1250 d->stringOpenMode = openMode;
1254
1255
1256
1257
1258
1259QString *QTextStream::string()
const
1261 Q_D(
const QTextStream);
1266
1267
1268
1269
1270
1271
1272
1273void QTextStream::setFieldAlignment(FieldAlignment mode)
1276 d->params.fieldAlignment = mode;
1280
1281
1282
1283
1284QTextStream::FieldAlignment QTextStream::fieldAlignment()
const
1286 Q_D(
const QTextStream);
1287 return d->params.fieldAlignment;
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305void QTextStream::setPadChar(QChar ch)
1308 d->params.padChar = ch;
1312
1313
1314
1315
1316QChar QTextStream::padChar()
const
1318 Q_D(
const QTextStream);
1319 return d->params.padChar;
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334void QTextStream::setFieldWidth(
int width)
1337 d->params.fieldWidth = width;
1341
1342
1343
1344
1345int QTextStream::fieldWidth()
const
1347 Q_D(
const QTextStream);
1348 return d->params.fieldWidth;
1352
1353
1354
1355
1356
1357
1358
1359void QTextStream::setNumberFlags(NumberFlags flags)
1362 d->params.numberFlags = flags;
1366
1367
1368
1369
1370QTextStream::NumberFlags QTextStream::numberFlags()
const
1372 Q_D(
const QTextStream);
1373 return d->params.numberFlags;
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386void QTextStream::setIntegerBase(
int base)
1389 d->params.integerBase = base;
1393
1394
1395
1396
1397
1398int QTextStream::integerBase()
const
1400 Q_D(
const QTextStream);
1401 return d->params.integerBase;
1405
1406
1407
1408
1409
1410
1411
1412void QTextStream::setRealNumberNotation(RealNumberNotation notation)
1415 d->params.realNumberNotation = notation;
1419
1420
1421
1422
1423QTextStream::RealNumberNotation QTextStream::realNumberNotation()
const
1425 Q_D(
const QTextStream);
1426 return d->params.realNumberNotation;
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439void QTextStream::setRealNumberPrecision(
int precision)
1442 if (precision < 0) {
1443 qWarning(
"QTextStream::setRealNumberPrecision: Invalid precision (%d)", precision);
1444 d->params.realNumberPrecision = 6;
1447 d->params.realNumberPrecision = precision;
1451
1452
1453
1454
1455
1456
1457
1458int QTextStream::realNumberPrecision()
const
1460 Q_D(
const QTextStream);
1461 return d->params.realNumberPrecision;
1465
1466
1467
1468
1470QTextStream::Status QTextStream::status()
const
1472 Q_D(
const QTextStream);
1477
1478
1479
1480
1481
1482
1483void QTextStream::resetStatus()
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499void QTextStream::setStatus(Status status)
1502 if (d->status == Ok)
1507
1508
1509
1510
1511
1512bool QTextStream::atEnd()
const
1514 Q_D(
const QTextStream);
1518 return d->string->size() == d->stringOffset;
1519 return d->readBuffer.isEmpty() && d->device->atEnd();
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532QString QTextStream::readAll()
1537 return d->read(std::numeric_limits<qsizetype>::max());
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557QString QTextStream::readLine(qint64 maxlen)
1561 readLineInto(&line, maxlen);
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590bool QTextStream::readLineInto(QString *line, qint64 maxlen)
1594 if (!d->string && !d->device) {
1595 qWarning(
"QTextStream: No device");
1596 if (line && !line->isNull())
1601 const QChar *readPtr;
1603 qsizetype clampedlen = q26::saturating_cast<qsizetype>(maxlen);
1604 if (!d->scan(&readPtr, &length, clampedlen, QTextStreamPrivate::EndOfLine)) {
1605 if (line && !line->isNull())
1611 line->setUnicode(readPtr, length);
1612 d->consumeLastToken();
1617
1618
1619
1620
1621
1622
1623
1624QString QTextStream::read(qint64 maxlen)
1630 return QString::fromLatin1(
"");
1632 return d->read(q26::saturating_cast<qsizetype>(maxlen));
1636
1637
1638QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong *ret)
1640 scan(
nullptr,
nullptr, 0, NotSpace);
1644 int base = params.integerBase;
1648 return npsInvalidPrefix;
1651 if (!getChar(&ch2)) {
1656 ch2 = ch2.toLower();
1660 }
else if (ch2 == u'b') {
1662 }
else if (ch2.isDigit() && ch2.digitValue() >= 0 && ch2.digitValue() <= 7) {
1668 }
else if (ch == locale.negativeSign() || ch == locale.positiveSign() || ch.isDigit()) {
1672 return npsInvalidPrefix;
1683 QChar pf1, pf2, dig;
1685 if (!getChar(&pf1) || pf1 != u'0')
1686 return npsInvalidPrefix;
1687 if (!getChar(&pf2) || pf2.toLower() != u'b')
1688 return npsInvalidPrefix;
1690 qsizetype ndigits = 0;
1691 while (getChar(&dig)) {
1692 char16_t n = dig.toLower().unicode();
1693 if (n == u'0' || n == u'1') {
1706 return npsMissingDigit;
1713 if (!getChar(&pf) || pf != u'0')
1714 return npsInvalidPrefix;
1716 qsizetype ndigits = 0;
1717 while (getChar(&dig)) {
1718 char16_t n = dig.toLower().unicode();
1719 if (isOctalDigit(n)) {
1731 return npsMissingDigit;
1738 qsizetype ndigits = 0;
1739 if (!getChar(&sign))
1740 return npsMissingDigit;
1741 if (sign != locale.negativeSign() && sign != locale.positiveSign()) {
1742 if (!sign.isDigit()) {
1744 return npsMissingDigit;
1746 val += sign.digitValue();
1751 while (getChar(&ch)) {
1754 val += ch.digitValue();
1755 }
else if (locale != QLocale::c() && ch == locale.groupSeparator()) {
1764 return npsMissingDigit;
1765 if (sign == locale.negativeSign()) {
1766 qlonglong ival = qlonglong(val);
1769 val = qulonglong(ival);
1774 QChar pf1, pf2, dig;
1776 if (!getChar(&pf1) || pf1 != u'0')
1777 return npsInvalidPrefix;
1778 if (!getChar(&pf2) || pf2.toLower() != u'x')
1779 return npsInvalidPrefix;
1781 qsizetype ndigits = 0;
1782 while (getChar(&dig)) {
1783 const int h = fromHex(dig.unicode());
1794 return npsMissingDigit;
1800 return npsInvalidPrefix;
1809
1810
1811
1812bool QTextStreamPrivate::getReal(
double *f)
1846 static const uchar table[13][10] = {
1848 { 0, Sign, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 },
1849 { 0, 0, Mantissa, Dot, 0, Inf1, Nan1, 0, 0, 0 },
1850 { Done, Done, Mantissa, Dot, ExpMark, 0, 0, 0, 0, 0 },
1851 { 0, 0, Abscissa, 0, 0, 0, 0, 0, 0, 0 },
1852 { Done, Done, Abscissa, Done, ExpMark, 0, 0, 0, 0, 0 },
1853 { 0, ExpSign, Exponent, 0, 0, 0, 0, 0, 0, 0 },
1854 { 0, 0, Exponent, 0, 0, 0, 0, 0, 0, 0 },
1855 { Done, Done, Exponent, Done, Done, 0, 0, 0, 0, 0 },
1856 { 0, 0, 0, 0, 0, 0, 0, 0, Nan2, 0 },
1857 { 0, 0, 0, 0, 0, 0, NanInf, 0, 0, 0 },
1858 { 0, 0, 0, 0, 0, 0, Inf2, 0, 0, 0 },
1859 { 0, 0, 0, 0, 0, 0, 0, NanInf, 0, 0 },
1860 { Done, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
1863 ParserState state = Init;
1864 InputToken input = None;
1866 scan(
nullptr,
nullptr, 0, NotSpace);
1869 const qsizetype BufferSize = 128;
1870 char buf[BufferSize];
1874 while (getChar(&c)) {
1875 switch (c.unicode()) {
1876 case u'0':
case u'1':
case u'2':
case u'3':
case u'4':
1877 case u'5':
case u'6':
case u'7':
case u'8':
case u'9':
1880 case u'i':
case u'I':
1883 case u'n':
case u'N':
1886 case u'f':
case u'F':
1889 case u'a':
case u'A':
1892 case u't':
case u'T':
1896 QChar lc = c.toLower();
1897 if (lc == locale.decimalPoint().toLower())
1899 else if (lc == locale.exponential().toLower())
1901 else if (lc == locale.negativeSign().toLower()
1902 || lc == locale.positiveSign().toLower())
1904 else if (locale != QLocale::c()
1905 && lc == locale.groupSeparator().toLower())
1913 state = ParserState(table[state][input]);
1915 if (state == Init || state == Done || i > (BufferSize - 5)) {
1917 if (i > (BufferSize - 5)) {
1918 while (getChar(&c)) {
1928 buf[i++] = c.toLatin1();
1941 if (!qstricmp(buf,
"nan") || !qstricmp(buf,
"+nan") || !qstricmp(buf,
"-nan")) {
1944 }
else if (!qstricmp(buf,
"+inf") || !qstricmp(buf,
"inf")) {
1947 }
else if (!qstricmp(buf,
"-inf")) {
1952 *f = locale.toDouble(QString::fromLatin1(buf), &ok);
1957
1958
1959
1960
1961
1962
1963
1964
1966QTextStream &QTextStream::operator>>(QChar &c)
1970 d->scan(
nullptr,
nullptr, 0, QTextStreamPrivate::NotSpace);
1971 if (!d->getChar(&c))
1972 setStatus(ReadPastEnd);
1977
1978
1979
1980
1981
1982
1983
1984
1985QTextStream &QTextStream::operator>>(
char &c)
1994
1995
1996
1997
1998
1999
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025QTextStream &QTextStream::operator>>(
signed short &i)
2031
2032
2033
2034
2035QTextStream &QTextStream::operator>>(
unsigned short &i)
2041
2042
2043
2044
2045QTextStream &QTextStream::operator>>(
signed int &i)
2051
2052
2053
2054
2055QTextStream &QTextStream::operator>>(
unsigned int &i)
2061
2062
2063
2064
2065QTextStream &QTextStream::operator>>(
signed long &i)
2071
2072
2073
2074
2075QTextStream &QTextStream::operator>>(
unsigned long &i)
2081
2082
2083
2084
2085QTextStream &QTextStream::operator>>(qlonglong &i)
2091
2092
2093
2094
2095QTextStream &QTextStream::operator>>(qulonglong &i)
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111QTextStream &QTextStream::operator>>(
float &f)
2117
2118
2119
2120
2121QTextStream &QTextStream::operator>>(
double &f)
2127
2128
2129
2130
2131
2132
2133QTextStream &QTextStream::operator>>(QString &str)
2139 d->scan(
nullptr,
nullptr, 0, QTextStreamPrivate::NotSpace);
2140 d->consumeLastToken();
2144 if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2145 setStatus(ReadPastEnd);
2149 str = QString(ptr, length);
2150 d->consumeLastToken();
2155
2156
2157
2158
2159
2160
2161QTextStream &QTextStream::operator>>(QByteArray &array)
2166 d->scan(
nullptr,
nullptr, 0, QTextStreamPrivate::NotSpace);
2167 d->consumeLastToken();
2171 if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2172 setStatus(ReadPastEnd);
2177 array = QStringView(ptr, length).toUtf8();
2179 d->consumeLastToken();
2183#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6
, 12
)
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201QTextStream &QTextStream::operator>>(
char *c)
2205 CHECK_VALID_STREAM(*
this);
2206 d->scan(
nullptr,
nullptr, 0, QTextStreamPrivate::NotSpace);
2207 d->consumeLastToken();
2211 if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
2212 setStatus(ReadPastEnd);
2216 QStringEncoder encoder(QStringConverter::Utf8);
2217 char *e = encoder.appendToBuffer(c, QStringView(ptr, length));
2219 d->consumeLastToken();
2225
2226
2227void QTextStreamPrivate::putNumber(qulonglong number,
bool negative)
2230 const QTextStream::NumberFlags numberFlags = params.numberFlags;
2231 if (numberFlags & QTextStream::ShowBase)
2232 flags |= QLocaleData::ShowBase;
2234 if ((numberFlags & QTextStream::ForceSign) && !negative)
2235 flags |= QLocaleData::AlwaysShowSign;
2236 if (numberFlags & QTextStream::UppercaseBase)
2237 flags |= QLocaleData::UppercaseBase;
2238 if (numberFlags & QTextStream::UppercaseDigits)
2239 flags |= QLocaleData::CapitalEorX;
2242 if (locale != QLocale::c() && !locale.numberOptions().testFlag(QLocale::OmitGroupSeparator))
2243 flags |= QLocaleData::GroupDigits;
2245 const QLocaleData *dd = locale.d->m_data;
2246 int base = params.integerBase ? params.integerBase : 10;
2247 QString result = dd->unsLongLongToString(number, -1, base, -1, flags);
2249 result.prepend(locale.negativeSign());
2250 }
else if (number == 0 && base == 8 && params.numberFlags & QTextStream::ShowBase
2251 && result ==
"0"_L1) {
2256 result.prepend(u'0');
2258 putString(result, PutStringMode::Number);
2262
2263
2264
2265
2266
2267QTextStream &QTextStream::operator<<(QChar c)
2276
2277
2278
2279
2280QTextStream &QTextStream::operator<<(
char c)
2284 d->putChar(QChar::fromLatin1(c));
2289
2290
2291
2292
2293
2294
2295
2298
2299
2300
2301
2302
2303
2304
2305QTextStream &QTextStream::operator<<(
signed short i)
2309 d->putNumber(QtPrivate::qUnsignedAbs(i), i < 0);
2314
2315
2316
2317
2318QTextStream &QTextStream::operator<<(
unsigned short i)
2322 d->putNumber((qulonglong)i,
false);
2327
2328
2329
2330
2331QTextStream &QTextStream::operator<<(
signed int i)
2335 d->putNumber(QtPrivate::qUnsignedAbs(i), i < 0);
2340
2341
2342
2343
2344QTextStream &QTextStream::operator<<(
unsigned int i)
2348 d->putNumber((qulonglong)i,
false);
2353
2354
2355
2356
2357QTextStream &QTextStream::operator<<(
signed long i)
2361 d->putNumber(QtPrivate::qUnsignedAbs(i), i < 0);
2366
2367
2368
2369
2370QTextStream &QTextStream::operator<<(
unsigned long i)
2374 d->putNumber((qulonglong)i,
false);
2379
2380
2381
2382
2383QTextStream &QTextStream::operator<<(qlonglong i)
2387 d->putNumber(QtPrivate::qUnsignedAbs(i), i < 0);
2392
2393
2394
2395
2396QTextStream &QTextStream::operator<<(qulonglong i)
2400 d->putNumber(i,
false);
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415QTextStream &QTextStream::operator<<(
float f)
2417 return *
this <<
double(f);
2421
2422
2423
2424
2425QTextStream &QTextStream::operator<<(
double f)
2430 QLocaleData::DoubleForm form = QLocaleData::DFDecimal;
2431 switch (realNumberNotation()) {
2433 form = QLocaleData::DFDecimal;
2435 case ScientificNotation:
2436 form = QLocaleData::DFExponent;
2439 form = QLocaleData::DFSignificantDigits;
2444 const QLocale::NumberOptions numberOptions = locale().numberOptions();
2445 if (numberFlags() & ShowBase)
2446 flags |= QLocaleData::ShowBase;
2447 if (numberFlags() & ForceSign)
2448 flags |= QLocaleData::AlwaysShowSign;
2449 if (numberFlags() & UppercaseBase)
2450 flags |= QLocaleData::UppercaseBase;
2451 if (numberFlags() & UppercaseDigits)
2452 flags |= QLocaleData::CapitalEorX;
2453 if (numberFlags() & ForcePoint) {
2454 flags |= QLocaleData::ForcePoint;
2457 flags |= QLocaleData::AddTrailingZeroes | QLocaleData::ShowBase;
2459 if (locale() != QLocale::c() && !(numberOptions & QLocale::OmitGroupSeparator))
2460 flags |= QLocaleData::GroupDigits;
2461 if (!(numberOptions & QLocale::OmitLeadingZeroInExponent))
2462 flags |= QLocaleData::ZeroPadExponent;
2463 if (numberOptions & QLocale::IncludeTrailingZeroesAfterDot)
2464 flags |= QLocaleData::AddTrailingZeroes;
2466 const QLocaleData *dd = d->locale.d->m_data;
2467 QString num = dd->doubleToString(f, d->params.realNumberPrecision, form, -1, flags);
2468 d->putString(num, QTextStreamPrivate::PutStringMode::Number);
2473
2474
2475
2476
2477
2478
2479QTextStream &QTextStream::operator<<(
const QString &string)
2483 d->putString(string);
2488
2489
2490
2491
2492
2493
2494QTextStream &QTextStream::operator<<(QStringView string)
2498 d->putString(string);
2503
2504
2505
2506
2507
2508QTextStream &QTextStream::operator<<(QLatin1StringView string)
2512 d->putString(string);
2517
2518
2519
2520
2521
2522QTextStream &QTextStream::operator<<(
const QByteArray &array)
2526 d->putString(QUtf8StringView{array});
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543QTextStream &QTextStream::operator<<(
const char *string)
2547 d->putString(QUtf8StringView(string));
2552
2553
2554
2555
2557QTextStream &QTextStream::operator<<(
const void *ptr)
2561 const int oldBase = d->params.integerBase;
2562 const NumberFlags oldFlags = d->params.numberFlags;
2563 d->params.integerBase = 16;
2564 d->params.numberFlags |= ShowBase;
2565 d->putNumber(
reinterpret_cast<quintptr>(ptr),
false);
2566 d->params.integerBase = oldBase;
2567 d->params.numberFlags = oldFlags;
2572
2573
2574
2575
2576
2581
2582
2583
2584
2585
2586
2587
2595
2596
2597
2598
2599
2600
2601
2609
2610
2611
2612
2613
2614
2615
2623
2624
2625
2626
2627
2628
2629
2630
2638
2639
2640
2641
2642
2643
2644
2652
2653
2654
2655
2656
2657
2658
2666
2667
2668
2669
2670
2671
2672
2680
2681
2682
2683
2684
2685
2686
2694
2695
2696
2697
2698
2699
2700
2708
2709
2710
2711
2712
2713
2714
2722
2723
2724
2725
2726
2727
2728
2736
2737
2738
2739
2740
2741
2742
2750
2751
2752
2753
2754
2755
2756
2764
2765
2766
2767
2768
2769
2770
2778
2779
2780
2781
2782
2783
2784
2792
2793
2794
2795
2796
2797
2798
2806
2807
2808
2809
2810
2811
2812
2820
2821
2822
2823
2824
2825
2826
2834
2835
2836
2837
2838
2839
2840
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2867
2868
2869
2870
2871
2872
2880
2881
2882
2883
2884
2885
2893
2894
2895
2896
2897
2898
2908
2909
2910
2911
2912
2915
2916
2917
2918
2919
2922
2923
2924
2925
2926
2931
2932
2933
2934
2935
2936
2937
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963void QTextStream::setEncoding(QStringConverter::Encoding encoding)
2966 if (d->encoding == encoding)
2969 qint64 seekPos = -1;
2970 if (!d->readBuffer.isEmpty()) {
2971 if (!d->device->isSequential()) {
2976 d->encoding = encoding;
2977 d->toUtf16 = QStringDecoder(d->encoding);
2978 bool generateBOM = !d->hasWrittenData && d->generateBOM;
2979 d->fromUtf16 = QStringEncoder(d->encoding,
2980 generateBOM ? QStringEncoder::Flag::WriteBom : QStringEncoder::Flag::Default);
2982 if (seekPos >=0 && !d->readBuffer.isEmpty())
2987
2988
2989
2990
2991QStringConverter::Encoding QTextStream::encoding()
const
2993 Q_D(
const QTextStream);
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008void QTextStream::setAutoDetectUnicode(
bool enabled)
3011 d->autoDetectUnicode = enabled;
3015
3016
3017
3018
3019
3020bool QTextStream::autoDetectUnicode()
const
3022 Q_D(
const QTextStream);
3023 return d->autoDetectUnicode;
3027
3028
3029
3030
3031
3032
3033
3034void QTextStream::setGenerateByteOrderMark(
bool generate)
3037 if (d->hasWrittenData || d->generateBOM == generate)
3040 d->generateBOM = generate;
3041 d->fromUtf16 = QStringEncoder(d->encoding, generate ? QStringConverter::Flag::WriteBom : QStringConverter::Flag::Default);
3045
3046
3047
3048
3049
3050
3051bool QTextStream::generateByteOrderMark()
const
3053 Q_D(
const QTextStream);
3054 return d->generateBOM;
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068void QTextStream::setLocale(
const QLocale &locale)
3075
3076
3077
3078
3079
3080
3081QLocale QTextStream::locale()
const
3083 Q_D(
const QTextStream);
Combined button and popup list for selecting options.
static const qsizetype QTEXTSTREAM_BUFFERSIZE
#define IMPLEMENT_STREAM_RIGHT_REAL_OPERATOR(type)
#define IMPLEMENT_STREAM_RIGHT_INT_OPERATOR(type)
#define CHECK_VALID_STREAM(x)