LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <QtDebug>
32 
33 #ifdef USE_PCRE
34 #include <pcre.h>
35 #else
36 #include <QRegExp>
37 #endif
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
43 #ifdef USE_PCRE
44 
45 #ifndef PCRE_STUDY_JIT_COMPILE
46 #define PCRE_STUDY_JIT_COMPILE 0
47 #endif
48 
49  class PCREWrapper
50  {
51  pcre *RE_;
52  pcre_extra *Extra_;
53 
54  QString Pattern_;
55  Qt::CaseSensitivity CS_;
56  public:
57  PCREWrapper ()
58  : RE_ (0)
59  , Extra_ (0)
60  {
61  }
62 
63  PCREWrapper (const QString& str, Qt::CaseSensitivity cs)
64  : RE_ (Compile (str, cs))
65  , Extra_ (0)
66  , Pattern_ (str)
67  , CS_ (cs)
68  {
69  if (RE_)
70  {
71  pcre_refcount (RE_, 1);
72  const char *error = 0;
73  const int opts = PCRE_STUDY_JIT_COMPILE;
74  Extra_ = pcre_study (RE_, opts, &error);
75  }
76  }
77 
78  PCREWrapper (const PCREWrapper& other)
79  : RE_ (0)
80  , Extra_ (0)
81  {
82  *this = other;
83  }
84 
85  PCREWrapper& operator= (const PCREWrapper& other)
86  {
87  if (RE_ && !pcre_refcount (RE_, -1))
88  {
89  FreeStudy ();
90  pcre_free (RE_);
91  }
92 
93  RE_ = other.RE_;
94  Extra_ = other.Extra_;
95  if (RE_)
96  pcre_refcount (RE_, 1);
97 
98  return *this;
99  }
100 
101  ~PCREWrapper ()
102  {
103  if (!RE_)
104  return;
105 
106  if (!pcre_refcount (RE_, -1))
107  {
108  FreeStudy ();
109  pcre_free (RE_);
110  }
111  }
112 
113  const QString& GetPattern () const
114  {
115  return Pattern_;
116  }
117 
118  Qt::CaseSensitivity GetCS () const
119  {
120  return CS_;
121  }
122 
123  int Exec (const QByteArray& utf8) const
124  {
125  return RE_ ? pcre_exec (RE_, Extra_, utf8.constData (), utf8.size (), 0, 0, NULL, 0) : -1;
126  }
127  private:
128  pcre* Compile (const QString& str, Qt::CaseSensitivity cs)
129  {
130  const char *error = 0;
131  int errOffset = 0;
132  int options = PCRE_UTF8;
133  if (cs == Qt::CaseInsensitive)
134  options |= PCRE_CASELESS;
135  auto re = pcre_compile (str.toUtf8 ().constData (), options, &error, &errOffset, NULL);
136  if (!re)
137  qWarning () << Q_FUNC_INFO
138  << "failed compiling"
139  << str
140  << error;
141  return re;
142  }
143 
144  void FreeStudy ()
145  {
146  if (Extra_)
147 #ifdef PCRE_CONFIG_JIT
148  pcre_free_study (Extra_);
149 #else
150  pcre_free (Extra_);
151 #endif
152  }
153  };
154 #endif
155 
156  namespace
157  {
158  struct RegExpRegisterGuard
159  {
160  RegExpRegisterGuard ()
161  {
162  qRegisterMetaType<RegExp> ("Util::RegExp");
163  qRegisterMetaTypeStreamOperators<RegExp> ();
164  }
165  } Guard;
166  }
167 
168  struct RegExpImpl
169  {
170 #if USE_PCRE
171  PCREWrapper PRx_;
172 #else
173  QRegExp Rx_;
174 #endif
175  };
176 
178  {
179 #ifdef USE_PCRE
180  return true;
181 #else
182  return false;
183 #endif
184  }
185 
186  RegExp::RegExp (const QString& str, Qt::CaseSensitivity cs)
187 #ifdef USE_PCRE
188  : Impl_ { new RegExpImpl { { str, cs } } }
189 #else
190  : Impl_ { new RegExpImpl { QRegExp { str, cs, QRegExp::RegExp } } }
191 #endif
192  {
193  }
194 
195  bool RegExp::Matches (const QString& str) const
196  {
197  if (!Impl_)
198  return {};
199 
200 #ifdef USE_PCRE
201  return Impl_->PRx_.Exec (str.toUtf8 ()) >= 0;
202 #else
203  return Impl_->Rx_.exactMatch (str);
204 #endif
205  }
206 
207  QString RegExp::GetPattern () const
208  {
209  if (!Impl_)
210  return {};
211 
212 #ifdef USE_PCRE
213  return Impl_->PRx_.GetPattern ();
214 #else
215  return Impl_->Rx_.pattern ();
216 #endif
217  }
218 
219  Qt::CaseSensitivity RegExp::GetCaseSensitivity () const
220  {
221  if (!Impl_)
222  return {};
223 
224 #ifdef USE_PCRE
225  return Impl_->PRx_.GetCS ();
226 #else
227  return Impl_->Rx_.caseSensitivity ();
228 #endif
229  }
230 }
231 }
232 
233 QDataStream& operator<< (QDataStream& out, const LeechCraft::Util::RegExp& rx)
234 {
235  out << static_cast<quint8> (1);
236  out << rx.GetPattern ()
237  << static_cast<quint8> (rx.GetCaseSensitivity ());
238  return out;
239 }
240 
241 QDataStream& operator>> (QDataStream& in, LeechCraft::Util::RegExp& rx)
242 {
243  quint8 version = 0;
244  in >> version;
245  if (version != 1)
246  {
247  qWarning () << Q_FUNC_INFO
248  << "unknown version"
249  << version;
250  return in;
251  }
252 
253  QString pattern;
254  quint8 cs;
255  in >> pattern
256  >> cs;
257 
258  rx = LeechCraft::Util::RegExp { pattern, static_cast<Qt::CaseSensitivity> (cs) };
259 
260  return in;
261 }
Qt::CaseSensitivity GetCaseSensitivity() const
Definition: regexp.cpp:219
QString GetPattern() const
Definition: regexp.cpp:207
QDataStream & operator>>(QDataStream &in, LeechCraft::Util::RegExp &rx)
Definition: regexp.cpp:241
QDataStream & operator<<(QDataStream &out, const LeechCraft::Util::RegExp &rx)
Definition: regexp.cpp:233
bool Matches(const QString &) const
Definition: regexp.cpp:195
static bool IsFast()
Definition: regexp.cpp:177