OSDN Git Service

C++ editor: Remove unnecessary line split in find usages
authorLeandro Melo <leandro.melo@nokia.com>
Tue, 31 May 2011 09:57:00 +0000 (11:57 +0200)
committerErik Verbruggen <erik.verbruggen@nokia.com>
Tue, 31 May 2011 10:08:17 +0000 (12:08 +0200)
Avoid extra allocations since we only the actual line for the case.

Change-Id: I0d0f0db394d9075bbdce24d1d5b5efa55c52f9b3
Reviewed-on: http://codereview.qt.nokia.com/261
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Erik Verbruggen <erik.verbruggen@nokia.com>
src/libs/cplusplus/FindUsages.cpp

index 7c4f75d..95aa412 100644 (file)
 
 using namespace CPlusPlus;
 
+namespace {
+
+QString fetchLine(const QByteArray &bytes, const int line)
+{
+    int current = 0;
+    const char *s = bytes.constData();
+    while (*s) {
+        if (*s == '\n') {
+            ++current;
+            if (current == line)
+                break;
+        }
+        ++s;
+    }
+
+    if (current == line) {
+        ++s;
+        const char *e = s;
+        while (*e && *e != '\n')
+            ++e;
+        return QString::fromUtf8(s, e - s);
+    }
+    return QString();
+}
+
+} // Anonymous
+
 FindUsages::FindUsages(const QByteArray &originalSource, Document::Ptr doc, const Snapshot &snapshot)
     : ASTVisitor(doc->translationUnit()),
       _id(0),
@@ -167,9 +194,9 @@ void FindUsages::reportResult(unsigned tokenIndex)
     unsigned line, col;
     getTokenStartPosition(tokenIndex, &line, &col);
     QString lineText;
-    QList<QByteArray> lines = _originalSource.split('\n');
-    if (((int) line - 1) < lines.size())
-        lineText = QString::fromUtf8(lines.at(line - 1));
+    const int lines = _originalSource.count('\n') + 1;
+    if (((int) line - 1) < lines)
+        lineText = fetchLine(_originalSource, line - 1);
     else
         lineText = matchingLine(tk);