[scintilla:feature-requests] #1589 Code simplify 202607

1 view
Skip to first unread message

Zufu Liu

unread,
Jul 26, 2026, 8:04:46 AMJul 26
to scintill...@googlegroups.com

[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.

Zufu Liu

unread,
Jul 26, 2026, 8:09:26 AMJul 26
to scintill...@googlegroups.com

Range::ContainsCharacter() and SelectionRange::ContainsCharacter(Sci::Position posCharacter) can be simplified to unreadable ((pos - start) * (pos - end)) < 0 or ((pos - start) ^ (pos - end)) < 0.

Neil Hodgson

unread,
Jul 26, 2026, 8:04:48 PMJul 26
to scintill...@googlegroups.com
  • status: open --> accepted
  • Group: Initial --> Committed
  • Comment:

Committed with [dea670] and [230a0b].


[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:

Neil Hodgson

unread,
Jul 26, 2026, 8:06:20 PMJul 26
to scintill...@googlegroups.com

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:

Zufu Liu

unread,
Jul 27, 2026, 6:04:09 AMJul 27
to scintill...@googlegroups.com

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:

Zufu Liu

unread,
Jul 27, 2026, 6:15:15 AMJul 27
to scintill...@googlegroups.com

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:

Neil Hodgson

unread,
Jul 27, 2026, 10:19:19 PMJul 27
to scintill...@googlegroups.com

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:

Zufu Liu

unread,
Jul 28, 2026, 6:03:52 AMJul 28
to scintill...@googlegroups.com

((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:

Zufu Liu

unread,
Jul 28, 2026, 6:07:36 AMJul 28
to scintill...@googlegroups.com

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:

Zufu Liu

unread,
Jul 28, 2026, 6:26:04 AMJul 28
to scintill...@googlegroups.com

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:

Neil Hodgson

unread,
Jul 30, 2026, 6:06:42 PMJul 30
to scintill...@googlegroups.com

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:

Zufu Liu

unread,
Jul 31, 2026, 6:15:25 AMJul 31
to scintill...@googlegroups.com

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 lineLength just a new name for lineEnd?

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:

Neil Hodgson

unread,
Jul 31, 2026, 8:32:41 PMJul 31
to scintill...@googlegroups.com

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:

Neil Hodgson

unread,
Aug 10, 2026, 7:09:18 PM (8 days ago) Aug 10
to scintill...@googlegroups.com

A patch with ForwardRange.

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:

Zufu Liu

unread,
Aug 11, 2026, 6:23:30 AM (8 days ago) Aug 11
to scintill...@googlegroups.com

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:

Reply all
Reply to author
Forward
0 new messages