Utils: simplify Text::textAt

Change-Id: Ibada4af8b7bf76bb61fa45b00ac37bb8ed8a56b2
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
David Schulz
2023-07-06 09:40:13 +02:00
parent e75b81b0a8
commit 5bf78e9955

View File

@@ -124,11 +124,10 @@ QString textAt(QTextCursor tc, int pos, int length)
if (pos < 0) if (pos < 0)
pos = 0; pos = 0;
tc.movePosition(QTextCursor::End); tc.movePosition(QTextCursor::End);
if (pos + length > tc.position()) const int end = std::min(pos + length, tc.position());
length = tc.position() - pos;
tc.setPosition(pos); tc.setPosition(pos);
tc.setPosition(pos + length, QTextCursor::KeepAnchor); tc.setPosition(end, QTextCursor::KeepAnchor);
// selectedText() returns U+2029 (PARAGRAPH SEPARATOR) instead of newline // selectedText() returns U+2029 (PARAGRAPH SEPARATOR) instead of newline
return tc.selectedText().replace(QChar::ParagraphSeparator, QLatin1Char('\n')); return tc.selectedText().replace(QChar::ParagraphSeparator, QLatin1Char('\n'));