aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2021-07-17 10:04:20 +1000
committerNeil <nyamatongwe@gmail.com>2021-07-17 10:04:20 +1000
commitbea9d7216879bc80a8513d3374a931cba14f6951 (patch)
tree0a34eab53e9e7e8c3b91fec994f64b1bfba42df9
parent046b280114d11ff24b179dbe4df44127e80d39b5 (diff)
downloadscintilla-mirror-bea9d7216879bc80a8513d3374a931cba14f6951.tar.gz
Fix minor inconsistency with word searching results at start and end of document
which were considered word start / end positions even when first or last character did not fit requirements.
-rw-r--r--doc/ScintillaHistory.html3
-rw-r--r--src/Document.cxx14
-rw-r--r--test/unit/testDocument.cxx8
3 files changed, 20 insertions, 5 deletions
diff --git a/doc/ScintillaHistory.html b/doc/ScintillaHistory.html
index f433322ea..c6261693d 100644
--- a/doc/ScintillaHistory.html
+++ b/doc/ScintillaHistory.html
@@ -580,6 +580,9 @@
<a href="https://sourceforge.net/p/scintilla/feature-requests/1381/">Feature #1381</a>.
</li>
<li>
+ Word searching behaves more consistently at start and end of document.
+ </li>
+ <li>
Add SCI_ALLOCATELINES to allocate indices to hold some number of lines.
This can decrease reallocation overhead when the application can count or estimate the number of lines in huge files.
<a href="https://sourceforge.net/p/scintilla/feature-requests/1370/">Feature #1370</a>.
diff --git a/src/Document.cxx b/src/Document.cxx
index 3a3627fdc..b5ec9a275 100644
--- a/src/Document.cxx
+++ b/src/Document.cxx
@@ -1923,23 +1923,27 @@ constexpr bool IsWordEdge(CharacterClass cc, CharacterClass ccNext) noexcept {
bool Document::IsWordStartAt(Sci::Position pos) const {
if (pos >= LengthNoExcept())
return false;
- if (pos > 0) {
+ if (pos >= 0) {
const CharacterExtracted cePos = CharacterAfter(pos);
- const CharacterExtracted cePrev = CharacterBefore(pos);
+ // At start of document, treat as if space before so can be word start
+ const CharacterExtracted cePrev = (pos > 0) ?
+ CharacterBefore(pos) : CharacterExtracted(' ', 1);
return IsWordEdge(WordCharacterClass(cePos.character), WordCharacterClass(cePrev.character));
}
return true;
}
/**
- * Check that the character at the given position is a word or punctuation character and that
+ * Check that the character before the given position is a word or punctuation character and that
* the next character is of a different character class.
*/
bool Document::IsWordEndAt(Sci::Position pos) const {
if (pos <= 0)
return false;
- if (pos < LengthNoExcept()) {
- const CharacterExtracted cePos = CharacterAfter(pos);
+ if (pos <= LengthNoExcept()) {
+ // At end of document, treat as if space after so can be word end
+ const CharacterExtracted cePos = (pos < LengthNoExcept()) ?
+ CharacterAfter(pos) : CharacterExtracted(' ', 1);
const CharacterExtracted cePrev = CharacterBefore(pos);
return IsWordEdge(WordCharacterClass(cePrev.character), WordCharacterClass(cePos.character));
}
diff --git a/test/unit/testDocument.cxx b/test/unit/testDocument.cxx
index 0f8e399fa..809647e19 100644
--- a/test/unit/testDocument.cxx
+++ b/test/unit/testDocument.cxx
@@ -477,4 +477,12 @@ TEST_CASE("Words") {
REQUIRE(docOverSpace.document.IsWordAt(1, 4));
}
+ SECTION("WordsAtEnds") {
+ const DocPlus doc("a c", 0);
+ REQUIRE(doc.document.IsWordAt(0, 1));
+ REQUIRE(doc.document.IsWordAt(2, 3));
+ const DocPlus docEndSpace(" a c ", 0);
+ REQUIRE(!docEndSpace.document.IsWordAt(0, 2));
+ REQUIRE(!docEndSpace.document.IsWordAt(3, 5));
+ }
}