kdevstringhandler.cpp 7.63 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  This file is part of KDevelop
    Copyright 2009 Andreas Pakulat <apaku@gmx.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
18
19

    This file mostly code takes from Qt's QSettings class, the copyright
20
    header from that file follows:
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    ****************************************************************************
    **
    ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    ** Contact: Nokia Corporation (qt-info@nokia.com)
    **
    ** This file is part of the QtCore module of the Qt Toolkit.
    **
    ** $QT_BEGIN_LICENSE:LGPL$
    ** Commercial Usage
    ** Licensees holding valid Qt Commercial licenses may use this file in
    ** accordance with the Qt Commercial License Agreement provided with the
    ** Software or, alternatively, in accordance with the terms contained in
    ** a written agreement between you and Nokia.
    **
    ** GNU Lesser General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU Lesser
    ** General Public License version 2.1 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.LGPL included in the
    ** packaging of this file.  Please review the following information to
    ** ensure the GNU Lesser General Public License version 2.1 requirements
    ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    **
    ** In addition, as a special exception, Nokia gives you certain
    ** additional rights. These rights are described in the Nokia Qt LGPL
    ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    ** package.
    **
    ** GNU General Public License Usage
    ** Alternatively, this file may be used under the terms of the GNU
    ** General Public License version 3.0 as published by the Free Software
    ** Foundation and appearing in the file LICENSE.GPL included in the
    ** packaging of this file.  Please review the following information to
    ** ensure the GNU General Public License version 3.0 requirements will be
    ** met: http://www.gnu.org/copyleft/gpl.html.
    **
    ** If you are unsure which license is appropriate for your use, please
    ** contact the sales department at http://www.qtsoftware.com/contact.
    ** $QT_END_LICENSE$
    **
    ****************************************************************************
62
63
64
65
66
67
*/

#include "kdevstringhandler.h"

#include <QStringList>
#include <QString>
68
#include <QByteArray>
69
#include <QChar>
70
71
#include <QDataStream>
#include <QVariant>
72
73
#include <QRegExp>
#include <QTextDocument>
74

75
namespace KDevelop {
76
QString joinWithEscaping(const QStringList& input, QChar joinchar, QChar escapechar)
77
{
78
79
80
81
    QStringList tmp = input;
    return tmp.replaceInStrings(joinchar, QString(joinchar) + QString(escapechar)).join(joinchar);
}

82
QStringList splitWithEscaping(const QString& input, QChar splitchar, QChar escapechar)
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
{
    enum State { Normal, SeenEscape } state;

    state = Normal;

    QStringList result;
    QString currentstring;
    for (int i = 0; i < input.size(); i++) {
        switch (state) {
        case Normal:
            if (input[i] == escapechar) {
                state = SeenEscape;
            } else if (input[i] == splitchar) {
                result << currentstring;
                currentstring.clear();
            } else {
                currentstring += input[i];
100
            }
101
102
103
104
105
            break;
        case SeenEscape:
            currentstring += input[i];
            state = Normal;
            break;
106
107
        }
    }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

    if (!currentstring.isEmpty()) {
        result << currentstring;
    }
    return result;
}

QVariant stringToQVariant(const QString& s)
{
    // Taken from qsettings.cpp, stringToVariant()
    if (s.startsWith(QLatin1Char('@'))) {
        if (s.endsWith(QLatin1Char(')'))) {
            if (s.startsWith(QLatin1String("@Variant("))) {
                QByteArray a(s.toLatin1().mid(9));
                QDataStream stream(&a, QIODevice::ReadOnly);
                stream.setVersion(QDataStream::Qt_4_4);
                QVariant result;
                stream >> result;
                return result;
127
128
129
            }
        }
    }
130
131
132
133
134
135
136
137
    return QVariant();

}

QString qvariantToString(const QVariant& variant)
{
    // Taken from qsettings.cpp, variantToString()
    QByteArray a;
138
    {
139
140
141
        QDataStream s(&a, QIODevice::WriteOnly);
        s.setVersion(QDataStream::Qt_4_4);
        s << variant;
142
    }
143

144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    QString result = QStringLiteral("@Variant(") + QString::fromLatin1(a.constData(), a.size()) + QLatin1Char(')');
    return result;

}

QString htmlToPlainText(const QString& s, HtmlToPlainTextMode mode)
{
    switch (mode) {
    case FastMode: {
        QString result(s);
        result.remove(QRegExp(QStringLiteral("<[^>]+>")));
        return result;
    }
    case CompleteMode: {
        QTextDocument doc;
        doc.setHtml(s);
        return doc.toPlainText();
    }
162
    }
163
164
    return QString();     // never reached
}
165
166
}

Kevin Funk's avatar
Kevin Funk committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
QString KDevelop::stripAnsiSequences(const QString& str)
{
    if (str.isEmpty()) {
        return QString(); // fast path
    }

    enum {
        PLAIN,
        ANSI_START,
        ANSI_CSI,
        ANSI_SEQUENCE,
        ANSI_WAITING_FOR_ST,
        ANSI_ST_STARTED
    } state = PLAIN;

    QString result;
    result.reserve(str.count());

185
    for (const QChar c : str) {
Kevin Funk's avatar
Kevin Funk committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        const auto val = c.unicode();
        switch (state) {
        case PLAIN:
            if (val == 27) // 'ESC'
                state = ANSI_START;
            else if (val == 155) // equivalent to 'ESC'-'['
                state = ANSI_CSI;
            else
                result.append(c);
            break;
        case ANSI_START:
            if (val == 91) // [
                state = ANSI_CSI;
            else if (val == 80 || val == 93 || val == 94 || val == 95) // 'P', ']', '^' and '_'
                state = ANSI_WAITING_FOR_ST;
            else if (val >= 64 && val <= 95)
                state = PLAIN;
            else
                state = ANSI_SEQUENCE;
            break;
        case ANSI_CSI:
            if (val >= 64 && val <= 126) // Anything between '@' and '~'
                state = PLAIN;
            break;
        case ANSI_SEQUENCE:
            if (val >= 64 && val <= 95) // Anything between '@' and '_'
                state = PLAIN;
            break;
        case ANSI_WAITING_FOR_ST:
            if (val == 7) // 'BEL'
                state = PLAIN;
            else if (val == 27) // 'ESC'
                state = ANSI_ST_STARTED;
            break;
        case ANSI_ST_STARTED:
            if (val == 92) // '\'
                state = PLAIN;
            else
                state = ANSI_WAITING_FOR_ST;
            break;
        }
    }
228

Kevin Funk's avatar
Kevin Funk committed
229
230
    return result;
}
231
232
233
234
235
236
237
238
239
240
241
242
243
244

void KDevelop::normalizeLineEndings(QByteArray& text)
{
    for (int i = 0, s = text.size(); i < s; ++i) {
        if (text[i] != '\r') {
            continue;
        }
        if (i + 1 < s && text[i + 1] == '\n') {
            text.remove(i, 1);
        } else {
            text[i] = '\n';
        }
    }
}