Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/regexp.cc @ 8140:cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
author | Thomas Weber <thomas.weber.mail@gmail.com> |
---|---|
date | Wed, 24 Sep 2008 11:40:33 -0400 |
parents | dcc31f473596 |
children | 25bc2d31e1bf |
rev | line source |
---|---|
5582 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2005, 2006, 2007 David Bateman |
7016 | 4 Copyright (C) 2002, 2003, 2004, 2005 Paul Kienzle |
5 | |
6 This file is part of Octave. | |
5582 | 7 |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5582 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5582 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5773 | 28 #include <algorithm> |
5765 | 29 #include <sstream> |
30 | |
5582 | 31 #include "defun-dld.h" |
32 #include "error.h" | |
33 #include "gripes.h" | |
34 #include "oct-obj.h" | |
35 #include "utils.h" | |
36 | |
37 #include "Cell.h" | |
38 #include "oct-map.h" | |
39 #include "str-vec.h" | |
5785 | 40 #include "quit.h" |
41 #include "parse.h" | |
5582 | 42 |
7173 | 43 #if defined (HAVE_PCRE) |
5582 | 44 #include <pcre.h> |
7117 | 45 #elif defined (HAVE_REGEX) |
46 #if defined (__MINGW32__) | |
5582 | 47 #define __restrict |
48 #endif | |
7237 | 49 #if defined (HAVE_SYS_TYPES_H) |
50 #include <sys/types.h> | |
51 #endif | |
5582 | 52 #include <regex.h> |
53 #endif | |
54 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
55 // Define the maximum number of retries for a pattern that |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
56 // possibly results in an infinite recursion. |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
57 #define PCRE_MATCHLIMIT_MAX 10 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
58 |
5785 | 59 // The regexp is constructed as a linked list to avoid resizing the |
60 // return values in arrays at each new match. | |
61 | |
62 // FIXME don't bother collecting and composing return values the user | |
63 // doesn't want. | |
64 | |
65 class regexp_elem | |
5582 | 66 { |
5785 | 67 public: |
5787 | 68 regexp_elem (const string_vector& _named_token, const Cell& _t, |
69 const std::string& _m, const Matrix& _te, double _s, | |
70 double _e) : | |
5785 | 71 named_token (_named_token), t (_t), m (_m), te (_te), s (_s), e (_e) { } |
72 | |
73 regexp_elem (const regexp_elem &a) : named_token (a.named_token), t (a.t), | |
74 m (a.m), te (a.te), s (a.s), e (a.e) | |
75 { } | |
76 | |
77 string_vector named_token; | |
78 Cell t; | |
79 std::string m; | |
80 Matrix te; | |
81 double s; | |
82 double e; | |
83 }; | |
84 | |
85 typedef std::list<regexp_elem>::const_iterator const_iterator; | |
86 | |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
87 #define MAXLOOKBEHIND 10 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
88 static bool lookbehind_warned = false; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
89 |
5785 | 90 static int |
91 octregexp_list (const octave_value_list &args, const std::string &nm, | |
92 bool case_insensitive, std::list<regexp_elem> &lst, | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
93 string_vector &named, int &nopts, bool &once) |
5785 | 94 { |
95 int sz = 0; | |
5582 | 96 #if defined (HAVE_REGEX) || defined (HAVE_PCRE) |
97 int nargin = args.length(); | |
5779 | 98 bool lineanchors = false; |
99 bool dotexceptnewline = false; | |
100 bool freespacing = false; | |
5582 | 101 |
5785 | 102 nopts = nargin - 2; |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
103 once = false; |
5785 | 104 |
5582 | 105 std::string buffer = args(0).string_value (); |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
106 size_t max_length = (buffer.length () > MAXLOOKBEHIND ? |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
107 MAXLOOKBEHIND: buffer.length ()); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
108 |
5582 | 109 if (error_state) |
110 { | |
111 gripe_wrong_type_arg (nm.c_str(), args(0)); | |
5785 | 112 return 0; |
5582 | 113 } |
114 | |
115 std::string pattern = args(1).string_value (); | |
116 if (error_state) | |
117 { | |
118 gripe_wrong_type_arg (nm.c_str(), args(1)); | |
5785 | 119 return 0; |
5582 | 120 } |
121 | |
122 for (int i = 2; i < nargin; i++) | |
123 { | |
124 std::string str = args(i).string_value(); | |
125 if (error_state) | |
126 { | |
127 error ("%s: optional arguments must be strings", nm.c_str()); | |
128 break; | |
129 } | |
130 std::transform (str.begin (), str.end (), str.begin (), tolower); | |
131 if (str.find("once", 0) == 0) | |
132 { | |
133 once = true; | |
134 nopts--; | |
135 } | |
5779 | 136 else if (str.find("matchcase", 0) == 0) |
137 { | |
138 case_insensitive = false; | |
139 nopts--; | |
140 } | |
141 else if (str.find("ignorecase", 0) == 0) | |
142 { | |
143 case_insensitive = true; | |
144 nopts--; | |
145 } | |
5785 | 146 else if (str.find("dotall", 0) == 0) |
5779 | 147 { |
5785 | 148 dotexceptnewline = false; |
149 nopts--; | |
150 } | |
151 else if (str.find("stringanchors", 0) == 0) | |
152 { | |
153 lineanchors = false; | |
5779 | 154 nopts--; |
155 } | |
156 else if (str.find("literalspacing", 0) == 0) | |
157 { | |
158 freespacing = false; | |
159 nopts--; | |
160 } | |
5785 | 161 #if HAVE_PCRE |
162 // Only accept these options with pcre | |
163 else if (str.find("dotexceptnewline", 0) == 0) | |
164 { | |
165 dotexceptnewline = true; | |
166 nopts--; | |
167 } | |
168 else if (str.find("lineanchors", 0) == 0) | |
169 { | |
170 lineanchors = true; | |
171 nopts--; | |
172 } | |
173 else if (str.find("freespacing", 0) == 0) | |
174 { | |
175 freespacing = true; | |
176 nopts--; | |
177 } | |
5582 | 178 else if (str.find("start", 0) && str.find("end", 0) && |
179 str.find("tokenextents", 0) && str.find("match", 0) && | |
180 str.find("tokens", 0) && str.find("names", 0)) | |
181 error ("%s: unrecognized option", nm.c_str()); | |
182 #else | |
5779 | 183 else if (str.find("names", 0) == 0 || |
184 str.find("dotexceptnewline", 0) == 0 || | |
185 str.find("lineanchors", 0) == 0 || | |
186 str.find("freespacing", 0) == 0) | |
5785 | 187 error ("%s: %s not implemented in this version", str.c_str(), nm.c_str()); |
5582 | 188 else if (str.find("start", 0) && str.find("end", 0) && |
189 str.find("tokenextents", 0) && str.find("match", 0) && | |
190 str.find("tokens", 0)) | |
191 error ("%s: unrecognized option", nm.c_str()); | |
192 #endif | |
193 } | |
194 | |
195 if (!error_state) | |
196 { | |
5785 | 197 Cell t; |
198 std::string m; | |
199 double s, e; | |
5582 | 200 |
201 // named tokens "(?<name>...)" are only treated with PCRE not regex. | |
202 #if HAVE_PCRE | |
203 | |
5619 | 204 size_t pos = 0; |
205 size_t new_pos; | |
206 int nnames = 0; | |
207 int inames = 0; | |
5765 | 208 std::ostringstream buf; |
5619 | 209 Array<int> named_idx; |
5582 | 210 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
211 while ((new_pos = pattern.find ("(?",pos)) != std::string::npos) |
5619 | 212 { |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
213 if (pattern.at (new_pos + 2) == '<' && |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
214 !(pattern.at (new_pos + 3) == '=' || |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
215 pattern.at (new_pos + 3) == '!')) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
216 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
217 // The syntax of named tokens in pcre is "(?P<name>...)" while |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
218 // we need a syntax "(?<name>...)", so fix that here. Also an |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
219 // expression like |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
220 // "(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)" |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
221 // should be perfectly legal, while pcre does not allow the same |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
222 // named token name on both sides of the alternative. Also fix |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
223 // that here by replacing name tokens by dummy names, and dealing |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
224 // with the dummy names later. |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
225 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
226 size_t tmp_pos = pattern.find_first_of ('>',new_pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
227 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
228 if (tmp_pos == std::string::npos) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
229 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
230 error ("syntax error in pattern"); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
231 break; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
232 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
233 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
234 std::string tmp_name = |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
235 pattern.substr(new_pos+3,tmp_pos-new_pos-3); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
236 bool found = false; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
237 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
238 for (int i = 0; i < nnames; i++) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
239 if (named(i) == tmp_name) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
240 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
241 named_idx.resize(inames+1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
242 named_idx(inames) = i; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
243 found = true; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
244 break; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
245 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
246 if (! found) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
247 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
248 named_idx.resize(inames+1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
249 named_idx(inames) = nnames; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
250 named.append(tmp_name); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
251 nnames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
252 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
253 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
254 if (new_pos - pos > 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
255 buf << pattern.substr(pos,new_pos-pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
256 if (inames < 10) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
257 buf << "(?P<n00" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
258 else if (inames < 100) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
259 buf << "(?P<n0" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
260 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
261 buf << "(?P<n" << inames++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
262 pos = tmp_pos; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
263 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
264 else if (pattern.at (new_pos + 2) == '<') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
265 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
266 // Find lookbehind operators of arbitrary length (ie like |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
267 // "(?<=[a-z]*)") and replace with a maximum length operator |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
268 // as PCRE can not yet handle arbitrary length lookahead |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
269 // operators. Use the string length as the maximum length to |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
270 // avoid issues. |
5582 | 271 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
272 int brackets = 1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
273 size_t tmp_pos1 = new_pos + 2; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
274 size_t tmp_pos2 = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
275 while (tmp_pos1 <= pattern.length () && brackets > 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
276 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
277 char ch = pattern.at (tmp_pos1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
278 if (ch == '(') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
279 brackets++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
280 else if (ch == ')') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
281 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
282 if (brackets > 1) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
283 tmp_pos2 = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
284 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
285 brackets--; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
286 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
287 tmp_pos1++; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
288 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
289 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
290 if (brackets != 0) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
291 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
292 buf << pattern.substr (pos, new_pos - pos) << "(?"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
293 pos = new_pos + 2; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
294 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
295 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
296 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
297 size_t tmp_pos3 = pattern.find_first_of ("*+", tmp_pos2); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
298 if (tmp_pos3 != std::string::npos && tmp_pos3 < tmp_pos1) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
299 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
300 if (!lookbehind_warned) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
301 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
302 lookbehind_warned = true; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
303 warning ("%s: arbitrary length lookbehind patterns are only support up to length %d", nm.c_str(), MAXLOOKBEHIND); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
304 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
305 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
306 buf << pattern.substr (pos, new_pos - pos) << "("; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
307 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
308 size_t i; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
309 if (pattern.at (tmp_pos3) == '*') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
310 i = 0; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
311 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
312 i = 1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
313 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
314 for (; i < max_length + 1; i++) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
315 { |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
316 buf <<pattern.substr(new_pos, tmp_pos3 - new_pos) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
317 << "{" << i << "}"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
318 buf << pattern.substr(tmp_pos3 + 1, |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
319 tmp_pos1 - tmp_pos3 - 1); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
320 if (i != max_length) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
321 buf << "|"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
322 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
323 buf << ")"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
324 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
325 else |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
326 buf << pattern.substr (pos, tmp_pos1 - pos); |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
327 pos = tmp_pos1; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
328 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
329 } |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
330 else |
5619 | 331 { |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
332 buf << pattern.substr (pos, new_pos - pos) << "(?"; |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
333 pos = new_pos + 2; |
5619 | 334 } |
335 | |
336 } | |
337 | |
5765 | 338 buf << pattern.substr(pos); |
5619 | 339 |
340 if (error_state) | |
5785 | 341 return 0; |
5582 | 342 |
343 // Compile expression | |
344 pcre *re; | |
345 const char *err; | |
346 int erroffset; | |
5765 | 347 std::string buf_str = buf.str (); |
348 re = pcre_compile (buf_str.c_str (), | |
5779 | 349 (case_insensitive ? PCRE_CASELESS : 0) | |
350 (dotexceptnewline ? 0 : PCRE_DOTALL) | | |
351 (lineanchors ? PCRE_MULTILINE : 0) | | |
352 (freespacing ? PCRE_EXTENDED : 0), | |
7520 | 353 &err, &erroffset, 0); |
5582 | 354 |
7520 | 355 if (re == 0) { |
5582 | 356 error("%s: %s at position %d of expression", nm.c_str(), |
357 err, erroffset); | |
5785 | 358 return 0; |
5582 | 359 } |
360 | |
361 int subpatterns; | |
362 int namecount; | |
363 int nameentrysize; | |
364 char *nametable; | |
365 int idx = 0; | |
366 | |
7520 | 367 pcre_fullinfo(re, 0, PCRE_INFO_CAPTURECOUNT, &subpatterns); |
368 pcre_fullinfo(re, 0, PCRE_INFO_NAMECOUNT, &namecount); | |
369 pcre_fullinfo(re, 0, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); | |
370 pcre_fullinfo(re, 0, PCRE_INFO_NAMETABLE, &nametable); | |
5582 | 371 |
372 OCTAVE_LOCAL_BUFFER(int, ovector, (subpatterns+1)*3); | |
373 OCTAVE_LOCAL_BUFFER(int, nidx, namecount); | |
374 | |
375 for (int i = 0; i < namecount; i++) | |
376 { | |
377 // Index of subpattern in first two bytes MSB first of name. | |
5619 | 378 // Extract index. |
5779 | 379 nidx[i] = (static_cast<int>(nametable[i*nameentrysize])) << 8 | |
380 static_cast<int>(nametable[i*nameentrysize+1]); | |
5582 | 381 } |
382 | |
383 while(true) | |
384 { | |
5785 | 385 OCTAVE_QUIT; |
386 | |
7520 | 387 int matches = pcre_exec(re, 0, buffer.c_str(), |
5582 | 388 buffer.length(), idx, |
389 (idx ? PCRE_NOTBOL : 0), | |
390 ovector, (subpatterns+1)*3); | |
391 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
392 if (matches == PCRE_ERROR_MATCHLIMIT) |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
393 { |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
394 // try harder; start with default value for MATCH_LIMIT and increase it |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
395 warning("Your pattern caused PCRE to hit its MATCH_LIMIT.\nTrying harder now, but this will be slow."); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
396 pcre_extra pe; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
397 pcre_config(PCRE_CONFIG_MATCH_LIMIT, static_cast <void *> (&pe.match_limit)); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
398 pe.flags = PCRE_EXTRA_MATCH_LIMIT; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
399 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
400 int i = 0; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
401 while (matches == PCRE_ERROR_MATCHLIMIT && |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
402 i++ < PCRE_MATCHLIMIT_MAX) |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
403 { |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
404 OCTAVE_QUIT; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
405 |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
406 pe.match_limit *= 10; |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
407 matches = pcre_exec(re, &pe, buffer.c_str(), |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
408 buffer.length(), idx, |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
409 (idx ? PCRE_NOTBOL : 0), |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
410 ovector, (subpatterns+1)*3); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
411 } |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
412 } |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
413 |
5582 | 414 if (matches < 0 && matches != PCRE_ERROR_NOMATCH) |
415 { | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
416 error ("%s: internal error calling pcre_exec\nError code from pcre_exec is %i", nm.c_str(), matches); |
5582 | 417 pcre_free(re); |
5785 | 418 return 0; |
5582 | 419 } |
420 else if (matches == PCRE_ERROR_NOMATCH) | |
421 break; | |
5779 | 422 else if (ovector[1] <= ovector[0]) |
423 break; | |
5582 | 424 else |
425 { | |
5619 | 426 int pos_match = 0; |
5785 | 427 Matrix te(matches-1,2); |
5582 | 428 for (int i = 1; i < matches; i++) |
429 { | |
5619 | 430 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
431 { | |
5785 | 432 te(pos_match,0) = double (ovector[2*i]+1); |
433 te(pos_match++,1) = double (ovector[2*i+1]); | |
5619 | 434 } |
5582 | 435 } |
5785 | 436 te.resize(pos_match,2); |
437 s = double (ovector[0]+1); | |
438 e = double (ovector[1]); | |
5582 | 439 |
440 const char **listptr; | |
441 int status = pcre_get_substring_list(buffer.c_str(), ovector, | |
442 matches, &listptr); | |
443 | |
444 if (status == PCRE_ERROR_NOMEMORY) { | |
445 error("%s: cannot allocate memory in pcre_get_substring_list", | |
446 nm.c_str()); | |
447 pcre_free(re); | |
5785 | 448 return 0; |
5582 | 449 } |
450 | |
5619 | 451 Cell cell_t (dim_vector(1,pos_match)); |
452 pos_match = 0; | |
5582 | 453 for (int i = 1; i < matches; i++) |
5619 | 454 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
455 cell_t(pos_match++) = std::string(*(listptr+i)); | |
5582 | 456 |
5785 | 457 m = std::string(*listptr); |
458 t = cell_t; | |
459 | |
460 string_vector named_tokens(nnames); | |
5619 | 461 if (namecount > 0) |
462 for (int i = 1; i < matches; i++) | |
463 { | |
464 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) | |
465 { | |
5785 | 466 named_tokens(named_idx(i-1)) = |
467 std::string(*(listptr+nidx[i-1])); | |
5619 | 468 } |
469 } | |
5582 | 470 |
471 pcre_free_substring_list(listptr); | |
472 | |
5785 | 473 regexp_elem new_elem (named_tokens, t, m, te, s, e); |
474 lst.push_back (new_elem); | |
475 idx = ovector[1]; | |
476 sz++; | |
477 | |
5582 | 478 if (once) |
479 break; | |
480 | |
481 } | |
482 } | |
483 | |
484 pcre_free(re); | |
485 #else | |
486 regex_t compiled; | |
487 int err=regcomp(&compiled, pattern.c_str(), REG_EXTENDED | | |
488 (case_insensitive ? REG_ICASE : 0)); | |
489 if (err) | |
490 { | |
7520 | 491 int len = regerror(err, &compiled, 0, 0); |
5760 | 492 OCTAVE_LOCAL_BUFFER (char, errmsg, len); |
493 regerror(err, &compiled, errmsg, len); | |
494 error("%s: %s in pattern (%s)", nm.c_str(), errmsg, | |
495 pattern.c_str()); | |
5582 | 496 regfree(&compiled); |
5785 | 497 return 0; |
5582 | 498 } |
499 | |
500 int subexpr = 1; | |
501 int idx = 0; | |
502 for (unsigned int i=0; i < pattern.length(); i++) | |
503 subexpr += ( pattern[i] == '(' ? 1 : 0 ); | |
504 OCTAVE_LOCAL_BUFFER (regmatch_t, match, subexpr ); | |
505 | |
506 while(true) | |
507 { | |
5785 | 508 OCTAVE_QUIT; |
509 | |
5582 | 510 if (regexec(&compiled, buffer.c_str() + idx, subexpr, |
511 match, (idx ? REG_NOTBOL : 0)) == 0) | |
512 { | |
513 // Count actual matches | |
514 int matches = 0; | |
515 while (matches < subexpr && match[matches].rm_so >= 0) | |
516 matches++; | |
517 | |
5785 | 518 s = double (match[0].rm_so+1+idx); |
519 e = double (match[0].rm_eo+idx); | |
520 Matrix te(matches-1,2); | |
5582 | 521 for (int i = 1; i < matches; i++) |
522 { | |
5785 | 523 te(i-1,0) = double (match[i].rm_so+1+idx); |
524 te(i-1,1) = double (match[i].rm_eo+idx); | |
5582 | 525 } |
526 | |
5785 | 527 m = buffer.substr (match[0].rm_so+idx, |
5582 | 528 match[0].rm_eo-match[0].rm_so); |
529 | |
530 Cell cell_t (dim_vector(1,matches-1)); | |
531 for (int i = 1; i < matches; i++) | |
532 cell_t(i-1) = buffer.substr (match[i].rm_so+idx, | |
533 match[i].rm_eo-match[i].rm_so); | |
5785 | 534 t = cell_t; |
5582 | 535 |
536 idx += match[0].rm_eo; | |
5785 | 537 |
5866 | 538 string_vector sv; |
539 regexp_elem new_elem (sv, t, m, te, s, e); | |
5785 | 540 lst.push_back (new_elem); |
5582 | 541 sz++; |
542 | |
543 if (once) | |
544 break; | |
545 } | |
546 else | |
547 break; | |
548 } | |
549 regfree(&compiled); | |
550 #endif | |
5785 | 551 } |
552 #else | |
553 error ("%s: not available in this version of Octave", nm.c_str()); | |
554 #endif | |
555 return sz; | |
556 } | |
5582 | 557 |
5785 | 558 static octave_value_list |
559 octregexp (const octave_value_list &args, int nargout, const std::string &nm, | |
560 bool case_insensitive) | |
561 { | |
562 octave_value_list retval; | |
563 int nargin = args.length(); | |
564 std::list<regexp_elem> lst; | |
565 string_vector named; | |
566 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
567 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
568 int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts, once); |
5785 | 569 |
570 if (! error_state) | |
571 { | |
572 // Converted the linked list in the correct form for the return values | |
573 | |
574 octave_idx_type i = 0; | |
575 #ifdef HAVE_PCRE | |
576 Octave_map nmap; | |
577 if (sz == 1) | |
578 { | |
579 for (int j = 0; j < named.length(); j++) | |
580 nmap.assign (named(j), lst.begin()->named_token(j)); | |
581 retval(5) = nmap; | |
582 } | |
583 else | |
584 { | |
585 for (int j = 0; j < named.length (); j++) | |
586 { | |
587 i = 0; | |
588 Cell tmp(dim_vector (1, sz)); | |
589 for (const_iterator p = lst.begin(); p != lst.end(); p++) | |
590 tmp(i++) = p->named_token(j); | |
591 nmap.assign (named(j), octave_value (tmp)); | |
592 } | |
593 retval(5) = nmap; | |
594 } | |
595 #else | |
596 retval(5) = Octave_map(); | |
597 #endif | |
598 | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
599 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
600 retval(4) = sz ? lst.front ().t : Cell(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
601 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
602 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
603 Cell t (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
604 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
605 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
606 t(i++) = p->t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
607 retval(4) = t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
608 } |
5785 | 609 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
610 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
611 retval(3) = sz ? lst.front ().m : std::string(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
612 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
613 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
614 Cell m (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
615 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
616 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
617 m(i++) = p->m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
618 retval(3) = m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
619 } |
5785 | 620 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
621 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
622 retval(2) = sz ? lst.front ().te : Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
623 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
624 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
625 Cell te (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
626 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
627 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
628 te(i++) = p->te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
629 retval(2) = te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
630 } |
5785 | 631 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
632 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
633 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
634 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
635 retval(1) = lst.front ().e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
636 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
637 retval(1) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
638 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
639 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
640 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
641 NDArray e (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
642 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
643 for (const_iterator p = lst.begin(); p != lst.end(); p++) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
644 e(i++) = p->e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
645 retval(1) = e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
646 } |
5785 | 647 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
648 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
649 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
650 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
651 retval(0) = lst.front ().s; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
652 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
653 retval(0) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
654 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
655 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
656 { |
5785 | 657 NDArray s (dim_vector(1, sz)); |
658 i = 0; | |
659 for (const_iterator p = lst.begin(); p != lst.end(); p++) | |
660 s(i++) = p->s; | |
5582 | 661 retval(0) = s; |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
662 } |
5582 | 663 |
664 // Alter the order of the output arguments | |
665 if (nopts > 0) | |
666 { | |
667 int n = 0; | |
668 octave_value_list new_retval; | |
669 new_retval.resize(nargout); | |
670 | |
671 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); | |
5785 | 672 for (int j = 0; j < 6; j++) |
673 arg_used[j] = false; | |
5582 | 674 |
5785 | 675 for (int j = 2; j < nargin; j++) |
5582 | 676 { |
677 int k = 0; | |
5785 | 678 std::string str = args(j).string_value(); |
5582 | 679 std::transform (str.begin (), str.end (), str.begin (), tolower); |
5779 | 680 if (str.find("once", 0) == 0 |
681 || str.find("stringanchors", 0) == 0 | |
682 || str.find("lineanchors", 0) == 0 | |
683 || str.find("matchcase", 0) == 0 | |
684 || str.find("ignorecase", 0) == 0 | |
685 || str.find("dotall", 0) == 0 | |
686 || str.find("dotexceptnewline", 0) == 0 | |
687 || str.find("literalspacing", 0) == 0 | |
688 || str.find("freespacing", 0) == 0 | |
689 ) | |
5582 | 690 continue; |
691 else if (str.find("start", 0) == 0) | |
692 k = 0; | |
693 else if (str.find("end", 0) == 0) | |
694 k = 1; | |
695 else if (str.find("tokenextents", 0) == 0) | |
696 k = 2; | |
697 else if (str.find("match", 0) == 0) | |
698 k = 3; | |
699 else if (str.find("tokens", 0) == 0) | |
700 k = 4; | |
701 else if (str.find("names", 0) == 0) | |
702 k = 5; | |
703 | |
704 new_retval(n++) = retval(k); | |
705 arg_used[k] = true; | |
706 | |
707 if (n == nargout) | |
708 break; | |
709 } | |
710 | |
711 // Fill in the rest of the arguments | |
712 if (n < nargout) | |
713 { | |
5785 | 714 for (int j = 0; j < 6; j++) |
5582 | 715 { |
5785 | 716 if (! arg_used[j]) |
717 new_retval(n++) = retval(j); | |
5582 | 718 } |
719 } | |
720 | |
721 retval = new_retval; | |
722 } | |
723 } | |
724 | |
725 return retval; | |
726 } | |
727 | |
6361 | 728 static octave_value_list |
729 octcellregexp (const octave_value_list &args, int nargout, const std::string &nm, | |
730 bool case_insensitive) | |
731 { | |
732 octave_value_list retval; | |
733 | |
734 if (args(0).is_cell()) | |
735 { | |
736 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
737 octave_value_list new_args = args; | |
738 Cell cellstr = args(0).cell_value(); | |
739 if (args(1).is_cell()) | |
740 { | |
741 Cell cellpat = args(1).cell_value(); | |
742 | |
743 if (cellpat.numel() == 1) | |
744 { | |
745 for (int j = 0; j < nargout; j++) | |
746 newretval[j].resize(cellstr.dims()); | |
747 | |
748 new_args(1) = cellpat(0); | |
749 | |
750 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
751 { | |
752 new_args(0) = cellstr(i); | |
753 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
754 case_insensitive); | |
755 | |
756 if (error_state) | |
757 break; | |
758 | |
759 for (int j = 0; j < nargout; j++) | |
760 newretval[j](i) = tmp(j); | |
761 } | |
762 } | |
763 else if (cellstr.numel() == 1) | |
764 { | |
765 for (int j = 0; j < nargout; j++) | |
766 newretval[j].resize(cellpat.dims()); | |
767 | |
768 new_args(0) = cellstr(0); | |
769 | |
770 for (octave_idx_type i = 0; i < cellpat.numel (); i++) | |
771 { | |
772 new_args(1) = cellpat(i); | |
773 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
774 case_insensitive); | |
775 | |
776 if (error_state) | |
777 break; | |
778 | |
779 for (int j = 0; j < nargout; j++) | |
780 newretval[j](i) = tmp(j); | |
781 } | |
782 } | |
783 else if (cellstr.numel() == cellpat.numel()) | |
784 { | |
785 | |
786 if (cellstr.dims() != cellpat.dims()) | |
787 error ("%s: Inconsistent cell array dimensions", nm.c_str()); | |
788 else | |
789 { | |
790 for (int j = 0; j < nargout; j++) | |
791 newretval[j].resize(cellstr.dims()); | |
792 | |
793 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
794 { | |
795 new_args(0) = cellstr(i); | |
796 new_args(1) = cellpat(i); | |
797 | |
798 octave_value_list tmp = octregexp (new_args, nargout, nm, | |
799 case_insensitive); | |
800 | |
801 if (error_state) | |
802 break; | |
803 | |
804 for (int j = 0; j < nargout; j++) | |
805 newretval[j](i) = tmp(j); | |
806 } | |
807 } | |
808 } | |
809 else | |
810 error ("regexp: cell array arguments must be scalar or equal size"); | |
811 } | |
812 else | |
813 { | |
814 for (int j = 0; j < nargout; j++) | |
815 newretval[j].resize(cellstr.dims()); | |
816 | |
817 for (octave_idx_type i = 0; i < cellstr.numel (); i++) | |
818 { | |
819 new_args(0) = cellstr(i); | |
820 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); | |
821 | |
822 if (error_state) | |
823 break; | |
824 | |
825 for (int j = 0; j < nargout; j++) | |
826 newretval[j](i) = tmp(j); | |
827 } | |
828 } | |
829 | |
830 if (!error_state) | |
831 for (int j = 0; j < nargout; j++) | |
832 retval(j) = octave_value (newretval[j]); | |
833 } | |
834 else if (args(1).is_cell()) | |
835 { | |
836 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
837 octave_value_list new_args = args; | |
838 Cell cellpat = args(1).cell_value(); | |
839 | |
840 for (int j = 0; j < nargout; j++) | |
841 newretval[j].resize(cellpat.dims()); | |
842 | |
843 for (octave_idx_type i = 0; i < cellpat.numel (); i++) | |
844 { | |
845 new_args(1) = cellpat(i); | |
846 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); | |
847 | |
848 if (error_state) | |
849 break; | |
850 | |
851 for (int j = 0; j < nargout; j++) | |
852 newretval[j](i) = tmp(j); | |
853 } | |
854 | |
855 if (!error_state) | |
856 for (int j = 0; j < nargout; j++) | |
857 retval(j) = octave_value (newretval[j]); | |
858 } | |
859 else | |
860 retval = octregexp (args, nargout, nm, case_insensitive); | |
861 | |
862 return retval; | |
863 | |
864 } | |
865 | |
5582 | 866 DEFUN_DLD (regexp, args, nargout, |
867 "-*- texinfo -*-\n\ | |
868 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ | |
869 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ | |
870 \n\ | |
871 Regular expression string matching. Matches @var{pat} in @var{str} and\n\ | |
872 returns the position and matching substrings or empty values if there are\n\ | |
873 none.\n\ | |
874 \n\ | |
875 The matched pattern @var{pat} can include any of the standard regex\n\ | |
876 operators, including:\n\ | |
877 \n\ | |
878 @table @code\n\ | |
879 @item .\n\ | |
880 Match any character\n\ | |
881 @item * + ? @{@}\n\ | |
882 Repetition operators, representing\n\ | |
883 @table @code\n\ | |
884 @item *\n\ | |
885 Match zero or more times\n\ | |
886 @item +\n\ | |
887 Match one or more times\n\ | |
888 @item ?\n\ | |
889 Match zero or one times\n\ | |
890 @item @{@}\n\ | |
891 Match range operator, which is of the form @code{@{@var{n}@}} to match exactly\n\ | |
892 @var{n} times, @code{@{@var{m},@}} to match @var{m} or more times,\n\ | |
893 @code{@{@var{m},@var{n}@}} to match between @var{m} and @var{n} times.\n\ | |
894 @end table\n\ | |
895 @item [@dots{}] [^@dots{}]\n\ | |
896 List operators, where for example @code{[ab]c} matches @code{ac} and @code{bc}\n\ | |
897 @item ()\n\ | |
898 Grouping operator\n\ | |
899 @item |\n\ | |
900 Alternation operator. Match one of a choice of regular expressions. The\n\ | |
7001 | 901 alternatives must be delimited by the grouping operator @code{()} above\n\ |
5582 | 902 @item ^ $\n\ |
903 Anchoring operator. @code{^} matches the start of the string @var{str} and\n\ | |
904 @code{$} the end\n\ | |
905 @end table\n\ | |
906 \n\ | |
907 In addition the following escaped characters have special meaning. It should\n\ | |
908 be noted that it is recommended to quote @var{pat} in single quotes rather\n\ | |
909 than double quotes, to avoid the escape sequences being interpreted by octave\n\ | |
910 before being passed to @code{regexp}.\n\ | |
911 \n\ | |
912 @table @code\n\ | |
913 @item \\b\n\ | |
914 Match a word boundary\n\ | |
915 @item \\B\n\ | |
916 Match within a word\n\ | |
917 @item \\w\n\ | |
918 Matches any word character\n\ | |
919 @item \\W\n\ | |
920 Matches any non word character\n\ | |
921 @item \\<\n\ | |
922 Matches the beginning of a word\n\ | |
923 @item \\>\n\ | |
924 Matches the end of a word\n\ | |
925 @item \\s\n\ | |
926 Matches any whitespace character\n\ | |
927 @item \\S\n\ | |
928 Matches any non whitespace character\n\ | |
929 @item \\d\n\ | |
930 Matches any digit\n\ | |
931 @item \\D\n\ | |
932 Matches any non-digit\n\ | |
933 @end table\n\ | |
934 \n\ | |
935 The outputs of @code{regexp} by default are in the order as given below\n\ | |
936 \n\ | |
937 @table @asis\n\ | |
938 @item @var{s}\n\ | |
939 The start indices of each of the matching substrings\n\ | |
940 \n\ | |
941 @item @var{e}\n\ | |
942 The end indices of each matching substring\n\ | |
943 \n\ | |
944 @item @var{te}\n\ | |
945 The extents of each of the matched token surrounded by @code{(@dots{})} in\n\ | |
946 @var{pat}.\n\ | |
947 \n\ | |
948 @item @var{m}\n\ | |
949 A cell array of the text of each match.\n\ | |
950 \n\ | |
951 @item @var{t}\n\ | |
952 A cell array of the text of each token matched.\n\ | |
953 \n\ | |
954 @item @var{nm}\n\ | |
955 A structure containing the text of each matched named token, with the name\n\ | |
956 being used as the fieldname. A named token is denoted as\n\ | |
957 @code{(?<name>@dots{})}\n\ | |
958 @end table\n\ | |
959 \n\ | |
960 Particular output arguments or the order of the output arguments can be\n\ | |
961 selected by additional @var{opts} arguments. These are strings and the\n\ | |
962 correspondence between the output arguments and the optional argument\n\ | |
963 are\n\ | |
964 \n\ | |
965 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ | |
966 @item @tab 'start' @tab @var{s} @tab\n\ | |
967 @item @tab 'end' @tab @var{e} @tab\n\ | |
968 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ | |
969 @item @tab 'match' @tab @var{m} @tab\n\ | |
970 @item @tab 'tokens' @tab @var{t} @tab\n\ | |
971 @item @tab 'names' @tab @var{nm} @tab\n\ | |
972 @end multitable\n\ | |
973 \n\ | |
974 A further optional argument is 'once', that limits the number of returned\n\ | |
5779 | 975 matches to the first match. Additional arguments are\n\ |
976 \n\ | |
977 @table @asis\n\ | |
978 @item matchcase\n\ | |
979 Make the matching case sensitive.\n\ | |
980 @item ignorecase\n\ | |
981 Make the matching case insensitive.\n\ | |
982 @item stringanchors\n\ | |
983 Match the anchor characters at the beginning and end of the string.\n\ | |
984 @item lineanchors\n\ | |
985 Match the anchor characters at the beginning and end of the line.\n\ | |
986 @item dotall\n\ | |
987 The character @code{.} matches the newline character.\n\ | |
988 @item dotexceptnewline\n\ | |
989 The character @code{.} matches all but the newline character.\n\ | |
990 @item freespacing\n\ | |
991 The pattern can include arbitrary whitespace and comments starting with\n\ | |
992 @code{#}.\n\ | |
993 @item literalspacing\n\ | |
994 The pattern is taken literally.\n\ | |
995 @end table\n\ | |
5582 | 996 @end deftypefn") |
997 { | |
6361 | 998 octave_value_list retval; |
999 int nargin = args.length(); | |
1000 | |
1001 if (nargin < 2) | |
1002 print_usage (); | |
1003 else if (args(0).is_cell() || args(1).is_cell()) | |
1004 retval = octcellregexp (args, nargout, "regexp", false); | |
1005 else | |
1006 retval = octregexp (args, nargout, "regexp", false); | |
1007 | |
1008 return retval; | |
5582 | 1009 } |
1010 | |
1011 /* | |
1012 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1013 ## PCRE_ERROR_MATCHLIMIT test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1014 %!test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1015 %! s=sprintf('\t4\n0000\t-0.00\t-0.0000\t4\t-0.00\t-0.0000\t4\n0000\t-0.00\t-0.0000\t0\t-0.00\t-'); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1016 %! ws = warning("query"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1017 %! unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1018 %! warning("off"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1019 %! regexp(s, '(\s*-*\d+[.]*\d*\s*)+\n'); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1020 %! unwind_protect_cleanup |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1021 %! warning(ws); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1022 %! end_unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1023 |
5582 | 1024 ## seg-fault test |
1025 %!assert(regexp("abcde","."),[1,2,3,4,5]) | |
1026 | |
1027 ## Check that anchoring of pattern works correctly | |
1028 %!assert(regexp('abcabc','^abc'),1); | |
1029 %!assert(regexp('abcabc','abc$'),4); | |
5785 | 1030 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); |
5582 | 1031 |
1032 %!test | |
1033 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); | |
5785 | 1034 %! assert (s,zeros(1,0)) |
1035 %! assert (e,zeros(1,0)) | |
1036 %! assert (te,cell(1,0)) | |
1037 %! assert (m, cell(1,0)) | |
1038 %! assert (t, cell(1,0)) | |
5582 | 1039 |
1040 %!test | |
1041 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); | |
5785 | 1042 %! assert (s,zeros(1,0)) |
1043 %! assert (e,zeros(1,0)) | |
1044 %! assert (te,cell(1,0)) | |
1045 %! assert (m, cell(1,0)) | |
1046 %! assert (t, cell(1,0)) | |
5582 | 1047 |
1048 %!test | |
1049 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); | |
1050 %! assert (s,2) | |
1051 %! assert (e,10) | |
1052 %! assert (te{1},[3,7]) | |
1053 %! assert (m{1}, 'firetruck') | |
1054 %! assert (t{1}{1}, 'iretr') | |
1055 | |
1056 %!test | |
1057 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); | |
1058 %! assert (s,[1,12]) | |
1059 %! assert (e,[5,17]) | |
1060 %! assert (size(te), [1,2]) | |
1061 %! assert (isempty(te{1})) | |
1062 %! assert (isempty(te{2})) | |
1063 %! assert (m{1},'short') | |
1064 %! assert (m{2},'string') | |
1065 %! assert (size(t), [1,2]) | |
1066 %! assert (isempty(t{1})) | |
1067 %! assert (isempty(t{2})) | |
1068 | |
1069 %!test | |
1070 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); | |
1071 %! assert (s,1) | |
1072 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1073 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1074 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1075 %! assert (isempty(t)) |
5582 | 1076 |
1077 %!test | |
1078 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1079 %! assert (s,1) | |
1080 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1081 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1082 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1083 %! assert (isempty(t)) |
5582 | 1084 |
7242 | 1085 %!testif HAVE_PCRE |
5582 | 1086 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1087 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
1088 %! assert (s,1) | |
1089 %! assert (e,10) | |
1090 %! assert (size(te), [1,1]) | |
1091 %! assert (te{1}, [1 5; 7, 10]) | |
1092 %! assert (m{1},'short test') | |
1093 %! assert (size(t),[1,1]) | |
1094 %! assert (t{1}{1},'short') | |
1095 %! assert (t{1}{2},'test') | |
1096 %! assert (size(nm), [1,1]) | |
1097 %! assert (!isempty(fieldnames(nm))) | |
1098 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1099 %! assert (nm.word1,'short') | |
1100 %! assert (nm.word2,'test') | |
5582 | 1101 |
7242 | 1102 %!testif HAVE_PCRE |
5582 | 1103 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1104 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
1105 %! assert (s,1) | |
1106 %! assert (e,10) | |
1107 %! assert (size(te), [1,1]) | |
1108 %! assert (te{1}, [1 5; 7, 10]) | |
1109 %! assert (m{1},'short test') | |
1110 %! assert (size(t),[1,1]) | |
1111 %! assert (t{1}{1},'short') | |
1112 %! assert (t{1}{2},'test') | |
1113 %! assert (size(nm), [1,1]) | |
1114 %! assert (!isempty(fieldnames(nm))) | |
1115 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1116 %! assert (nm.word1,'short') | |
1117 %! assert (nm.word2,'test') | |
5619 | 1118 |
7242 | 1119 %!testif HAVE_PCRE |
5619 | 1120 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1121 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); |
1122 %! assert (size(t), [1,2]); | |
1123 %! assert (t{1}{1},'John'); | |
1124 %! assert (t{1}{2},'Davis'); | |
1125 %! assert (t{2}{1},'Rogers'); | |
1126 %! assert (t{2}{2},'James'); | |
1127 %! assert (size(nm), [1,1]); | |
1128 %! assert (nm.first{1},'John'); | |
1129 %! assert (nm.first{2},'James'); | |
1130 %! assert (nm.last{1},'Davis'); | |
1131 %! assert (nm.last{2},'Rogers'); | |
5582 | 1132 |
5779 | 1133 %!assert(regexp("abc\nabc",'.'),[1:7]) |
1134 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1135 %!testif HAVE_PCRE |
1136 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) | |
1137 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1138 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1139 |
1140 %!assert(regexp("caseCaSe",'case'),1) | |
1141 %!assert(regexp("caseCaSe",'case',"matchcase"),1) | |
1142 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1143 %!testif HAVE_PCRE |
1144 %! assert(regexp("caseCaSe",'(?-i)case'),1) | |
1145 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1146 |
1147 %!assert (regexp("abc\nabc",'c$'),7) | |
1148 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) | |
7242 | 1149 %!testif HAVE_PCRE |
1150 %! assert (regexp("abc\nabc",'(?-m)c$'),7) | |
1151 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) | |
1152 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) | |
5779 | 1153 |
1154 %!assert (regexp("this word",'s w'),4) | |
1155 %!assert (regexp("this word",'s w','literalspacing'),4) | |
7242 | 1156 %!testif HAVE_PCRE |
1157 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) | |
1158 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) | |
1159 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) | |
5779 | 1160 |
5582 | 1161 %!error regexp('string', 'tri', 'BadArg'); |
1162 %!error regexp('string'); | |
1163 | |
6361 | 1164 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1165 %!assert(regexp({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1166 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1167 %!assert(regexp('Strings',{'t','s'}),{2,7}) | |
1168 | |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1169 ## Test case for lookaround operators |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1170 %!assert(regexp('Iraq','q(?!u)'),4) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1171 %!assert(regexp('quit','q(?!u)'), zeros(1,0)) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1172 %!assert(regexp('quit','q(?=u)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1173 %!assert(regexp("quit",'q(?=u+)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1174 %!assert(regexp("qit",'q(?=u+)','match'), cell(1,0)) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1175 %!assert(regexp("qit",'q(?=u*)','match'), {'q'}) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1176 |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1177 %!assert(regexp('thingamabob','(?<=a)b'), 9) |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1178 |
5582 | 1179 */ |
1180 | |
6549 | 1181 DEFUN_DLD (regexpi, args, nargout, |
5582 | 1182 "-*- texinfo -*-\n\ |
1183 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ | |
1184 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, @var{opts}, @dots{})\n\ | |
1185 \n\ | |
1186 Case insensitive regular expression string matching. Matches @var{pat} in\n\ | |
1187 @var{str} and returns the position and matching substrings or empty values\n\ | |
1188 if there are none. See @code{regexp} for more details\n\ | |
1189 @end deftypefn") | |
1190 { | |
6361 | 1191 octave_value_list retval; |
1192 int nargin = args.length(); | |
1193 | |
1194 if (nargin < 2) | |
1195 print_usage (); | |
1196 else if (args(0).is_cell() || args(1).is_cell()) | |
1197 retval = octcellregexp (args, nargout, "regexpi", true); | |
1198 else | |
1199 retval = octregexp (args, nargout, "regexpi", true); | |
1200 | |
1201 return retval; | |
5582 | 1202 } |
1203 | |
1204 /* | |
1205 | |
1206 ## seg-fault test | |
1207 %!assert(regexpi("abcde","."),[1,2,3,4,5]) | |
1208 | |
1209 ## Check that anchoring of pattern works correctly | |
1210 %!assert(regexpi('abcabc','^abc'),1); | |
1211 %!assert(regexpi('abcabc','abc$'),4); | |
5785 | 1212 %!assert(regexpi('abcabc','^abc$'),zeros(1,0)); |
5582 | 1213 |
1214 %!test | |
1215 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); | |
5785 | 1216 %! assert (s,zeros(1,0)) |
1217 %! assert (e,zeros(1,0)) | |
1218 %! assert (te,cell(1,0)) | |
1219 %! assert (m, cell(1,0)) | |
1220 %! assert (t, cell(1,0)) | |
5582 | 1221 |
1222 %!test | |
1223 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); | |
1224 %! assert (s,2) | |
1225 %! assert (e,10) | |
1226 %! assert (te{1},[3,7]) | |
1227 %! assert (m{1}, 'FiRetrUck') | |
1228 %! assert (t{1}{1}, 'iRetr') | |
1229 | |
1230 %!test | |
1231 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); | |
1232 %! assert (s,2) | |
1233 %! assert (e,10) | |
1234 %! assert (te{1},[3,7]) | |
1235 %! assert (m{1}, 'firetruck') | |
1236 %! assert (t{1}{1}, 'iretr') | |
1237 | |
1238 %!test | |
1239 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); | |
1240 %! assert (s,[1,12]) | |
1241 %! assert (e,[5,17]) | |
1242 %! assert (size(te), [1,2]) | |
1243 %! assert (isempty(te{1})) | |
1244 %! assert (isempty(te{2})) | |
1245 %! assert (m{1},'ShoRt') | |
1246 %! assert (m{2},'String') | |
1247 %! assert (size(t), [1,2]) | |
1248 %! assert (isempty(t{1})) | |
1249 %! assert (isempty(t{2})) | |
1250 | |
1251 %!test | |
1252 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); | |
1253 %! assert (s,1) | |
1254 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1255 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1256 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1257 %! assert (isempty(t)) |
5582 | 1258 |
1259 %!test | |
1260 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1261 %! assert (s,1) | |
1262 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1263 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1264 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1265 %! assert (isempty(t)) |
5582 | 1266 |
7242 | 1267 %!testif HAVE_PCRE |
5582 | 1268 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1269 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); |
1270 %! assert (s,1) | |
1271 %! assert (e,10) | |
1272 %! assert (size(te), [1,1]) | |
1273 %! assert (te{1}, [1 5; 7, 10]) | |
1274 %! assert (m{1},'ShoRt Test') | |
1275 %! assert (size(t),[1,1]) | |
1276 %! assert (t{1}{1},'ShoRt') | |
1277 %! assert (t{1}{2},'Test') | |
1278 %! assert (size(nm), [1,1]) | |
1279 %! assert (!isempty(fieldnames(nm))) | |
1280 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1281 %! assert (nm.word1,'ShoRt') | |
1282 %! assert (nm.word2,'Test') | |
5582 | 1283 |
7242 | 1284 %!testif HAVE_PCRE |
5582 | 1285 %! ## This test is expected to fail if PCRE is not installed |
7242 | 1286 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); |
1287 %! assert (s,1) | |
1288 %! assert (e,10) | |
1289 %! assert (size(te), [1,1]) | |
1290 %! assert (te{1}, [1 5; 7, 10]) | |
1291 %! assert (m{1},'ShoRt Test') | |
1292 %! assert (size(t),[1,1]) | |
1293 %! assert (t{1}{1},'ShoRt') | |
1294 %! assert (t{1}{2},'Test') | |
1295 %! assert (size(nm), [1,1]) | |
1296 %! assert (!isempty(fieldnames(nm))) | |
1297 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1298 %! assert (nm.word1,'ShoRt') | |
1299 %! assert (nm.word2,'Test') | |
5582 | 1300 |
5779 | 1301 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
1302 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1303 %!testif HAVE_PCRE |
1304 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) | |
1305 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1306 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1307 |
1308 %!assert(regexpi("caseCaSe",'case'),[1,5]) | |
1309 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) | |
1310 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1311 %!testif HAVE_PCRE |
1312 %! assert(regexpi("caseCaSe",'(?-i)case'),1) | |
1313 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1314 |
1315 %!assert (regexpi("abc\nabc",'c$'),7) | |
1316 %!assert (regexpi("abc\nabc",'c$',"stringanchors"),7) | |
7242 | 1317 %!testif HAVE_PCRE |
1318 %! assert (regexpi("abc\nabc",'(?-m)c$'),7) | |
1319 %! assert (regexpi("abc\nabc",'c$',"lineanchors"),[3,7]) | |
1320 %! assert (regexpi("abc\nabc",'(?m)c$'),[3,7]) | |
5779 | 1321 |
1322 %!assert (regexpi("this word",'s w'),4) | |
1323 %!assert (regexpi("this word",'s w','literalspacing'),4) | |
7242 | 1324 %!testif HAVE_PCRE |
1325 %! assert (regexpi("this word",'(?-x)s w','literalspacing'),4) | |
1326 %! assert (regexpi("this word",'s w','freespacing'),zeros(1,0)) | |
1327 %! assert (regexpi("this word",'(?x)s w'),zeros(1,0)) | |
5779 | 1328 |
5582 | 1329 %!error regexpi('string', 'tri', 'BadArg'); |
1330 %!error regexpi('string'); | |
1331 | |
6361 | 1332 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1333 %!assert(regexpi({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1334 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1335 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]}) | |
1336 | |
5582 | 1337 */ |
1338 | |
6361 | 1339 |
1340 static octave_value | |
1341 octregexprep (const octave_value_list &args, const std::string &nm) | |
5785 | 1342 { |
6361 | 1343 octave_value retval; |
5785 | 1344 int nargin = args.length(); |
1345 | |
1346 // Make sure we have string,pattern,replacement | |
1347 const std::string buffer = args(0).string_value (); | |
1348 if (error_state) return retval; | |
1349 const std::string pattern = args(1).string_value (); | |
1350 if (error_state) return retval; | |
1351 const std::string replacement = args(2).string_value (); | |
1352 if (error_state) return retval; | |
1353 | |
1354 // Pack options excluding 'tokenize' and various output | |
1355 // reordering strings into regexp arg list | |
1356 octave_value_list regexpargs(nargin-1,octave_value()); | |
1357 regexpargs(0) = args(0); | |
1358 regexpargs(1) = args(1); | |
1359 int len=2; | |
1360 for (int i = 3; i < nargin; i++) | |
1361 { | |
1362 const std::string opt = args(i).string_value(); | |
1363 if (opt != "tokenize" && opt != "start" && opt != "end" | |
1364 && opt != "tokenextents" && opt != "match" && opt != "tokens" | |
1365 && opt != "names" && opt != "warnings") | |
1366 { | |
1367 regexpargs(len++) = args(i); | |
1368 } | |
1369 } | |
1370 regexpargs.resize(len); | |
1371 | |
1372 // Identify replacement tokens; build a vector of group numbers in | |
1373 // the replacement string so that we can quickly calculate the size | |
1374 // of the replacement. | |
1375 int tokens = 0; | |
1376 for (size_t i=1; i < replacement.size(); i++) | |
1377 { | |
1378 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
1379 { | |
1380 tokens++, i++; | |
1381 } | |
1382 } | |
1383 std::vector<int> token(tokens); | |
1384 int kk = 0; | |
1385 for (size_t i = 1; i < replacement.size(); i++) | |
1386 { | |
1387 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
1388 { | |
1389 token[kk++] = replacement[i]-'0'; | |
1390 i++; | |
1391 } | |
1392 } | |
1393 | |
1394 // Perform replacement | |
1395 std::string rep; | |
1396 if (tokens > 0) | |
1397 { | |
1398 std::list<regexp_elem> lst; | |
1399 string_vector named; | |
1400 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1401 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1402 int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts, once); |
5785 | 1403 |
1404 if (error_state) | |
1405 return retval; | |
1406 if (sz == 0) | |
1407 { | |
6361 | 1408 retval = args(0); |
5785 | 1409 return retval; |
1410 } | |
1411 | |
1412 // Determine replacement length | |
1413 const size_t replen = replacement.size() - 2*tokens; | |
1414 int delta = 0; | |
1415 const_iterator p = lst.begin(); | |
1416 for (int i = 0; i < sz; i++) | |
1417 { | |
1418 OCTAVE_QUIT; | |
1419 | |
1420 const Matrix pairs(p->te); | |
1421 size_t pairlen = 0; | |
1422 for (int j = 0; j < tokens; j++) | |
1423 { | |
1424 if (token[j] == 0) | |
1425 pairlen += static_cast<size_t>(p->e - p->s) + 1; | |
1426 else if (token[j] <= pairs.rows()) | |
1427 pairlen += static_cast<size_t>(pairs(token[j]-1,1) - | |
1428 pairs(token[j]-1,0)) + 1; | |
1429 } | |
1430 delta += static_cast<int>(replen + pairlen) - | |
1431 static_cast<int>(p->e - p->s + 1); | |
1432 p++; | |
1433 } | |
1434 | |
1435 // Build replacement string | |
1436 rep.reserve(buffer.size()+delta); | |
1437 size_t from = 0; | |
1438 p = lst.begin(); | |
1439 for (int i=0; i < sz; i++) | |
1440 { | |
1441 OCTAVE_QUIT; | |
1442 | |
1443 const Matrix pairs(p->te); | |
1444 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); | |
1445 from = static_cast<size_t>(p->e - 1) + 1; | |
1446 for (size_t j = 1; j < replacement.size(); j++) | |
1447 { | |
1448 if (replacement[j-1]=='$' && isdigit(replacement[j])) | |
1449 { | |
1450 int k = replacement[j]-'0'; | |
1451 if (k == 0) | |
1452 { | |
1453 // replace with entire match | |
1454 rep.append(&buffer[static_cast<size_t>(p->e - 1)], | |
1455 static_cast<size_t>(p->e - p->s) + 1); | |
1456 } | |
1457 else if (k <= pairs.rows()) | |
1458 { | |
1459 // replace with group capture | |
1460 rep.append(&buffer[static_cast<size_t>(pairs(k-1,0)-1)], | |
1461 static_cast<size_t>(pairs(k-1,1) - | |
1462 pairs(k-1,0))+1); | |
1463 } | |
1464 else | |
1465 { | |
1466 // replace with nothing | |
1467 } | |
1468 j++; | |
1469 } | |
1470 else | |
1471 { | |
1472 rep.append(1,replacement[j-1]); | |
1473 } | |
1474 if (j+1 == replacement.size()) | |
1475 { | |
1476 rep.append(1,replacement[j]); | |
1477 } | |
1478 } | |
1479 p++; | |
1480 } | |
1481 rep.append(&buffer[from],buffer.size()-from); | |
1482 } | |
1483 else | |
1484 { | |
1485 std::list<regexp_elem> lst; | |
1486 string_vector named; | |
1487 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1488 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1489 int sz = octregexp_list (regexpargs, nm, false, lst, named, nopts, once); |
5785 | 1490 |
1491 if (error_state) | |
1492 return retval; | |
1493 if (sz == 0) | |
1494 { | |
6361 | 1495 retval = args(0); |
5785 | 1496 return retval; |
1497 } | |
1498 | |
1499 // Determine replacement length | |
1500 const size_t replen = replacement.size(); | |
1501 int delta = 0; | |
1502 const_iterator p = lst.begin(); | |
1503 for (int i = 0; i < sz; i++) | |
1504 { | |
1505 OCTAVE_QUIT; | |
1506 delta += static_cast<int>(replen) - | |
1507 static_cast<int>(p->e - p->s + 1); | |
1508 p++; | |
1509 } | |
1510 | |
1511 // Build replacement string | |
1512 rep.reserve(buffer.size()+delta); | |
1513 size_t from = 0; | |
1514 p = lst.begin(); | |
1515 for (int i=0; i < sz; i++) | |
1516 { | |
1517 OCTAVE_QUIT; | |
1518 rep.append(&buffer[from], static_cast<size_t>(p->s - 1) - from); | |
1519 from = static_cast<size_t>(p->e - 1) + 1; | |
1520 rep.append(replacement); | |
1521 p++; | |
1522 } | |
1523 rep.append(&buffer[from],buffer.size()-from); | |
1524 } | |
1525 | |
6361 | 1526 retval = rep; |
1527 return retval; | |
1528 } | |
1529 | |
6549 | 1530 DEFUN_DLD (regexprep, args, , |
6361 | 1531 "-*- texinfo -*-\n\ |
6678 | 1532 @deftypefn {Loadable Function} {@var{string} =} regexprep (@var{string}, @var{pat}, @var{repstr}, @var{options})\n\ |
6361 | 1533 Replace matches of @var{pat} in @var{string} with @var{repstr}.\n\ |
1534 \n\ | |
1535 \n\ | |
7007 | 1536 The replacement can contain @code{$i}, which substitutes\n\ |
6361 | 1537 for the ith set of parentheses in the match string. E.g.,\n\ |
1538 @example\n\ | |
1539 \n\ | |
1540 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\ | |
1541 \n\ | |
1542 @end example\n\ | |
1543 returns \"Dunn, Bill\"\n\ | |
1544 \n\ | |
1545 @var{options} may be zero or more of\n\ | |
1546 @table @samp\n\ | |
1547 \n\ | |
1548 @item once\n\ | |
7001 | 1549 Replace only the first occurrence of @var{pat} in the result.\n\ |
6361 | 1550 \n\ |
1551 @item warnings\n\ | |
1552 This option is present for compatibility but is ignored.\n\ | |
1553 \n\ | |
1554 @item ignorecase or matchcase\n\ | |
1555 Ignore case for the pattern matching (see @code{regexpi}).\n\ | |
1556 Alternatively, use (?i) or (?-i) in the pattern.\n\ | |
1557 \n\ | |
1558 @item lineanchors and stringanchors\n\ | |
1559 Whether characters ^ and $ match the beginning and ending of lines.\n\ | |
1560 Alternatively, use (?m) or (?-m) in the pattern.\n\ | |
1561 \n\ | |
1562 @item dotexceptnewline and dotall\n\ | |
1563 Whether . matches newlines in the string.\n\ | |
1564 Alternatively, use (?s) or (?-s) in the pattern.\n\ | |
1565 \n\ | |
1566 @item freespacing or literalspacing\n\ | |
1567 Whether whitespace and # comments can be used to make the regular expression more readable.\n\ | |
1568 Alternatively, use (?x) or (?-x) in the pattern.\n\ | |
1569 \n\ | |
1570 @end table\n\ | |
1571 @seealso{regexp,regexpi}\n\ | |
1572 @end deftypefn") | |
1573 { | |
1574 octave_value_list retval; | |
1575 int nargin = args.length(); | |
1576 | |
1577 if (nargin < 3) | |
1578 { | |
1579 print_usage (); | |
1580 return retval; | |
1581 } | |
1582 | |
1583 if (args(0).is_cell() || args(1).is_cell() || args(2).is_cell()) | |
1584 { | |
1585 Cell str; | |
1586 Cell pat; | |
1587 Cell rep; | |
6495 | 1588 dim_vector dv0; |
1589 dim_vector dv1(1,1); | |
6361 | 1590 |
1591 if (args(0).is_cell()) | |
1592 str = args(0).cell_value(); | |
1593 else | |
1594 str = Cell (args(0)); | |
1595 | |
1596 if (args(1).is_cell()) | |
1597 pat = args(1).cell_value(); | |
1598 else | |
1599 pat = Cell (args(1)); | |
1600 | |
1601 if (args(2).is_cell()) | |
1602 rep = args(2).cell_value(); | |
1603 else | |
1604 rep = Cell (args(2)); | |
1605 | |
6495 | 1606 dv0 = str.dims(); |
1607 if (pat.numel() != 1) | |
6361 | 1608 { |
6495 | 1609 dv1 = pat.dims(); |
1610 if (rep.numel() != 1 && dv1 != rep.dims()) | |
6361 | 1611 error ("regexprep: Inconsistent cell array dimensions"); |
1612 } | |
1613 else if (rep.numel() != 1) | |
6495 | 1614 dv1 = rep.dims(); |
6361 | 1615 |
1616 if (!error_state) | |
1617 { | |
6495 | 1618 Cell ret (dv0); |
6361 | 1619 octave_value_list new_args = args; |
1620 | |
6495 | 1621 for (octave_idx_type i = 0; i < dv0.numel(); i++) |
1622 { | |
1623 new_args(0) = str(i); | |
1624 if (pat.numel() == 1) | |
1625 new_args(1) = pat(0); | |
1626 if (rep.numel() == 1) | |
1627 new_args(2) = rep(0); | |
1628 for (octave_idx_type j = 0; j < dv1.numel(); j++) | |
1629 { | |
1630 if (pat.numel() != 1) | |
1631 new_args(1) = pat(j); | |
1632 if (rep.numel() != 1) | |
1633 new_args(2) = rep(j); | |
1634 new_args(0) = octregexprep (new_args, "regexprep"); | |
6361 | 1635 |
6495 | 1636 if (error_state) |
1637 break; | |
1638 } | |
6361 | 1639 |
1640 if (error_state) | |
1641 break; | |
6495 | 1642 |
1643 ret(i) = new_args(0); | |
6361 | 1644 } |
1645 | |
1646 if (!error_state) | |
1647 retval = octave_value (ret); | |
1648 } | |
1649 } | |
1650 else | |
1651 retval = octregexprep (args, "regexprep"); | |
1652 | |
5785 | 1653 return retval; |
1654 } | |
1655 | |
1656 /* | |
1657 %!test # Replace with empty | |
1658 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1659 %! t = regexprep(xml,'<[!?][^>]*>',''); | |
1660 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1661 | |
1662 %!test # Replace with non-empty | |
1663 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1664 %! t = regexprep(xml,'<[!?][^>]*>','?'); | |
1665 %! assert(t,'? <tag v="hello">some stuff?</tag>') | |
1666 | |
1667 %!test # Check that 'tokenize' is ignored | |
1668 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1669 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); | |
1670 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1671 | |
7242 | 1672 %!testif HAVE_PCRE # Capture replacement |
1673 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; | |
1674 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; | |
1675 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); | |
1676 %! assert(t,result) | |
5785 | 1677 |
1678 # Return the original if no match | |
1679 %!assert(regexprep('hello','world','earth'),'hello') | |
1680 | |
1681 ## Test a general replacement | |
1682 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); | |
1683 | |
1684 ## Make sure it works at the beginning and end | |
1685 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); | |
1686 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); | |
1687 | |
1688 ## Options | |
1689 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); | |
1690 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); | |
1691 | |
1692 ## Option combinations | |
1693 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); | |
1694 | |
1695 ## End conditions on replacement | |
1696 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); | |
1697 %!assert(regexprep("abc","(b)","$1"),"abc"); | |
1698 %!assert(regexprep("abc","(b)","$1."),"ab.c"); | |
1699 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); | |
1700 | |
6361 | 1701 ## Test cell array arguments |
6503 | 1702 %!assert(regexprep("abc",{"b","a"},"?"),{"??c"}) |
6361 | 1703 %!assert(regexprep({"abc","cba"},"b","?"),{"a?c","c?a"}) |
6503 | 1704 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"}) |
6361 | 1705 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1706 # Nasty lookbehind expression |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1707 %!assert(regexprep('x^(-1)+y(-1)+z(-1)=0','(?<=[a-z]+)\(\-[1-9]*\)','_minus1'),'x^(-1)+y_minus1+z_minus1=0') |
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1708 |
5785 | 1709 */ |
1710 | |
5582 | 1711 /* |
1712 ;;; Local Variables: *** | |
1713 ;;; mode: C++ *** | |
1714 ;;; End: *** | |
1715 */ |