LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
regexp.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include "sllconfig.h"
12#include <memory>
13#include <QString>
14#include <QMetaType>
15#include <QRegularExpression>
16#include "visitor.h"
17
18namespace LC::Util
19{
20 // TODO mark this deprecated once Qt6 port is complete
22 {
23 QRegularExpression Rx_;
24 public:
25 static bool IsFast ();
26
27 RegExp () = default;
28 RegExp (const QString&, Qt::CaseSensitivity);
29
30 bool Matches (const QString&) const;
31 bool Matches (const QByteArray&) const;
32
33 QString GetPattern () const;
34 Qt::CaseSensitivity GetCaseSensitivity () const;
35 };
36
37 struct StopReplace {};
38
40 {
41 qsizetype Shift_;
42
43 explicit ReplaceAdvance (qsizetype shift)
44 : Shift_ { std::max<qsizetype> (shift, 1) }
45 {
46 }
47 };
48
49 using ReplacerResult = std::variant<StopReplace, ReplaceAdvance>;
50
51 template<typename R>
52 void ReplaceByRegexp (QString& body,
53 const QRegularExpression& rx,
54 R&& replacer)
55 requires requires { { replacer (body, QRegularExpressionMatch {}) } -> std::convertible_to<ReplacerResult>; }
56 {
57 int pos = 0;
58 bool keepGoing = true;
59 while (keepGoing)
60 {
61 const auto& match = rx.match (body, pos);
62 if (!match.hasMatch ())
63 return;
64
65 Util::Visit (ReplacerResult { replacer (body, match) },
66 [&] (StopReplace) { keepGoing = false; },
67 [&] (ReplaceAdvance adv) { pos = match.capturedStart (0) + adv.Shift_; });
68 }
69 }
70}
71
72UTIL_SLL_API QDataStream& operator<< (QDataStream&, const LC::Util::RegExp&);
73UTIL_SLL_API QDataStream& operator>> (QDataStream&, LC::Util::RegExp&);
74
QString GetPattern() const
Definition regexp.cpp:48
Qt::CaseSensitivity GetCaseSensitivity() const
Definition regexp.cpp:53
RegExp()=default
bool Matches(const QString &) const
Definition regexp.cpp:38
static bool IsFast()
Definition regexp.cpp:26
std::variant< StopReplace, ReplaceAdvance > ReplacerResult
Definition regexp.h:49
void ReplaceByRegexp(QString &body, const QRegularExpression &rx, R &&replacer)
Definition regexp.h:52
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition either.h:204
UTIL_SLL_API QDataStream & operator>>(QDataStream &, LC::Util::RegExp &)
Definition regexp.cpp:69
UTIL_SLL_API QDataStream & operator<<(QDataStream &, const LC::Util::RegExp &)
Definition regexp.cpp:61
#define UTIL_SLL_API
Definition sllconfig.h:16
ReplaceAdvance(qsizetype shift)
Definition regexp.h:43
Q_DECLARE_METATYPE(QVariantList *)