xref: /MusicPlayer2/scintilla/src/CellBuffer.cxx (revision 8af74909132ed5e696cb05b6689ae4baf14c1c96)
1*8af74909SZhong Yang // Scintilla source code edit control
2*8af74909SZhong Yang /** @file CellBuffer.cxx
3*8af74909SZhong Yang  ** Manages a buffer of cells.
4*8af74909SZhong Yang  **/
5*8af74909SZhong Yang // Copyright 1998-2001 by Neil Hodgson <[email protected]>
6*8af74909SZhong Yang // The License.txt file describes the conditions under which this software may be distributed.
7*8af74909SZhong Yang 
8*8af74909SZhong Yang #include <cstddef>
9*8af74909SZhong Yang #include <cstdlib>
10*8af74909SZhong Yang #include <cassert>
11*8af74909SZhong Yang #include <cstring>
12*8af74909SZhong Yang #include <cstdio>
13*8af74909SZhong Yang #include <cstdarg>
14*8af74909SZhong Yang 
15*8af74909SZhong Yang #include <stdexcept>
16*8af74909SZhong Yang #include <string>
17*8af74909SZhong Yang #include <string_view>
18*8af74909SZhong Yang #include <vector>
19*8af74909SZhong Yang #include <algorithm>
20*8af74909SZhong Yang #include <memory>
21*8af74909SZhong Yang 
22*8af74909SZhong Yang #include "Platform.h"
23*8af74909SZhong Yang 
24*8af74909SZhong Yang #include "Scintilla.h"
25*8af74909SZhong Yang #include "Position.h"
26*8af74909SZhong Yang #include "SplitVector.h"
27*8af74909SZhong Yang #include "Partitioning.h"
28*8af74909SZhong Yang #include "CellBuffer.h"
29*8af74909SZhong Yang #include "UniConversion.h"
30*8af74909SZhong Yang 
31*8af74909SZhong Yang namespace Scintilla {
32*8af74909SZhong Yang 
33*8af74909SZhong Yang struct CountWidths {
34*8af74909SZhong Yang 	// Measures the number of characters in a string divided into those
35*8af74909SZhong Yang 	// from the Base Multilingual Plane and those from other planes.
36*8af74909SZhong Yang 	Sci::Position countBasePlane;
37*8af74909SZhong Yang 	Sci::Position countOtherPlanes;
CountWidthsScintilla::CountWidths38*8af74909SZhong Yang 	CountWidths(Sci::Position countBasePlane_=0, Sci::Position countOtherPlanes_=0) noexcept :
39*8af74909SZhong Yang 		countBasePlane(countBasePlane_),
40*8af74909SZhong Yang 		countOtherPlanes(countOtherPlanes_) {
41*8af74909SZhong Yang 	}
operator -Scintilla::CountWidths42*8af74909SZhong Yang 	CountWidths operator-() const noexcept {
43*8af74909SZhong Yang 		return CountWidths(-countBasePlane , -countOtherPlanes);
44*8af74909SZhong Yang 	}
WidthUTF32Scintilla::CountWidths45*8af74909SZhong Yang 	Sci::Position WidthUTF32() const noexcept {
46*8af74909SZhong Yang 		// All code points take one code unit in UTF-32.
47*8af74909SZhong Yang 		return countBasePlane + countOtherPlanes;
48*8af74909SZhong Yang 	}
WidthUTF16Scintilla::CountWidths49*8af74909SZhong Yang 	Sci::Position WidthUTF16() const noexcept {
50*8af74909SZhong Yang 		// UTF-16 takes 2 code units for other planes
51*8af74909SZhong Yang 		return countBasePlane + 2 * countOtherPlanes;
52*8af74909SZhong Yang 	}
CountCharScintilla::CountWidths53*8af74909SZhong Yang 	void CountChar(int lenChar) noexcept {
54*8af74909SZhong Yang 		if (lenChar == 4) {
55*8af74909SZhong Yang 			countOtherPlanes++;
56*8af74909SZhong Yang 		} else {
57*8af74909SZhong Yang 			countBasePlane++;
58*8af74909SZhong Yang 		}
59*8af74909SZhong Yang 	}
60*8af74909SZhong Yang };
61*8af74909SZhong Yang 
62*8af74909SZhong Yang class ILineVector {
63*8af74909SZhong Yang public:
64*8af74909SZhong Yang 	virtual void Init() = 0;
65*8af74909SZhong Yang 	virtual void SetPerLine(PerLine *pl) noexcept = 0;
66*8af74909SZhong Yang 	virtual void InsertText(Sci::Line line, Sci::Position delta) noexcept = 0;
67*8af74909SZhong Yang 	virtual void InsertLine(Sci::Line line, Sci::Position position, bool lineStart) = 0;
68*8af74909SZhong Yang 	virtual void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) = 0;
69*8af74909SZhong Yang 	virtual void SetLineStart(Sci::Line line, Sci::Position position) noexcept = 0;
70*8af74909SZhong Yang 	virtual void RemoveLine(Sci::Line line) = 0;
71*8af74909SZhong Yang 	virtual Sci::Line Lines() const noexcept = 0;
72*8af74909SZhong Yang 	virtual Sci::Line LineFromPosition(Sci::Position pos) const noexcept = 0;
73*8af74909SZhong Yang 	virtual Sci::Position LineStart(Sci::Line line) const noexcept = 0;
74*8af74909SZhong Yang 	virtual void InsertCharacters(Sci::Line line, CountWidths delta) noexcept = 0;
75*8af74909SZhong Yang 	virtual void SetLineCharactersWidth(Sci::Line line, CountWidths width) noexcept = 0;
76*8af74909SZhong Yang 	virtual int LineCharacterIndex() const noexcept = 0;
77*8af74909SZhong Yang 	virtual bool AllocateLineCharacterIndex(int lineCharacterIndex, Sci::Line lines) = 0;
78*8af74909SZhong Yang 	virtual bool ReleaseLineCharacterIndex(int lineCharacterIndex) = 0;
79*8af74909SZhong Yang 	virtual Sci::Position IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept = 0;
80*8af74909SZhong Yang 	virtual Sci::Line LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept = 0;
~ILineVector()81*8af74909SZhong Yang 	virtual ~ILineVector() {}
82*8af74909SZhong Yang };
83*8af74909SZhong Yang 
84*8af74909SZhong Yang }
85*8af74909SZhong Yang 
86*8af74909SZhong Yang using namespace Scintilla;
87*8af74909SZhong Yang 
88*8af74909SZhong Yang template <typename POS>
89*8af74909SZhong Yang class LineStartIndex {
90*8af74909SZhong Yang public:
91*8af74909SZhong Yang 	int refCount;
92*8af74909SZhong Yang 	Partitioning<POS> starts;
93*8af74909SZhong Yang 
LineStartIndex()94*8af74909SZhong Yang 	LineStartIndex() : refCount(0), starts(4) {
95*8af74909SZhong Yang 		// Minimal initial allocation
96*8af74909SZhong Yang 	}
97*8af74909SZhong Yang 	// Deleted so LineStartIndex objects can not be copied.
98*8af74909SZhong Yang 	LineStartIndex(const LineStartIndex &) = delete;
99*8af74909SZhong Yang 	LineStartIndex(LineStartIndex &&) = delete;
100*8af74909SZhong Yang 	void operator=(const LineStartIndex &) = delete;
101*8af74909SZhong Yang 	void operator=(LineStartIndex &&) = delete;
~LineStartIndex()102*8af74909SZhong Yang 	virtual ~LineStartIndex() {
103*8af74909SZhong Yang 	}
Allocate(Sci::Line lines)104*8af74909SZhong Yang 	bool Allocate(Sci::Line lines) {
105*8af74909SZhong Yang 		refCount++;
106*8af74909SZhong Yang 		Sci::Position length = starts.PositionFromPartition(starts.Partitions());
107*8af74909SZhong Yang 		for (Sci::Line line = starts.Partitions(); line < lines; line++) {
108*8af74909SZhong Yang 			// Produce an ascending sequence that will be filled in with correct widths later
109*8af74909SZhong Yang 			length++;
110*8af74909SZhong Yang 			starts.InsertPartition(static_cast<POS>(line), static_cast<POS>(length));
111*8af74909SZhong Yang 		}
112*8af74909SZhong Yang 		return refCount == 1;
113*8af74909SZhong Yang 	}
Release()114*8af74909SZhong Yang 	bool Release() {
115*8af74909SZhong Yang 		if (refCount == 1) {
116*8af74909SZhong Yang 			starts.DeleteAll();
117*8af74909SZhong Yang 		}
118*8af74909SZhong Yang 		refCount--;
119*8af74909SZhong Yang 		return refCount == 0;
120*8af74909SZhong Yang 	}
Active() const121*8af74909SZhong Yang 	bool Active() const noexcept {
122*8af74909SZhong Yang 		return refCount > 0;
123*8af74909SZhong Yang 	}
LineWidth(Sci::Line line) const124*8af74909SZhong Yang 	Sci::Position LineWidth(Sci::Line line) const noexcept {
125*8af74909SZhong Yang 		return starts.PositionFromPartition(static_cast<POS>(line) + 1) -
126*8af74909SZhong Yang 			starts.PositionFromPartition(static_cast<POS>(line));
127*8af74909SZhong Yang 	}
SetLineWidth(Sci::Line line,Sci::Position width)128*8af74909SZhong Yang 	void SetLineWidth(Sci::Line line, Sci::Position width) noexcept {
129*8af74909SZhong Yang 		const Sci::Position widthCurrent = LineWidth(line);
130*8af74909SZhong Yang 		starts.InsertText(static_cast<POS>(line), static_cast<POS>(width - widthCurrent));
131*8af74909SZhong Yang 	}
InsertLines(Sci::Line line,Sci::Line lines)132*8af74909SZhong Yang 	void InsertLines(Sci::Line line, Sci::Line lines) {
133*8af74909SZhong Yang 		// Insert multiple lines with each temporarily 1 character wide.
134*8af74909SZhong Yang 		// The line widths will be fixed up by later measuring code.
135*8af74909SZhong Yang 		const POS lineAsPos = static_cast<POS>(line);
136*8af74909SZhong Yang 		const POS lineStart = starts.PositionFromPartition(lineAsPos - 1) + 1;
137*8af74909SZhong Yang 		for (POS l = 0; l < static_cast<POS>(lines); l++) {
138*8af74909SZhong Yang 			starts.InsertPartition(lineAsPos + l, lineStart + l);
139*8af74909SZhong Yang 		}
140*8af74909SZhong Yang 	}
141*8af74909SZhong Yang };
142*8af74909SZhong Yang 
143*8af74909SZhong Yang template <typename POS>
144*8af74909SZhong Yang class LineVector : public ILineVector {
145*8af74909SZhong Yang 	Partitioning<POS> starts;
146*8af74909SZhong Yang 	PerLine *perLine;
147*8af74909SZhong Yang 	LineStartIndex<POS> startsUTF16;
148*8af74909SZhong Yang 	LineStartIndex<POS> startsUTF32;
149*8af74909SZhong Yang 	int activeIndices;
150*8af74909SZhong Yang 
SetActiveIndices()151*8af74909SZhong Yang 	void SetActiveIndices() noexcept {
152*8af74909SZhong Yang 		activeIndices = (startsUTF32.Active() ? SC_LINECHARACTERINDEX_UTF32 : 0)
153*8af74909SZhong Yang 			| (startsUTF16.Active() ? SC_LINECHARACTERINDEX_UTF16 : 0);
154*8af74909SZhong Yang 	}
155*8af74909SZhong Yang 
156*8af74909SZhong Yang public:
LineVector()157*8af74909SZhong Yang 	LineVector() : starts(256), perLine(nullptr), activeIndices(0) {
158*8af74909SZhong Yang 	}
159*8af74909SZhong Yang 	// Deleted so LineVector objects can not be copied.
160*8af74909SZhong Yang 	LineVector(const LineVector &) = delete;
161*8af74909SZhong Yang 	LineVector(LineVector &&) = delete;
162*8af74909SZhong Yang 	LineVector &operator=(const LineVector &) = delete;
163*8af74909SZhong Yang 	LineVector &operator=(LineVector &&) = delete;
~LineVector()164*8af74909SZhong Yang 	~LineVector() override {
165*8af74909SZhong Yang 	}
Init()166*8af74909SZhong Yang 	void Init() override {
167*8af74909SZhong Yang 		starts.DeleteAll();
168*8af74909SZhong Yang 		if (perLine) {
169*8af74909SZhong Yang 			perLine->Init();
170*8af74909SZhong Yang 		}
171*8af74909SZhong Yang 		startsUTF32.starts.DeleteAll();
172*8af74909SZhong Yang 		startsUTF16.starts.DeleteAll();
173*8af74909SZhong Yang 	}
SetPerLine(PerLine * pl)174*8af74909SZhong Yang 	void SetPerLine(PerLine *pl) noexcept override {
175*8af74909SZhong Yang 		perLine = pl;
176*8af74909SZhong Yang 	}
InsertText(Sci::Line line,Sci::Position delta)177*8af74909SZhong Yang 	void InsertText(Sci::Line line, Sci::Position delta) noexcept override {
178*8af74909SZhong Yang 		starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta));
179*8af74909SZhong Yang 	}
InsertLine(Sci::Line line,Sci::Position position,bool lineStart)180*8af74909SZhong Yang 	void InsertLine(Sci::Line line, Sci::Position position, bool lineStart) override {
181*8af74909SZhong Yang 		const POS lineAsPos = static_cast<POS>(line);
182*8af74909SZhong Yang 		starts.InsertPartition(lineAsPos, static_cast<POS>(position));
183*8af74909SZhong Yang 		if (activeIndices) {
184*8af74909SZhong Yang 			if (activeIndices & SC_LINECHARACTERINDEX_UTF32) {
185*8af74909SZhong Yang 				startsUTF32.InsertLines(line, 1);
186*8af74909SZhong Yang 			}
187*8af74909SZhong Yang 			if (activeIndices & SC_LINECHARACTERINDEX_UTF16) {
188*8af74909SZhong Yang 				startsUTF16.InsertLines(line, 1);
189*8af74909SZhong Yang 			}
190*8af74909SZhong Yang 		}
191*8af74909SZhong Yang 		if (perLine) {
192*8af74909SZhong Yang 			if ((line > 0) && lineStart)
193*8af74909SZhong Yang 				line--;
194*8af74909SZhong Yang 			perLine->InsertLine(line);
195*8af74909SZhong Yang 		}
196*8af74909SZhong Yang 	}
InsertLines(Sci::Line line,const Sci::Position * positions,size_t lines,bool lineStart)197*8af74909SZhong Yang 	void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) override {
198*8af74909SZhong Yang 		const POS lineAsPos = static_cast<POS>(line);
199*8af74909SZhong Yang 		if constexpr (sizeof(Sci::Position) == sizeof(POS)) {
200*8af74909SZhong Yang 			starts.InsertPartitions(lineAsPos, positions, lines);
201*8af74909SZhong Yang 		} else {
202*8af74909SZhong Yang 			starts.InsertPartitionsWithCast(lineAsPos, positions, lines);
203*8af74909SZhong Yang 		}
204*8af74909SZhong Yang 		if (activeIndices) {
205*8af74909SZhong Yang 			if (activeIndices & SC_LINECHARACTERINDEX_UTF32) {
206*8af74909SZhong Yang 				startsUTF32.InsertLines(line, lines);
207*8af74909SZhong Yang 			}
208*8af74909SZhong Yang 			if (activeIndices & SC_LINECHARACTERINDEX_UTF16) {
209*8af74909SZhong Yang 				startsUTF16.InsertLines(line, lines);
210*8af74909SZhong Yang 			}
211*8af74909SZhong Yang 		}
212*8af74909SZhong Yang 		if (perLine) {
213*8af74909SZhong Yang 			if ((line > 0) && lineStart)
214*8af74909SZhong Yang 				line--;
215*8af74909SZhong Yang 			perLine->InsertLines(line, lines);
216*8af74909SZhong Yang 		}
217*8af74909SZhong Yang 	}
SetLineStart(Sci::Line line,Sci::Position position)218*8af74909SZhong Yang 	void SetLineStart(Sci::Line line, Sci::Position position) noexcept override {
219*8af74909SZhong Yang 		starts.SetPartitionStartPosition(static_cast<POS>(line), static_cast<POS>(position));
220*8af74909SZhong Yang 	}
RemoveLine(Sci::Line line)221*8af74909SZhong Yang 	void RemoveLine(Sci::Line line) override {
222*8af74909SZhong Yang 		starts.RemovePartition(static_cast<POS>(line));
223*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF32) {
224*8af74909SZhong Yang 			startsUTF32.starts.RemovePartition(static_cast<POS>(line));
225*8af74909SZhong Yang 		}
226*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF16) {
227*8af74909SZhong Yang 			startsUTF16.starts.RemovePartition(static_cast<POS>(line));
228*8af74909SZhong Yang 		}
229*8af74909SZhong Yang 		if (perLine) {
230*8af74909SZhong Yang 			perLine->RemoveLine(line);
231*8af74909SZhong Yang 		}
232*8af74909SZhong Yang 	}
Lines() const233*8af74909SZhong Yang 	Sci::Line Lines() const noexcept override {
234*8af74909SZhong Yang 		return static_cast<Sci::Line>(starts.Partitions());
235*8af74909SZhong Yang 	}
LineFromPosition(Sci::Position pos) const236*8af74909SZhong Yang 	Sci::Line LineFromPosition(Sci::Position pos) const noexcept override {
237*8af74909SZhong Yang 		return static_cast<Sci::Line>(starts.PartitionFromPosition(static_cast<POS>(pos)));
238*8af74909SZhong Yang 	}
LineStart(Sci::Line line) const239*8af74909SZhong Yang 	Sci::Position LineStart(Sci::Line line) const noexcept override {
240*8af74909SZhong Yang 		return starts.PositionFromPartition(static_cast<POS>(line));
241*8af74909SZhong Yang 	}
InsertCharacters(Sci::Line line,CountWidths delta)242*8af74909SZhong Yang 	void InsertCharacters(Sci::Line line, CountWidths delta) noexcept override {
243*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF32) {
244*8af74909SZhong Yang 			startsUTF32.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF32()));
245*8af74909SZhong Yang 		}
246*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF16) {
247*8af74909SZhong Yang 			startsUTF16.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF16()));
248*8af74909SZhong Yang 		}
249*8af74909SZhong Yang 	}
SetLineCharactersWidth(Sci::Line line,CountWidths width)250*8af74909SZhong Yang 	void SetLineCharactersWidth(Sci::Line line, CountWidths width) noexcept override {
251*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF32) {
252*8af74909SZhong Yang 			assert(startsUTF32.starts.Partitions() == starts.Partitions());
253*8af74909SZhong Yang 			startsUTF32.SetLineWidth(line, width.WidthUTF32());
254*8af74909SZhong Yang 		}
255*8af74909SZhong Yang 		if (activeIndices & SC_LINECHARACTERINDEX_UTF16) {
256*8af74909SZhong Yang 			assert(startsUTF16.starts.Partitions() == starts.Partitions());
257*8af74909SZhong Yang 			startsUTF16.SetLineWidth(line, width.WidthUTF16());
258*8af74909SZhong Yang 		}
259*8af74909SZhong Yang 	}
260*8af74909SZhong Yang 
LineCharacterIndex() const261*8af74909SZhong Yang 	int LineCharacterIndex() const noexcept override {
262*8af74909SZhong Yang 		return activeIndices;
263*8af74909SZhong Yang 	}
AllocateLineCharacterIndex(int lineCharacterIndex,Sci::Line lines)264*8af74909SZhong Yang 	bool AllocateLineCharacterIndex(int lineCharacterIndex, Sci::Line lines) override {
265*8af74909SZhong Yang 		const int activeIndicesStart = activeIndices;
266*8af74909SZhong Yang 		if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF32) != 0) {
267*8af74909SZhong Yang 			startsUTF32.Allocate(lines);
268*8af74909SZhong Yang 			assert(startsUTF32.starts.Partitions() == starts.Partitions());
269*8af74909SZhong Yang 		}
270*8af74909SZhong Yang 		if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF16) != 0) {
271*8af74909SZhong Yang 			startsUTF16.Allocate(lines);
272*8af74909SZhong Yang 			assert(startsUTF16.starts.Partitions() == starts.Partitions());
273*8af74909SZhong Yang 		}
274*8af74909SZhong Yang 		SetActiveIndices();
275*8af74909SZhong Yang 		return activeIndicesStart != activeIndices;
276*8af74909SZhong Yang 	}
ReleaseLineCharacterIndex(int lineCharacterIndex)277*8af74909SZhong Yang 	bool ReleaseLineCharacterIndex(int lineCharacterIndex) override {
278*8af74909SZhong Yang 		const int activeIndicesStart = activeIndices;
279*8af74909SZhong Yang 		if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF32) != 0) {
280*8af74909SZhong Yang 			startsUTF32.Release();
281*8af74909SZhong Yang 		}
282*8af74909SZhong Yang 		if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF16) != 0) {
283*8af74909SZhong Yang 			startsUTF16.Release();
284*8af74909SZhong Yang 		}
285*8af74909SZhong Yang 		SetActiveIndices();
286*8af74909SZhong Yang 		return activeIndicesStart != activeIndices;
287*8af74909SZhong Yang 	}
IndexLineStart(Sci::Line line,int lineCharacterIndex) const288*8af74909SZhong Yang 	Sci::Position IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept override {
289*8af74909SZhong Yang 		if (lineCharacterIndex == SC_LINECHARACTERINDEX_UTF32) {
290*8af74909SZhong Yang 			return startsUTF32.starts.PositionFromPartition(static_cast<POS>(line));
291*8af74909SZhong Yang 		} else {
292*8af74909SZhong Yang 			return startsUTF16.starts.PositionFromPartition(static_cast<POS>(line));
293*8af74909SZhong Yang 		}
294*8af74909SZhong Yang 	}
LineFromPositionIndex(Sci::Position pos,int lineCharacterIndex) const295*8af74909SZhong Yang 	Sci::Line LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept override {
296*8af74909SZhong Yang 		if (lineCharacterIndex == SC_LINECHARACTERINDEX_UTF32) {
297*8af74909SZhong Yang 			return static_cast<Sci::Line>(startsUTF32.starts.PartitionFromPosition(static_cast<POS>(pos)));
298*8af74909SZhong Yang 		} else {
299*8af74909SZhong Yang 			return static_cast<Sci::Line>(startsUTF16.starts.PartitionFromPosition(static_cast<POS>(pos)));
300*8af74909SZhong Yang 		}
301*8af74909SZhong Yang 	}
302*8af74909SZhong Yang };
303*8af74909SZhong Yang 
Action()304*8af74909SZhong Yang Action::Action() noexcept {
305*8af74909SZhong Yang 	at = startAction;
306*8af74909SZhong Yang 	position = 0;
307*8af74909SZhong Yang 	lenData = 0;
308*8af74909SZhong Yang 	mayCoalesce = false;
309*8af74909SZhong Yang }
310*8af74909SZhong Yang 
~Action()311*8af74909SZhong Yang Action::~Action() {
312*8af74909SZhong Yang }
313*8af74909SZhong Yang 
Create(actionType at_,Sci::Position position_,const char * data_,Sci::Position lenData_,bool mayCoalesce_)314*8af74909SZhong Yang void Action::Create(actionType at_, Sci::Position position_, const char *data_, Sci::Position lenData_, bool mayCoalesce_) {
315*8af74909SZhong Yang 	data = nullptr;
316*8af74909SZhong Yang 	position = position_;
317*8af74909SZhong Yang 	at = at_;
318*8af74909SZhong Yang 	if (lenData_) {
319*8af74909SZhong Yang 		data = std::make_unique<char[]>(lenData_);
320*8af74909SZhong Yang 		memcpy(&data[0], data_, lenData_);
321*8af74909SZhong Yang 	}
322*8af74909SZhong Yang 	lenData = lenData_;
323*8af74909SZhong Yang 	mayCoalesce = mayCoalesce_;
324*8af74909SZhong Yang }
325*8af74909SZhong Yang 
Clear()326*8af74909SZhong Yang void Action::Clear() noexcept {
327*8af74909SZhong Yang 	data = nullptr;
328*8af74909SZhong Yang 	lenData = 0;
329*8af74909SZhong Yang }
330*8af74909SZhong Yang 
331*8af74909SZhong Yang // The undo history stores a sequence of user operations that represent the user's view of the
332*8af74909SZhong Yang // commands executed on the text.
333*8af74909SZhong Yang // Each user operation contains a sequence of text insertion and text deletion actions.
334*8af74909SZhong Yang // All the user operations are stored in a list of individual actions with 'start' actions used
335*8af74909SZhong Yang // as delimiters between user operations.
336*8af74909SZhong Yang // Initially there is one start action in the history.
337*8af74909SZhong Yang // As each action is performed, it is recorded in the history. The action may either become
338*8af74909SZhong Yang // part of the current user operation or may start a new user operation. If it is to be part of the
339*8af74909SZhong Yang // current operation, then it overwrites the current last action. If it is to be part of a new
340*8af74909SZhong Yang // operation, it is appended after the current last action.
341*8af74909SZhong Yang // After writing the new action, a new start action is appended at the end of the history.
342*8af74909SZhong Yang // The decision of whether to start a new user operation is based upon two factors. If a
343*8af74909SZhong Yang // compound operation has been explicitly started by calling BeginUndoAction and no matching
344*8af74909SZhong Yang // EndUndoAction (these calls nest) has been called, then the action is coalesced into the current
345*8af74909SZhong Yang // operation. If there is no outstanding BeginUndoAction call then a new operation is started
346*8af74909SZhong Yang // unless it looks as if the new action is caused by the user typing or deleting a stream of text.
347*8af74909SZhong Yang // Sequences that look like typing or deletion are coalesced into a single user operation.
348*8af74909SZhong Yang 
UndoHistory()349*8af74909SZhong Yang UndoHistory::UndoHistory() {
350*8af74909SZhong Yang 
351*8af74909SZhong Yang 	actions.resize(3);
352*8af74909SZhong Yang 	maxAction = 0;
353*8af74909SZhong Yang 	currentAction = 0;
354*8af74909SZhong Yang 	undoSequenceDepth = 0;
355*8af74909SZhong Yang 	savePoint = 0;
356*8af74909SZhong Yang 	tentativePoint = -1;
357*8af74909SZhong Yang 
358*8af74909SZhong Yang 	actions[currentAction].Create(startAction);
359*8af74909SZhong Yang }
360*8af74909SZhong Yang 
~UndoHistory()361*8af74909SZhong Yang UndoHistory::~UndoHistory() {
362*8af74909SZhong Yang }
363*8af74909SZhong Yang 
EnsureUndoRoom()364*8af74909SZhong Yang void UndoHistory::EnsureUndoRoom() {
365*8af74909SZhong Yang 	// Have to test that there is room for 2 more actions in the array
366*8af74909SZhong Yang 	// as two actions may be created by the calling function
367*8af74909SZhong Yang 	if (static_cast<size_t>(currentAction) >= (actions.size() - 2)) {
368*8af74909SZhong Yang 		// Run out of undo nodes so extend the array
369*8af74909SZhong Yang 		actions.resize(actions.size() * 2);
370*8af74909SZhong Yang 	}
371*8af74909SZhong Yang }
372*8af74909SZhong Yang 
AppendAction(actionType at,Sci::Position position,const char * data,Sci::Position lengthData,bool & startSequence,bool mayCoalesce)373*8af74909SZhong Yang const char *UndoHistory::AppendAction(actionType at, Sci::Position position, const char *data, Sci::Position lengthData,
374*8af74909SZhong Yang 	bool &startSequence, bool mayCoalesce) {
375*8af74909SZhong Yang 	EnsureUndoRoom();
376*8af74909SZhong Yang 	//Platform::DebugPrintf("%% %d action %d %d %d\n", at, position, lengthData, currentAction);
377*8af74909SZhong Yang 	//Platform::DebugPrintf("^ %d action %d %d\n", actions[currentAction - 1].at,
378*8af74909SZhong Yang 	//	actions[currentAction - 1].position, actions[currentAction - 1].lenData);
379*8af74909SZhong Yang 	if (currentAction < savePoint) {
380*8af74909SZhong Yang 		savePoint = -1;
381*8af74909SZhong Yang 	}
382*8af74909SZhong Yang 	int oldCurrentAction = currentAction;
383*8af74909SZhong Yang 	if (currentAction >= 1) {
384*8af74909SZhong Yang 		if (0 == undoSequenceDepth) {
385*8af74909SZhong Yang 			// Top level actions may not always be coalesced
386*8af74909SZhong Yang 			int targetAct = -1;
387*8af74909SZhong Yang 			const Action *actPrevious = &(actions[currentAction + targetAct]);
388*8af74909SZhong Yang 			// Container actions may forward the coalesce state of Scintilla Actions.
389*8af74909SZhong Yang 			while ((actPrevious->at == containerAction) && actPrevious->mayCoalesce) {
390*8af74909SZhong Yang 				targetAct--;
391*8af74909SZhong Yang 				actPrevious = &(actions[currentAction + targetAct]);
392*8af74909SZhong Yang 			}
393*8af74909SZhong Yang 			// See if current action can be coalesced into previous action
394*8af74909SZhong Yang 			// Will work if both are inserts or deletes and position is same
395*8af74909SZhong Yang 			if ((currentAction == savePoint) || (currentAction == tentativePoint)) {
396*8af74909SZhong Yang 				currentAction++;
397*8af74909SZhong Yang 			} else if (!actions[currentAction].mayCoalesce) {
398*8af74909SZhong Yang 				// Not allowed to coalesce if this set
399*8af74909SZhong Yang 				currentAction++;
400*8af74909SZhong Yang 			} else if (!mayCoalesce || !actPrevious->mayCoalesce) {
401*8af74909SZhong Yang 				currentAction++;
402*8af74909SZhong Yang 			} else if (at == containerAction || actions[currentAction].at == containerAction) {
403*8af74909SZhong Yang 				;	// A coalescible containerAction
404*8af74909SZhong Yang 			} else if ((at != actPrevious->at) && (actPrevious->at != startAction)) {
405*8af74909SZhong Yang 				currentAction++;
406*8af74909SZhong Yang 			} else if ((at == insertAction) &&
407*8af74909SZhong Yang 			           (position != (actPrevious->position + actPrevious->lenData))) {
408*8af74909SZhong Yang 				// Insertions must be immediately after to coalesce
409*8af74909SZhong Yang 				currentAction++;
410*8af74909SZhong Yang 			} else if (at == removeAction) {
411*8af74909SZhong Yang 				if ((lengthData == 1) || (lengthData == 2)) {
412*8af74909SZhong Yang 					if ((position + lengthData) == actPrevious->position) {
413*8af74909SZhong Yang 						; // Backspace -> OK
414*8af74909SZhong Yang 					} else if (position == actPrevious->position) {
415*8af74909SZhong Yang 						; // Delete -> OK
416*8af74909SZhong Yang 					} else {
417*8af74909SZhong Yang 						// Removals must be at same position to coalesce
418*8af74909SZhong Yang 						currentAction++;
419*8af74909SZhong Yang 					}
420*8af74909SZhong Yang 				} else {
421*8af74909SZhong Yang 					// Removals must be of one character to coalesce
422*8af74909SZhong Yang 					currentAction++;
423*8af74909SZhong Yang 				}
424*8af74909SZhong Yang 			} else {
425*8af74909SZhong Yang 				// Action coalesced.
426*8af74909SZhong Yang 			}
427*8af74909SZhong Yang 
428*8af74909SZhong Yang 		} else {
429*8af74909SZhong Yang 			// Actions not at top level are always coalesced unless this is after return to top level
430*8af74909SZhong Yang 			if (!actions[currentAction].mayCoalesce)
431*8af74909SZhong Yang 				currentAction++;
432*8af74909SZhong Yang 		}
433*8af74909SZhong Yang 	} else {
434*8af74909SZhong Yang 		currentAction++;
435*8af74909SZhong Yang 	}
436*8af74909SZhong Yang 	startSequence = oldCurrentAction != currentAction;
437*8af74909SZhong Yang 	const int actionWithData = currentAction;
438*8af74909SZhong Yang 	actions[currentAction].Create(at, position, data, lengthData, mayCoalesce);
439*8af74909SZhong Yang 	currentAction++;
440*8af74909SZhong Yang 	actions[currentAction].Create(startAction);
441*8af74909SZhong Yang 	maxAction = currentAction;
442*8af74909SZhong Yang 	return actions[actionWithData].data.get();
443*8af74909SZhong Yang }
444*8af74909SZhong Yang 
BeginUndoAction()445*8af74909SZhong Yang void UndoHistory::BeginUndoAction() {
446*8af74909SZhong Yang 	EnsureUndoRoom();
447*8af74909SZhong Yang 	if (undoSequenceDepth == 0) {
448*8af74909SZhong Yang 		if (actions[currentAction].at != startAction) {
449*8af74909SZhong Yang 			currentAction++;
450*8af74909SZhong Yang 			actions[currentAction].Create(startAction);
451*8af74909SZhong Yang 			maxAction = currentAction;
452*8af74909SZhong Yang 		}
453*8af74909SZhong Yang 		actions[currentAction].mayCoalesce = false;
454*8af74909SZhong Yang 	}
455*8af74909SZhong Yang 	undoSequenceDepth++;
456*8af74909SZhong Yang }
457*8af74909SZhong Yang 
EndUndoAction()458*8af74909SZhong Yang void UndoHistory::EndUndoAction() {
459*8af74909SZhong Yang 	PLATFORM_ASSERT(undoSequenceDepth > 0);
460*8af74909SZhong Yang 	EnsureUndoRoom();
461*8af74909SZhong Yang 	undoSequenceDepth--;
462*8af74909SZhong Yang 	if (0 == undoSequenceDepth) {
463*8af74909SZhong Yang 		if (actions[currentAction].at != startAction) {
464*8af74909SZhong Yang 			currentAction++;
465*8af74909SZhong Yang 			actions[currentAction].Create(startAction);
466*8af74909SZhong Yang 			maxAction = currentAction;
467*8af74909SZhong Yang 		}
468*8af74909SZhong Yang 		actions[currentAction].mayCoalesce = false;
469*8af74909SZhong Yang 	}
470*8af74909SZhong Yang }
471*8af74909SZhong Yang 
DropUndoSequence()472*8af74909SZhong Yang void UndoHistory::DropUndoSequence() {
473*8af74909SZhong Yang 	undoSequenceDepth = 0;
474*8af74909SZhong Yang }
475*8af74909SZhong Yang 
DeleteUndoHistory()476*8af74909SZhong Yang void UndoHistory::DeleteUndoHistory() {
477*8af74909SZhong Yang 	for (int i = 1; i < maxAction; i++)
478*8af74909SZhong Yang 		actions[i].Clear();
479*8af74909SZhong Yang 	maxAction = 0;
480*8af74909SZhong Yang 	currentAction = 0;
481*8af74909SZhong Yang 	actions[currentAction].Create(startAction);
482*8af74909SZhong Yang 	savePoint = 0;
483*8af74909SZhong Yang 	tentativePoint = -1;
484*8af74909SZhong Yang }
485*8af74909SZhong Yang 
SetSavePoint()486*8af74909SZhong Yang void UndoHistory::SetSavePoint() noexcept {
487*8af74909SZhong Yang 	savePoint = currentAction;
488*8af74909SZhong Yang }
489*8af74909SZhong Yang 
IsSavePoint() const490*8af74909SZhong Yang bool UndoHistory::IsSavePoint() const noexcept {
491*8af74909SZhong Yang 	return savePoint == currentAction;
492*8af74909SZhong Yang }
493*8af74909SZhong Yang 
TentativeStart()494*8af74909SZhong Yang void UndoHistory::TentativeStart() {
495*8af74909SZhong Yang 	tentativePoint = currentAction;
496*8af74909SZhong Yang }
497*8af74909SZhong Yang 
TentativeCommit()498*8af74909SZhong Yang void UndoHistory::TentativeCommit() {
499*8af74909SZhong Yang 	tentativePoint = -1;
500*8af74909SZhong Yang 	// Truncate undo history
501*8af74909SZhong Yang 	maxAction = currentAction;
502*8af74909SZhong Yang }
503*8af74909SZhong Yang 
TentativeActive() const504*8af74909SZhong Yang bool UndoHistory::TentativeActive() const noexcept {
505*8af74909SZhong Yang 	return tentativePoint >= 0;
506*8af74909SZhong Yang }
507*8af74909SZhong Yang 
TentativeSteps()508*8af74909SZhong Yang int UndoHistory::TentativeSteps() noexcept {
509*8af74909SZhong Yang 	// Drop any trailing startAction
510*8af74909SZhong Yang 	if (actions[currentAction].at == startAction && currentAction > 0)
511*8af74909SZhong Yang 		currentAction--;
512*8af74909SZhong Yang 	if (tentativePoint >= 0)
513*8af74909SZhong Yang 		return currentAction - tentativePoint;
514*8af74909SZhong Yang 	else
515*8af74909SZhong Yang 		return -1;
516*8af74909SZhong Yang }
517*8af74909SZhong Yang 
CanUndo() const518*8af74909SZhong Yang bool UndoHistory::CanUndo() const noexcept {
519*8af74909SZhong Yang 	return (currentAction > 0) && (maxAction > 0);
520*8af74909SZhong Yang }
521*8af74909SZhong Yang 
StartUndo()522*8af74909SZhong Yang int UndoHistory::StartUndo() {
523*8af74909SZhong Yang 	// Drop any trailing startAction
524*8af74909SZhong Yang 	if (actions[currentAction].at == startAction && currentAction > 0)
525*8af74909SZhong Yang 		currentAction--;
526*8af74909SZhong Yang 
527*8af74909SZhong Yang 	// Count the steps in this action
528*8af74909SZhong Yang 	int act = currentAction;
529*8af74909SZhong Yang 	while (actions[act].at != startAction && act > 0) {
530*8af74909SZhong Yang 		act--;
531*8af74909SZhong Yang 	}
532*8af74909SZhong Yang 	return currentAction - act;
533*8af74909SZhong Yang }
534*8af74909SZhong Yang 
GetUndoStep() const535*8af74909SZhong Yang const Action &UndoHistory::GetUndoStep() const {
536*8af74909SZhong Yang 	return actions[currentAction];
537*8af74909SZhong Yang }
538*8af74909SZhong Yang 
CompletedUndoStep()539*8af74909SZhong Yang void UndoHistory::CompletedUndoStep() {
540*8af74909SZhong Yang 	currentAction--;
541*8af74909SZhong Yang }
542*8af74909SZhong Yang 
CanRedo() const543*8af74909SZhong Yang bool UndoHistory::CanRedo() const noexcept {
544*8af74909SZhong Yang 	return maxAction > currentAction;
545*8af74909SZhong Yang }
546*8af74909SZhong Yang 
StartRedo()547*8af74909SZhong Yang int UndoHistory::StartRedo() {
548*8af74909SZhong Yang 	// Drop any leading startAction
549*8af74909SZhong Yang 	if (currentAction < maxAction && actions[currentAction].at == startAction)
550*8af74909SZhong Yang 		currentAction++;
551*8af74909SZhong Yang 
552*8af74909SZhong Yang 	// Count the steps in this action
553*8af74909SZhong Yang 	int act = currentAction;
554*8af74909SZhong Yang 	while (act < maxAction && actions[act].at != startAction) {
555*8af74909SZhong Yang 		act++;
556*8af74909SZhong Yang 	}
557*8af74909SZhong Yang 	return act - currentAction;
558*8af74909SZhong Yang }
559*8af74909SZhong Yang 
GetRedoStep() const560*8af74909SZhong Yang const Action &UndoHistory::GetRedoStep() const {
561*8af74909SZhong Yang 	return actions[currentAction];
562*8af74909SZhong Yang }
563*8af74909SZhong Yang 
CompletedRedoStep()564*8af74909SZhong Yang void UndoHistory::CompletedRedoStep() {
565*8af74909SZhong Yang 	currentAction++;
566*8af74909SZhong Yang }
567*8af74909SZhong Yang 
CellBuffer(bool hasStyles_,bool largeDocument_)568*8af74909SZhong Yang CellBuffer::CellBuffer(bool hasStyles_, bool largeDocument_) :
569*8af74909SZhong Yang 	hasStyles(hasStyles_), largeDocument(largeDocument_) {
570*8af74909SZhong Yang 	readOnly = false;
571*8af74909SZhong Yang 	utf8Substance = false;
572*8af74909SZhong Yang 	utf8LineEnds = 0;
573*8af74909SZhong Yang 	collectingUndo = true;
574*8af74909SZhong Yang 	if (largeDocument)
575*8af74909SZhong Yang 		plv = std::make_unique<LineVector<Sci::Position>>();
576*8af74909SZhong Yang 	else
577*8af74909SZhong Yang 		plv = std::make_unique<LineVector<int>>();
578*8af74909SZhong Yang }
579*8af74909SZhong Yang 
~CellBuffer()580*8af74909SZhong Yang CellBuffer::~CellBuffer() {
581*8af74909SZhong Yang }
582*8af74909SZhong Yang 
CharAt(Sci::Position position) const583*8af74909SZhong Yang char CellBuffer::CharAt(Sci::Position position) const noexcept {
584*8af74909SZhong Yang 	return substance.ValueAt(position);
585*8af74909SZhong Yang }
586*8af74909SZhong Yang 
UCharAt(Sci::Position position) const587*8af74909SZhong Yang unsigned char CellBuffer::UCharAt(Sci::Position position) const noexcept {
588*8af74909SZhong Yang 	return substance.ValueAt(position);
589*8af74909SZhong Yang }
590*8af74909SZhong Yang 
GetCharRange(char * buffer,Sci::Position position,Sci::Position lengthRetrieve) const591*8af74909SZhong Yang void CellBuffer::GetCharRange(char *buffer, Sci::Position position, Sci::Position lengthRetrieve) const {
592*8af74909SZhong Yang 	if (lengthRetrieve <= 0)
593*8af74909SZhong Yang 		return;
594*8af74909SZhong Yang 	if (position < 0)
595*8af74909SZhong Yang 		return;
596*8af74909SZhong Yang 	if ((position + lengthRetrieve) > substance.Length()) {
597*8af74909SZhong Yang 		Platform::DebugPrintf("Bad GetCharRange %.0f for %.0f of %.0f\n",
598*8af74909SZhong Yang 				      static_cast<double>(position),
599*8af74909SZhong Yang 				      static_cast<double>(lengthRetrieve),
600*8af74909SZhong Yang 				      static_cast<double>(substance.Length()));
601*8af74909SZhong Yang 		return;
602*8af74909SZhong Yang 	}
603*8af74909SZhong Yang 	substance.GetRange(buffer, position, lengthRetrieve);
604*8af74909SZhong Yang }
605*8af74909SZhong Yang 
StyleAt(Sci::Position position) const606*8af74909SZhong Yang char CellBuffer::StyleAt(Sci::Position position) const noexcept {
607*8af74909SZhong Yang 	return hasStyles ? style.ValueAt(position) : 0;
608*8af74909SZhong Yang }
609*8af74909SZhong Yang 
GetStyleRange(unsigned char * buffer,Sci::Position position,Sci::Position lengthRetrieve) const610*8af74909SZhong Yang void CellBuffer::GetStyleRange(unsigned char *buffer, Sci::Position position, Sci::Position lengthRetrieve) const {
611*8af74909SZhong Yang 	if (lengthRetrieve < 0)
612*8af74909SZhong Yang 		return;
613*8af74909SZhong Yang 	if (position < 0)
614*8af74909SZhong Yang 		return;
615*8af74909SZhong Yang 	if (!hasStyles) {
616*8af74909SZhong Yang 		std::fill(buffer, buffer + lengthRetrieve, static_cast<unsigned char>(0));
617*8af74909SZhong Yang 		return;
618*8af74909SZhong Yang 	}
619*8af74909SZhong Yang 	if ((position + lengthRetrieve) > style.Length()) {
620*8af74909SZhong Yang 		Platform::DebugPrintf("Bad GetStyleRange %.0f for %.0f of %.0f\n",
621*8af74909SZhong Yang 				      static_cast<double>(position),
622*8af74909SZhong Yang 				      static_cast<double>(lengthRetrieve),
623*8af74909SZhong Yang 				      static_cast<double>(style.Length()));
624*8af74909SZhong Yang 		return;
625*8af74909SZhong Yang 	}
626*8af74909SZhong Yang 	style.GetRange(reinterpret_cast<char *>(buffer), position, lengthRetrieve);
627*8af74909SZhong Yang }
628*8af74909SZhong Yang 
BufferPointer()629*8af74909SZhong Yang const char *CellBuffer::BufferPointer() {
630*8af74909SZhong Yang 	return substance.BufferPointer();
631*8af74909SZhong Yang }
632*8af74909SZhong Yang 
RangePointer(Sci::Position position,Sci::Position rangeLength)633*8af74909SZhong Yang const char *CellBuffer::RangePointer(Sci::Position position, Sci::Position rangeLength) noexcept {
634*8af74909SZhong Yang 	return substance.RangePointer(position, rangeLength);
635*8af74909SZhong Yang }
636*8af74909SZhong Yang 
GapPosition() const637*8af74909SZhong Yang Sci::Position CellBuffer::GapPosition() const noexcept {
638*8af74909SZhong Yang 	return substance.GapPosition();
639*8af74909SZhong Yang }
640*8af74909SZhong Yang 
641*8af74909SZhong Yang // The char* returned is to an allocation owned by the undo history
InsertString(Sci::Position position,const char * s,Sci::Position insertLength,bool & startSequence)642*8af74909SZhong Yang const char *CellBuffer::InsertString(Sci::Position position, const char *s, Sci::Position insertLength, bool &startSequence) {
643*8af74909SZhong Yang 	// InsertString and DeleteChars are the bottleneck though which all changes occur
644*8af74909SZhong Yang 	const char *data = s;
645*8af74909SZhong Yang 	if (!readOnly) {
646*8af74909SZhong Yang 		if (collectingUndo) {
647*8af74909SZhong Yang 			// Save into the undo/redo stack, but only the characters - not the formatting
648*8af74909SZhong Yang 			// This takes up about half load time
649*8af74909SZhong Yang 			data = uh.AppendAction(insertAction, position, s, insertLength, startSequence);
650*8af74909SZhong Yang 		}
651*8af74909SZhong Yang 
652*8af74909SZhong Yang 		BasicInsertString(position, s, insertLength);
653*8af74909SZhong Yang 	}
654*8af74909SZhong Yang 	return data;
655*8af74909SZhong Yang }
656*8af74909SZhong Yang 
SetStyleAt(Sci::Position position,char styleValue)657*8af74909SZhong Yang bool CellBuffer::SetStyleAt(Sci::Position position, char styleValue) noexcept {
658*8af74909SZhong Yang 	if (!hasStyles) {
659*8af74909SZhong Yang 		return false;
660*8af74909SZhong Yang 	}
661*8af74909SZhong Yang 	const char curVal = style.ValueAt(position);
662*8af74909SZhong Yang 	if (curVal != styleValue) {
663*8af74909SZhong Yang 		style.SetValueAt(position, styleValue);
664*8af74909SZhong Yang 		return true;
665*8af74909SZhong Yang 	} else {
666*8af74909SZhong Yang 		return false;
667*8af74909SZhong Yang 	}
668*8af74909SZhong Yang }
669*8af74909SZhong Yang 
SetStyleFor(Sci::Position position,Sci::Position lengthStyle,char styleValue)670*8af74909SZhong Yang bool CellBuffer::SetStyleFor(Sci::Position position, Sci::Position lengthStyle, char styleValue) noexcept {
671*8af74909SZhong Yang 	if (!hasStyles) {
672*8af74909SZhong Yang 		return false;
673*8af74909SZhong Yang 	}
674*8af74909SZhong Yang 	bool changed = false;
675*8af74909SZhong Yang 	PLATFORM_ASSERT(lengthStyle == 0 ||
676*8af74909SZhong Yang 		(lengthStyle > 0 && lengthStyle + position <= style.Length()));
677*8af74909SZhong Yang 	while (lengthStyle--) {
678*8af74909SZhong Yang 		const char curVal = style.ValueAt(position);
679*8af74909SZhong Yang 		if (curVal != styleValue) {
680*8af74909SZhong Yang 			style.SetValueAt(position, styleValue);
681*8af74909SZhong Yang 			changed = true;
682*8af74909SZhong Yang 		}
683*8af74909SZhong Yang 		position++;
684*8af74909SZhong Yang 	}
685*8af74909SZhong Yang 	return changed;
686*8af74909SZhong Yang }
687*8af74909SZhong Yang 
688*8af74909SZhong Yang // The char* returned is to an allocation owned by the undo history
DeleteChars(Sci::Position position,Sci::Position deleteLength,bool & startSequence)689*8af74909SZhong Yang const char *CellBuffer::DeleteChars(Sci::Position position, Sci::Position deleteLength, bool &startSequence) {
690*8af74909SZhong Yang 	// InsertString and DeleteChars are the bottleneck though which all changes occur
691*8af74909SZhong Yang 	PLATFORM_ASSERT(deleteLength > 0);
692*8af74909SZhong Yang 	const char *data = nullptr;
693*8af74909SZhong Yang 	if (!readOnly) {
694*8af74909SZhong Yang 		if (collectingUndo) {
695*8af74909SZhong Yang 			// Save into the undo/redo stack, but only the characters - not the formatting
696*8af74909SZhong Yang 			// The gap would be moved to position anyway for the deletion so this doesn't cost extra
697*8af74909SZhong Yang 			data = substance.RangePointer(position, deleteLength);
698*8af74909SZhong Yang 			data = uh.AppendAction(removeAction, position, data, deleteLength, startSequence);
699*8af74909SZhong Yang 		}
700*8af74909SZhong Yang 
701*8af74909SZhong Yang 		BasicDeleteChars(position, deleteLength);
702*8af74909SZhong Yang 	}
703*8af74909SZhong Yang 	return data;
704*8af74909SZhong Yang }
705*8af74909SZhong Yang 
Length() const706*8af74909SZhong Yang Sci::Position CellBuffer::Length() const noexcept {
707*8af74909SZhong Yang 	return substance.Length();
708*8af74909SZhong Yang }
709*8af74909SZhong Yang 
Allocate(Sci::Position newSize)710*8af74909SZhong Yang void CellBuffer::Allocate(Sci::Position newSize) {
711*8af74909SZhong Yang 	substance.ReAllocate(newSize);
712*8af74909SZhong Yang 	if (hasStyles) {
713*8af74909SZhong Yang 		style.ReAllocate(newSize);
714*8af74909SZhong Yang 	}
715*8af74909SZhong Yang }
716*8af74909SZhong Yang 
SetUTF8Substance(bool utf8Substance_)717*8af74909SZhong Yang void CellBuffer::SetUTF8Substance(bool utf8Substance_) noexcept {
718*8af74909SZhong Yang 	utf8Substance = utf8Substance_;
719*8af74909SZhong Yang }
720*8af74909SZhong Yang 
SetLineEndTypes(int utf8LineEnds_)721*8af74909SZhong Yang void CellBuffer::SetLineEndTypes(int utf8LineEnds_) {
722*8af74909SZhong Yang 	if (utf8LineEnds != utf8LineEnds_) {
723*8af74909SZhong Yang 		const int indexes = plv->LineCharacterIndex();
724*8af74909SZhong Yang 		utf8LineEnds = utf8LineEnds_;
725*8af74909SZhong Yang 		ResetLineEnds();
726*8af74909SZhong Yang 		AllocateLineCharacterIndex(indexes);
727*8af74909SZhong Yang 	}
728*8af74909SZhong Yang }
729*8af74909SZhong Yang 
ContainsLineEnd(const char * s,Sci::Position length) const730*8af74909SZhong Yang bool CellBuffer::ContainsLineEnd(const char *s, Sci::Position length) const noexcept {
731*8af74909SZhong Yang 	unsigned char chBeforePrev = 0;
732*8af74909SZhong Yang 	unsigned char chPrev = 0;
733*8af74909SZhong Yang 	for (Sci::Position i = 0; i < length; i++) {
734*8af74909SZhong Yang 		const unsigned char ch = s[i];
735*8af74909SZhong Yang 		if ((ch == '\r') || (ch == '\n')) {
736*8af74909SZhong Yang 			return true;
737*8af74909SZhong Yang 		} else if (utf8LineEnds) {
738*8af74909SZhong Yang 			if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) {
739*8af74909SZhong Yang 				return true;
740*8af74909SZhong Yang 			}
741*8af74909SZhong Yang 		}
742*8af74909SZhong Yang 		chBeforePrev = chPrev;
743*8af74909SZhong Yang 		chPrev = ch;
744*8af74909SZhong Yang 	}
745*8af74909SZhong Yang 	return false;
746*8af74909SZhong Yang }
747*8af74909SZhong Yang 
SetPerLine(PerLine * pl)748*8af74909SZhong Yang void CellBuffer::SetPerLine(PerLine *pl) noexcept {
749*8af74909SZhong Yang 	plv->SetPerLine(pl);
750*8af74909SZhong Yang }
751*8af74909SZhong Yang 
LineCharacterIndex() const752*8af74909SZhong Yang int CellBuffer::LineCharacterIndex() const noexcept {
753*8af74909SZhong Yang 	return plv->LineCharacterIndex();
754*8af74909SZhong Yang }
755*8af74909SZhong Yang 
AllocateLineCharacterIndex(int lineCharacterIndex)756*8af74909SZhong Yang void CellBuffer::AllocateLineCharacterIndex(int lineCharacterIndex) {
757*8af74909SZhong Yang 	if (utf8Substance) {
758*8af74909SZhong Yang 		if (plv->AllocateLineCharacterIndex(lineCharacterIndex, Lines())) {
759*8af74909SZhong Yang 			// Changed so recalculate whole file
760*8af74909SZhong Yang 			RecalculateIndexLineStarts(0, Lines() - 1);
761*8af74909SZhong Yang 		}
762*8af74909SZhong Yang 	}
763*8af74909SZhong Yang }
764*8af74909SZhong Yang 
ReleaseLineCharacterIndex(int lineCharacterIndex)765*8af74909SZhong Yang void CellBuffer::ReleaseLineCharacterIndex(int lineCharacterIndex) {
766*8af74909SZhong Yang 	plv->ReleaseLineCharacterIndex(lineCharacterIndex);
767*8af74909SZhong Yang }
768*8af74909SZhong Yang 
Lines() const769*8af74909SZhong Yang Sci::Line CellBuffer::Lines() const noexcept {
770*8af74909SZhong Yang 	return plv->Lines();
771*8af74909SZhong Yang }
772*8af74909SZhong Yang 
LineStart(Sci::Line line) const773*8af74909SZhong Yang Sci::Position CellBuffer::LineStart(Sci::Line line) const noexcept {
774*8af74909SZhong Yang 	if (line < 0)
775*8af74909SZhong Yang 		return 0;
776*8af74909SZhong Yang 	else if (line >= Lines())
777*8af74909SZhong Yang 		return Length();
778*8af74909SZhong Yang 	else
779*8af74909SZhong Yang 		return plv->LineStart(line);
780*8af74909SZhong Yang }
781*8af74909SZhong Yang 
LineFromPosition(Sci::Position pos) const782*8af74909SZhong Yang Sci::Line CellBuffer::LineFromPosition(Sci::Position pos) const noexcept {
783*8af74909SZhong Yang 	return plv->LineFromPosition(pos);
784*8af74909SZhong Yang }
785*8af74909SZhong Yang 
IndexLineStart(Sci::Line line,int lineCharacterIndex) const786*8af74909SZhong Yang Sci::Position CellBuffer::IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept {
787*8af74909SZhong Yang 	return plv->IndexLineStart(line, lineCharacterIndex);
788*8af74909SZhong Yang }
789*8af74909SZhong Yang 
LineFromPositionIndex(Sci::Position pos,int lineCharacterIndex) const790*8af74909SZhong Yang Sci::Line CellBuffer::LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept {
791*8af74909SZhong Yang 	return plv->LineFromPositionIndex(pos, lineCharacterIndex);
792*8af74909SZhong Yang }
793*8af74909SZhong Yang 
IsReadOnly() const794*8af74909SZhong Yang bool CellBuffer::IsReadOnly() const noexcept {
795*8af74909SZhong Yang 	return readOnly;
796*8af74909SZhong Yang }
797*8af74909SZhong Yang 
SetReadOnly(bool set)798*8af74909SZhong Yang void CellBuffer::SetReadOnly(bool set) noexcept {
799*8af74909SZhong Yang 	readOnly = set;
800*8af74909SZhong Yang }
801*8af74909SZhong Yang 
IsLarge() const802*8af74909SZhong Yang bool CellBuffer::IsLarge() const noexcept {
803*8af74909SZhong Yang 	return largeDocument;
804*8af74909SZhong Yang }
805*8af74909SZhong Yang 
HasStyles() const806*8af74909SZhong Yang bool CellBuffer::HasStyles() const noexcept {
807*8af74909SZhong Yang 	return hasStyles;
808*8af74909SZhong Yang }
809*8af74909SZhong Yang 
SetSavePoint()810*8af74909SZhong Yang void CellBuffer::SetSavePoint() {
811*8af74909SZhong Yang 	uh.SetSavePoint();
812*8af74909SZhong Yang }
813*8af74909SZhong Yang 
IsSavePoint() const814*8af74909SZhong Yang bool CellBuffer::IsSavePoint() const noexcept {
815*8af74909SZhong Yang 	return uh.IsSavePoint();
816*8af74909SZhong Yang }
817*8af74909SZhong Yang 
TentativeStart()818*8af74909SZhong Yang void CellBuffer::TentativeStart() {
819*8af74909SZhong Yang 	uh.TentativeStart();
820*8af74909SZhong Yang }
821*8af74909SZhong Yang 
TentativeCommit()822*8af74909SZhong Yang void CellBuffer::TentativeCommit() {
823*8af74909SZhong Yang 	uh.TentativeCommit();
824*8af74909SZhong Yang }
825*8af74909SZhong Yang 
TentativeSteps()826*8af74909SZhong Yang int CellBuffer::TentativeSteps() noexcept {
827*8af74909SZhong Yang 	return uh.TentativeSteps();
828*8af74909SZhong Yang }
829*8af74909SZhong Yang 
TentativeActive() const830*8af74909SZhong Yang bool CellBuffer::TentativeActive() const noexcept {
831*8af74909SZhong Yang 	return uh.TentativeActive();
832*8af74909SZhong Yang }
833*8af74909SZhong Yang 
834*8af74909SZhong Yang // Without undo
835*8af74909SZhong Yang 
InsertLine(Sci::Line line,Sci::Position position,bool lineStart)836*8af74909SZhong Yang void CellBuffer::InsertLine(Sci::Line line, Sci::Position position, bool lineStart) {
837*8af74909SZhong Yang 	plv->InsertLine(line, position, lineStart);
838*8af74909SZhong Yang }
839*8af74909SZhong Yang 
RemoveLine(Sci::Line line)840*8af74909SZhong Yang void CellBuffer::RemoveLine(Sci::Line line) {
841*8af74909SZhong Yang 	plv->RemoveLine(line);
842*8af74909SZhong Yang }
843*8af74909SZhong Yang 
UTF8LineEndOverlaps(Sci::Position position) const844*8af74909SZhong Yang bool CellBuffer::UTF8LineEndOverlaps(Sci::Position position) const noexcept {
845*8af74909SZhong Yang 	const unsigned char bytes[] = {
846*8af74909SZhong Yang 		static_cast<unsigned char>(substance.ValueAt(position-2)),
847*8af74909SZhong Yang 		static_cast<unsigned char>(substance.ValueAt(position-1)),
848*8af74909SZhong Yang 		static_cast<unsigned char>(substance.ValueAt(position)),
849*8af74909SZhong Yang 		static_cast<unsigned char>(substance.ValueAt(position+1)),
850*8af74909SZhong Yang 	};
851*8af74909SZhong Yang 	return UTF8IsSeparator(bytes) || UTF8IsSeparator(bytes+1) || UTF8IsNEL(bytes+1);
852*8af74909SZhong Yang }
853*8af74909SZhong Yang 
UTF8IsCharacterBoundary(Sci::Position position) const854*8af74909SZhong Yang bool CellBuffer::UTF8IsCharacterBoundary(Sci::Position position) const {
855*8af74909SZhong Yang 	assert(position >= 0 && position <= Length());
856*8af74909SZhong Yang 	if (position > 0) {
857*8af74909SZhong Yang 		std::string back;
858*8af74909SZhong Yang 		for (int i = 0; i < UTF8MaxBytes; i++) {
859*8af74909SZhong Yang 			const Sci::Position posBack = position - i;
860*8af74909SZhong Yang 			if (posBack < 0) {
861*8af74909SZhong Yang 				return false;
862*8af74909SZhong Yang 			}
863*8af74909SZhong Yang 			back.insert(0, 1, substance.ValueAt(posBack));
864*8af74909SZhong Yang 			if (!UTF8IsTrailByte(back.front())) {
865*8af74909SZhong Yang 				if (i > 0) {
866*8af74909SZhong Yang 					// Have reached a non-trail
867*8af74909SZhong Yang 					const int cla = UTF8Classify(back);
868*8af74909SZhong Yang 					if ((cla & UTF8MaskInvalid) || (cla != i)) {
869*8af74909SZhong Yang 						return false;
870*8af74909SZhong Yang 					}
871*8af74909SZhong Yang 				}
872*8af74909SZhong Yang 				break;
873*8af74909SZhong Yang 			}
874*8af74909SZhong Yang 		}
875*8af74909SZhong Yang 	}
876*8af74909SZhong Yang 	if (position < Length()) {
877*8af74909SZhong Yang 		const unsigned char fore = substance.ValueAt(position);
878*8af74909SZhong Yang 		if (UTF8IsTrailByte(fore)) {
879*8af74909SZhong Yang 			return false;
880*8af74909SZhong Yang 		}
881*8af74909SZhong Yang 	}
882*8af74909SZhong Yang 	return true;
883*8af74909SZhong Yang }
884*8af74909SZhong Yang 
ResetLineEnds()885*8af74909SZhong Yang void CellBuffer::ResetLineEnds() {
886*8af74909SZhong Yang 	// Reinitialize line data -- too much work to preserve
887*8af74909SZhong Yang 	plv->Init();
888*8af74909SZhong Yang 
889*8af74909SZhong Yang 	const Sci::Position position = 0;
890*8af74909SZhong Yang 	const Sci::Position length = Length();
891*8af74909SZhong Yang 	Sci::Line lineInsert = 1;
892*8af74909SZhong Yang 	const bool atLineStart = true;
893*8af74909SZhong Yang 	plv->InsertText(lineInsert-1, length);
894*8af74909SZhong Yang 	unsigned char chBeforePrev = 0;
895*8af74909SZhong Yang 	unsigned char chPrev = 0;
896*8af74909SZhong Yang 	for (Sci::Position i = 0; i < length; i++) {
897*8af74909SZhong Yang 		const unsigned char ch = substance.ValueAt(position + i);
898*8af74909SZhong Yang 		if (ch == '\r') {
899*8af74909SZhong Yang 			InsertLine(lineInsert, (position + i) + 1, atLineStart);
900*8af74909SZhong Yang 			lineInsert++;
901*8af74909SZhong Yang 		} else if (ch == '\n') {
902*8af74909SZhong Yang 			if (chPrev == '\r') {
903*8af74909SZhong Yang 				// Patch up what was end of line
904*8af74909SZhong Yang 				plv->SetLineStart(lineInsert - 1, (position + i) + 1);
905*8af74909SZhong Yang 			} else {
906*8af74909SZhong Yang 				InsertLine(lineInsert, (position + i) + 1, atLineStart);
907*8af74909SZhong Yang 				lineInsert++;
908*8af74909SZhong Yang 			}
909*8af74909SZhong Yang 		} else if (utf8LineEnds) {
910*8af74909SZhong Yang 			if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) {
911*8af74909SZhong Yang 				InsertLine(lineInsert, (position + i) + 1, atLineStart);
912*8af74909SZhong Yang 				lineInsert++;
913*8af74909SZhong Yang 			}
914*8af74909SZhong Yang 		}
915*8af74909SZhong Yang 		chBeforePrev = chPrev;
916*8af74909SZhong Yang 		chPrev = ch;
917*8af74909SZhong Yang 	}
918*8af74909SZhong Yang }
919*8af74909SZhong Yang 
920*8af74909SZhong Yang namespace {
921*8af74909SZhong Yang 
CountCharacterWidthsUTF8(std::string_view sv)922*8af74909SZhong Yang CountWidths CountCharacterWidthsUTF8(std::string_view sv) noexcept {
923*8af74909SZhong Yang 	CountWidths cw;
924*8af74909SZhong Yang 	size_t remaining = sv.length();
925*8af74909SZhong Yang 	while (remaining > 0) {
926*8af74909SZhong Yang 		const int utf8Status = UTF8Classify(sv);
927*8af74909SZhong Yang 		const int lenChar = utf8Status & UTF8MaskWidth;
928*8af74909SZhong Yang 		cw.CountChar(lenChar);
929*8af74909SZhong Yang 		sv.remove_prefix(lenChar);
930*8af74909SZhong Yang 		remaining -= lenChar;
931*8af74909SZhong Yang 	}
932*8af74909SZhong Yang 	return cw;
933*8af74909SZhong Yang }
934*8af74909SZhong Yang 
935*8af74909SZhong Yang }
936*8af74909SZhong Yang 
MaintainingLineCharacterIndex() const937*8af74909SZhong Yang bool CellBuffer::MaintainingLineCharacterIndex() const noexcept {
938*8af74909SZhong Yang 	return plv->LineCharacterIndex() != SC_LINECHARACTERINDEX_NONE;
939*8af74909SZhong Yang }
940*8af74909SZhong Yang 
RecalculateIndexLineStarts(Sci::Line lineFirst,Sci::Line lineLast)941*8af74909SZhong Yang void CellBuffer::RecalculateIndexLineStarts(Sci::Line lineFirst, Sci::Line lineLast) {
942*8af74909SZhong Yang 	std::string text;
943*8af74909SZhong Yang 	Sci::Position posLineEnd = LineStart(lineFirst);
944*8af74909SZhong Yang 	for (Sci::Line line = lineFirst; line <= lineLast; line++) {
945*8af74909SZhong Yang 		// Find line start and end, retrieve text of line, count characters and update line width
946*8af74909SZhong Yang 		const Sci::Position posLineStart = posLineEnd;
947*8af74909SZhong Yang 		posLineEnd = LineStart(line+1);
948*8af74909SZhong Yang 		const Sci::Position width = posLineEnd - posLineStart;
949*8af74909SZhong Yang 		text.resize(width);
950*8af74909SZhong Yang 		GetCharRange(text.data(), posLineStart, width);
951*8af74909SZhong Yang 		const CountWidths cw = CountCharacterWidthsUTF8(text);
952*8af74909SZhong Yang 		plv->SetLineCharactersWidth(line, cw);
953*8af74909SZhong Yang 	}
954*8af74909SZhong Yang }
955*8af74909SZhong Yang 
BasicInsertString(Sci::Position position,const char * s,Sci::Position insertLength)956*8af74909SZhong Yang void CellBuffer::BasicInsertString(Sci::Position position, const char *s, Sci::Position insertLength) {
957*8af74909SZhong Yang 	if (insertLength == 0)
958*8af74909SZhong Yang 		return;
959*8af74909SZhong Yang 	PLATFORM_ASSERT(insertLength > 0);
960*8af74909SZhong Yang 
961*8af74909SZhong Yang 	const unsigned char chAfter = substance.ValueAt(position);
962*8af74909SZhong Yang 	bool breakingUTF8LineEnd = false;
963*8af74909SZhong Yang 	if (utf8LineEnds && UTF8IsTrailByte(chAfter)) {
964*8af74909SZhong Yang 		breakingUTF8LineEnd = UTF8LineEndOverlaps(position);
965*8af74909SZhong Yang 	}
966*8af74909SZhong Yang 
967*8af74909SZhong Yang 	const Sci::Line linePosition = plv->LineFromPosition(position);
968*8af74909SZhong Yang 	Sci::Line lineInsert = linePosition + 1;
969*8af74909SZhong Yang 
970*8af74909SZhong Yang 	// A simple insertion is one that inserts valid text on a single line at a character boundary
971*8af74909SZhong Yang 	bool simpleInsertion = false;
972*8af74909SZhong Yang 
973*8af74909SZhong Yang 	const bool maintainingIndex = MaintainingLineCharacterIndex();
974*8af74909SZhong Yang 
975*8af74909SZhong Yang 	// Check for breaking apart a UTF-8 sequence and inserting invalid UTF-8
976*8af74909SZhong Yang 	if (utf8Substance && maintainingIndex) {
977*8af74909SZhong Yang 		// Actually, don't need to check that whole insertion is valid just that there
978*8af74909SZhong Yang 		// are no potential fragments at ends.
979*8af74909SZhong Yang 		simpleInsertion = UTF8IsCharacterBoundary(position) &&
980*8af74909SZhong Yang 			UTF8IsValid(std::string_view(s, insertLength));
981*8af74909SZhong Yang 	}
982*8af74909SZhong Yang 
983*8af74909SZhong Yang 	substance.InsertFromArray(position, s, 0, insertLength);
984*8af74909SZhong Yang 	if (hasStyles) {
985*8af74909SZhong Yang 		style.InsertValue(position, insertLength, 0);
986*8af74909SZhong Yang 	}
987*8af74909SZhong Yang 
988*8af74909SZhong Yang 	const bool atLineStart = plv->LineStart(lineInsert-1) == position;
989*8af74909SZhong Yang 	// Point all the lines after the insertion point further along in the buffer
990*8af74909SZhong Yang 	plv->InsertText(lineInsert-1, insertLength);
991*8af74909SZhong Yang 	unsigned char chBeforePrev = substance.ValueAt(position - 2);
992*8af74909SZhong Yang 	unsigned char chPrev = substance.ValueAt(position - 1);
993*8af74909SZhong Yang 	if (chPrev == '\r' && chAfter == '\n') {
994*8af74909SZhong Yang 		// Splitting up a crlf pair at position
995*8af74909SZhong Yang 		InsertLine(lineInsert, position, false);
996*8af74909SZhong Yang 		lineInsert++;
997*8af74909SZhong Yang 	}
998*8af74909SZhong Yang 	if (breakingUTF8LineEnd) {
999*8af74909SZhong Yang 		RemoveLine(lineInsert);
1000*8af74909SZhong Yang 	}
1001*8af74909SZhong Yang 
1002*8af74909SZhong Yang 	constexpr size_t PositionBlockSize = 128;
1003*8af74909SZhong Yang 	Sci::Position positions[PositionBlockSize]{};
1004*8af74909SZhong Yang 	size_t nPositions = 0;
1005*8af74909SZhong Yang 	const Sci::Line lineStart = lineInsert;
1006*8af74909SZhong Yang 
1007*8af74909SZhong Yang 	// s may not NULL-terminated, ensure *ptr == '\n' or *next == '\n' is valid.
1008*8af74909SZhong Yang 	const char * const end = s + insertLength - 1;
1009*8af74909SZhong Yang 	const char *ptr = s;
1010*8af74909SZhong Yang 	unsigned char ch = 0;
1011*8af74909SZhong Yang 
1012*8af74909SZhong Yang 	if (chPrev == '\r' && *ptr == '\n') {
1013*8af74909SZhong Yang 		++ptr;
1014*8af74909SZhong Yang 		// Patch up what was end of line
1015*8af74909SZhong Yang 		plv->SetLineStart(lineInsert - 1, (position + ptr - s));
1016*8af74909SZhong Yang 		simpleInsertion = false;
1017*8af74909SZhong Yang 	}
1018*8af74909SZhong Yang 
1019*8af74909SZhong Yang 	if (ptr < end) {
1020*8af74909SZhong Yang 		uint8_t eolTable[256]{};
1021*8af74909SZhong Yang 		eolTable[static_cast<uint8_t>('\n')] = 1;
1022*8af74909SZhong Yang 		eolTable[static_cast<uint8_t>('\r')] = 2;
1023*8af74909SZhong Yang 		if (utf8LineEnds) {
1024*8af74909SZhong Yang 			// see UniConversion.h for LS, PS and NEL
1025*8af74909SZhong Yang 			eolTable[0x85] = 4;
1026*8af74909SZhong Yang 			eolTable[0xa8] = 3;
1027*8af74909SZhong Yang 			eolTable[0xa9] = 3;
1028*8af74909SZhong Yang 		}
1029*8af74909SZhong Yang 
1030*8af74909SZhong Yang 		do {
1031*8af74909SZhong Yang 			// skip to line end
1032*8af74909SZhong Yang 			ch = *ptr++;
1033*8af74909SZhong Yang 			uint8_t type;
1034*8af74909SZhong Yang 			while ((type = eolTable[ch]) == 0 && ptr < end) {
1035*8af74909SZhong Yang 				chBeforePrev = chPrev;
1036*8af74909SZhong Yang 				chPrev = ch;
1037*8af74909SZhong Yang 				ch = *ptr++;
1038*8af74909SZhong Yang 			}
1039*8af74909SZhong Yang 			switch (type) {
1040*8af74909SZhong Yang 			case 2: // '\r'
1041*8af74909SZhong Yang 				if (*ptr == '\n') {
1042*8af74909SZhong Yang 					++ptr;
1043*8af74909SZhong Yang 				}
1044*8af74909SZhong Yang 				[[fallthrough]];
1045*8af74909SZhong Yang 			case 1: // '\n'
1046*8af74909SZhong Yang 				positions[nPositions++] = position + ptr - s;
1047*8af74909SZhong Yang 				if (nPositions == PositionBlockSize) {
1048*8af74909SZhong Yang 					plv->InsertLines(lineInsert, positions, nPositions, atLineStart);
1049*8af74909SZhong Yang 					lineInsert += nPositions;
1050*8af74909SZhong Yang 					nPositions = 0;
1051*8af74909SZhong Yang 				}
1052*8af74909SZhong Yang 				break;
1053*8af74909SZhong Yang 			case 3:
1054*8af74909SZhong Yang 			case 4:
1055*8af74909SZhong Yang 				// LS, PS and NEL
1056*8af74909SZhong Yang 				if ((type == 3 && chPrev == 0x80 && chBeforePrev == 0xe2) || (type == 4 && chPrev == 0xc2)) {
1057*8af74909SZhong Yang 					positions[nPositions++] = position + ptr - s;
1058*8af74909SZhong Yang 					if (nPositions == PositionBlockSize) {
1059*8af74909SZhong Yang 						plv->InsertLines(lineInsert, positions, nPositions, atLineStart);
1060*8af74909SZhong Yang 						lineInsert += nPositions;
1061*8af74909SZhong Yang 						nPositions = 0;
1062*8af74909SZhong Yang 					}
1063*8af74909SZhong Yang 				}
1064*8af74909SZhong Yang 				break;
1065*8af74909SZhong Yang 			}
1066*8af74909SZhong Yang 
1067*8af74909SZhong Yang 			chBeforePrev = chPrev;
1068*8af74909SZhong Yang 			chPrev = ch;
1069*8af74909SZhong Yang 		} while (ptr < end);
1070*8af74909SZhong Yang 	}
1071*8af74909SZhong Yang 
1072*8af74909SZhong Yang 	if (nPositions != 0) {
1073*8af74909SZhong Yang 		plv->InsertLines(lineInsert, positions, nPositions, atLineStart);
1074*8af74909SZhong Yang 		lineInsert += nPositions;
1075*8af74909SZhong Yang 	}
1076*8af74909SZhong Yang 
1077*8af74909SZhong Yang 	ch = *end;
1078*8af74909SZhong Yang 	if (ptr == end) {
1079*8af74909SZhong Yang 		++ptr;
1080*8af74909SZhong Yang 		if (ch == '\r' || ch == '\n') {
1081*8af74909SZhong Yang 			InsertLine(lineInsert, (position + ptr - s), atLineStart);
1082*8af74909SZhong Yang 			lineInsert++;
1083*8af74909SZhong Yang 		} else if (utf8LineEnds && !UTF8IsAscii(ch)) {
1084*8af74909SZhong Yang 			if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) {
1085*8af74909SZhong Yang 				InsertLine(lineInsert, (position + ptr - s), atLineStart);
1086*8af74909SZhong Yang 				lineInsert++;
1087*8af74909SZhong Yang 			}
1088*8af74909SZhong Yang 		}
1089*8af74909SZhong Yang 	}
1090*8af74909SZhong Yang 
1091*8af74909SZhong Yang 	// Joining two lines where last insertion is cr and following substance starts with lf
1092*8af74909SZhong Yang 	if (chAfter == '\n') {
1093*8af74909SZhong Yang 		if (ch == '\r') {
1094*8af74909SZhong Yang 			// End of line already in buffer so drop the newly created one
1095*8af74909SZhong Yang 			RemoveLine(lineInsert - 1);
1096*8af74909SZhong Yang 			simpleInsertion = false;
1097*8af74909SZhong Yang 		}
1098*8af74909SZhong Yang 	} else if (utf8LineEnds && !UTF8IsAscii(chAfter)) {
1099*8af74909SZhong Yang 		chBeforePrev = chPrev;
1100*8af74909SZhong Yang 		chPrev = ch;
1101*8af74909SZhong Yang 		// May have end of UTF-8 line end in buffer and start in insertion
1102*8af74909SZhong Yang 		for (int j = 0; j < UTF8SeparatorLength-1; j++) {
1103*8af74909SZhong Yang 			const unsigned char chAt = substance.ValueAt(position + insertLength + j);
1104*8af74909SZhong Yang 			const unsigned char back3[3] = {chBeforePrev, chPrev, chAt};
1105*8af74909SZhong Yang 			if (UTF8IsSeparator(back3)) {
1106*8af74909SZhong Yang 				InsertLine(lineInsert, (position + insertLength + j) + 1, atLineStart);
1107*8af74909SZhong Yang 				lineInsert++;
1108*8af74909SZhong Yang 			}
1109*8af74909SZhong Yang 			if ((j == 0) && UTF8IsNEL(back3+1)) {
1110*8af74909SZhong Yang 				InsertLine(lineInsert, (position + insertLength + j) + 1, atLineStart);
1111*8af74909SZhong Yang 				lineInsert++;
1112*8af74909SZhong Yang 			}
1113*8af74909SZhong Yang 			chBeforePrev = chPrev;
1114*8af74909SZhong Yang 			chPrev = chAt;
1115*8af74909SZhong Yang 		}
1116*8af74909SZhong Yang 	}
1117*8af74909SZhong Yang 	if (maintainingIndex) {
1118*8af74909SZhong Yang 		if (simpleInsertion && (lineInsert == lineStart)) {
1119*8af74909SZhong Yang 			const CountWidths cw = CountCharacterWidthsUTF8(std::string_view(s, insertLength));
1120*8af74909SZhong Yang 			plv->InsertCharacters(linePosition, cw);
1121*8af74909SZhong Yang 		} else {
1122*8af74909SZhong Yang 			RecalculateIndexLineStarts(linePosition, lineInsert - 1);
1123*8af74909SZhong Yang 		}
1124*8af74909SZhong Yang 	}
1125*8af74909SZhong Yang }
1126*8af74909SZhong Yang 
BasicDeleteChars(Sci::Position position,Sci::Position deleteLength)1127*8af74909SZhong Yang void CellBuffer::BasicDeleteChars(Sci::Position position, Sci::Position deleteLength) {
1128*8af74909SZhong Yang 	if (deleteLength == 0)
1129*8af74909SZhong Yang 		return;
1130*8af74909SZhong Yang 
1131*8af74909SZhong Yang 	Sci::Line lineRecalculateStart = INVALID_POSITION;
1132*8af74909SZhong Yang 
1133*8af74909SZhong Yang 	if ((position == 0) && (deleteLength == substance.Length())) {
1134*8af74909SZhong Yang 		// If whole buffer is being deleted, faster to reinitialise lines data
1135*8af74909SZhong Yang 		// than to delete each line.
1136*8af74909SZhong Yang 		plv->Init();
1137*8af74909SZhong Yang 	} else {
1138*8af74909SZhong Yang 		// Have to fix up line positions before doing deletion as looking at text in buffer
1139*8af74909SZhong Yang 		// to work out which lines have been removed
1140*8af74909SZhong Yang 
1141*8af74909SZhong Yang 		const Sci::Line linePosition = plv->LineFromPosition(position);
1142*8af74909SZhong Yang 		Sci::Line lineRemove = linePosition + 1;
1143*8af74909SZhong Yang 
1144*8af74909SZhong Yang 		plv->InsertText(lineRemove-1, - (deleteLength));
1145*8af74909SZhong Yang 		const unsigned char chPrev = substance.ValueAt(position - 1);
1146*8af74909SZhong Yang 		const unsigned char chBefore = chPrev;
1147*8af74909SZhong Yang 		unsigned char chNext = substance.ValueAt(position);
1148*8af74909SZhong Yang 
1149*8af74909SZhong Yang 		// Check for breaking apart a UTF-8 sequence
1150*8af74909SZhong Yang 		// Needs further checks that text is UTF-8 or that some other break apart is occurring
1151*8af74909SZhong Yang 		if (utf8Substance && MaintainingLineCharacterIndex()) {
1152*8af74909SZhong Yang 			const Sci::Position posEnd = position + deleteLength;
1153*8af74909SZhong Yang 			const Sci::Line lineEndRemove = plv->LineFromPosition(posEnd);
1154*8af74909SZhong Yang 			const bool simpleDeletion =
1155*8af74909SZhong Yang 				(linePosition == lineEndRemove) &&
1156*8af74909SZhong Yang 				UTF8IsCharacterBoundary(position) && UTF8IsCharacterBoundary(posEnd);
1157*8af74909SZhong Yang 			if (simpleDeletion) {
1158*8af74909SZhong Yang 				std::string text(deleteLength, '\0');
1159*8af74909SZhong Yang 				GetCharRange(text.data(), position, deleteLength);
1160*8af74909SZhong Yang 				if (UTF8IsValid(text)) {
1161*8af74909SZhong Yang 					// Everything is good
1162*8af74909SZhong Yang 					const CountWidths cw = CountCharacterWidthsUTF8(text);
1163*8af74909SZhong Yang 					plv->InsertCharacters(linePosition, -cw);
1164*8af74909SZhong Yang 				} else {
1165*8af74909SZhong Yang 					lineRecalculateStart = linePosition;
1166*8af74909SZhong Yang 				}
1167*8af74909SZhong Yang 			} else {
1168*8af74909SZhong Yang 				lineRecalculateStart = linePosition;
1169*8af74909SZhong Yang 			}
1170*8af74909SZhong Yang 		}
1171*8af74909SZhong Yang 
1172*8af74909SZhong Yang 		bool ignoreNL = false;
1173*8af74909SZhong Yang 		if (chPrev == '\r' && chNext == '\n') {
1174*8af74909SZhong Yang 			// Move back one
1175*8af74909SZhong Yang 			plv->SetLineStart(lineRemove, position);
1176*8af74909SZhong Yang 			lineRemove++;
1177*8af74909SZhong Yang 			ignoreNL = true; 	// First \n is not real deletion
1178*8af74909SZhong Yang 		}
1179*8af74909SZhong Yang 		if (utf8LineEnds && UTF8IsTrailByte(chNext)) {
1180*8af74909SZhong Yang 			if (UTF8LineEndOverlaps(position)) {
1181*8af74909SZhong Yang 				RemoveLine(lineRemove);
1182*8af74909SZhong Yang 			}
1183*8af74909SZhong Yang 		}
1184*8af74909SZhong Yang 
1185*8af74909SZhong Yang 		unsigned char ch = chNext;
1186*8af74909SZhong Yang 		for (Sci::Position i = 0; i < deleteLength; i++) {
1187*8af74909SZhong Yang 			chNext = substance.ValueAt(position + i + 1);
1188*8af74909SZhong Yang 			if (ch == '\r') {
1189*8af74909SZhong Yang 				if (chNext != '\n') {
1190*8af74909SZhong Yang 					RemoveLine(lineRemove);
1191*8af74909SZhong Yang 				}
1192*8af74909SZhong Yang 			} else if (ch == '\n') {
1193*8af74909SZhong Yang 				if (ignoreNL) {
1194*8af74909SZhong Yang 					ignoreNL = false; 	// Further \n are real deletions
1195*8af74909SZhong Yang 				} else {
1196*8af74909SZhong Yang 					RemoveLine(lineRemove);
1197*8af74909SZhong Yang 				}
1198*8af74909SZhong Yang 			} else if (utf8LineEnds) {
1199*8af74909SZhong Yang 				if (!UTF8IsAscii(ch)) {
1200*8af74909SZhong Yang 					const unsigned char next3[3] = {ch, chNext,
1201*8af74909SZhong Yang 						static_cast<unsigned char>(substance.ValueAt(position + i + 2))};
1202*8af74909SZhong Yang 					if (UTF8IsSeparator(next3) || UTF8IsNEL(next3)) {
1203*8af74909SZhong Yang 						RemoveLine(lineRemove);
1204*8af74909SZhong Yang 					}
1205*8af74909SZhong Yang 				}
1206*8af74909SZhong Yang 			}
1207*8af74909SZhong Yang 
1208*8af74909SZhong Yang 			ch = chNext;
1209*8af74909SZhong Yang 		}
1210*8af74909SZhong Yang 		// May have to fix up end if last deletion causes cr to be next to lf
1211*8af74909SZhong Yang 		// or removes one of a crlf pair
1212*8af74909SZhong Yang 		const char chAfter = substance.ValueAt(position + deleteLength);
1213*8af74909SZhong Yang 		if (chBefore == '\r' && chAfter == '\n') {
1214*8af74909SZhong Yang 			// Using lineRemove-1 as cr ended line before start of deletion
1215*8af74909SZhong Yang 			RemoveLine(lineRemove - 1);
1216*8af74909SZhong Yang 			plv->SetLineStart(lineRemove - 1, position + 1);
1217*8af74909SZhong Yang 		}
1218*8af74909SZhong Yang 	}
1219*8af74909SZhong Yang 	substance.DeleteRange(position, deleteLength);
1220*8af74909SZhong Yang 	if (lineRecalculateStart >= 0) {
1221*8af74909SZhong Yang 		RecalculateIndexLineStarts(lineRecalculateStart, lineRecalculateStart);
1222*8af74909SZhong Yang 	}
1223*8af74909SZhong Yang 	if (hasStyles) {
1224*8af74909SZhong Yang 		style.DeleteRange(position, deleteLength);
1225*8af74909SZhong Yang 	}
1226*8af74909SZhong Yang }
1227*8af74909SZhong Yang 
SetUndoCollection(bool collectUndo)1228*8af74909SZhong Yang bool CellBuffer::SetUndoCollection(bool collectUndo) {
1229*8af74909SZhong Yang 	collectingUndo = collectUndo;
1230*8af74909SZhong Yang 	uh.DropUndoSequence();
1231*8af74909SZhong Yang 	return collectingUndo;
1232*8af74909SZhong Yang }
1233*8af74909SZhong Yang 
IsCollectingUndo() const1234*8af74909SZhong Yang bool CellBuffer::IsCollectingUndo() const noexcept {
1235*8af74909SZhong Yang 	return collectingUndo;
1236*8af74909SZhong Yang }
1237*8af74909SZhong Yang 
BeginUndoAction()1238*8af74909SZhong Yang void CellBuffer::BeginUndoAction() {
1239*8af74909SZhong Yang 	uh.BeginUndoAction();
1240*8af74909SZhong Yang }
1241*8af74909SZhong Yang 
EndUndoAction()1242*8af74909SZhong Yang void CellBuffer::EndUndoAction() {
1243*8af74909SZhong Yang 	uh.EndUndoAction();
1244*8af74909SZhong Yang }
1245*8af74909SZhong Yang 
AddUndoAction(Sci::Position token,bool mayCoalesce)1246*8af74909SZhong Yang void CellBuffer::AddUndoAction(Sci::Position token, bool mayCoalesce) {
1247*8af74909SZhong Yang 	bool startSequence;
1248*8af74909SZhong Yang 	uh.AppendAction(containerAction, token, nullptr, 0, startSequence, mayCoalesce);
1249*8af74909SZhong Yang }
1250*8af74909SZhong Yang 
DeleteUndoHistory()1251*8af74909SZhong Yang void CellBuffer::DeleteUndoHistory() {
1252*8af74909SZhong Yang 	uh.DeleteUndoHistory();
1253*8af74909SZhong Yang }
1254*8af74909SZhong Yang 
CanUndo() const1255*8af74909SZhong Yang bool CellBuffer::CanUndo() const noexcept {
1256*8af74909SZhong Yang 	return uh.CanUndo();
1257*8af74909SZhong Yang }
1258*8af74909SZhong Yang 
StartUndo()1259*8af74909SZhong Yang int CellBuffer::StartUndo() {
1260*8af74909SZhong Yang 	return uh.StartUndo();
1261*8af74909SZhong Yang }
1262*8af74909SZhong Yang 
GetUndoStep() const1263*8af74909SZhong Yang const Action &CellBuffer::GetUndoStep() const {
1264*8af74909SZhong Yang 	return uh.GetUndoStep();
1265*8af74909SZhong Yang }
1266*8af74909SZhong Yang 
PerformUndoStep()1267*8af74909SZhong Yang void CellBuffer::PerformUndoStep() {
1268*8af74909SZhong Yang 	const Action &actionStep = uh.GetUndoStep();
1269*8af74909SZhong Yang 	if (actionStep.at == insertAction) {
1270*8af74909SZhong Yang 		if (substance.Length() < actionStep.lenData) {
1271*8af74909SZhong Yang 			throw std::runtime_error(
1272*8af74909SZhong Yang 				"CellBuffer::PerformUndoStep: deletion must be less than document length.");
1273*8af74909SZhong Yang 		}
1274*8af74909SZhong Yang 		BasicDeleteChars(actionStep.position, actionStep.lenData);
1275*8af74909SZhong Yang 	} else if (actionStep.at == removeAction) {
1276*8af74909SZhong Yang 		BasicInsertString(actionStep.position, actionStep.data.get(), actionStep.lenData);
1277*8af74909SZhong Yang 	}
1278*8af74909SZhong Yang 	uh.CompletedUndoStep();
1279*8af74909SZhong Yang }
1280*8af74909SZhong Yang 
CanRedo() const1281*8af74909SZhong Yang bool CellBuffer::CanRedo() const noexcept {
1282*8af74909SZhong Yang 	return uh.CanRedo();
1283*8af74909SZhong Yang }
1284*8af74909SZhong Yang 
StartRedo()1285*8af74909SZhong Yang int CellBuffer::StartRedo() {
1286*8af74909SZhong Yang 	return uh.StartRedo();
1287*8af74909SZhong Yang }
1288*8af74909SZhong Yang 
GetRedoStep() const1289*8af74909SZhong Yang const Action &CellBuffer::GetRedoStep() const {
1290*8af74909SZhong Yang 	return uh.GetRedoStep();
1291*8af74909SZhong Yang }
1292*8af74909SZhong Yang 
PerformRedoStep()1293*8af74909SZhong Yang void CellBuffer::PerformRedoStep() {
1294*8af74909SZhong Yang 	const Action &actionStep = uh.GetRedoStep();
1295*8af74909SZhong Yang 	if (actionStep.at == insertAction) {
1296*8af74909SZhong Yang 		BasicInsertString(actionStep.position, actionStep.data.get(), actionStep.lenData);
1297*8af74909SZhong Yang 	} else if (actionStep.at == removeAction) {
1298*8af74909SZhong Yang 		BasicDeleteChars(actionStep.position, actionStep.lenData);
1299*8af74909SZhong Yang 	}
1300*8af74909SZhong Yang 	uh.CompletedRedoStep();
1301*8af74909SZhong Yang }
1302*8af74909SZhong Yang 
1303