File: stringhelpers.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (564 lines) | stat: -rw-r--r-- 17,570 bytes parent folder | download | duplicates (2)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
    SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "stringhelpers.h"
#include <debug.h>

#include <QString>

#include <algorithm>
#include <utility>

namespace {
bool endsWithWordBoundary(QStringView str)
{
    if (str.isEmpty()) {
        return true;
    }
    const auto boundary = str.last();
    return !boundary.isLetterOrNumber() && boundary != QLatin1Char('_');
}

/// libclang surrounds binary operators but not angle brackets with spaces.
bool isOperatorSurroundedWithSpaces(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());

    if (pos == 0 || pos == str.size() - 1) {
        return false; // there is no place for surrounding spaces
    }

    constexpr QLatin1Char lt{'<'}, gt{'>'}, eq{'='}, space{' '};

    const auto c = str[pos];
    Q_ASSERT(c == lt || c == gt);

    // Note: due to the `pos == 0 || pos == str.size() - 1` check above,
    // most conditionals below don't need to check boundaries.
    int operatorEnd = pos + 1;
    if (str[pos + 1] == c)
        ++operatorEnd; // << or >>
    else if (str[pos - 1] == c) {
        --pos; // << or >>
    } else {
        // <=>
        if (c == lt && str[pos + 1] == eq && pos + 2 < str.size() && str[pos + 2] == gt) {
            operatorEnd += 2;
        } else if (c == gt && str[pos - 1] == eq && pos >= 2 && str[pos - 2] == lt) {
            pos -= 2;
        }
    }

    if (operatorEnd - pos < 3 && operatorEnd < str.size() && str[operatorEnd] == eq) {
        ++operatorEnd; // <= or >= or <<= or >>=
    }

    return pos > 0 && str[pos - 1] == space && operatorEnd < str.size() && str[operatorEnd] == space;
}

bool isOperator(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());

    if (isOperatorSurroundedWithSpaces(str, pos)) {
        return true;
    }

    const auto op = QLatin1String("operator");
    if (pos < op.size()) {
        return false;
    }

    const auto c = str[pos];
    Q_ASSERT(c == QLatin1Char('<') || c == QLatin1Char('>'));

    --pos;

    // note: due to the `pos < op.size()` check above, the below conditionals don't need to check boundaries
    if (str[pos] == c) {
        // handle `operator<<` and `operator>>`
        --pos;
    } else if (c == QLatin1Char('>') && str[pos] == QLatin1Char('=') && str[pos - 1] == QLatin1Char('<')) {
        // handle `operator<=>`
        pos -= 2;
    }

    // skip spaces, e.g. `operator <`
    while (pos > 0 && str[pos].isSpace()) {
        --pos;
    }

    auto prefix = str.first(pos + 1);
    if (!prefix.endsWith(op)) {
        return false;
    }

    prefix.chop(op.size());
    return endsWithWordBoundary(prefix);
}

// check for operator-> but don't get confused by operator-->
bool isArrowOperator(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());

    Q_ASSERT(str[pos] == QLatin1Char('>'));
    return pos > 0 && str[pos - 1] == QLatin1Char('-') && (pos == 1 || str[pos - 2] != QLatin1Char('-'));
}

bool isOperatorOrArrowOperator(QStringView str, int pos)
{
    return isOperator(str, pos) || isArrowOperator(str, pos);
}

/// Skips literals enclosed in single or double quotes.
/// No need to support raw string literals, because they cannot appear within a macro parameter list;
/// in other contexts libclang converts them into non-raw string literals in each string that ends up here.
int skipStringOrCharLiteral(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());

    const auto quote = str[pos];
    Q_ASSERT(quote == QLatin1Char('\'') || quote == QLatin1Char('"'));

    const auto end = str.size();
    pos++;
    while (pos < end && (str[pos] != quote || str[pos - 1] == QLatin1Char('\\'))) {
        pos++;
    }
    return pos;
}

/// Skips multi-line comments.
/// No need to support single-line comments, because they cannot appear within a macro parameter list;
/// in other contexts libclang removes comments from each string that ends up here.
int skipComment(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());
    Q_ASSERT(str[pos] == QLatin1Char{'/'});

    if (pos + 1 == str.size() || str[pos + 1] != QLatin1Char{'*'})
        return pos; // not a comment
    pos += 2;

    while (pos < str.size() && (str[pos] != QLatin1Char{'/'} || str[pos - 1] != QLatin1Char{'*'})) {
        ++pos;
    }

    return pos;
}

