1/*
2 SPDX-FileCopyrightText: 2023 Jonathan Poelen <jonathan.poelen+kde@gmail.com>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#ifndef KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H
8#define KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H
9
10#include <QCache>
11#include <QRegularExpression>
12#include <QString>
13
14#include <utility>
15
16namespace KSyntaxHighlighting
17{
18
19class DynamicRegexpCache
20{
21public:
22 const QRegularExpression &compileRegexp(QString &&pattern, QRegularExpression::PatternOptions patternOptions)
23 {
24 const auto key = std::pair{std::move(pattern), patternOptions};
25 if (const auto regexp = m_cache.object(key)) {
26 return *regexp;
27 }
28 auto regexp = new QRegularExpression(key.first, patternOptions);
29 m_cache.insert(key, object: regexp);
30 return *regexp;
31 }
32
33private:
34 QCache<std::pair<QString, QRegularExpression::PatternOptions>, QRegularExpression> m_cache;
35};
36
37}
38
39#endif
40

source code of syntax-highlighting/src/lib/dynamicregexpcache_p.h