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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/** @file RunStyles.cxx
** Data structure used to store sparse styles.
**/
// Copyright 1998-2007 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <cstdarg>
#include <climits>
#include <stdexcept>
#include <string_view>
#include <vector>
#include <optional>
#include <algorithm>
#include <memory>
#include "Debugging.h"
#include "Position.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
using namespace Scintilla::Internal;
// Find the first run at a position
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::RunFromPosition(DISTANCE position) const noexcept {
DISTANCE run = starts.PartitionFromPosition(position);
// Go to first element with this position
while ((run > 0) && (position == starts.PositionFromPartition(run-1))) {
run--;
}
return run;
}
// If there is no run boundary at position, insert one continuing style.
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::SplitRun(DISTANCE position) {
DISTANCE run = RunFromPosition(position);
const DISTANCE posRun = starts.PositionFromPartition(run);
if (posRun < position) {
STYLE runStyle = ValueAt(position);
run++;
starts.InsertPartition(run, position);
styles.InsertValue(run, 1, runStyle);
}
return run;
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::RemoveRun(DISTANCE run) {
starts.RemovePartition(run);
styles.DeleteRange(run, 1);
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::RemoveRunIfEmpty(DISTANCE run) {
if ((run < starts.Partitions()) && (starts.Partitions() > 1)) {
if (starts.PositionFromPartition(run) == starts.PositionFromPartition(run+1)) {
RemoveRun(run);
}
}
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::RemoveRunIfSameAsPrevious(DISTANCE run) {
if ((run > 0) && (run < starts.Partitions())) {
const DISTANCE runBefore = run - 1;
if (styles.ValueAt(runBefore) == styles.ValueAt(run)) {
RemoveRun(run);
}
}
}
template <typename DISTANCE, typename STYLE>
RunStyles<DISTANCE, STYLE>::RunStyles() {
styles.InsertValue(0, 2, 0);
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::Length() const noexcept {
return starts.PositionFromPartition(starts.Partitions());
}
template <typename DISTANCE, typename STYLE>
STYLE RunStyles<DISTANCE, STYLE>::ValueAt(DISTANCE position) const noexcept {
return styles.ValueAt(starts.PartitionFromPosition(position));
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::FindNextChange(DISTANCE position, DISTANCE end) const noexcept {
const DISTANCE run = starts.PartitionFromPosition(position);
if (run < starts.Partitions()) {
const DISTANCE runChange = starts.PositionFromPartition(run);
if (runChange > position)
return runChange;
const DISTANCE nextChange = starts.PositionFromPartition(run + 1);
if (nextChange > position) {
return nextChange;
} else if (position < end) {
return end;
} else {
return end + 1;
}
} else {
return end + 1;
}
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::StartRun(DISTANCE position) const noexcept {
return starts.PositionFromPartition(starts.PartitionFromPosition(position));
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::EndRun(DISTANCE position) const noexcept {
return starts.PositionFromPartition(starts.PartitionFromPosition(position) + 1);
}
template <typename DISTANCE, typename STYLE>
FillResult<DISTANCE> RunStyles<DISTANCE, STYLE>::FillRange(DISTANCE position, STYLE value, DISTANCE fillLength) {
const FillResult<DISTANCE> resultNoChange{false, position, fillLength};
if (fillLength <= 0) {
return resultNoChange;
}
DISTANCE end = position + fillLength;
if (end > Length()) {
return resultNoChange;
}
DISTANCE runEnd = RunFromPosition(end);
const STYLE valueCurrent = styles.ValueAt(runEnd);
if (valueCurrent == value) {
// End already has value so trim range.
end = starts.PositionFromPartition(runEnd);
if (position >= end) {
// Whole range is already same as value so no action
return resultNoChange;
}
fillLength = end - position;
} else {
const DISTANCE startRun = starts.PositionFromPartition(runEnd);
if (position > startRun) {
const DISTANCE runNext = runEnd + 1;
const DISTANCE endRun = starts.PositionFromPartition(runNext);
if (end < endRun) {
// New piece is completely inside a run with a different value so its a simple
// insertion of two points [ (position, value), (end, valueCurrent) ]
const DISTANCE range[] { position, end};
starts.InsertPartitions(runEnd + 1, range, 2);
// Temporary runEndIndex silences non-useful arithmetic overflow warnings
const ptrdiff_t runEndIndex = runEnd;
styles.Insert(runEndIndex + 1, value);
styles.Insert(runEndIndex + 2, valueCurrent);
return { true, position, fillLength };
}
}
runEnd = SplitRun(end);
}
DISTANCE runStart = RunFromPosition(position);
if (styles.ValueAt(runStart) == value) {
// Start is in expected value so trim range.
runStart++;
position = starts.PositionFromPartition(runStart);
fillLength = end - position;
} else {
if (starts.PositionFromPartition(runStart) < position) {
runStart = SplitRun(position);
runEnd++;
}
}
if (runStart < runEnd) {
const FillResult<DISTANCE> result{ true, position, fillLength };
styles.SetValueAt(runStart, value);
// Remove each old run over the range
for (DISTANCE run=runStart+1; run<runEnd; run++) {
RemoveRun(runStart+1);
}
runEnd = RunFromPosition(end);
RemoveRunIfSameAsPrevious(runEnd);
RemoveRunIfSameAsPrevious(runStart);
runEnd = RunFromPosition(end);
RemoveRunIfEmpty(runEnd);
return result;
}
return resultNoChange;
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::SetValueAt(DISTANCE position, STYLE value) {
FillRange(position, value, 1);
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::InsertSpace(DISTANCE position, DISTANCE insertLength) {
DISTANCE runStart = RunFromPosition(position);
if (starts.PositionFromPartition(runStart) == position) {
STYLE runStyle = ValueAt(position);
// Inserting at start of run so make previous longer
if (runStart == 0) {
// Inserting at start of document so ensure 0
if (runStyle) {
styles.SetValueAt(0, STYLE());
starts.InsertPartition(1, 0);
styles.InsertValue(1, 1, runStyle);
starts.InsertText(0, insertLength);
} else {
starts.InsertText(runStart, insertLength);
}
} else {
if (runStyle) {
starts.InsertText(runStart-1, insertLength);
} else {
// Insert at end of run so do not extend style
starts.InsertText(runStart, insertLength);
}
}
} else {
starts.InsertText(runStart, insertLength);
}
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::DeleteAll() {
starts = Partitioning<DISTANCE>();
styles = SplitVector<STYLE>();
styles.InsertValue(0, 2, 0);
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::DeleteRange(DISTANCE position, DISTANCE deleteLength) {
DISTANCE end = position + deleteLength;
DISTANCE runStart = RunFromPosition(position);
DISTANCE runEnd = RunFromPosition(end);
if (runStart == runEnd) {
// Deleting from inside one run
starts.InsertText(runStart, -deleteLength);
RemoveRunIfEmpty(runStart);
} else {
runStart = SplitRun(position);
runEnd = SplitRun(end);
starts.InsertText(runStart, -deleteLength);
// Remove each old run over the range
for (DISTANCE run=runStart; run<runEnd; run++) {
RemoveRun(runStart);
}
RemoveRunIfEmpty(runStart);
RemoveRunIfSameAsPrevious(runStart);
}
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::Runs() const noexcept {
return starts.Partitions();
}
template <typename DISTANCE, typename STYLE>
bool RunStyles<DISTANCE, STYLE>::AllSame() const noexcept {
for (DISTANCE run = 1; run < starts.Partitions(); run++) {
const DISTANCE runBefore = run - 1;
if (styles.ValueAt(run) != styles.ValueAt(runBefore))
return false;
}
return true;
}
template <typename DISTANCE, typename STYLE>
bool RunStyles<DISTANCE, STYLE>::AllSameAs(STYLE value) const noexcept {
return AllSame() && (styles.ValueAt(0) == value);
}
template <typename DISTANCE, typename STYLE>
DISTANCE RunStyles<DISTANCE, STYLE>::Find(STYLE value, DISTANCE start) const noexcept {
if (start < Length()) {
DISTANCE run = start ? RunFromPosition(start) : 0;
if (styles.ValueAt(run) == value)
return start;
run++;
while (run < starts.Partitions()) {
if (styles.ValueAt(run) == value)
return starts.PositionFromPartition(run);
run++;
}
}
return -1;
}
template <typename DISTANCE, typename STYLE>
void RunStyles<DISTANCE, STYLE>::Check() const {
if (Length() < 0) {
throw std::runtime_error("RunStyles: Length can not be negative.");
}
if (starts.Partitions() < 1) {
throw std::runtime_error("RunStyles: Must always have 1 or more partitions.");
}
if (starts.Partitions() != styles.Length()-1) {
throw std::runtime_error("RunStyles: Partitions and styles different lengths.");
}
DISTANCE start=0;
while (start < Length()) {
const DISTANCE end = EndRun(start);
if (start >= end) {
throw std::runtime_error("RunStyles: Partition is 0 length.");
}
start = end;
}
if (styles.ValueAt(styles.Length()-1) != 0) {
throw std::runtime_error("RunStyles: Unused style at end changed.");
}
for (ptrdiff_t j=1; j<styles.Length()-1; j++) {
if (styles.ValueAt(j) == styles.ValueAt(j-1)) {
throw std::runtime_error("RunStyles: Style of a partition same as previous.");
}
}
}
template class Scintilla::Internal::RunStyles<int, int>;
template class Scintilla::Internal::RunStyles<int, char>;
#if (PTRDIFF_MAX != INT_MAX) || defined(__HAIKU__) || defined(__NetBSD__)
template class Scintilla::Internal::RunStyles<ptrdiff_t, int>;
template class Scintilla::Internal::RunStyles<ptrdiff_t, char>;
#endif
|