int trySkipStringOrCharLiteralOrComment(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());

    switch (str[pos].unicode()) {
    case '"':
    case '\'':
        return skipStringOrCharLiteral(str, pos);
    case '/':
        return skipComment(str, pos);
    }
    return pos;
}
} // unnamed namespace

namespace KDevelop {
bool consistsOfWhitespace(QStringView str)
{
    return std::all_of(str.cbegin(), str.cend(), [](QChar c) {
        return c.isSpace();
    });
}

class ParamIteratorPrivate
{
    Q_DISABLE_COPY_MOVE(ParamIteratorPrivate)
public:
    explicit ParamIteratorPrivate(QStringView parens, QStringView source)
        : m_parens(parens)
        , m_source(source)
    {
    }

    const QStringView m_parens;
    const QStringView m_source;
    QStringView m_prefix;
    int m_cur;
    int m_curEnd;
    int m_end;

    QStringView sourceRange(int first, int last) const
    {
        return m_source.sliced(first, last - first);
    }

    int next() const
    {
        return findCommaOrEnd(m_source, m_cur, m_parens[1]);
    }
};

namespace {
QChar fittingClosingNonAngleBracket(QChar openingBracket)
{
    switch (openingBracket.unicode()) {
    case '(':
        return QLatin1Char(')');
    case '[':
        return QLatin1Char(']');
    case '{':
        return QLatin1Char('}');
    default:
        Q_UNREACHABLE();
    }
}

// findClosingNonAngleBracket() and findClosingAngleBracket() have different implementations for the following reason.
// Taking all bracket types into account while looking for a closing angle bracket may improve correctness, because the
// characters of other bracket types are always brackets, not [parts of] operators; distinguishing between angle
// brackets and operators is heuristic and unreliable. For example, in `Foo<(A>B)>` the round brackets help to recognize
// the first '>' character as an operator rather than a closing angle bracket. Conversely, taking all bracket types into
// account while looking for a closing non-angle bracket may adversely affect correctness. For example, in `Foo<(A<B)>`
// the second '<' character would be regarded as an opening angle bracket, which would prevent recognizing the closing
// round bracket.

/// Finds in @p str the position of a fitting closing bracket for the opening bracket @p str[@p pos], e.g. ')' for '('.
/// @return the position of a fitting closing bracket or str.size() if not found.
/// @warning This function does not support angle brackets. Use findClosingAngleBracket() for that.
int findClosingNonAngleBracket(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());
    Q_ASSERT(str[pos] == QLatin1Char{'('} || str[pos] == QLatin1Char{'['} || str[pos] == QLatin1Char{'{'});

    const auto openingBracket = str[pos];
    const auto closingBracket = fittingClosingNonAngleBracket(openingBracket);

    int depth = 1;

    for (++pos; pos < str.size(); ++pos) {
        if (str[pos] == openingBracket) {
            ++depth;
        } else if (str[pos] == closingBracket) {
            if (--depth == 0) {
                return pos;
            }
        } else {
            pos = trySkipStringOrCharLiteralOrComment(str, pos);
        }
    }

    Q_ASSERT(depth > 0);
    return str.size();
}

/// Finds in @p str the position of a fitting closing angle bracket for the opening angle bracket @p str[@p pos] == '<'.
/// @return the position of a fitting closing bracket or str.size() if not found.
int findClosingAngleBracket(QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos < str.size());
    Q_ASSERT(str[pos] == QLatin1Char{'<'});

    int depth = 1;

    for (++pos; pos < str.size(); ++pos) {
        switch (str[pos].unicode()) {
        case '<':
            if (!isOperator(str, pos)) {
                ++depth;
            }
            break;
        case '>':
            if (!isOperatorOrArrowOperator(str, pos)) {
                if (--depth == 0) {
                    return pos;
                }
            }
            break;
        case '(':
        case '[':
        case '{':
            pos = findClosingNonAngleBracket(str, pos);
            break;
        default:
            pos = trySkipStringOrCharLiteralOrComment(str, pos);
        }
    }

    Q_ASSERT(depth > 0);
    return str.size();
}

