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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-09-14
 * Description : a parse results map for token management
 *
 * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "parseresults.h"

// Local includes

#include "digikam_debug.h"

namespace
{
    static const quint8 INVALID_KEY_ID = -1;
}

namespace Digikam
{

void ParseResults::addEntry(const ResultsKey& key, const ResultsValue& value)
{
    m_results.insert(key, value);
}

void ParseResults::deleteEntry(const ResultsKey& key)
{
    m_results.remove(key);
}

QList<ParseResults::ResultsKey> ParseResults::keys() const
{
    return m_results.keys();
}

bool ParseResults::hasKey(const ResultsKey& key)
{
    return keys().contains(key);
}

QList<ParseResults::ResultsValue> ParseResults::values() const
{
    return m_results.values();
}

QString ParseResults::result(const ResultsKey& key) const
{
    if (m_results.isEmpty())
    {
        return QString();
    }

    QString result = m_results.value(key).second;

    return result;
}

QString ParseResults::token(const ResultsKey& key) const
{
    if (m_results.isEmpty())
    {
        return QString();
    }

    QString token = m_results.value(key).first;

    return token;
}

int ParseResults::offset(const ResultsKey& key) const
{
    int pos    = key.first;
    int length = key.second;

    if      (hasKeyAtPosition(pos))
    {
        return (pos + length);
    }
    else if (hasKeyAtApproximatePosition(pos))
    {
        ResultsKey key2 = keyAtApproximatePosition(pos);

        return ((key2.first + key2.second) - pos);
    }

    return INVALID_KEY_ID;
}

ParseResults::ResultsKey ParseResults::keyAtPosition(int pos) const
{
    const auto keys = m_results.keys();<--- Shadow variable

    for (const ResultsKey& key : keys)
    {
        if (pos == key.first)
        {
            // cppcheck-suppress useStlAlgorithm
            return key;
        }
    }

    return createInvalidKey();
}

bool ParseResults::hasKeyAtPosition(int pos) const
{
    ResultsKey key = keyAtPosition(pos);

    return keyIsValid(key);
}

ParseResults::ResultsKey ParseResults::keyAtApproximatePosition(int pos) const
{
    const auto keys = m_results.keys();<--- Shadow variable

    for (const ResultsKey& key : keys)
    {
        int start  = key.first;
        int length = key.second;

        if ((pos >= start) && (pos <= (start + length)))
        {
            return key;
        }
    }

    return createInvalidKey();
}

bool ParseResults::hasKeyAtApproximatePosition(int pos) const
{
    ResultsKey key = keyAtApproximatePosition(pos);

    return keyIsValid(key);
}

void ParseResults::clear()
{
    m_results.clear();
}

void ParseResults::append(const ParseResults& results)
{
    m_results.unite(results.m_results);
}

bool ParseResults::isEmpty() const
{
    return m_results.isEmpty();
}

ParseResults::ResultsKey ParseResults::createInvalidKey() const
{
    return ResultsKey(INVALID_KEY_ID, INVALID_KEY_ID);
}

bool ParseResults::keyIsValid(const ResultsKey& key) const
{
    return ((key.first != INVALID_KEY_ID) && (key.second != INVALID_KEY_ID));
}

QString ParseResults::replaceTokens(const QString& markedString) const
{
    QString result;<--- Shadow variable

    for (int i = 0 ; i < markedString.count() ; )
    {
        if (hasKeyAtPosition(i))
        {
            ResultsKey key     = keyAtPosition(i);
            ResultsValue value = m_results.value(key);
            result.append(value.second);
            i                 += key.second;
        }
        else
        {
            result.append(markedString.at(i));
            ++i;
        }
    }

    return result;
}

QString ParseResults::resultValuesAsString() const
{
    QString valuesString;
    const auto keys = m_results.keys();<--- Shadow variable

    for (const ResultsKey& key : keys)
    {
        valuesString += result(key);
    }

    return valuesString;
}

void ParseResults::debug() const
{
    const auto keys = m_results.keys();<--- Shadow variable

    for (const ResultsKey& key : keys)
    {
        QString _token  = token(key);
        QString _result = result(key);

        qCDebug(DIGIKAM_GENERAL_LOG) << "(" << key.first << ":" << key.second << ") => "
                                     << "(" << _token    << ":" << _result    << ")";
    }
}

} // namespace Digikam