From 63cee0686964d789e6b53406abcc0bdbed50206a Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Thu, 21 Aug 2025 23:04:57 +0000 Subject: implicitly instantiate RunStyles: support ptrdiff_t if it has the same storage size as int, but does *not* alias it * This is the case e.g. on NetBSD 10 for ARMv6 where Sci::Position == ptrdiff_t == long int, but obviously for other platforms as well, where it causes "invalid conversion" and "undefined symbol" errors. Scintilla was testing for aliasability by comparing the storage size with sizeof() or PTRDIFF_MAX == INT_MAX at the preprocessor level. This was fundamentally flawed. * In LineVector::InsertLines() we are now using the C++17 construct std::is_convertible_v instead. * We need RunStyles as well on the affected platforms. AFAIK this is impossible to test for in a constant expression that can be used with the preprocessor. A workaround has been added previously for Haiku: https://groups.google.com/g/scintilla-interest/c/xPXquJUIXo8/m/BLXBpTTgBwAJ The workaround is not very robust, as probably nobody guarantees that ptrdiff_t never aliases on Haiku. If it does, you will suddenly get errors about duplicate template instantiations. Quite possibly, the explicit instantiations of RunStyles were wrong on certain 32-bit Linux variants as well. * We could have tried to explicitly instantiate RunStyles for all scalar types that could possibly be behind ptrdiff_t. Unfortunately, it would result in "possible loss of data" warnings on MSVC. Instead, we now implicitly instantiate RunStyles. --- src/CellBuffer.cxx | 3 +- src/RunStyles.cxx | 327 ----------------------------------------------------- src/RunStyles.h | 293 +++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 272 insertions(+), 351 deletions(-) delete mode 100644 src/RunStyles.cxx (limited to 'src') diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx index 04486d4c6..3e9deb934 100644 --- a/src/CellBuffer.cxx +++ b/src/CellBuffer.cxx @@ -21,6 +21,7 @@ #include #include #include +#include #include "ScintillaTypes.h" @@ -215,7 +216,7 @@ public: } void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) override { const POS lineAsPos = pos_cast(line); - if constexpr (sizeof(Sci::Position) == sizeof(POS)) { + if constexpr (std::is_convertible_v) { starts.InsertPartitions(lineAsPos, positions, lines); } else { starts.InsertPartitionsWithCast(lineAsPos, positions, lines); diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx deleted file mode 100644 index 848670ba9..000000000 --- a/src/RunStyles.cxx +++ /dev/null @@ -1,327 +0,0 @@ -/** @file RunStyles.cxx - ** Data structure used to store sparse styles. - **/ -// Copyright 1998-2007 by Neil Hodgson -// The License.txt file describes the conditions under which this software may be distributed. - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#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 -DISTANCE RunStyles::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 -DISTANCE RunStyles::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 -void RunStyles::RemoveRun(DISTANCE run) { - starts.RemovePartition(run); - styles.DeleteRange(run, 1); -} - -template -void RunStyles::RemoveRunIfEmpty(DISTANCE run) { - if ((run < starts.Partitions()) && (starts.Partitions() > 1)) { - if (starts.PositionFromPartition(run) == starts.PositionFromPartition(run+1)) { - RemoveRun(run); - } - } -} - -template -void RunStyles::RemoveRunIfSameAsPrevious(DISTANCE run) { - if ((run > 0) && (run < starts.Partitions())) { - const DISTANCE runBefore = run - 1; - if (styles.ValueAt(runBefore) == styles.ValueAt(run)) { - RemoveRun(run); - } - } -} - -template -RunStyles::RunStyles() { - styles.InsertValue(0, 2, 0); -} - -template -DISTANCE RunStyles::Length() const noexcept { - return starts.PositionFromPartition(starts.Partitions()); -} - -template -STYLE RunStyles::ValueAt(DISTANCE position) const noexcept { - return styles.ValueAt(starts.PartitionFromPosition(position)); -} - -template -DISTANCE RunStyles::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 -DISTANCE RunStyles::StartRun(DISTANCE position) const noexcept { - return starts.PositionFromPartition(starts.PartitionFromPosition(position)); -} - -template -DISTANCE RunStyles::EndRun(DISTANCE position) const noexcept { - return starts.PositionFromPartition(starts.PartitionFromPosition(position) + 1); -} - -template -FillResult RunStyles::FillRange(DISTANCE position, STYLE value, DISTANCE fillLength) { - const FillResult 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 result{ true, position, fillLength }; - styles.SetValueAt(runStart, value); - // Remove each old run over the range - for (DISTANCE run=runStart+1; run -void RunStyles::SetValueAt(DISTANCE position, STYLE value) { - FillRange(position, value, 1); -} - -template -void RunStyles::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 -void RunStyles::DeleteAll() { - starts = Partitioning(); - styles = SplitVector