// Copyright (C) 2021 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "quoter.h" #include #include #include QT_BEGIN_NAMESPACE QHash Quoter::s_commentHash; static void replaceMultipleNewlines(QString &s) { const qsizetype n = s.size(); bool slurping = false; int j = -1; const QChar newLine = QLatin1Char('\n'); QChar *d = s.data(); for (int i = 0; i != n; ++i) { const QChar c = d[i]; bool hit = (c == newLine); if (slurping && hit) continue; d[++j] = c; slurping = hit; } s.resize(++j); } // This is equivalent to line.split( QRegularExpression("\n(?!\n|$)") ) but much faster QStringList Quoter::splitLines(const QString &line) { QStringList result; qsizetype i = line.size(); while (true) { qsizetype j = i - 1; while (j >= 0 && line.at(j) == QLatin1Char('\n')) --j; while (j >= 0 && line.at(j) != QLatin1Char('\n')) --j; result.prepend(line.mid(j + 1, i - j - 1)); if (j < 0) break; i = j; } return result; } /* Transforms 'int x = 3 + 4' into 'int x=3+4'. A white space is kept between 'int' and 'x' because it is meaningful in C++. */ static void trimWhiteSpace(QString &str) { enum { Normal, MetAlnum, MetSpace } state = Normal; const qsizetype n = str.size(); int j = -1; QChar *d = str.data(); for (int i = 0; i != n; ++i) { const QChar c = d[i]; if (c.isLetterOrNumber()) { if (state == Normal) { state = MetAlnum; } else { if (state == MetSpace) str[++j] = c; state = Normal; } str[++j] = c; } else if (c.isSpace()) { if (state == MetAlnum) state = MetSpace; } else { state = Normal; str[++j] = c; } } str.resize(++j); } Quoter::Quoter() : m_silent(false) { /* We're going to hard code these delimiters: * C++, Qt, Qt Script, Java: //! [] * .pro, .py, CMake files: #! [] * .html, .qrc, .ui, .xq, .xml files: */ if (s_commentHash.empty()) { s_commentHash["pro"] = "#!"; s_commentHash["py"] = "#!"; s_commentHash["cmake"] = "#!"; s_commentHash["html"] = "