diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-10-17 11:00:24 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-10-24 12:23:49 +0000 |
commit | 918713a059d06c3b9a570223dbbf6a9e5b2986c9 (patch) | |
tree | 78061a27419a137e3e6c3eaba0731e18f1f7046c /src/plugins | |
parent | 075ead9cd99a47c1ce189de0f599d7a7b54e763c (diff) | |
download | qt-creator-918713a059d06c3b9a570223dbbf6a9e5b2986c9.tar.gz |
ClangFormat: Make more readable helper functions
Change-Id: I25828a17104a778d4be156bf2a2879cefbd08691
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/clangformat/clangformatindenter.cpp | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/src/plugins/clangformat/clangformatindenter.cpp b/src/plugins/clangformat/clangformatindenter.cpp index f6cc910be8..b8c1ebbb9a 100644 --- a/src/plugins/clangformat/clangformatindenter.cpp +++ b/src/plugins/clangformat/clangformatindenter.cpp @@ -133,12 +133,12 @@ void trimFirstNonEmptyBlock(const QTextBlock ¤tBlock) if (!initialText.at(initialText.size() - 1).isSpace()) return; - int extraSpaceCount = 1; - for (int i = initialText.size() - 2; i >= 0; --i) { - if (!initialText.at(i).isSpace()) - break; - ++extraSpaceCount; - } + auto lastNonSpace = std::find_if_not(initialText.rbegin(), + initialText.rend(), + [](const QChar &letter) { + return letter.isSpace(); + }); + const int extraSpaceCount = static_cast<int>(std::distance(initialText.rbegin(), lastNonSpace)); QTextCursor cursor(prevBlock); cursor.beginEditBlock(); @@ -249,28 +249,25 @@ Utils::LineColumn utf16LineColumn(const QTextBlock &block, const QByteArray &utf8Buffer, int utf8Offset) { + // Do not search if the offset is less - we are not interested. if (utf8Offset < blockOffsetUtf8 - 1) return Utils::LineColumn(); - if (utf8Offset == blockOffsetUtf8 - 1) { - const int lineStart = utf8Buffer.lastIndexOf('\n', utf8Offset - 1) + 1; - const QByteArray lineText = utf8Buffer.mid(lineStart, utf8Offset - lineStart); - return Utils::LineColumn(block.blockNumber(), QString::fromUtf8(lineText).size() + 1); - } + // If lastIndexOf('\n') returns -1 then we are fine to add 1 and get 0 offset. + const int lineStartUtf8Offset = utf8Buffer.lastIndexOf('\n', utf8Offset - 1) + 1; - int pos = blockOffsetUtf8; - int prevPos = pos; - int line = block.blockNumber(); // Start with previous line. - while (pos != -1 && pos <= utf8Offset) { - // Find the first pos which comes after offset and take the previous line. - ++line; - prevPos = pos; - pos = utf8Buffer.indexOf('\n', pos); - if (pos != -1) - ++pos; + int line = block.blockNumber() + 1; // Init with the line corresponding the block. + if (utf8Offset == blockOffsetUtf8 - 1) { + // Our offset is the end of the previous line + --line; + } else { + line += static_cast<int>(std::count(utf8Buffer.begin() + blockOffsetUtf8, + utf8Buffer.begin() + lineStartUtf8Offset, + '\n')); } - const QByteArray lineText = utf8Buffer.mid(prevPos, utf8Offset - prevPos); + const QByteArray lineText = utf8Buffer.mid(lineStartUtf8Offset, + utf8Offset - lineStartUtf8Offset); return Utils::LineColumn(line, QString::fromUtf8(lineText).size() + 1); } |