[feature-requests:#1589] Code simplify 202607
Status: open
Group: Initial
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Sun Jul 26, 2026 12:04 PM UTC
Owner: Neil Hodgson
Attachments:
Range::Length() and SelectionRange::Length() can be simplified to use std::abs() on the distance.
Sent from sourceforge.net because scintill...@googlegroups.com is subscribed to https://sourceforge.net/p/scintilla/feature-requests/
To unsubscribe from further messages, a project admin can change settings at https://sourceforge.net/p/scintilla/admin/feature-requests/options. Or, if this is a mailing list, you can unsubscribe from the mailing list.
Range::ContainsCharacter() and SelectionRange::ContainsCharacter(Sci::Position posCharacter) can be simplified to unreadable ((pos - start) * (pos - end)) < 0 or ((pos - start) ^ (pos - end)) < 0.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Sun Jul 26, 2026 12:09 PM UTC
Owner: Neil Hodgson
Attachments:
Not worth the weirdness. The multiply version may overflow on huge documents.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Mon Jul 27, 2026 12:04 AM UTC
Owner: Neil Hodgson
Attachments:
Yeah, both may introduce new UB when either number is PTRDIFF_MIN, https://alive2.llvm.org/ce/z/4eA5gv
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Mon Jul 27, 2026 12:06 AM UTC
Owner: Neil Hodgson
Attachments:
It might worth to bring back old manually expanded code for SelectionPosition::operator <= and operator >= (used by Contains() and ContainsCharacter()), Clang and MSVC seems honor other == *this and think that's the likely case, then do a literal compile, result in four comparisons instead of just two, see https://godbolt.org/z/qnvePMaYh
direct expansion with virtualSpace comparison merged:
bool SelectionPosition::operator <=(const SelectionPosition &other) const noexcept {
if (position == other.position) {
return virtualSpace <= other.virtualSpace;
}
return position < other.position;
}
bool SelectionPosition::operator >=(const SelectionPosition &other) const noexcept {
if (position == other.position) {
return virtualSpace >= other.virtualSpace;
}
return position > other.position;
}
or equivalents that MSVC generate small code. also looks close to operator < and operator > with just compare operator replaced.
bool SelectionPosition::operator <=(const SelectionPosition &other) const noexcept {
if (position == other.position) {
return virtualSpace <= other.virtualSpace;
}
return position <= other.position;
}
bool SelectionPosition::operator >=(const SelectionPosition &other) const noexcept {
if (position == other.position) {
return virtualSpace >= other.virtualSpace;
}
return position >= other.position;
}
SelectionRange::Contains(Sci::Position pos) and SelectionRange::ContainsCharacter(Sci::Position posCharacter) seems can be simplified to if (anchor.Position() > caret.Position()) as virtualSpace isn't used for further comparison.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Mon Jul 27, 2026 10:04 AM UTC
Owner: Neil Hodgson
Attachments:
OK, replaced <= and >= with second versions above and simplified Contains and ContainsCharacter with [aa6f5a].
For C++20, it may be reasonable to move to the <=> spaceship operator to simplify this and its likely it can just be an =default implementation.
The change to text drop in [230a0b] has been overwritten by replacing that code with a call to DropAt with [a61de1] which handle more cases.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Mon Jul 27, 2026 10:15 AM UTC
Owner: Neil Hodgson
Attachments:
((pos - start) * (pos - end)) < 0 or ((pos - start) ^ (pos - end)) < 0 is actually incorrect, for start <= end, it corers both pos >= start && pos < end and pos > start && pos <= end.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Tue Jul 28, 2026 02:19 AM UTC
Owner: Neil Hodgson
Attachments:
See https://godbolt.org/z/zoszjGxPx, MSVC has poor codegen for <=> (defaulted or manually), Clang is identical, GCC is mostly same with minor condition negated.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Tue Jul 28, 2026 10:03 AM UTC
Owner: Neil Hodgson
Attachments:
rangeLine.ContainsCharacter() for braces can be omitted/simplified.
for LineLayout::SetBracesHighlight() and LineLayout::RestoreBracesHighlight(), the code can be simplified as braceOffset >= 0 && braceOffset < numCharsInLine, as parameter rangeLine is whole line range with EOL. not merge the two if (!ignoreStyle) blocks may yield a cppcheck warning (block has condition).
for void DrawIndicators(), the code can be simplified as if (braceOffset >= lineStart && braceOffset < lineLength) (where lineLength = posLineEnd - posLineStart), here may has a redundant compare.
Other Range::ContainsCharacter() seems can be simplified too, start <= end looks is always true.
Attachments:
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Tue Jul 28, 2026 10:07 AM UTC
Owner: Neil Hodgson
Attachments:
The check for 'is this in the line' documents the intent better than breaking it down into smaller pieces. Maybe the range could be shortened to just the visible characters.
Isn't lineLength just a new name for lineEnd?
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Tue Jul 28, 2026 10:26 AM UTC
Owner: Neil Hodgson
Attachments:
The check for 'is this in the line' documents the intent better than breaking it down into smaller pieces.
would it worth to add Range::InLineRange(pos) or InForwardRange() that returns pos >= start && pos < end? model.hotspot also meets start <= end.
Maybe the range could be shortened to just the visible characters.
using braceOffset < numCharsBeforeEOL?
Isn't
lineLengthjust a new name forlineEnd?
Oh, that's indeed, just a liberal transform for brace in [posLineStart + lineStart, posLineEnd) => braceOffset in [lineStart, posLineEnd - posLineStart).
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Thu Jul 30, 2026 10:06 PM UTC
Owner: Neil Hodgson
Attachments:
would it worth to add Range::InLineRange(pos)
Maybe a new ForwardRange type that promises end >= start.
I was thinking of something equivalent to this change in EditView::PaintText. It would change highlight guide behaviour when braces set after the new line characters but that's unlikely so probably OK.
const Range rangeLine(model.pdoc->LineStart(lineDoc),
model.pdoc->LineStart(lineDoc) + ll->numCharsInLine);
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Fri Jul 31, 2026 10:15 AM UTC
Owner: Neil Hodgson
Attachments:
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Sat Aug 01, 2026 12:32 AM UTC
Owner: Neil Hodgson
Attachments:
It can be used for other places, e.g. all the lineRange.
[feature-requests:#1589] Code simplify 202607
Status: accepted
Group: Committed
Labels: Scintilla optimization selection
Created: Sun Jul 26, 2026 12:04 PM UTC by Zufu Liu
Last Updated: Mon Aug 10, 2026 11:09 PM UTC
Owner: Neil Hodgson
Attachments: