aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/textutils.cpp
diff options
context:
space:
mode:
authorDavid Schulz <[email protected]>2023-05-11 09:34:53 +0200
committerDavid Schulz <[email protected]>2023-05-12 11:03:33 +0000
commit1a98dda5c417e892cadbedb656829cf2ba4d1d0a (patch)
tree8b6bb6b91796e16f0a95ec7ec1003d4f49d68523 /src/libs/utils/textutils.cpp
parent70609cdec6fa3115af215f16fc5ae48602736027 (diff)
Utils: fix Text::Range length and remove mid
Those functions are based on the assumption that the passed text starts at the begin position, which was good enough for search results, but if used in other parts of the codebase it might give unwanted results. Calculate the length of the range now as expected and subtract the beginning lines. In order to still got the correct results for the text result texts modify the result range to always start at the first line before calculating the length of the range. Also add tests for the modified functionality Change-Id: I7ccd75b642dda6dd4f738877cbe3543d46c03652 Reviewed-by: Qt CI Bot <[email protected]> Reviewed-by: Jarek Kobus <[email protected]>
Diffstat (limited to 'src/libs/utils/textutils.cpp')
-rw-r--r--src/libs/utils/textutils.cpp24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/libs/utils/textutils.cpp b/src/libs/utils/textutils.cpp
index 34dbb5e1fa5..8cf7a41ed60 100644
--- a/src/libs/utils/textutils.cpp
+++ b/src/libs/utils/textutils.cpp
@@ -58,22 +58,30 @@ Position Position::fromPositionInDocument(const QTextDocument *document, int pos
int Range::length(const QString &text) const
{
+ if (end.line < begin.line)
+ return -1;
+
if (begin.line == end.line)
return end.column - begin.column;
- const int lineCount = end.line - begin.line;
- int index = text.indexOf(QChar::LineFeed);
+ int index = 0;
int currentLine = 1;
- while (index > 0 && currentLine < lineCount) {
+ while (currentLine < begin.line) {
+ index = text.indexOf(QChar::LineFeed, index);
+ if (index < 0)
+ return -1;
++index;
+ ++currentLine;
+ }
+ const int beginIndex = index + begin.column;
+ while (currentLine < end.line) {
index = text.indexOf(QChar::LineFeed, index);
+ if (index < 0)
+ return -1;
+ ++index;
++currentLine;
}
-
- if (index < 0)
- return 0;
-
- return index - begin.column + end.column;
+ return index + end.column - beginIndex;
}
bool Range::operator==(const Range &other) const