LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
regexp.cpp
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "regexp.h"
31 #include <QDataStream>
32 #include <QtDebug>
33 
34 #ifdef USE_PCRE
35 #include <pcre.h>
36 #else
37 #include <QRegExp>
38 #endif
39 
40 namespace LC
41 {
42 namespace Util
43 {
44 #ifdef USE_PCRE
45 
46 #ifndef PCRE_STUDY_JIT_COMPILE
47 #define PCRE_STUDY_JIT_COMPILE 0
48 #endif
49 
50  class PCREWrapper
51  {
52  pcre *RE_ = nullptr;
53  pcre_extra *Extra_ = nullptr;
54 
55  QString Pattern_;
56  Qt::CaseSensitivity CS_ = Qt::CaseSensitive;
57  public:
58  PCREWrapper () = default;
59 
60  PCREWrapper (const QString& str, Qt::CaseSensitivity cs)
61  : RE_ (Compile (str, cs))
62  , Pattern_ (str)
63  , CS_ (cs)
64  {
65  if (RE_)
66  {
67  pcre_refcount (RE_, 1);
68  const char *error = 0;
69  const int opts = PCRE_STUDY_JIT_COMPILE;
70  Extra_ = pcre_study (RE_, opts, &error);
71  }
72  }
73 
74  PCREWrapper (const PCREWrapper& other)
75  {
76  *this = other;
77  }
78 
79  PCREWrapper& operator= (const PCREWrapper& other)
80  {
81  if (RE_ && !pcre_refcount (RE_, -1))
82  {
83  FreeStudy ();
84  pcre_free (RE_);
85  }
86 
87  RE_ = other.RE_;
88  Extra_ = other.Extra_;
89  if (RE_)
90  pcre_refcount (RE_, 1);
91 
92  return *this;
93  }
94 
95  ~PCREWrapper ()
96  {
97  if (!RE_)
98  return;
99 
100  if (!pcre_refcount (RE_, -1))
101  {
102  FreeStudy ();
103  pcre_free (RE_);
104  }
105  }
106 
107  const QString& GetPattern () const
108  {
109  return Pattern_;
110  }
111 
112  Qt::CaseSensitivity GetCS () const
113  {
114  return CS_;
115  }
116 
117  int Exec (const QByteArray& utf8) const
118  {
119  return RE_ ? pcre_exec (RE_, Extra_, utf8.constData (), utf8.size (), 0, 0, NULL, 0) : -1;
120  }
121  private:
122  pcre* Compile (const QString& str, Qt::CaseSensitivity cs)
123  {
124  const char *error = 0;
125  int errOffset = 0;
126  int options = PCRE_UTF8;
127  if (cs == Qt::CaseInsensitive)
128  options |= PCRE_CASELESS;
129  auto re = pcre_compile (str.toUtf8 ().constData (), options, &error, &errOffset, NULL);
130  if (!re)
131  qWarning () << Q_FUNC_INFO
132  << "failed compiling"
133  << str
134  << error;
135  return re;
136  }
137 
138  void FreeStudy ()
139  {
140  if (Extra_)
141 #ifdef PCRE_CONFIG_JIT
142  pcre_free_study (Extra_);
143 #else
144  pcre_free (Extra_);
145 #endif
146  }
147  };
148 #endif
149 
150  namespace
151  {
152  struct RegExpRegisterGuard
153  {
154  RegExpRegisterGuard ()
155  {
156  qRegisterMetaType<RegExp> ("Util::RegExp");
157  qRegisterMetaTypeStreamOperators<RegExp> ();
158  }
159  } Guard;
160  }
161 
162  struct RegExpImpl
163  {
164 #if USE_PCRE
165  PCREWrapper PRx_;
166 #else
167  QRegExp Rx_;
168 #endif
169  };
170 
172  {
173 #ifdef USE_PCRE
174  return true;
175 #else
176  return false;
177 #endif
178  }
179 
180  RegExp::RegExp (const QString& str, Qt::CaseSensitivity cs)
181 #ifdef USE_PCRE
182  : Impl_ { new RegExpImpl { { str, cs } } }
183 #else
184  : Impl_ { new RegExpImpl { QRegExp { str, cs, QRegExp::RegExp } } }
185 #endif
186  {
187  }
188 
189  bool RegExp::Matches (const QString& str) const
190  {
191  if (!Impl_)
192  return {};
193 
194 #ifdef USE_PCRE
195  return Impl_->PRx_.Exec (str.toUtf8 ()) >= 0;
196 #else
197  return Impl_->Rx_.exactMatch (str);
198 #endif
199  }
200 
201  bool RegExp::Matches (const QByteArray& ba) const
202  {
203  if (!Impl_)
204  return {};
205 
206 #ifdef USE_PCRE
207  return Impl_->PRx_.Exec (ba) >= 0;
208 #else
209  return Impl_->Rx_.exactMatch (ba);
210 #endif
211  }
212 
213  QString RegExp::GetPattern () const
214  {
215  if (!Impl_)
216  return {};
217 
218 #ifdef USE_PCRE
219  return Impl_->PRx_.GetPattern ();
220 #else
221  return Impl_->Rx_.pattern ();
222 #endif
223  }
224 
225  Qt::CaseSensitivity RegExp::GetCaseSensitivity () const
226  {
227  if (!Impl_)
228  return {};
229 
230 #ifdef USE_PCRE
231  return Impl_->PRx_.GetCS ();
232 #else
233  return Impl_->Rx_.caseSensitivity ();
234 #endif
235  }
236 }
237 }
238 
239 QDataStream& operator<< (QDataStream& out, const LC::Util::RegExp& rx)
240 {
241  out << static_cast<quint8> (1);
242  out << rx.GetPattern ()
243  << static_cast<quint8> (rx.GetCaseSensitivity ());
244  return out;
245 }
246 
247 QDataStream& operator>> (QDataStream& in, LC::Util::RegExp& rx)
248 {
249  quint8 version = 0;
250  in >> version;
251  if (version != 1)
252  {
253  qWarning () << Q_FUNC_INFO
254  << "unknown version"
255  << version;
256  return in;
257  }
258 
259  QString pattern;
260  quint8 cs;
261  in >> pattern
262  >> cs;
263 
264  rx = LC::Util::RegExp { pattern, static_cast<Qt::CaseSensitivity> (cs) };
265 
266  return in;
267 }
bool Matches(const QString &) const
Definition: regexp.cpp:189
QString GetPattern() const
Definition: regexp.cpp:213
QDataStream & operator>>(QDataStream &in, LC::Util::RegExp &rx)
Definition: regexp.cpp:247
RegExp()=default
static bool IsFast()
Definition: regexp.cpp:171
Qt::CaseSensitivity GetCaseSensitivity() const
Definition: regexp.cpp:225
QDataStream & operator<<(QDataStream &out, const LC::Util::RegExp &rx)
Definition: regexp.cpp:239
Definition: constants.h:35