File indexing completed on 2024-12-15 04:01:21

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 #include <QRegularExpression>
0009 
0010 
0011 namespace glaxnimate::utils::regexp {
0012 
0013 namespace detail {
0014     class EndIterator {};
0015     class NotDumbIterator
0016     {
0017     public:
0018         NotDumbIterator(QRegularExpressionMatchIterator dumb)
0019         : dumb(std::move(dumb))
0020         {
0021             ++*this;
0022         }
0023 
0024         NotDumbIterator& operator++()
0025         {
0026             end = !this->dumb.hasNext();
0027             if ( !end )
0028                 match = this->dumb.next();
0029             return *this;
0030         }
0031 
0032         QRegularExpressionMatch& operator*() { return match; }
0033 
0034         bool operator!=(const NotDumbIterator& oth) const
0035         {
0036             return end != oth.end;
0037         };
0038 
0039         bool operator!=(const EndIterator&) const
0040         {
0041             return !end;
0042         }
0043 
0044     private:
0045         QRegularExpressionMatchIterator dumb;
0046         QRegularExpressionMatch match;
0047         bool end = false;
0048     };
0049 } // namespace detail
0050 
0051 struct MatchRange
0052 {
0053     const detail::NotDumbIterator& begin() const { return begin_iter; }
0054     const detail::EndIterator& end() const { return end_iter; }
0055 
0056     detail::NotDumbIterator begin_iter;
0057     detail::EndIterator end_iter = {};
0058 };
0059 
0060 inline MatchRange find_all(const QRegularExpression& pattern, const QString& subject)
0061 {
0062     return MatchRange{ detail::NotDumbIterator(pattern.globalMatch(subject)) };
0063 }
0064 
0065 } // namespace glaxnimate::utils::regexp