/// Finds in @p str the position of @p parens[0] or @p parens[2] starting from @p pos at the top level.
/// @return the position of the found symbol or str.size() if not found.
/// @param parens see ParamIterator().
int findOpeningBracketOrEnd(QStringView parens, QStringView str, int pos)
{
    Q_ASSERT(pos >= 0 && pos <= str.size());

    Q_ASSERT(parens.size() == 2 || parens.size() == 3);

    Q_ASSERT(QStringView(u"<([{").contains(parens[0]));
    Q_ASSERT(parens.first(2) == u"<>" || parens[1] == fittingClosingNonAngleBracket(parens[0]));

    Q_ASSERT(parens.size() == 2 || !QStringView(u"<>()[]{}").contains(parens[2]));

    for (; pos < str.size(); ++pos) {
        switch (str[pos].unicode()) {
        // Take into account brackets of all types to skip searched-for symbols within them (i.e. not at the top level).
        case '<':
            if (!isOperator(str, pos)) {
                if (str[pos] == parens[0]) {
                    return pos;
                }
                pos = findClosingAngleBracket(str, pos);
            }
            break;
        case '(':
        case '[':
        case '{':
            if (str[pos] == parens[0]) {
                return pos;
            }
            pos = findClosingNonAngleBracket(str, pos);
            break;
        default:
            if (parens.size() > 2 && str[pos] == parens[2]) {
                return pos;
            }
            pos = trySkipStringOrCharLiteralOrComment(str, pos);
        }
    }

    return str.size();
}
} // unnamed namespace

int findCommaOrEnd(QStringView str, int pos, QChar validEnd)
{
    const auto size = str.size();
    Q_ASSERT(pos >= 0 && pos <= size);

    for (; pos < size; ++pos) {
        switch (str[pos].unicode()) {
        // Take into account brackets of all types, not just the validEnd type, to skip ',' within them.
        case '<':
            if (!isOperator(str, pos)) {
                pos = findClosingAngleBracket(str, pos);
            }
            break;
        case '(':
        case '[':
        case '{':
            pos = findClosingNonAngleBracket(str, pos);
            break;
        case ',':
            return pos;
        default:
            if (str[pos] == validEnd && !(str[pos] == QLatin1Char('>') && isOperatorOrArrowOperator(str, pos))) {
                return pos;
            }
            pos = trySkipStringOrCharLiteralOrComment(str, pos);
        }
    }

    return size;
}

// NOTE: keep in sync with QString overload below
QByteArray formatComment(const QByteArray& comment)
{
    if (comment.isEmpty())
        return comment;

    auto lines = comment.split('\n');
    // remove common leading & trailing chars from the lines
    for (auto& l : lines) {
        // don't trigger repeated temporary allocations here

        // possible comment starts, sorted from longest to shortest
        static const QByteArray startMatches[] = {
            QByteArrayLiteral("//!<"), QByteArrayLiteral("/*!<"), QByteArrayLiteral("/**<"), QByteArrayLiteral("///<"),
            QByteArrayLiteral("///"),  QByteArrayLiteral("//!"),  QByteArrayLiteral("/**"),  QByteArrayLiteral("/*!"),
            QByteArrayLiteral("//"),   QByteArrayLiteral("/*"),   QByteArrayLiteral("/"),    QByteArrayLiteral("*")};

        // possible comment ends, sorted from longest to shortest
        static const QByteArray endMatches[] = {QByteArrayLiteral("**/"), QByteArrayLiteral("*/")};

        l = l.trimmed();

        // check for ends first, as the starting pattern "*" might interfere with the ending pattern
        for (const auto& m : endMatches) {
            if (l.endsWith(m)) {
                l.chop(m.length());
                break;
            }
        }

        for (const auto& m : startMatches) {
            if (l.startsWith(m)) {
                l.remove(0, m.length());
                break;
            }
        }
    }

    QByteArray ret;
    for (const auto& line : std::as_const(lines)) {
        if (!ret.isEmpty())
            ret += '\n';
        ret += line;
    }
    return ret.trimmed();
}

