From 60f7ef8e9c87bef52c68f5951b5de716263f8315 Mon Sep 17 00:00:00 2001
From: Lukas Holecek <hluk@email.cz>
Date: Sun, 19 Apr 2020 12:00:48 +0200
Subject: [PATCH] Improve text elision performance

Signed-off-by: Lukas Holecek <hluk@email.cz>
---
 src/common/common.cpp | 51 +++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/src/common/common.cpp b/src/common/common.cpp
index 1b56ec53a..47c2d1914 100644
--- a/src/common/common.cpp
+++ b/src/common/common.cpp
@@ -470,19 +470,34 @@ bool anySessionOwnsClipboardData(const QVariantMap &data)
 QString elideText(const QString &text, const QFont &font, const QString &format,
                   bool escapeAmpersands, int maxWidthPixels, int maxLines)
 {
+    if ( text.isEmpty() )
+        return QString();
+
     if (maxWidthPixels <= 0)
         maxWidthPixels = smallIconSize() * 20;
 
     QStringList lines = text.split('\n');
 
-    // Ignore empty lines at beginning.
-    static const QRegularExpression reNonEmpty(".*\\S.*");
-    const int firstLine = qMax(0, lines.indexOf(reNonEmpty));
-    const int lastLine = qMax(0, lines.lastIndexOf(reNonEmpty, firstLine + maxLines - 1));
+    int firstLine = -1;
+    int lastLine = -1;
+    int commonIndent = text.size();
+    static const QRegularExpression reNonSpace("\\S");
+    for (int i = 0; i < lines.size(); ++i) {
+        const auto &line = lines[i];
+        const int lineIndent = line.indexOf(reNonSpace);
+        if (lineIndent == -1)
+            continue;
 
-    // If empty lines are at beginning, prepend triple dot.
-    if (firstLine != 0)
-        lines[firstLine].prepend("...");
+        if (firstLine == -1)
+            firstLine = i;
+
+        lastLine = i;
+        if (lineIndent < commonIndent)
+            commonIndent = lineIndent;
+
+        if (firstLine - lastLine + 1 >= maxLines)
+            break;
+    }
 
     if (lastLine == -1)
         return QString("...");
@@ -501,19 +516,9 @@ QString elideText(const QString &text, const QFont &font, const QString &format,
 #endif
 
     // Remove redundant spaces from single line text.
-    if (lines.size() == 1)
+    if (lines.size() == 1) {
         lines[0] = lines[0].simplified();
-
-    // Find common indentation.
-    int commonIndent = lines.value(0).size();
-    static const QRegularExpression reNonSpace("\\S");
-    for (const auto &line : lines) {
-        const int lineIndent = line.indexOf(reNonSpace);
-        if (lineIndent != -1 && lineIndent < commonIndent) {
-            commonIndent = lineIndent;
-            if (commonIndent == 0)
-                break;
-        }
+        commonIndent = 0;
     }
 
     // Remove common indentation each line and elide text if too long.
@@ -527,6 +532,14 @@ QString elideText(const QString &text, const QFont &font, const QString &format,
         line = fm.elidedText(line, Qt::ElideMiddle, maxWidthPixels - formatWidth);
     }
 
+    // If empty lines are at beginning, prepend triple dot.
+    if (firstLine != 0) {
+        if (lines.size() == 1)
+            lines.first().prepend("...");
+        else
+            lines.prepend("...");
+    }
+
     QString result = lines.join("\n");
 
     // Escape all ampersands.
-- 
GitLab