From 83e4a7260dd4131b0028ab3d8fa1bd51bc0e55f7 Mon Sep 17 00:00:00 2001 From: Leandro Melo Date: Tue, 31 May 2011 11:57:00 +0200 Subject: [PATCH] C++ editor: Remove unnecessary line split in find usages 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 Reviewed-by: Erik Verbruggen --- src/libs/cplusplus/FindUsages.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/libs/cplusplus/FindUsages.cpp b/src/libs/cplusplus/FindUsages.cpp index 7c4f75d558..95aa41221a 100644 --- a/src/libs/cplusplus/FindUsages.cpp +++ b/src/libs/cplusplus/FindUsages.cpp @@ -44,6 +44,33 @@ 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 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); -- 2.11.0