// NOTE: keep in sync with QByteArray overload above
QString formatComment(const QString& comment)
{
    if (comment.isEmpty())
        return comment;

    auto lines = QStringView{comment}.split(QLatin1Char('\n'));

    // remove common leading & trailing chars from the lines
    for (auto& l : lines) {
        // don't trigger repeated temporary allocations here

        // possible comment starts, sorted from longest to shortest
        static const QString startMatches[] = {QStringLiteral("//!<"), QStringLiteral("/*!<"), QStringLiteral("/**<"),
                                               QStringLiteral("///<"), QStringLiteral("///"),  QStringLiteral("//!"),
                                               QStringLiteral("/**"),  QStringLiteral("/*!"),  QStringLiteral("//"),
                                               QStringLiteral("/*"),   QStringLiteral("/"),    QStringLiteral("*")};

        // possible comment ends, sorted from longest to shortest
        static const QString endMatches[] = {QStringLiteral("**/"), QStringLiteral("*/")};

        l = l.trimmed();

        // check for ends first, as the starting pattern "*" might interfere with the ending pattern
        for (const auto& m : endMatches) {
            if (l.endsWith(m)) {
                l.chop(m.length());
                break;
            }
        }

        for (const auto& m : startMatches) {
            if (l.startsWith(m)) {
                l = l.sliced(m.length());
                break;
            }
        }
    }

    QString ret;
    for (const auto line : std::as_const(lines)) {
        if (!ret.isEmpty())
            ret += QLatin1Char('\n');
        ret += line;
    }

    return ret.trimmed();
}

QString removeWhitespace(const QString& str)
{
    return str.simplified().remove(QLatin1Char(' '));
}

ParamIterator::~ParamIterator() = default;

ParamIterator::ParamIterator(QStringView parens, QStringView source, int offset)
    : d_ptr(new ParamIteratorPrivate{parens, source})
{
    Q_D(ParamIterator);

    const auto foundPos = findOpeningBracketOrEnd(parens, source, offset);
    if (foundPos != source.size()) {
        if (parens.size() > 2 && source[foundPos] == parens[2]) {
            //We have to stop the search, because we found an interrupting end-sign before the opening-paren
            d->m_prefix = d->sourceRange(offset, foundPos);
            d->m_curEnd = d->m_end = d->m_cur = foundPos;
            return;
        }

        Q_ASSERT(source[foundPos] == parens[0]);
        //We have a valid prefix before an opening-paren. Take the prefix, and start iterating parameters.
        d->m_cur = foundPos + 1;
        d->m_curEnd = d->next();
        if (d->m_curEnd != d->m_source.length()) {
            d->m_prefix = d->sourceRange(offset, foundPos);
            d->m_end = d->m_source.size();

            if (d->m_source[d->m_curEnd] == d->m_parens[1]) {
                const auto singleParam = d->sourceRange(d->m_cur, d->m_curEnd);
                if (consistsOfWhitespace(singleParam)) {
                    // Only whitespace characters are present between parentheses => assume that
                    // there are zero parameters, not a single empty parameter, and stop iterating.
                    d->m_cur = d->m_end = d->m_curEnd + 1;
                }
            }

            return;
        } // else: the paren was not closed. It might be an identifier like "operator<", so count everything as prefix.
    } // else: we have neither found an ending-character, nor an opening-paren, so take the whole input and end.

    d->m_prefix = d->m_source.sliced(offset);
    d->m_curEnd = d->m_end = d->m_cur = d->m_source.length();
}

ParamIterator& ParamIterator::operator ++()
{
    Q_D(ParamIterator);

    Q_ASSERT(*this);

    if (d->m_curEnd >= d->m_source.size()) {
        //We have reached the end-paren. Stop iterating.
        d->m_cur = d->m_end = d->m_curEnd;
    } else if (d->m_source[d->m_curEnd] == d->m_parens[1]) {
        //We have reached the end-paren. Stop iterating.
        d->m_cur = d->m_end = d->m_curEnd + 1;
    } else {
        //Iterate on through parameters
        d->m_cur = d->m_curEnd + 1;
        if (d->m_cur < d->m_source.length()) {
            d->m_curEnd = d->next();
        }
    }
    return *this;
}

QStringView ParamIterator::operator*() const
{
    Q_D(const ParamIterator);

    Q_ASSERT(*this);

    return d->sourceRange(d->m_cur, d->m_curEnd).trimmed();
}

ParamIterator::operator bool() const
{
    Q_D(const ParamIterator);

    return d->m_cur < d->m_end;
}

QStringView ParamIterator::prefix() const
{
    Q_D(const ParamIterator);

    return d->m_prefix;
}

uint ParamIterator::position() const
{
    Q_D(const ParamIterator);

    return ( uint )d->m_cur;
}
}