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