File: kdevstringhandler.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (294 lines) | stat: -rw-r--r-- 9,231 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*  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.

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

    ****************************************************************************
    **
    ** 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$
    **
    ****************************************************************************
*/

#include "kdevstringhandler.h"

#include <QStringList>
#include <QString>
#include <QStringRef>
#include <QByteArray>
#include <QChar>
#include <QDataStream>
#include <QVariant>
#include <QRegExp>
#include <QTextDocument>

#include <algorithm>
#include <cctype>

namespace KDevelop {
QString joinWithEscaping(const QStringList& input, QChar joinchar, QChar escapechar)
{
    QStringList tmp = input;
    return tmp.replaceInStrings(joinchar, QString(joinchar) + QString(escapechar)).join(joinchar);
}

QStringList splitWithEscaping(const QString& input, QChar splitchar, QChar escapechar)
{
    enum State { Normal, SeenEscape } state;

    state = Normal;

    QStringList result;
    QString currentstring;
    for (const QChar c : input) {
        switch (state) {
        case Normal:
            if (c == escapechar) {
                state = SeenEscape;
            } else if (c == splitchar) {
                result << currentstring;
                currentstring.clear();
            } else {
                currentstring += c;
            }
            break;
        case SeenEscape:
            currentstring += c;
            state = Normal;
            break;
        }
    }

    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;
            }
        }
    }
    return QVariant();

}

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

    QString result = QLatin1String("@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();
    }
    }
    return QString();     // never reached
}
}

int KDevelop::findAsciiIdentifierLength(const QStringRef& str)
{
    if (str.isEmpty()) {
        return 0;
    }

    constexpr ushort maxAscii{127};
    const auto firstChar = str[0].unicode();
    const bool isIdentifier = firstChar <= maxAscii
                                && (std::isalpha(firstChar) || firstChar == '_');
    if (!isIdentifier) {
        return 0;
    }

    const auto partOfIdentifier = [=](QChar character) {
        const auto u = character.unicode();
        return u <= maxAscii && (std::isalnum(u) || u == '_');
    };
    return std::find_if_not(str.cbegin() + 1, str.cend(), partOfIdentifier) - str.cbegin();
}

KDevelop::VariableMatch KDevelop::matchPossiblyBracedAsciiVariable(const QStringRef& str)
{
    if (str.isEmpty()) {
        return {};
    }

    if (str[0].unicode() == '{') {
        const auto nameLength = findAsciiIdentifierLength(str.mid(1));
        if (nameLength == 0) {
            return {};
        }
        const auto closingBraceIndex = 1 + nameLength;
        if (closingBraceIndex < str.size() && str[closingBraceIndex].unicode() == '}') {
            return {nameLength + 2, str.mid(1, nameLength).toString()};
        }
    } else {
        const auto nameLength = findAsciiIdentifierLength(str);
        if (nameLength != 0) {
            return {nameLength, str.left(nameLength).toString()};
        }
    }

    return {};
}

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());

    for (const QChar c : str) {
        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;
        }
    }

    return result;
}

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';
        }
    }
}