| 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 | |
| 16 | namespace KSyntaxHighlighting |
| 17 | { |
| 18 | |
| 19 | class DynamicRegexpCache |
| 20 | { |
| 21 | public: |
| 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 | |
| 33 | private: |
| 34 | QCache<std::pair<QString, QRegularExpression::PatternOptions>, QRegularExpression> m_cache; |
| 35 | }; |
| 36 | |
| 37 | } |
| 38 | |
| 39 | #endif |
| 40 | |