1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia 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:
|
---|
127 | #! [<id>]
|
---|
128 | * .xq, .xml, .html files:
|
---|
129 | <!-- [<id>] -->
|
---|
130 | */
|
---|
131 | commentHash["pro"] = "#!";
|
---|
132 | commentHash["py"] = "#!";
|
---|
133 | commentHash["html"] = "<!--";
|
---|
134 | commentHash["qrc"] = "<!--";
|
---|
135 | commentHash["ui"] = "<!--";
|
---|
136 | commentHash["xml"] = "<!--";
|
---|
137 | commentHash["xq"] = "<!--";
|
---|
138 | }
|
---|
139 |
|
---|
140 | void Quoter::reset()
|
---|
141 | {
|
---|
142 | silent = false;
|
---|
143 | plainLines.clear();
|
---|
144 | markedLines.clear();
|
---|
|
---|