• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • search
kateregexp.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2009 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
4  * Copyright (C) 2007 Sebastian Pipping <webmaster@hartwork.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "kateregexp.h"
23 
24 KateRegExp::KateRegExp(const QString &pattern, Qt::CaseSensitivity cs,
25  QRegExp::PatternSyntax syntax)
26  : m_regExp(pattern, cs, syntax)
27 {
28 }
29 
30 // these things can besides '.' and '\s' make apptern multi-line:
31 // \n, \x000A, \x????-\x????, \0012, \0???-\0???
32 // a multi-line pattern must not pass as single-line, the other
33 // way around will just result in slower searches and is therefore
34 // not as critical
35 int KateRegExp::repairPattern(bool & stillMultiLine)
36 {
37  const QString & text = pattern(); // read-only input for parsing
38 
39  // get input
40  const int inputLen = text.length();
41  int input = 0; // walker index
42 
43  // prepare output
44  QString output;
45  output.reserve(2 * inputLen + 1); // twice should be enough for the average case
46 
47  // parser state
48  stillMultiLine = false;
49  int replaceCount = 0;
50  bool insideClass = false;
51 
52  while (input < inputLen)
53  {
54  if (insideClass)
55  {
56  // wait for closing, unescaped ']'
57  switch (text[input].unicode())
58  {
59  case L'\\':
60  switch (text[input + 1].unicode())
61  {
62  case L'x':
63  if (input + 5 < inputLen)
64  {
65  // copy "\x????" unmodified
66  output.append(text.mid(input, 6));
67  input += 6;
68  } else {
69  // copy "\x" unmodified
70  output.append(text.mid(input, 2));
71  input += 2;
72  }
73  stillMultiLine = true;
74  break;
75 
76  case L'0':
77  if (input + 4 < inputLen)
78  {
79  // copy "\0???" unmodified
80  output.append(text.mid(input, 5));
81  input += 5;
82  } else {
83  // copy "\0" unmodified
84  output.append(text.mid(input, 2));
85  input += 2;
86  }
87  stillMultiLine = true;
88  break;
89 
90  case L's':
91  // replace "\s" with "[ \t]"
92  output.append(" \\t");
93  input += 2;
94  replaceCount++;
95  break;
96 
97  case L'n':
98  stillMultiLine = true;
99  // FALLTROUGH
100 
101  default:
102  // copy "\?" unmodified
103  output.append(text.mid(input, 2));
104  input += 2;
105  }
106  break;
107 
108  case L']':
109  // copy "]" unmodified
110  insideClass = false;
111  output.append(text[input]);
112  input++;
113  break;
114 
115  default:
116  // copy "?" unmodified
117  output.append(text[input]);
118  input++;
119 
120  }
121  }
122  else
123  {
124  // search for real dots and \S
125  switch (text[input].unicode())
126  {
127  case L'\\':
128  switch (text[input + 1].unicode())
129  {
130  case L'x':
131  if (input + 5 < inputLen)
132  {
133  // copy "\x????" unmodified
134  output.append(text.mid(input, 6));
135  input += 6;
136  } else {
137  // copy "\x" unmodified
138  output.append(text.mid(input, 2));
139  input += 2;
140  }
141  stillMultiLine = true;
142  break;
143 
144  case L'0':
145  if (input + 4 < inputLen)
146  {
147  // copy "\0???" unmodified
148  output.append(text.mid(input, 5));
149  input += 5;
150  } else {
151  // copy "\0" unmodified
152  output.append(text.mid(input, 2));
153  input += 2;
154  }
155  stillMultiLine = true;
156  break;
157 
158  case L's':
159  // replace "\s" with "[ \t]"
160  output.append("[ \\t]");
161  input += 2;
162  replaceCount++;
163  break;
164 
165  case L'n':
166  stillMultiLine = true;
167  // FALLTROUGH
168 
169  default:
170  // copy "\?" unmodified
171  output.append(text.mid(input, 2));
172  input += 2;
173  }
174  break;
175 
176  case L'.':
177  // replace " with "[^\n]"
178  output.append("[^\\n]");
179  input++;
180  replaceCount++;
181  break;
182 
183  case L'[':
184  // copy "]" unmodified
185  insideClass = true;
186  output.append(text[input]);
187  input++;
188  break;
189 
190  default:
191  // copy "?" unmodified
192  output.append(text[input]);
193  input++;
194 
195  }
196  }
197  }
198 
199  // Overwrite with repaired pattern
200  m_regExp.setPattern(output);
201  return replaceCount;
202 }
203 
204 
205 
206 bool KateRegExp::isMultiLine() const
207 {
208  const QString &text = pattern();
209 
210  // parser state
211  bool insideClass = false;
212 
213  for (int input = 0; input < text.length(); /*empty*/ )
214  {
215  if (insideClass)
216  {
217  // wait for closing, unescaped ']'
218  switch (text[input].unicode())
219  {
220  case L'\\':
221  switch (text[input + 1].unicode())
222  {
223  case L'x':
224  return true;
225 
226  case L'0':
227  return true;
228 
229  case L's':
230  // replace "\s" with "[ \t]"
231  input += 2;
232  break;
233 
234  case L'n':
235  return true;
236  // FALLTROUGH
237 
238  default:
239  // copy "\?" unmodified
240  input += 2;
241  }
242  break;
243 
244  case L']':
245  // copy "]" unmodified
246  insideClass = false;
247  input++;
248  break;
249 
250  default:
251  // copy "?" unmodified
252  input++;
253 
254  }
255  }
256  else
257  {
258  // search for real dots and \S
259  switch (text[input].unicode())
260  {
261  case L'\\':
262  switch (text[input + 1].unicode())
263  {
264  case L'x':
265  return true;
266 
267  case L'0':
268  return true;
269 
270  case L's':
271  // replace "\s" with "[ \t]"
272  input += 2;
273  break;
274 
275  case L'n':
276  return true;
277 
278  default:
279  // copy "\?" unmodified
280  input += 2;
281  }
282  break;
283 
284  case L'.':
285  // replace " with "[^\n]"
286  input++;
287  break;
288 
289  case L'[':
290  // copy "]" unmodified
291  insideClass = true;
292  input++;
293  break;
294 
295  default:
296  // copy "?" unmodified
297  input++;
298 
299  }
300  }
301  }
302 
303  return false;
304 }
305 
306 
307 
308 int KateRegExp::indexIn(const QString &str, int start, int end) const
309 {
310  return m_regExp.indexIn(str.left(end), start, QRegExp::CaretAtZero);
311 }
312 
313 
314 
315 int KateRegExp::lastIndexIn(const QString &str, int start, int end) const
316 {
317  const int index = m_regExp.lastIndexIn(str.mid(start, end-start), -1, QRegExp::CaretAtZero);
318 
319  if (index == -1)
320  return -1;
321 
322  const int index2 = m_regExp.indexIn(str.left(end), start+index, QRegExp::CaretAtZero);
323 
324  return index2;
325 }
326 
327 // kate: space-indent on; indent-width 2; replace-tabs on;
KateRegExp::lastIndexIn
int lastIndexIn(const QString &str, int offset, int end) const
This function is a replacement for QRegExp.lastIndexIn that returns the last match that would have be...
Definition: kateregexp.cpp:315
QString::append
QString & append(QChar ch)
KateRegExp::repairPattern
int repairPattern(bool &stillMultiLine)
Repairs a regular Expression pattern.
Definition: kateregexp.cpp:35
KateRegExp::KateRegExp
KateRegExp(const QString &pattern, Qt::CaseSensitivity cs=Qt::CaseSensitive, QRegExp::PatternSyntax syntax=QRegExp::RegExp2)
Definition: kateregexp.cpp:24
kateregexp.h
QRegExp::setPattern
void setPattern(const QString &pattern)
QRegExp::indexIn
int indexIn(const QString &str, int offset, CaretMode caretMode) const
QString
KateRegExp::isMultiLine
bool isMultiLine() const
States, whether the pattern matches multiple lines, even if it was repaired using repairPattern()...
Definition: kateregexp.cpp:206
KateRegExp::pattern
QString pattern() const
Definition: kateregexp.h:35
QRegExp::lastIndexIn
int lastIndexIn(const QString &str, int offset, CaretMode caretMode) const
QString::mid
QString mid(int position, int n) const
QString::length
int length() const
QString::reserve
void reserve(int size)
QString::left
QString left(int n) const
KateRegExp::indexIn
int indexIn(const QString &str, int offset, int end) const
Definition: kateregexp.cpp:308
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal