diff options
Diffstat (limited to 'src/CellBuffer.cxx')
-rw-r--r-- | src/CellBuffer.cxx | 109 |
1 files changed, 56 insertions, 53 deletions
diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index 84c2c27a7..375fe2201 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -20,16 +20,17 @@ #include <algorithm> #include <memory> +#include "ScintillaTypes.h" + #include "Debugging.h" -#include "Scintilla.h" #include "Position.h" #include "SplitVector.h" #include "Partitioning.h" #include "CellBuffer.h" #include "UniConversion.h" -namespace Scintilla { +namespace Scintilla::Internal { struct CountWidths { // Measures the number of characters in a string divided into those @@ -74,17 +75,18 @@ public: virtual Sci::Position LineStart(Sci::Line line) const noexcept = 0; virtual void InsertCharacters(Sci::Line line, CountWidths delta) noexcept = 0; virtual void SetLineCharactersWidth(Sci::Line line, CountWidths width) noexcept = 0; - virtual int LineCharacterIndex() const noexcept = 0; - virtual bool AllocateLineCharacterIndex(int lineCharacterIndex, Sci::Line lines) = 0; - virtual bool ReleaseLineCharacterIndex(int lineCharacterIndex) = 0; - virtual Sci::Position IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept = 0; - virtual Sci::Line LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept = 0; + virtual Scintilla::LineCharacterIndexType LineCharacterIndex() const noexcept = 0; + virtual bool AllocateLineCharacterIndex(Scintilla::LineCharacterIndexType lineCharacterIndex, Sci::Line lines) = 0; + virtual bool ReleaseLineCharacterIndex(Scintilla::LineCharacterIndexType lineCharacterIndex) = 0; + virtual Sci::Position IndexLineStart(Sci::Line line, Scintilla::LineCharacterIndexType lineCharacterIndex) const noexcept = 0; + virtual Sci::Line LineFromPositionIndex(Sci::Position pos, Scintilla::LineCharacterIndexType lineCharacterIndex) const noexcept = 0; virtual ~ILineVector() {} }; } using namespace Scintilla; +using namespace Scintilla::Internal; template <typename POS> class LineStartIndex { @@ -147,15 +149,16 @@ class LineVector : public ILineVector { PerLine *perLine; LineStartIndex<POS> startsUTF16; LineStartIndex<POS> startsUTF32; - int activeIndices; + LineCharacterIndexType activeIndices; void SetActiveIndices() noexcept { - activeIndices = (startsUTF32.Active() ? SC_LINECHARACTERINDEX_UTF32 : 0) - | (startsUTF16.Active() ? SC_LINECHARACTERINDEX_UTF16 : 0); + activeIndices = + (startsUTF32.Active() ? LineCharacterIndexType::Utf32 : LineCharacterIndexType::None) + | (startsUTF16.Active() ? LineCharacterIndexType::Utf16 : LineCharacterIndexType::None); } public: - LineVector() : starts(256), perLine(nullptr), activeIndices(0) { + LineVector() : starts(256), perLine(nullptr), activeIndices(LineCharacterIndexType::None) { } // Deleted so LineVector objects can not be copied. LineVector(const LineVector &) = delete; @@ -181,11 +184,11 @@ public: void InsertLine(Sci::Line line, Sci::Position position, bool lineStart) override { const POS lineAsPos = static_cast<POS>(line); starts.InsertPartition(lineAsPos, static_cast<POS>(position)); - if (activeIndices) { - if (activeIndices & SC_LINECHARACTERINDEX_UTF32) { + if (activeIndices != LineCharacterIndexType::None) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { startsUTF32.InsertLines(line, 1); } - if (activeIndices & SC_LINECHARACTERINDEX_UTF16) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { startsUTF16.InsertLines(line, 1); } } @@ -202,11 +205,11 @@ public: } else { starts.InsertPartitionsWithCast(lineAsPos, positions, lines); } - if (activeIndices) { - if (activeIndices & SC_LINECHARACTERINDEX_UTF32) { + if (activeIndices != LineCharacterIndexType::None) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { startsUTF32.InsertLines(line, lines); } - if (activeIndices & SC_LINECHARACTERINDEX_UTF16) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { startsUTF16.InsertLines(line, lines); } } @@ -221,10 +224,10 @@ public: } void RemoveLine(Sci::Line line) override { starts.RemovePartition(static_cast<POS>(line)); - if (activeIndices & SC_LINECHARACTERINDEX_UTF32) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { startsUTF32.starts.RemovePartition(static_cast<POS>(line)); } - if (activeIndices & SC_LINECHARACTERINDEX_UTF16) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { startsUTF16.starts.RemovePartition(static_cast<POS>(line)); } if (perLine) { @@ -241,60 +244,60 @@ public: return starts.PositionFromPartition(static_cast<POS>(line)); } void InsertCharacters(Sci::Line line, CountWidths delta) noexcept override { - if (activeIndices & SC_LINECHARACTERINDEX_UTF32) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { startsUTF32.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF32())); } - if (activeIndices & SC_LINECHARACTERINDEX_UTF16) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { startsUTF16.starts.InsertText(static_cast<POS>(line), static_cast<POS>(delta.WidthUTF16())); } } void SetLineCharactersWidth(Sci::Line line, CountWidths width) noexcept override { - if (activeIndices & SC_LINECHARACTERINDEX_UTF32) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf32)) { assert(startsUTF32.starts.Partitions() == starts.Partitions()); startsUTF32.SetLineWidth(line, width.WidthUTF32()); } - if (activeIndices & SC_LINECHARACTERINDEX_UTF16) { + if (FlagSet(activeIndices, LineCharacterIndexType::Utf16)) { assert(startsUTF16.starts.Partitions() == starts.Partitions()); startsUTF16.SetLineWidth(line, width.WidthUTF16()); } } - int LineCharacterIndex() const noexcept override { + LineCharacterIndexType LineCharacterIndex() const noexcept override { return activeIndices; } - bool AllocateLineCharacterIndex(int lineCharacterIndex, Sci::Line lines) override { - const int activeIndicesStart = activeIndices; - if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF32) != 0) { + bool AllocateLineCharacterIndex(LineCharacterIndexType lineCharacterIndex, Sci::Line lines) override { + const LineCharacterIndexType activeIndicesStart = activeIndices; + if (FlagSet(lineCharacterIndex, LineCharacterIndexType::Utf32)) { startsUTF32.Allocate(lines); assert(startsUTF32.starts.Partitions() == starts.Partitions()); } - if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF16) != 0) { + if (FlagSet(lineCharacterIndex, LineCharacterIndexType::Utf16)) { startsUTF16.Allocate(lines); assert(startsUTF16.starts.Partitions() == starts.Partitions()); } SetActiveIndices(); return activeIndicesStart != activeIndices; } - bool ReleaseLineCharacterIndex(int lineCharacterIndex) override { - const int activeIndicesStart = activeIndices; - if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF32) != 0) { + bool ReleaseLineCharacterIndex(LineCharacterIndexType lineCharacterIndex) override { + const LineCharacterIndexType activeIndicesStart = activeIndices; + if (FlagSet(lineCharacterIndex, LineCharacterIndexType::Utf32)) { startsUTF32.Release(); } - if ((lineCharacterIndex & SC_LINECHARACTERINDEX_UTF16) != 0) { + if (FlagSet(lineCharacterIndex, LineCharacterIndexType::Utf16)) { startsUTF16.Release(); } SetActiveIndices(); return activeIndicesStart != activeIndices; } - Sci::Position IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept override { - if (lineCharacterIndex == SC_LINECHARACTERINDEX_UTF32) { + Sci::Position IndexLineStart(Sci::Line line, LineCharacterIndexType lineCharacterIndex) const noexcept override { + if (lineCharacterIndex == LineCharacterIndexType::Utf32) { return startsUTF32.starts.PositionFromPartition(static_cast<POS>(line)); } else { return startsUTF16.starts.PositionFromPartition(static_cast<POS>(line)); } } - Sci::Line LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept override { - if (lineCharacterIndex == SC_LINECHARACTERINDEX_UTF32) { + Sci::Line LineFromPositionIndex(Sci::Position pos, LineCharacterIndexType lineCharacterIndex) const noexcept override { + if (lineCharacterIndex == LineCharacterIndexType::Utf32) { return static_cast<Sci::Line>(startsUTF32.starts.PartitionFromPosition(static_cast<POS>(pos))); } else { return static_cast<Sci::Line>(startsUTF16.starts.PartitionFromPosition(static_cast<POS>(pos))); @@ -570,7 +573,7 @@ CellBuffer::CellBuffer(bool hasStyles_, bool largeDocument_) : hasStyles(hasStyles_), largeDocument(largeDocument_) { readOnly = false; utf8Substance = false; - utf8LineEnds = 0; + utf8LineEnds = LineEndType::Default; collectingUndo = true; if (largeDocument) plv = std::make_unique<LineVector<Sci::Position>>(); @@ -719,9 +722,9 @@ void CellBuffer::SetUTF8Substance(bool utf8Substance_) noexcept { utf8Substance = utf8Substance_; } -void CellBuffer::SetLineEndTypes(int utf8LineEnds_) { +void CellBuffer::SetLineEndTypes(LineEndType utf8LineEnds_) { if (utf8LineEnds != utf8LineEnds_) { - const int indexes = plv->LineCharacterIndex(); + const LineCharacterIndexType indexes = plv->LineCharacterIndex(); utf8LineEnds = utf8LineEnds_; ResetLineEnds(); AllocateLineCharacterIndex(indexes); @@ -735,7 +738,7 @@ bool CellBuffer::ContainsLineEnd(const char *s, Sci::Position length) const noex const unsigned char ch = s[i]; if ((ch == '\r') || (ch == '\n')) { return true; - } else if (utf8LineEnds) { + } else if (utf8LineEnds == LineEndType::Unicode) { if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) { return true; } @@ -750,11 +753,11 @@ void CellBuffer::SetPerLine(PerLine *pl) noexcept { plv->SetPerLine(pl); } -int CellBuffer::LineCharacterIndex() const noexcept { +LineCharacterIndexType CellBuffer::LineCharacterIndex() const noexcept { return plv->LineCharacterIndex(); } -void CellBuffer::AllocateLineCharacterIndex(int lineCharacterIndex) { +void CellBuffer::AllocateLineCharacterIndex(LineCharacterIndexType lineCharacterIndex) { if (utf8Substance) { if (plv->AllocateLineCharacterIndex(lineCharacterIndex, Lines())) { // Changed so recalculate whole file @@ -763,7 +766,7 @@ void CellBuffer::AllocateLineCharacterIndex(int lineCharacterIndex) { } } -void CellBuffer::ReleaseLineCharacterIndex(int lineCharacterIndex) { +void CellBuffer::ReleaseLineCharacterIndex(LineCharacterIndexType lineCharacterIndex) { plv->ReleaseLineCharacterIndex(lineCharacterIndex); } @@ -784,11 +787,11 @@ Sci::Line CellBuffer::LineFromPosition(Sci::Position pos) const noexcept { return plv->LineFromPosition(pos); } -Sci::Position CellBuffer::IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept { +Sci::Position CellBuffer::IndexLineStart(Sci::Line line, LineCharacterIndexType lineCharacterIndex) const noexcept { return plv->IndexLineStart(line, lineCharacterIndex); } -Sci::Line CellBuffer::LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept { +Sci::Line CellBuffer::LineFromPositionIndex(Sci::Position pos, LineCharacterIndexType lineCharacterIndex) const noexcept { return plv->LineFromPositionIndex(pos, lineCharacterIndex); } @@ -907,7 +910,7 @@ void CellBuffer::ResetLineEnds() { InsertLine(lineInsert, (position + i) + 1, atLineStart); lineInsert++; } - } else if (utf8LineEnds) { + } else if (utf8LineEnds == LineEndType::Unicode) { if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) { InsertLine(lineInsert, (position + i) + 1, atLineStart); lineInsert++; @@ -936,7 +939,7 @@ CountWidths CountCharacterWidthsUTF8(std::string_view sv) noexcept { } bool CellBuffer::MaintainingLineCharacterIndex() const noexcept { - return plv->LineCharacterIndex() != SC_LINECHARACTERINDEX_NONE; + return plv->LineCharacterIndex() != LineCharacterIndexType::None; } void CellBuffer::RecalculateIndexLineStarts(Sci::Line lineFirst, Sci::Line lineLast) { @@ -961,7 +964,7 @@ void CellBuffer::BasicInsertString(Sci::Position position, const char *s, Sci::P const unsigned char chAfter = substance.ValueAt(position); bool breakingUTF8LineEnd = false; - if (utf8LineEnds && UTF8IsTrailByte(chAfter)) { + if (utf8LineEnds == LineEndType::Unicode && UTF8IsTrailByte(chAfter)) { breakingUTF8LineEnd = UTF8LineEndOverlaps(position); } @@ -1021,7 +1024,7 @@ void CellBuffer::BasicInsertString(Sci::Position position, const char *s, Sci::P uint8_t eolTable[256]{}; eolTable[static_cast<uint8_t>('\n')] = 1; eolTable[static_cast<uint8_t>('\r')] = 2; - if (utf8LineEnds) { + if (utf8LineEnds == LineEndType::Unicode) { // see UniConversion.h for LS, PS and NEL eolTable[0x85] = 4; eolTable[0xa8] = 3; @@ -1081,7 +1084,7 @@ void CellBuffer::BasicInsertString(Sci::Position position, const char *s, Sci::P if (ch == '\r' || ch == '\n') { InsertLine(lineInsert, (position + ptr - s), atLineStart); lineInsert++; - } else if (utf8LineEnds && !UTF8IsAscii(ch)) { + } else if (utf8LineEnds == LineEndType::Unicode && !UTF8IsAscii(ch)) { if (UTF8IsMultibyteLineEnd(chBeforePrev, chPrev, ch)) { InsertLine(lineInsert, (position + ptr - s), atLineStart); lineInsert++; @@ -1096,7 +1099,7 @@ void CellBuffer::BasicInsertString(Sci::Position position, const char *s, Sci::P RemoveLine(lineInsert - 1); simpleInsertion = false; } - } else if (utf8LineEnds && !UTF8IsAscii(chAfter)) { + } else if (utf8LineEnds == LineEndType::Unicode && !UTF8IsAscii(chAfter)) { chBeforePrev = chPrev; chPrev = ch; // May have end of UTF-8 line end in buffer and start in insertion @@ -1177,7 +1180,7 @@ void CellBuffer::BasicDeleteChars(Sci::Position position, Sci::Position deleteLe lineRemove++; ignoreNL = true; // First \n is not real deletion } - if (utf8LineEnds && UTF8IsTrailByte(chNext)) { + if (utf8LineEnds == LineEndType::Unicode && UTF8IsTrailByte(chNext)) { if (UTF8LineEndOverlaps(position)) { RemoveLine(lineRemove); } @@ -1196,7 +1199,7 @@ void CellBuffer::BasicDeleteChars(Sci::Position position, Sci::Position deleteLe } else { RemoveLine(lineRemove); } - } else if (utf8LineEnds) { + } else if (utf8LineEnds == LineEndType::Unicode) { if (!UTF8IsAscii(ch)) { const unsigned char next3[3] = {ch, chNext, static_cast<unsigned char>(substance.ValueAt(position + i + 2))}; |