aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/silversearcher/silversearcheroutputparser.cpp
blob: adaad10b676490b1e16b7b862978c21bc7927179 (plain)
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
// Copyright (C) 2017 Przemyslaw Gorszkowski <pgorszkowski@gmail.com>.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "silversearcheroutputparser.h"

#include <QString>

namespace SilverSearcher {

SilverSearcherOutputParser::SilverSearcherOutputParser(
        const QString &output, const QRegularExpression &regexp)
    : output(output)
    , regexp(regexp)
    , outputSize(output.size())
{
    hasRegexp = !regexp.pattern().isEmpty();
}

QList<Utils::FileSearchResult> SilverSearcherOutputParser::parse()
{
    while (index < outputSize - 1) {
        if (output[index] == '\n') {
            ++index;
            continue;
        }
        parseFilePath();
        while (index < outputSize && output[index] != '\n') {
            parseLineNumber();
            if (index >= outputSize - 1)
                break;
            int matches = parseMatches();
            if (index >= outputSize - 1)
                break;
            parseText();
            for (int i = 0; i < matches; ++i)
                items[items.size() - i - 1].matchingLine = item.matchingLine;
        }
    }

    return items;
}

bool SilverSearcherOutputParser::parseFilePath()
{
    int startIndex = ++index;
    while (index < outputSize && output[index] != '\n')
        ++index;
    item.fileName = QString(output.data() + startIndex, index - startIndex);
    ++index;
    return true;
}

bool SilverSearcherOutputParser::parseLineNumber()
{
    int startIndex = index;
    while (index < outputSize && output[++index] != ';') { }

    item.lineNumber = QString(output.data() + startIndex, index - startIndex).toInt();
    ++index;
    return true;
}

bool SilverSearcherOutputParser::parseMatchIndex()
{
    int startIndex = index;
    while (index < outputSize && output[++index] != ' ') { }

    item.matchStart = QString(output.data() + startIndex, index - startIndex).toInt();
    ++index;
    return true;
}

bool SilverSearcherOutputParser::parseMatchLength()
{
    int startIndex = index;
    while (index < outputSize && output[++index] != ':' && output[index] != ',') { }

    item.matchLength = QString(output.data() + startIndex, index - startIndex).toInt();
    return true;
}

int SilverSearcherOutputParser::parseMatches()
{
    int matches = 1;
    const int colon = output.indexOf(':', index);
    QString text;
    if (colon != -1) {
        const int textStart = colon + 1;
        const int newline = output.indexOf('\n', textStart);
        text = output.mid(textStart, newline >= 0 ? newline - textStart : -1);
    }
    while (index < outputSize && output[index] != ':') {
        if (output[index] == ',') {
            ++matches;
            ++index;
        }
        parseMatchIndex();
        parseMatchLength();
        if (hasRegexp) {
            const QString part = QString(text.mid(item.matchStart, item.matchLength));
            item.regexpCapturedTexts = regexp.match(part).capturedTexts();
        }
        items << item;
    }

    ++index;
    return matches;
}

bool SilverSearcherOutputParser::parseText()
{
    int startIndex = index;
    while (index < outputSize && output[++index] != '\n') { }
    item.matchingLine = QString(output.data() + startIndex, index - startIndex);
    ++index;
    return true;
}

} // namespace SilverSearcher