LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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 
18 namespace 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 
72 UTIL_SLL_API QDataStream& operator<< (QDataStream&, const LC::Util::RegExp&);
73 UTIL_SLL_API QDataStream& operator>> (QDataStream&, LC::Util::RegExp&);
74 
bool keepGoing
Definition: regexp.h:58
#define UTIL_SLL_API
Definition: sllconfig.h:16
ReplaceAdvance(qsizetype shift)
Definition: regexp.h:43
std::variant< StopReplace, ReplaceAdvance > ReplacerResult
Definition: regexp.h:49
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:180
requires(Tup1Size==Tup2Size) const expr auto ZipWith(Tup1 &&tup1
UTIL_SLL_API QDataStream & operator>>(QDataStream &, LC::Util::RegExp &)
Definition: regexp.cpp:69
bool Matches(const QString &string, const LC::AN::StringMatcher &pattern)
Definition: anutil.cpp:202
Q_DECLARE_METATYPE(QVariantList *)
UTIL_SLL_API QDataStream & operator<<(QDataStream &, const LC::Util::RegExp &)
Definition: regexp.cpp:61
void ReplaceByRegexp(QString &body, const QRegularExpression &rx, R &&replacer) requires requires
Definition: regexp.h:52