aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2026-01-26 11:53:30 +1100
committerNeil <nyamatongwe@gmail.com>2026-01-26 11:53:30 +1100
commitdb1e3e906de332fe783b99d5ed464f8a7fda8942 (patch)
treea208b1c72424806b757cd8e8c30588b4c2f0cbe5
parented97cb6060c786a4ddc0567250911f7093244d55 (diff)
downloadscintilla-mirror-db1e3e906de332fe783b99d5ed464f8a7fda8942.tar.gz
Small optimizations and adding noexcept for column calculations.
Caches document length and specializes for ASCII characters in FindColumn.
-rw-r--r--src/Document.cxx35
-rw-r--r--src/Document.h6
2 files changed, 23 insertions, 18 deletions
diff --git a/src/Document.cxx b/src/Document.cxx
index e4db66a19..458c1e93f 100644
--- a/src/Document.cxx
+++ b/src/Document.cxx
@@ -864,8 +864,9 @@ Sci::Position Document::NextPosition(Sci::Position pos, int moveDir) const noexc
const int increment = (moveDir > 0) ? 1 : -1;
if (pos + increment <= 0)
return 0;
- if (pos + increment >= cb.Length())
- return cb.Length();
+ const Sci::Position length = LengthNoExcept();
+ if (pos + increment >= length)
+ return length;
if (dbcsCodePage) {
if (CpUtf8 == dbcsCodePage) {
@@ -904,9 +905,7 @@ Sci::Position Document::NextPosition(Sci::Position pos, int moveDir) const noexc
} else {
if (moveDir > 0) {
const int mbsize = IsDBCSDualByteAt(pos) ? 2 : 1;
- pos += mbsize;
- if (pos > cb.Length())
- pos = cb.Length();
+ pos = std::min(pos + mbsize, length);
} else {
// How to Go Backward in a DBCS String
// https://msdn.microsoft.com/en-us/library/cc194792.aspx
@@ -1764,22 +1763,23 @@ Sci::Position Document::SetLineIndentation(Sci::Line line, Sci::Position indent)
}
}
-Sci::Position Document::GetLineIndentPosition(Sci::Line line) const {
+Sci::Position Document::GetLineIndentPosition(Sci::Line line) const noexcept {
if (line < 0)
return 0;
- Sci::Position pos = LineStart(line);
- const Sci::Position length = Length();
+ Sci::Position pos = cb.LineStart(line);
+ const Sci::Position length = LengthNoExcept();
while ((pos < length) && IsSpaceOrTab(cb.CharAt(pos))) {
pos++;
}
return pos;
}
-Sci::Position Document::GetColumn(Sci::Position pos) const {
+Sci::Position Document::GetColumn(Sci::Position pos) const noexcept {
Sci::Position column = 0;
const Sci::Line line = SciLineFromPosition(pos);
if ((line >= 0) && (line < LinesTotal())) {
- for (Sci::Position i = LineStart(line); i < pos;) {
+ const Sci::Position length = LengthNoExcept();
+ for (Sci::Position i = cb.LineStart(line); i < pos;) {
const char ch = cb.CharAt(i);
if (ch == '\t') {
column = NextTab(column, tabInChars);
@@ -1788,7 +1788,7 @@ Sci::Position Document::GetColumn(Sci::Position pos) const {
return column;
} else if (ch == '\n') {
return column;
- } else if (i >= Length()) {
+ } else if (i >= length) {
return column;
} else if (UTF8IsAscii(ch)) {
column++;
@@ -1829,11 +1829,12 @@ Sci::Position Document::CountUTF16(Sci::Position startPos, Sci::Position endPos)
return count;
}
-Sci::Position Document::FindColumn(Sci::Line line, Sci::Position column) {
- Sci::Position position = LineStart(line);
+Sci::Position Document::FindColumn(Sci::Line line, Sci::Position column) const noexcept {
+ Sci::Position position = cb.LineStart(line);
if ((line >= 0) && (line < LinesTotal())) {
+ const Sci::Position length = LengthNoExcept();
Sci::Position columnCurrent = 0;
- while ((columnCurrent < column) && (position < Length())) {
+ while ((columnCurrent < column) && (position < length)) {
const char ch = cb.CharAt(position);
if (ch == '\t') {
columnCurrent = NextTab(columnCurrent, tabInChars);
@@ -1844,6 +1845,9 @@ Sci::Position Document::FindColumn(Sci::Line line, Sci::Position column) {
return position;
} else if (ch == '\n') {
return position;
+ } else if (UTF8IsAscii(ch)) {
+ columnCurrent++;
+ position++;
} else {
columnCurrent++;
position = NextPosition(position, 1);
@@ -1903,7 +1907,8 @@ std::string Document::TransformLineEnds(const char *s, size_t len, EndOfLine eol
void Document::ConvertLineEnds(EndOfLine eolModeSet) {
UndoGroup ug(this);
- for (Sci::Position pos = 0; pos < Length(); pos++) {
+ const Sci::Position length = Length();
+ for (Sci::Position pos = 0; pos < length; pos++) {
const char ch = cb.CharAt(pos);
if (ch == '\r') {
if (cb.CharAt(pos + 1) == '\n') {
diff --git a/src/Document.h b/src/Document.h
index 7655d5290..7fd150f37 100644
--- a/src/Document.h
+++ b/src/Document.h
@@ -455,11 +455,11 @@ public:
int SCI_METHOD GetLineIndentation(Sci_Position line) override;
Sci::Position SetLineIndentation(Sci::Line line, Sci::Position indent);
- Sci::Position GetLineIndentPosition(Sci::Line line) const;
- Sci::Position GetColumn(Sci::Position pos) const;
+ Sci::Position GetLineIndentPosition(Sci::Line line) const noexcept;
+ Sci::Position GetColumn(Sci::Position pos) const noexcept;
Sci::Position CountCharacters(Sci::Position startPos, Sci::Position endPos) const noexcept;
Sci::Position CountUTF16(Sci::Position startPos, Sci::Position endPos) const noexcept;
- Sci::Position FindColumn(Sci::Line line, Sci::Position column);
+ Sci::Position FindColumn(Sci::Line line, Sci::Position column) const noexcept;
void Indent(bool forwards, Sci::Line lineBottom, Sci::Line lineTop);
static std::string TransformLineEnds(const char *s, size_t len, Scintilla::EndOfLine eolModeWanted);
void ConvertLineEnds(Scintilla::EndOfLine eolModeSet);