| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the tools applications of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <qfileinfo.h>
|
|---|
| 43 | #include <qregexp.h>
|
|---|
| 44 | #include <qdebug.h>
|
|---|
| 45 |
|
|---|
| 46 | #include "quoter.h"
|
|---|
| 47 |
|
|---|
| 48 | QT_BEGIN_NAMESPACE
|
|---|
| 49 |
|
|---|
| 50 | static void replaceMultipleNewlines(QString &s)
|
|---|
| 51 | {
|
|---|
| 52 | const int n = s.size();
|
|---|
| 53 | bool slurping = false;
|
|---|
| 54 | int j = -1;
|
|---|
| 55 | const QChar newLine = QLatin1Char('\n');
|
|---|
| 56 | QChar *d = s.data();
|
|---|
| 57 | for (int i = 0; i != n; ++i) {
|
|---|
| 58 | const QChar c = d[i];
|
|---|
| 59 | bool hit = (c == newLine);
|
|---|
| 60 | if (slurping && hit)
|
|---|
| 61 | continue;
|
|---|
| 62 | d[++j] = c;
|
|---|
| 63 | slurping = hit;
|
|---|
| 64 | }
|
|---|
| 65 | s.resize(++j);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // This is equivalent to line.split( QRegExp("\n(?!\n|$)") ) but much faster
|
|---|
| 69 | static QStringList splitLines(const QString &line)
|
|---|
| 70 | {
|
|---|
| 71 | QStringList result;
|
|---|
| 72 | int i = line.size();
|
|---|
| 73 | while (true) {
|
|---|
| 74 | int j = i - 1;
|
|---|
| 75 | while (j >= 0 && line.at(j) == QLatin1Char('\n'))
|
|---|
| 76 | --j;
|
|---|
| 77 | while (j >= 0 && line.at(j) != QLatin1Char('\n'))
|
|---|
| 78 | --j;
|
|---|
| 79 | result.prepend(line.mid(j + 1, i - j - 1));
|
|---|
| 80 | if (j < 0)
|
|---|
| 81 | break;
|
|---|
| 82 | i = j;
|
|---|
| 83 | }
|
|---|
| 84 | return result;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /*
|
|---|
| 88 | Transforms 'int x = 3 + 4' into 'int x=3+4'. A white space is kept
|
|---|
| 89 | between 'int' and 'x' because it is meaningful in C++.
|
|---|
| 90 | */
|
|---|
| 91 | static void trimWhiteSpace( QString& str )
|
|---|
| 92 | {
|
|---|
| 93 | enum { Normal, MetAlnum, MetSpace } state = Normal;
|
|---|
| 94 | const int n = str.length();
|
|---|
| 95 |
|
|---|
| 96 | int j = -1;
|
|---|
| 97 | QChar *d = str.data();
|
|---|
| 98 | for ( int i = 0; i != n; ++i ) {
|
|---|
| 99 | const QChar c = d[i];
|
|---|
| 100 | if ( c.isLetterOrNumber() ) {
|
|---|
| 101 | if ( state == Normal ) {
|
|---|
| 102 | state = MetAlnum;
|
|---|
| 103 | } else {
|
|---|
| 104 | if ( state == MetSpace )
|
|---|
| 105 | str[++j] = c;
|
|---|
| 106 | state = Normal;
|
|---|
| 107 | }
|
|---|
| 108 | str[++j] = c;
|
|---|
| 109 | } else if ( c.isSpace() ) {
|
|---|
| 110 | if ( state == MetAlnum )
|
|---|
| 111 | state = MetSpace;
|
|---|
| 112 | } else {
|
|---|
| 113 | state = Normal;
|
|---|
| 114 | str[++j] = c;
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | str.resize(++j);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | Quoter::Quoter()
|
|---|
| 121 | : silent( false )
|
|---|
| 122 | {
|
|---|
| 123 | /* We're going to hard code these delimiters:
|
|---|
| 124 | * C++, Qt, Qt Script, Java:
|
|---|
| 125 | //! [<id>]
|
|---|
| 126 | * .pro files:
|
|---|
|
|---|