File indexing completed on 2024-05-12 04:02:18

0001 /*
0002     SPDX-FileCopyrightText: 2023 Jonathan Poelen <jonathan.poelen+kde@gmail.com>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H
0008 #define KSYNTAXHIGHLIGHTING_DYNAMICREGEXPCACHE_P_H
0009 
0010 #include <QCache>
0011 #include <QRegularExpression>
0012 #include <QString>
0013 
0014 #include <utility>
0015 
0016 namespace KSyntaxHighlighting
0017 {
0018 
0019 class DynamicRegexpCache
0020 {
0021 public:
0022     const QRegularExpression &compileRegexp(QString &&pattern, QRegularExpression::PatternOptions patternOptions)
0023     {
0024         const auto key = std::pair{std::move(pattern), patternOptions};
0025         if (const auto regexp = m_cache.object(key)) {
0026             return *regexp;
0027         }
0028         auto regexp = new QRegularExpression(key.first, patternOptions);
0029         m_cache.insert(key, regexp);
0030         return *regexp;
0031     }
0032 
0033 private:
0034     QCache<std::pair<QString, QRegularExpression::PatternOptions>, QRegularExpression> m_cache;
0035 };
0036 
0037 }
0038 
0039 #endif