-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinebuffer.cpp
More file actions
63 lines (48 loc) · 1.4 KB
/
linebuffer.cpp
File metadata and controls
63 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
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
#include "linebuffer.h"
#include <QDebug>
LineBuffer::LineBuffer()
{
}
int LineBuffer::append(QByteArray data)
{
QList<QByteArray> splitData = data.split('\n');
// Note: QByteArray splits "\n" as [[],[]]
int linesAdded = lines.isEmpty() ? 0 : -1;
linesAdded += splitData.length();
auto iter = splitData.begin();
if (!lines.isEmpty())
lines.last().append(*iter++);
while (iter != splitData.end())
lines.append(*iter++);
return linesAdded;
}
QByteArray LineBuffer::joined() const
{
return joinRange(0, lines.size());
}
QByteArray LineBuffer::joinLast(int count) const
{
int start = std::max(0, (int)lines.size() - count);
return joinRange(start, count);
}
QByteArray LineBuffer::joinRange(int start, int count) const
{
if (start >= lines.size())
return {};
// Ensure count doesn't go past the end of the list
count = std::min(start + count, (int)lines.size()) - start;
int targetLength = -1;
for (int i = start; i < start + count; ++i)
targetLength += lines[i].size() + 1;
QByteArray result;
result.reserve(targetLength);
// qDebug() << "CStart" << result.capacity();
result.append(lines[start]);
for (int i = start + 1; i < start + count; ++i)
{
result.append('\n');
result.append(lines[i]);
}
// qDebug() << "CEnd" << result.capacity() << result.size();
return result;
}