summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qringbuffer.cpp
diff options
context:
space:
mode:
authorRym Bouabid <rym.bouabid@qt.io>2024-10-30 09:23:33 +0100
committerMarc Mutz <marc.mutz@qt.io>2024-12-02 16:43:50 +0000
commitc39c3fe0cb104563c1baf0d474cc83bffd503b23 (patch)
treeda6357afe8f83a504208240c41f010b2df9fd605 /src/corelib/tools/qringbuffer.cpp
parente23dc7c420297fb62db9834a17c59bbf5992dad7 (diff)
QIODevice: Add overloads of QIODevice::readLineInto() that takes QSpan
The existing QIODevice::readLineInto takes a QByteArray *, so it's dependent on a QByteArray and therefore needs at least one (initial) allocation. Add QIODevice::readLineInto() that takes a QSpan, allowing the user to provide a buffer to write into and returns a QByteArrayView. QBAV and QSpan are not null terminated. Make null termination optional when reading using QIODevicePrivate::readLine(). Extend the function to accept a ReadLineOption enum parameter with a default value of "NullTerminated" to preserve old calls. Do the same to QRingBufferRef::readLine(). Add QRingBuffer::readLineWithoutTerminatingNull(). [ChangeLog][QtCore][QIODevice] Added overloads of QIODevice::readLineInto() taking QSpan and returnig a QByteArrayView. Task-number: QTBUG-126574 Change-Id: I278a42cef4c9aa64ed884027f79105b5b7d44813 Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qringbuffer.cpp')
-rw-r--r--src/corelib/tools/qringbuffer.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/corelib/tools/qringbuffer.cpp b/src/corelib/tools/qringbuffer.cpp
index 66de1f59c06..f6ba5706993 100644
--- a/src/corelib/tools/qringbuffer.cpp
+++ b/src/corelib/tools/qringbuffer.cpp
@@ -340,14 +340,22 @@ void QRingBuffer::append(QByteArray &&qba)
bufferSize += qbaSize;
}
-qint64 QRingBuffer::readLine(char *data, qint64 maxLength)
+qint64 QRingBuffer::readLineWithoutTerminatingNull(char *data, qint64 maxLength)
{
- Q_ASSERT(data != nullptr && maxLength > 1);
+ Q_ASSERT(data != nullptr && maxLength > 0);
- --maxLength;
qint64 i = indexOf('\n', maxLength);
i = read(data, i >= 0 ? (i + 1) : maxLength);
+ return i;
+}
+
+qint64 QRingBuffer::readLine(char *data, qint64 maxLength)
+{
+ Q_ASSERT(maxLength > 1);
+
+ qint64 i = readLineWithoutTerminatingNull(data, maxLength - 1);
+
// Terminate it.
data[i] = '\0';
return i;