-
Notifications
You must be signed in to change notification settings - Fork 1
/
LineBuffer.cpp
68 lines (51 loc) · 1.25 KB
/
LineBuffer.cpp
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
64
65
66
67
68
// LineBuffer.cpp
#include "LineBuffer.h"
#include "CharBuffer.h"
#include "CharChange.h"
LineBuffer::LineBuffer( const CharBuffer& charBuffer )
: m_charBuffer( charBuffer )
{
m_lineLengths.Insert( 0, 0 );
}
void LineBuffer::Update( CharChange change )
{
size_t start = LineContaining( change.start );
size_t end = start;
size_t textStart = LineStart( start );
size_t textLength = 0;
do
{
assert( end < LineCount() );
textLength += m_lineLengths[end++];
}
while ( textStart + textLength < change.end );
m_lineLengths.Erase( start, end - start );
}
size_t LineBuffer::LineCount() const
{
return m_lineLengths.Length();
}
size_t LineBuffer::LineStart( size_t lineIndex ) const
{
assert( lineIndex <= LineCount() );
size_t lineStart = 0;
for ( size_t i = 0; i < lineIndex; ++i )
lineStart += m_lineLengths[i];
return lineStart;
}
size_t LineBuffer::LineLength( size_t lineIndex ) const
{
assert( lineIndex < LineCount() );
return m_lineLengths[lineIndex];
}
size_t LineBuffer::LineContaining( size_t charPos ) const
{
size_t lineStart = 0;
for ( size_t i = 0, count = LineCount(); i < count; ++i )
{
size_t nextLineStart = lineStart + m_lineLengths[i];
if ( charPos < nextLineStart )
return i;
}
return LineCount() - 1;
}