aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/3rdparty/syntax-highlighting/src/lib/dynamicregexpcache_p.h
blob: dcef97a8416fe74c631206fc82604d812bc2d25e (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
/*
    SPDX-FileCopyrightText: 2023 Jonathan Poelen <jonathan.poelen+kde@gmail.com>

    SPDX-License-Identifier: MIT
*/

#ifndef KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H
#define KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H

#include <QCache>
#include <QRegularExpression>
#include <QString>

#include <utility>

namespace KSyntaxHighlighting
{

class DynamicRegexpCache
{
public:
    const QRegularExpression &compileRegexp(QString &&pattern, QRegularExpression::PatternOptions patternOptions)
    {
        const auto key = std::pair{std::move(pattern), patternOptions};
        if (const auto regexp = m_cache.object(key)) {
            return *regexp;
        }
        auto regexp = new QRegularExpression(key.first, patternOptions);
        m_cache.insert(key, regexp);
        return *regexp;
    }

private:
    QCache<std::pair<QString, QRegularExpression::PatternOptions>, QRegularExpression> m_cache;
};

}

#endif