Avoid redundant string length function calls

Also switch to `std::string_view` as it is more generic and can handle more types (including
view types).

PR #21861.
This commit is contained in:
Chocobo1
2024-11-19 02:53:16 +08:00
committed by GitHub
parent 530631322d
commit 6ddde3f4b6
9 changed files with 112 additions and 108 deletions

View File

@@ -74,6 +74,7 @@
#include <windows.h>
#endif
#include <QByteArray>
#include <QDataStream>
#include <QFileInfo>
#include <QLocalServer>
@@ -90,7 +91,7 @@ namespace QtLP_Private
#endif
}
const char ACK[] = "ack";
const QByteArray ACK = QByteArrayLiteral("ack");
QtLocalPeer::QtLocalPeer(const QString &path, QObject *parent)
: QObject(parent)
@@ -169,7 +170,7 @@ bool QtLocalPeer::sendMessage(const QString &message, const int timeout)
{
res &= socket.waitForReadyRead(timeout); // wait for ack
if (res)
res &= (socket.read(qstrlen(ACK)) == ACK);
res &= (socket.read(ACK.size()) == ACK);
}
return res;
}
@@ -220,7 +221,7 @@ void QtLocalPeer::receiveConnection()
return;
}
QString message(QString::fromUtf8(uMsg));
socket->write(ACK, qstrlen(ACK));
socket->write(ACK);
socket->waitForBytesWritten(1000);
socket->waitForDisconnected(1000); // make sure client reads ack
delete socket;