MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / appendToDevice

Method appendToDevice

app/src/Console/Handler.cpp:962–1025  ·  view source on GitHub ↗

* @brief Appends a string to the per-device buffer and returns the processed (timestamp-prefixed) * form. */

Source from the content-addressed store, hash-verified

960 * form.
961 */
962QString Console::Handler::appendToDevice(int deviceId, const QString& str, bool addTimestamp)
963{
964 if (str.isEmpty())
965 return QString();
966
967 auto& state = m_deviceState[deviceId];
968
969 auto data = str;
970 if (state.lastCharWasCR && data.startsWith('\n'))
971 data.removeFirst();
972
973 state.lastCharWasCR = data.endsWith('\r');
974
975 data = data.replace(QStringLiteral("\r\n"), QStringLiteral("\n"));
976 data = data.replace(QStringLiteral("\r"), QStringLiteral("\n"));
977
978 QString timestamp;
979 if (addTimestamp)
980 timestamp = cachedTimestampStr();
981
982 QString processedString;
983 processedString.reserve(data.length() + timestamp.length() * 4);
984
985 int pos = 0;
986 while (pos < data.length()) {
987 const int nlPos = data.indexOf('\n', pos);
988 const int end = (nlPos < 0) ? data.length() : nlPos;
989
990 if (end > pos) {
991 const auto segment = QStringView(data).mid(pos, end - pos);
992 if (state.isStartingLine && !segment.trimmed().isEmpty())
993 processedString.append(timestamp);
994
995 processedString.append(segment);
996 state.isStartingLine = false;
997 }
998
999 if (nlPos >= 0) {
1000 if (state.isStartingLine)
1001 processedString.append(timestamp);
1002
1003 processedString.append('\n');
1004 state.isStartingLine = true;
1005 pos = nlPos + 1;
1006 }
1007
1008 else
1009 pos = end;
1010 }
1011
1012 static constexpr int kMaxDeviceBuffer = 10 * 1024;
1013 state.buffer.append(processedString);
1014 if (state.buffer.size() > kMaxDeviceBuffer) {
1015 const int excess = state.buffer.size() - kMaxDeviceBuffer;
1016 state.buffer.remove(0, excess);
1017 }
1018
1019 if (deviceId == m_currentDeviceId || m_currentDeviceId < 0) {

Callers

nothing calls this directly

Calls 6

isEmptyMethod · 0.80
lengthMethod · 0.80
indexOfMethod · 0.45
appendMethod · 0.45
sizeMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected