1/*
2 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
3 SPDX-FileCopyrightText: 2018 Christoph Cullmann <cullmann@kde.org>
4
5 SPDX-License-Identifier: MIT
6*/
7
8#include "state.h"
9#include "state_p.h"
10
11#include "context_p.h"
12
13#include <QStringList>
14
15using namespace KSyntaxHighlighting;
16
17StateData *StateData::reset(State &state)
18{
19 auto *p = new StateData();
20 state.d.reset(ptr: p);
21 return p;
22}
23
24StateData *StateData::detach(State &state)
25{
26 state.d.detach();
27 return state.d.data();
28}
29
30void StateData::push(const Context *const *firstContext, const Context *const *lastContext, QStringList &&captures)
31{
32 Q_ASSERT(firstContext < lastContext);
33 if (lastContext - firstContext > 1) [[unlikely]] {
34 --lastContext;
35 for (; firstContext < lastContext; ++firstContext) {
36 m_contextStack.push_back(x: StackValue{.context: *firstContext, .captures: captures});
37 }
38 }
39 m_contextStack.push_back(x: StackValue{.context: *firstContext, .captures: std::move(captures)});
40}
41
42bool StateData::pop(int popCount)
43{
44 // nop if nothing to pop
45 if (popCount <= 0) {
46 return true;
47 }
48
49 // keep the initial context alive in any case
50 Q_ASSERT(!m_contextStack.empty());
51 const bool initialContextSurvived = int(m_contextStack.size()) > popCount;
52 m_contextStack.resize(new_size: std::max(a: 1, b: int(m_contextStack.size()) - popCount));
53 return initialContextSurvived;
54}
55
56State::State() = default;
57
58State::State(State &&other) noexcept = default;
59
60State::State(const State &other) noexcept = default;
61
62State::~State() = default;
63
64State &State::operator=(State &&other) noexcept = default;
65
66State &State::operator=(const State &other) noexcept = default;
67
68bool State::operator==(const State &other) const
69{
70 // use pointer equal as shortcut for shared states
71 return (d == other.d) || (d && other.d && d->m_contextStack == other.d->m_contextStack && d->m_defId == other.d->m_defId);
72}
73
74bool State::operator!=(const State &other) const
75{
76 return !(*this == other);
77}
78
79bool State::indentationBasedFoldingEnabled() const
80{
81 if (!d || d->m_contextStack.empty()) {
82 return false;
83 }
84 return d->m_contextStack.back().context->indentationBasedFoldingEnabled();
85}
86
87std::size_t KSyntaxHighlighting::qHash(const State &state, std::size_t seed)
88{
89 return state.d ? qHashMulti(seed, args: *state.d) : 0;
90}
91

source code of syntax-highlighting/src/lib/state.cpp