Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/regexp.cc @ 11045:cc3aad9dd3ef
dispatch.cc, fltk_backend.cc, regexp.cc: use octave_scalar_map instead of Octave_map
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 29 Sep 2010 03:39:56 -0400 |
parents | c9b0a75b02e8 |
children | fd0a3ac60b0e |
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; |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
302 warning ("%s: arbitrary length lookbehind patterns are only supported up to length %d", nm.c_str(), MAXLOOKBEHIND); |
10154
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 |
10550 | 354 if (re == 0) |
355 { | |
356 error("%s: %s at position %d of expression", nm.c_str(), | |
357 err, erroffset); | |
358 return 0; | |
359 } | |
5582 | 360 |
361 int subpatterns; | |
362 int namecount; | |
363 int nameentrysize; | |
364 char *nametable; | |
365 int idx = 0; | |
366 | |
7520 | 367 pcre_fullinfo(re, 0, PCRE_INFO_CAPTURECOUNT, &subpatterns); |
368 pcre_fullinfo(re, 0, PCRE_INFO_NAMECOUNT, &namecount); | |
369 pcre_fullinfo(re, 0, PCRE_INFO_NAMEENTRYSIZE, &nameentrysize); | |
370 pcre_fullinfo(re, 0, PCRE_INFO_NAMETABLE, &nametable); | |
5582 | 371 |
372 OCTAVE_LOCAL_BUFFER(int, ovector, (subpatterns+1)*3); | |
373 OCTAVE_LOCAL_BUFFER(int, nidx, namecount); | |
374 | |
375 for (int i = 0; i < namecount; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
376 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
377 // 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
|
378 // Extract index. |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
379 nidx[i] = (static_cast<int>(nametable[i*nameentrysize])) << 8 | |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
380 static_cast<int>(nametable[i*nameentrysize+1]); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
381 } |
5582 | 382 |
383 while(true) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
384 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
385 OCTAVE_QUIT; |
5785 | 386 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
387 int matches = pcre_exec(re, 0, buffer.c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
388 buffer.length(), idx, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
389 (idx ? PCRE_NOTBOL : 0), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
390 ovector, (subpatterns+1)*3); |
5582 | 391 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
392 if (matches == PCRE_ERROR_MATCHLIMIT) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
393 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
394 // 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
|
395 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
|
396 pcre_extra pe; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
397 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
|
398 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
|
399 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
400 int i = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
401 while (matches == PCRE_ERROR_MATCHLIMIT && |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
402 i++ < PCRE_MATCHLIMIT_MAX) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
403 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
404 OCTAVE_QUIT; |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
405 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
406 pe.match_limit *= 10; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
407 matches = pcre_exec(re, &pe, buffer.c_str(), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
408 buffer.length(), idx, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
409 (idx ? PCRE_NOTBOL : 0), |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
410 ovector, (subpatterns+1)*3); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
411 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
412 } |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
413 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
414 if (matches < 0 && matches != PCRE_ERROR_NOMATCH) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
415 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
416 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
|
417 pcre_free(re); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
418 return 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
419 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
420 else if (matches == PCRE_ERROR_NOMATCH) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
421 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
422 else if (ovector[1] <= ovector[0]) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
423 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
424 // 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
|
425 idx = ovector[0] + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
426 continue; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
427 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
428 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
429 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
430 int pos_match = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
431 Matrix te(matches-1,2); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
432 for (int i = 1; i < matches; i++) |
10518
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
433 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
434 if (i == 1 || ovector[2*i] != ovector[2*i-2] |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
435 || ovector[2*i-1] != ovector[2*i+1]) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
436 { |
10518
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
437 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
438 { |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
439 te(pos_match,0) = double (ovector[2*i]+1); |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
440 te(pos_match++,1) = double (ovector[2*i+1]); |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
441 } |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
442 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
443 te.resize(pos_match,2); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
444 s = double (ovector[0]+1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
445 e = double (ovector[1]); |
5582 | 446 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
447 const char **listptr; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
448 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
|
449 matches, &listptr); |
5582 | 450 |
10550 | 451 if (status == PCRE_ERROR_NOMEMORY) |
452 { | |
453 error("%s: cannot allocate memory in pcre_get_substring_list", | |
454 nm.c_str()); | |
455 pcre_free(re); | |
456 return 0; | |
457 } | |
5582 | 458 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
459 Cell cell_t (dim_vector(1,pos_match)); |
10518
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
460 string_vector named_tokens(nnames); |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
461 int pos_offset = 0; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
462 pos_match = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
463 for (int i = 1; i < matches; i++) |
10550 | 464 if (ovector[2*i] >= 0 && ovector[2*i+1] > 0) |
465 { | |
466 if (i == 1 || ovector[2*i] != ovector[2*i-2] | |
467 || ovector[2*i-1] != ovector[2*i+1]) | |
468 { | |
469 if (namecount > 0) | |
470 named_tokens(named_idx(i-pos_offset-1)) = | |
471 std::string(*(listptr+nidx[i-pos_offset-1])); | |
472 cell_t(pos_match++) = | |
473 std::string(*(listptr+i)); | |
474 } | |
475 else | |
476 pos_offset++; | |
10546
f5b8b28917a2
Eliminate compile warning about explicit braces
Rik <code@nomad.inbox5.com>
parents:
10518
diff
changeset
|
477 } |
5582 | 478 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
479 m = std::string(*listptr); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
480 t = cell_t; |
5785 | 481 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
482 pcre_free_substring_list(listptr); |
5582 | 483 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
484 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
|
485 lst.push_back (new_elem); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
486 idx = ovector[1]; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
487 sz++; |
5785 | 488 |
11002
2538d03489cc
avoid infinite loop in regexp searches with PCRE
John W. Eaton <jwe@octave.org>
parents:
10846
diff
changeset
|
489 if (once || idx >= buffer.length ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
490 break; |
5582 | 491 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
492 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
493 } |
5582 | 494 |
495 pcre_free(re); | |
496 #else | |
497 regex_t compiled; | |
498 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
|
499 (case_insensitive ? REG_ICASE : 0)); |
5582 | 500 if (err) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
501 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
502 int len = regerror(err, &compiled, 0, 0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
503 OCTAVE_LOCAL_BUFFER (char, errmsg, len); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
504 regerror(err, &compiled, errmsg, len); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
505 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
|
506 pattern.c_str()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
507 regfree(&compiled); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
508 return 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
509 } |
5582 | 510 |
511 int subexpr = 1; | |
512 int idx = 0; | |
513 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
|
514 subexpr += ( pattern[i] == '(' ? 1 : 0 ); |
5582 | 515 OCTAVE_LOCAL_BUFFER (regmatch_t, match, subexpr ); |
516 | |
517 while(true) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
518 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
519 OCTAVE_QUIT; |
5785 | 520 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
521 if (regexec(&compiled, buffer.c_str() + idx, subexpr, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
522 match, (idx ? REG_NOTBOL : 0)) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
523 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
524 // Count actual matches |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
525 int matches = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
526 while (matches < subexpr && match[matches].rm_so >= 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
527 matches++; |
5582 | 528 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
529 if (matches == 0 || match[0].rm_eo == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
530 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
|
531 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
532 s = double (match[0].rm_so+1+idx); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
533 e = double (match[0].rm_eo+idx); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
534 Matrix te(matches-1,2); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
535 for (int i = 1; i < matches; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
536 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
537 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
|
538 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
|
539 } |
5582 | 540 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
541 m = buffer.substr (match[0].rm_so+idx, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
542 match[0].rm_eo-match[0].rm_so); |
5582 | 543 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
544 Cell cell_t (dim_vector(1,matches-1)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
545 for (int i = 1; i < matches; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
546 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
|
547 match[i].rm_eo-match[i].rm_so); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
548 t = cell_t; |
5582 | 549 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
550 idx += match[0].rm_eo; |
5785 | 551 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
552 string_vector sv; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
553 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
|
554 lst.push_back (new_elem); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
555 sz++; |
5582 | 556 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
557 if (once) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
558 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
559 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
560 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
561 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
562 } |
5582 | 563 regfree(&compiled); |
564 #endif | |
5785 | 565 } |
566 #else | |
567 error ("%s: not available in this version of Octave", nm.c_str()); | |
568 #endif | |
569 return sz; | |
570 } | |
5582 | 571 |
5785 | 572 static octave_value_list |
573 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
|
574 bool case_insensitive) |
5785 | 575 { |
576 octave_value_list retval; | |
577 int nargin = args.length(); | |
578 std::list<regexp_elem> lst; | |
579 string_vector named; | |
580 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
581 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
582 int sz = octregexp_list (args, nm, case_insensitive, lst, named, nopts, once); |
5785 | 583 |
584 if (! error_state) | |
585 { | |
586 // Converted the linked list in the correct form for the return values | |
587 | |
588 octave_idx_type i = 0; | |
589 #ifdef HAVE_PCRE | |
11045
cc3aad9dd3ef
dispatch.cc, fltk_backend.cc, regexp.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11032
diff
changeset
|
590 octave_scalar_map nmap; |
5785 | 591 if (sz == 1) |
10154
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 for (int j = 0; j < named.length(); j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
594 nmap.assign (named(j), lst.begin()->named_token(j)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
595 retval(5) = nmap; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
596 } |
5785 | 597 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
598 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
599 for (int j = 0; j < named.length (); j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
600 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
601 i = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
602 Cell tmp(dim_vector (1, sz)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
603 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
|
604 tmp(i++) = p->named_token(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
605 nmap.assign (named(j), octave_value (tmp)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
606 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
607 retval(5) = nmap; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
608 } |
5785 | 609 #else |
11045
cc3aad9dd3ef
dispatch.cc, fltk_backend.cc, regexp.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
11032
diff
changeset
|
610 retval(5) = octave_scalar_map (); |
5785 | 611 #endif |
612 | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
613 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
614 retval(4) = sz ? lst.front ().t : Cell(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
615 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
616 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
617 Cell t (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
618 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
619 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
|
620 t(i++) = p->t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
621 retval(4) = t; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
622 } |
5785 | 623 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
624 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
625 retval(3) = sz ? lst.front ().m : std::string(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
626 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
627 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
628 Cell m (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
629 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
630 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
|
631 m(i++) = p->m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
632 retval(3) = m; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
633 } |
5785 | 634 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
635 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
636 retval(2) = sz ? lst.front ().te : Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
637 else |
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 Cell te (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
640 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
641 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
|
642 te(i++) = p->te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
643 retval(2) = te; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
644 } |
5785 | 645 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
646 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
647 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
648 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
649 retval(1) = lst.front ().e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
650 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
651 retval(1) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
652 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
653 else |
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 NDArray e (dim_vector(1, sz)); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
656 i = 0; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
657 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
|
658 e(i++) = p->e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
659 retval(1) = e; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
660 } |
5785 | 661 |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
662 if (once) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
663 { |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
664 if (sz) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
665 retval(0) = lst.front ().s; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
666 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
667 retval(0) = Matrix(); |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
668 } |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
669 else |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
670 { |
10551 | 671 NDArray s (dim_vector(1, sz)); |
672 | |
673 i = 0; | |
674 for (const_iterator p = lst.begin(); p != lst.end(); p++) | |
675 s(i++) = p->s; | |
676 retval(0) = s; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
677 } |
5582 | 678 |
679 // Alter the order of the output arguments | |
680 if (nopts > 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
681 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
682 int n = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
683 octave_value_list new_retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
684 new_retval.resize(nargout); |
5582 | 685 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
686 OCTAVE_LOCAL_BUFFER (int, arg_used, 6); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
687 for (int j = 0; j < 6; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
688 arg_used[j] = false; |
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 for (int j = 2; j < nargin; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
691 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
692 int k = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
693 std::string str = args(j).string_value(); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
694 std::transform (str.begin (), str.end (), str.begin (), tolower); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
695 if (str.find("once", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
696 || str.find("stringanchors", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
697 || str.find("lineanchors", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
698 || str.find("matchcase", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
699 || str.find("ignorecase", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
700 || str.find("dotall", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
701 || str.find("dotexceptnewline", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
702 || str.find("literalspacing", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
703 || str.find("freespacing", 0) == 0 |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
704 ) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
705 continue; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
706 else if (str.find("start", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
707 k = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
708 else if (str.find("end", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
709 k = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
710 else if (str.find("tokenextents", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
711 k = 2; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
712 else if (str.find("match", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
713 k = 3; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
714 else if (str.find("tokens", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
715 k = 4; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
716 else if (str.find("names", 0) == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
717 k = 5; |
5582 | 718 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
719 new_retval(n++) = retval(k); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
720 arg_used[k] = true; |
5582 | 721 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
722 if (n == nargout) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
723 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
724 } |
5582 | 725 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
726 // Fill in the rest of the arguments |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
727 if (n < nargout) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
728 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
729 for (int j = 0; j < 6; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
730 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
731 if (! arg_used[j]) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
732 new_retval(n++) = retval(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
733 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
734 } |
5582 | 735 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
736 retval = new_retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
737 } |
5582 | 738 } |
739 | |
740 return retval; | |
741 } | |
742 | |
6361 | 743 static octave_value_list |
744 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
|
745 bool case_insensitive) |
6361 | 746 { |
747 octave_value_list retval; | |
748 | |
749 if (args(0).is_cell()) | |
750 { | |
751 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
752 octave_value_list new_args = args; | |
753 Cell cellstr = args(0).cell_value(); | |
754 if (args(1).is_cell()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
755 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
756 Cell cellpat = args(1).cell_value(); |
6361 | 757 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
758 if (cellpat.numel() == 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
759 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
760 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
761 newretval[j].resize(cellstr.dims()); |
6361 | 762 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
763 new_args(1) = cellpat(0); |
6361 | 764 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
765 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
|
766 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
767 new_args(0) = cellstr(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
768 octave_value_list tmp = octregexp (new_args, nargout, nm, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
769 case_insensitive); |
6361 | 770 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
771 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
772 break; |
6361 | 773 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
774 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
775 newretval[j](i) = tmp(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
776 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
777 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
778 else if (cellstr.numel() == 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
779 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
780 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
781 newretval[j].resize(cellpat.dims()); |
6361 | 782 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
783 new_args(0) = cellstr(0); |
6361 | 784 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
785 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
|
786 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
787 new_args(1) = cellpat(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
788 octave_value_list tmp = octregexp (new_args, nargout, nm, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
789 case_insensitive); |
6361 | 790 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
791 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
792 break; |
6361 | 793 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
794 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
795 newretval[j](i) = tmp(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
796 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
797 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
798 else if (cellstr.numel() == cellpat.numel()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
799 { |
6361 | 800 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
801 if (cellstr.dims() != cellpat.dims()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
802 error ("%s: Inconsistent cell array dimensions", nm.c_str()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
803 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
804 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
805 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
806 newretval[j].resize(cellstr.dims()); |
6361 | 807 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
808 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
|
809 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
810 new_args(0) = cellstr(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
811 new_args(1) = cellpat(i); |
6361 | 812 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
813 octave_value_list tmp = octregexp (new_args, nargout, nm, |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
814 case_insensitive); |
6361 | 815 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
816 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
817 break; |
6361 | 818 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
819 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
820 newretval[j](i) = tmp(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
821 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
822 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
823 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
824 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
825 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
|
826 } |
6361 | 827 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
828 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
829 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
830 newretval[j].resize(cellstr.dims()); |
6361 | 831 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
832 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
|
833 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
834 new_args(0) = cellstr(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
835 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); |
6361 | 836 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
837 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
838 break; |
6361 | 839 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
840 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
841 newretval[j](i) = tmp(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
842 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
843 } |
6361 | 844 |
845 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
846 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
847 retval(j) = octave_value (newretval[j]); |
6361 | 848 } |
849 else if (args(1).is_cell()) | |
850 { | |
851 OCTAVE_LOCAL_BUFFER (Cell, newretval, nargout); | |
852 octave_value_list new_args = args; | |
853 Cell cellpat = args(1).cell_value(); | |
854 | |
855 for (int j = 0; j < nargout; j++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
856 newretval[j].resize(cellpat.dims()); |
6361 | 857 |
858 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
|
859 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
860 new_args(1) = cellpat(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
861 octave_value_list tmp = octregexp (new_args, nargout, nm, case_insensitive); |
6361 | 862 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
863 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
864 break; |
6361 | 865 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
866 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
867 newretval[j](i) = tmp(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
868 } |
6361 | 869 |
870 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
871 for (int j = 0; j < nargout; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
872 retval(j) = octave_value (newretval[j]); |
6361 | 873 } |
874 else | |
875 retval = octregexp (args, nargout, nm, case_insensitive); | |
876 | |
877 return retval; | |
878 | |
879 } | |
880 | |
5582 | 881 DEFUN_DLD (regexp, args, nargout, |
882 "-*- texinfo -*-\n\ | |
10840 | 883 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexp (@var{str}, @var{pat})\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
884 @deftypefnx {Loadable Function} {[@dots{}] =} regexp (@var{str}, @var{pat}, \"@var{opt1}\", @dots{})\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
885 Regular expression string matching. Search for @var{pat} in @var{str} and\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
886 return the positions and substrings of any matches, or empty values if there\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
887 are none. Note, some features and extended options are only available when\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
888 Octave is compiled with support for Perl Compatible Regular Expressions\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
889 (PCRE).\n\ |
5582 | 890 \n\ |
891 The matched pattern @var{pat} can include any of the standard regex\n\ | |
892 operators, including:\n\ | |
893 \n\ | |
894 @table @code\n\ | |
895 @item .\n\ | |
896 Match any character\n\ | |
10840 | 897 \n\ |
5582 | 898 @item * + ? @{@}\n\ |
899 Repetition operators, representing\n\ | |
900 @table @code\n\ | |
901 @item *\n\ | |
902 Match zero or more times\n\ | |
10840 | 903 \n\ |
5582 | 904 @item +\n\ |
905 Match one or more times\n\ | |
10840 | 906 \n\ |
5582 | 907 @item ?\n\ |
908 Match zero or one times\n\ | |
10840 | 909 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
910 @item @{@var{n}@}\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
911 Match exactly @var{n} times\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
912 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
913 @item @{@var{n},@}\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
914 Match @var{n} or more times\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
915 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
916 @item @{@var{m},@var{n}@}\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
917 Match between @var{m} and @var{n} times\n\ |
5582 | 918 @end table\n\ |
10840 | 919 \n\ |
5582 | 920 @item [@dots{}] [^@dots{}]\n\ |
10840 | 921 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
922 List operators. The pattern will match any character listed between \"[\"\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
923 and \"]\". If the first character is \"^\" then the pattern is inverted and\n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
924 any character except those listed between brackets will match.\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
925 \n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
926 With PCRE support, escape sequences defined below can be used inside list\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
927 operators. For example, a template for a floating point number might be\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
928 @code{[-+.\\d]+}. POSIX regular expressions do not use escape sequences\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
929 and any backslash @samp{\\} will be interpreted literally as one\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
930 of the list of characters to match.\n\ |
10840 | 931 \n\ |
5582 | 932 @item ()\n\ |
933 Grouping operator\n\ | |
10840 | 934 \n\ |
5582 | 935 @item |\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
936 Alternation operator. Match one of a choice of regular expressions. The\n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
937 alternatives must be delimited by the grouping operator @code{()} above.\n\ |
10840 | 938 \n\ |
5582 | 939 @item ^ $\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
940 Anchoring operators. Requires pattern to occur at the start (@code{^}) or\n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
941 end (@code{$}) of the string.\n\ |
5582 | 942 @end table\n\ |
943 \n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
944 In addition, the following escaped characters have special meaning. Note,\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
945 it is recommended to quote @var{pat} in single quotes, rather than double\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
946 quotes, to avoid the escape sequences being interpreted by Octave before\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
947 being passed to @code{regexp}.\n\ |
5582 | 948 \n\ |
949 @table @code\n\ | |
950 @item \\b\n\ | |
951 Match a word boundary\n\ | |
10840 | 952 \n\ |
5582 | 953 @item \\B\n\ |
954 Match within a word\n\ | |
10840 | 955 \n\ |
5582 | 956 @item \\w\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
957 Match any word character\n\ |
10840 | 958 \n\ |
5582 | 959 @item \\W\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
960 Match any non-word character\n\ |
10840 | 961 \n\ |
5582 | 962 @item \\<\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
963 Match the beginning of a word\n\ |
10840 | 964 \n\ |
5582 | 965 @item \\>\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
966 Match the end of a word\n\ |
10840 | 967 \n\ |
5582 | 968 @item \\s\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
969 Match any whitespace character\n\ |
10840 | 970 \n\ |
5582 | 971 @item \\S\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
972 Match any non-whitespace character\n\ |
10840 | 973 \n\ |
5582 | 974 @item \\d\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
975 Match any digit\n\ |
10840 | 976 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
977 This sequence is only available with PCRE support. For POSIX regular\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
978 expressions use the following list operator @code{[0-9]}.\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
979 \n\ |
5582 | 980 @item \\D\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
981 Match any non-digit\n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
982 \n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
983 This sequence is only available with PCRE support. For POSIX regular\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
984 expressions use the following list operator @code{[^0-9]}.\n\ |
5582 | 985 @end table\n\ |
986 \n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
987 The outputs of @code{regexp} default to the order given below\n\ |
5582 | 988 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
989 @table @var\n\ |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
990 @item s\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
991 The start indices of each matching substring\n\ |
5582 | 992 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
993 @item e\n\ |
5582 | 994 The end indices of each matching substring\n\ |
995 \n\ | |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
996 @item te\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
997 The extents of each matched token surrounded by @code{(@dots{})} in\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
998 @var{pat}\n\ |
5582 | 999 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1000 @item m\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1001 A cell array of the text of each match\n\ |
5582 | 1002 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1003 @item t\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1004 A cell array of the text of each token matched\n\ |
5582 | 1005 \n\ |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1006 @item nm\n\ |
5582 | 1007 A structure containing the text of each matched named token, with the name\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1008 being used as the fieldname. A named token is denoted by\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1009 @code{(?<name>@dots{})} and is only available with PCRE support.\n\ |
5582 | 1010 @end table\n\ |
1011 \n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1012 Particular output arguments, or the order of the output arguments, can be\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1013 selected by additional @var{opt} arguments. These are strings and the\n\ |
5582 | 1014 correspondence between the output arguments and the optional argument\n\ |
1015 are\n\ | |
1016 \n\ | |
1017 @multitable @columnfractions 0.2 0.3 0.3 0.2\n\ | |
1018 @item @tab 'start' @tab @var{s} @tab\n\ | |
1019 @item @tab 'end' @tab @var{e} @tab\n\ | |
1020 @item @tab 'tokenExtents' @tab @var{te} @tab\n\ | |
1021 @item @tab 'match' @tab @var{m} @tab\n\ | |
1022 @item @tab 'tokens' @tab @var{t} @tab\n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1023 @item @tab 'names' @tab @var{nm} @tab\n\ |
5582 | 1024 @end multitable\n\ |
1025 \n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1026 Additional arguments are summarized below.\n\ |
5779 | 1027 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1028 @table @samp\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1029 @item once\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1030 Return only the first occurrence of the pattern.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1031 \n\ |
5779 | 1032 @item matchcase\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1033 Make the matching case sensitive. (default)\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1034 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1035 Alternatively, use (?-i) in the pattern when PCRE is available.\n\ |
10840 | 1036 \n\ |
5779 | 1037 @item ignorecase\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1038 Ignore case when matching the pattern to the string.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1039 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1040 Alternatively, use (?i) in the pattern when PCRE is available.\n\ |
10840 | 1041 \n\ |
5779 | 1042 @item stringanchors\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1043 Match the anchor characters at the beginning and end of the string. \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1044 (default)\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1045 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1046 Alternatively, use (?-m) in the pattern when PCRE is available.\n\ |
10840 | 1047 \n\ |
5779 | 1048 @item lineanchors\n\ |
1049 Match the anchor characters at the beginning and end of the line.\n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1050 Only available when Octave is compiled with PCRE.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1051 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1052 Alternatively, use (?m) in the pattern when PCRE is available.\n\ |
10840 | 1053 \n\ |
5779 | 1054 @item dotall\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1055 The pattern @code{.} matches all characters including the newline character.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1056 (default)\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1057 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1058 Alternatively, use (?s) in the pattern when PCRE is available.\n\ |
10840 | 1059 \n\ |
5779 | 1060 @item dotexceptnewline\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1061 The pattern @code{.} matches all characters except the newline character.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1062 Only available when Octave is compiled with PCRE.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1063 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1064 Alternatively, use (?-s) in the pattern when PCRE is available.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1065 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1066 @item literalspacing\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1067 All characters in the pattern, including whitespace, are significant and are\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1068 used in pattern matching. (default)\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1069 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1070 Alternatively, use (?-x) in the pattern when PCRE is available.\n\ |
10840 | 1071 \n\ |
5779 | 1072 @item freespacing\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1073 The pattern may include arbitrary whitespace and also comments beginning with\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1074 the character @samp{#}.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1075 Only available when Octave is compiled with PCRE.\n\ |
10840 | 1076 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1077 Alternatively, use (?x) in the pattern when PCRE is available.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1078 \n\ |
5779 | 1079 @end table\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1080 @seealso{regexpi,strfind,regexprep}\n\ |
5582 | 1081 @end deftypefn") |
1082 { | |
6361 | 1083 octave_value_list retval; |
1084 int nargin = args.length(); | |
1085 | |
1086 if (nargin < 2) | |
1087 print_usage (); | |
1088 else if (args(0).is_cell() || args(1).is_cell()) | |
1089 retval = octcellregexp (args, nargout, "regexp", false); | |
1090 else | |
1091 retval = octregexp (args, nargout, "regexp", false); | |
1092 | |
1093 return retval; | |
5582 | 1094 } |
1095 | |
1096 /* | |
1097 | |
8140
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1098 ## PCRE_ERROR_MATCHLIMIT test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1099 %!test |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1100 %! 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
|
1101 %! ws = warning("query"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1102 %! unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1103 %! warning("off"); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1104 %! 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
|
1105 %! unwind_protect_cleanup |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1106 %! warning(ws); |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1107 %! end_unwind_protect |
cdd05e46f6c9
Increase pcre's match_limit for difficult regexps
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
8093
diff
changeset
|
1108 |
5582 | 1109 ## seg-fault test |
1110 %!assert(regexp("abcde","."),[1,2,3,4,5]) | |
1111 | |
1112 ## Check that anchoring of pattern works correctly | |
1113 %!assert(regexp('abcabc','^abc'),1); | |
1114 %!assert(regexp('abcabc','abc$'),4); | |
5785 | 1115 %!assert(regexp('abcabc','^abc$'),zeros(1,0)); |
5582 | 1116 |
1117 %!test | |
1118 %! [s, e, te, m, t] = regexp(' No Match ', 'f(.*)uck'); | |
5785 | 1119 %! assert (s,zeros(1,0)) |
1120 %! assert (e,zeros(1,0)) | |
1121 %! assert (te,cell(1,0)) | |
1122 %! assert (m, cell(1,0)) | |
1123 %! assert (t, cell(1,0)) | |
5582 | 1124 |
1125 %!test | |
1126 %! [s, e, te, m, t] = regexp(' FiRetrUck ', 'f(.*)uck'); | |
5785 | 1127 %! assert (s,zeros(1,0)) |
1128 %! assert (e,zeros(1,0)) | |
1129 %! assert (te,cell(1,0)) | |
1130 %! assert (m, cell(1,0)) | |
1131 %! assert (t, cell(1,0)) | |
5582 | 1132 |
1133 %!test | |
1134 %! [s, e, te, m, t] = regexp(' firetruck ', 'f(.*)uck'); | |
1135 %! assert (s,2) | |
1136 %! assert (e,10) | |
1137 %! assert (te{1},[3,7]) | |
1138 %! assert (m{1}, 'firetruck') | |
1139 %! assert (t{1}{1}, 'iretr') | |
1140 | |
1141 %!test | |
1142 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*'); | |
1143 %! assert (s,[1,12]) | |
1144 %! assert (e,[5,17]) | |
1145 %! assert (size(te), [1,2]) | |
1146 %! assert (isempty(te{1})) | |
1147 %! assert (isempty(te{2})) | |
1148 %! assert (m{1},'short') | |
1149 %! assert (m{2},'string') | |
1150 %! assert (size(t), [1,2]) | |
1151 %! assert (isempty(t{1})) | |
1152 %! assert (isempty(t{2})) | |
1153 | |
1154 %!test | |
1155 %! [s, e, te, m, t] = regexp('short test string','\w*r\w*','once'); | |
1156 %! assert (s,1) | |
1157 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1158 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1159 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1160 %! assert (isempty(t)) |
5582 | 1161 |
1162 %!test | |
1163 %! [m, te, e, s, t] = regexp('short test string','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1164 %! assert (s,1) | |
1165 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1166 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1167 %! assert (m,'short') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1168 %! assert (isempty(t)) |
5582 | 1169 |
7242 | 1170 %!testif HAVE_PCRE |
1171 %! [s, e, te, m, t, nm] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)'); | |
1172 %! assert (s,1) | |
1173 %! assert (e,10) | |
1174 %! assert (size(te), [1,1]) | |
1175 %! assert (te{1}, [1 5; 7, 10]) | |
1176 %! assert (m{1},'short test') | |
1177 %! assert (size(t),[1,1]) | |
1178 %! assert (t{1}{1},'short') | |
1179 %! assert (t{1}{2},'test') | |
1180 %! assert (size(nm), [1,1]) | |
1181 %! assert (!isempty(fieldnames(nm))) | |
1182 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1183 %! assert (nm.word1,'short') | |
1184 %! assert (nm.word2,'test') | |
5582 | 1185 |
7242 | 1186 %!testif HAVE_PCRE |
1187 %! [nm, m, te, e, s, t] = regexp('short test string','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1188 %! assert (s,1) | |
1189 %! assert (e,10) | |
1190 %! assert (size(te), [1,1]) | |
1191 %! assert (te{1}, [1 5; 7, 10]) | |
1192 %! assert (m{1},'short test') | |
1193 %! assert (size(t),[1,1]) | |
1194 %! assert (t{1}{1},'short') | |
1195 %! assert (t{1}{2},'test') | |
1196 %! assert (size(nm), [1,1]) | |
1197 %! assert (!isempty(fieldnames(nm))) | |
1198 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1199 %! assert (nm.word1,'short') | |
1200 %! assert (nm.word2,'test') | |
5619 | 1201 |
7242 | 1202 %!testif HAVE_PCRE |
1203 %! [t, nm] = regexp("John Davis\nRogers, James",'(?<first>\w+)\s+(?<last>\w+)|(?<last>\w+),\s+(?<first>\w+)','tokens','names'); | |
1204 %! assert (size(t), [1,2]); | |
1205 %! assert (t{1}{1},'John'); | |
1206 %! assert (t{1}{2},'Davis'); | |
1207 %! assert (t{2}{1},'Rogers'); | |
1208 %! assert (t{2}{2},'James'); | |
1209 %! assert (size(nm), [1,1]); | |
1210 %! assert (nm.first{1},'John'); | |
1211 %! assert (nm.first{2},'James'); | |
1212 %! assert (nm.last{1},'Davis'); | |
1213 %! assert (nm.last{2},'Rogers'); | |
5582 | 1214 |
10518
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
1215 %!testif HAVE_PCRE |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
1216 %! # Parenthesis in named token (ie (int)) causes a problem |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
1217 %! assert (regexp('qwe int asd', ['(?<typestr>(int))'], 'names'), struct ('typestr', 'int')); |
fcafe0e9bd58
Handle repeated matches in matches returned by pcre
David Bateman <dbateman@free.fr>
parents:
10504
diff
changeset
|
1218 |
5779 | 1219 %!assert(regexp("abc\nabc",'.'),[1:7]) |
1220 %!assert(regexp("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1221 %!testif HAVE_PCRE |
1222 %! assert(regexp("abc\nabc",'(?s).'),[1:7]) | |
1223 %! assert(regexp("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1224 %! assert(regexp("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1225 |
1226 %!assert(regexp("caseCaSe",'case'),1) | |
1227 %!assert(regexp("caseCaSe",'case',"matchcase"),1) | |
1228 %!assert(regexp("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1229 %!testif HAVE_PCRE |
1230 %! assert(regexp("caseCaSe",'(?-i)case'),1) | |
1231 %! assert(regexp("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1232 |
1233 %!assert (regexp("abc\nabc",'c$'),7) | |
1234 %!assert (regexp("abc\nabc",'c$',"stringanchors"),7) | |
7242 | 1235 %!testif HAVE_PCRE |
1236 %! assert (regexp("abc\nabc",'(?-m)c$'),7) | |
1237 %! assert (regexp("abc\nabc",'c$',"lineanchors"),[3,7]) | |
1238 %! assert (regexp("abc\nabc",'(?m)c$'),[3,7]) | |
5779 | 1239 |
1240 %!assert (regexp("this word",'s w'),4) | |
1241 %!assert (regexp("this word",'s w','literalspacing'),4) | |
7242 | 1242 %!testif HAVE_PCRE |
1243 %! assert (regexp("this word",'(?-x)s w','literalspacing'),4) | |
1244 %! assert (regexp("this word",'s w','freespacing'),zeros(1,0)) | |
1245 %! assert (regexp("this word",'(?x)s w'),zeros(1,0)) | |
5779 | 1246 |
5582 | 1247 %!error regexp('string', 'tri', 'BadArg'); |
1248 %!error regexp('string'); | |
1249 | |
6361 | 1250 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1251 %!assert(regexp({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1252 %!assert(regexp({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1253 %!assert(regexp('Strings',{'t','s'}),{2,7}) | |
1254 | |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1255 ## Test case for lookaround operators |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1256 %!testif HAVE_PCRE |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1257 %! assert(regexp('Iraq','q(?!u)'),4) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1258 %! assert(regexp('quit','q(?!u)'), zeros(1,0)) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1259 %! assert(regexp('quit','q(?=u)','match'), {'q'}) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1260 %! assert(regexp("quit",'q(?=u+)','match'), {'q'}) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1261 %! assert(regexp("qit",'q(?=u+)','match'), cell(1,0)) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1262 %! assert(regexp("qit",'q(?=u*)','match'), {'q'}) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1263 %! assert(regexp('thingamabob','(?<=a)b'), 9) |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1264 |
5582 | 1265 */ |
1266 | |
6549 | 1267 DEFUN_DLD (regexpi, args, nargout, |
5582 | 1268 "-*- texinfo -*-\n\ |
10840 | 1269 @deftypefn {Loadable Function} {[@var{s}, @var{e}, @var{te}, @var{m}, @var{t}, @var{nm}] =} regexpi (@var{str}, @var{pat})\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1270 @deftypefnx {Loadable Function} {[@dots{}] =} regexpi (@var{str}, @var{pat}, \"@var{opt1}\", @dots{})\n\ |
5582 | 1271 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1272 Case insensitive regular expression string matching. Search for @var{pat} in\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1273 @var{str} and return the positions and substrings of any matches, or empty\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1274 values if there are none. @xref{doc-regexp,,regexp}, for details on the\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1275 syntax of the search pattern.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1276 @seealso{regexp}\n\ |
5582 | 1277 @end deftypefn") |
1278 { | |
6361 | 1279 octave_value_list retval; |
1280 int nargin = args.length(); | |
1281 | |
1282 if (nargin < 2) | |
1283 print_usage (); | |
1284 else if (args(0).is_cell() || args(1).is_cell()) | |
1285 retval = octcellregexp (args, nargout, "regexpi", true); | |
1286 else | |
1287 retval = octregexp (args, nargout, "regexpi", true); | |
1288 | |
1289 return retval; | |
5582 | 1290 } |
1291 | |
1292 /* | |
1293 | |
1294 ## seg-fault test | |
1295 %!assert(regexpi("abcde","."),[1,2,3,4,5]) | |
1296 | |
1297 ## Check that anchoring of pattern works correctly | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1298 %!assert(regexpi('abcabc','^ABC'),1); |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1299 %!assert(regexpi('abcabc','ABC$'),4); |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1300 %!assert(regexpi('abcabc','^ABC$'),zeros(1,0)); |
5582 | 1301 |
1302 %!test | |
1303 %! [s, e, te, m, t] = regexpi(' No Match ', 'f(.*)uck'); | |
5785 | 1304 %! assert (s,zeros(1,0)) |
1305 %! assert (e,zeros(1,0)) | |
1306 %! assert (te,cell(1,0)) | |
1307 %! assert (m, cell(1,0)) | |
1308 %! assert (t, cell(1,0)) | |
5582 | 1309 |
1310 %!test | |
1311 %! [s, e, te, m, t] = regexpi(' FiRetrUck ', 'f(.*)uck'); | |
1312 %! assert (s,2) | |
1313 %! assert (e,10) | |
1314 %! assert (te{1},[3,7]) | |
1315 %! assert (m{1}, 'FiRetrUck') | |
1316 %! assert (t{1}{1}, 'iRetr') | |
1317 | |
1318 %!test | |
1319 %! [s, e, te, m, t] = regexpi(' firetruck ', 'f(.*)uck'); | |
1320 %! assert (s,2) | |
1321 %! assert (e,10) | |
1322 %! assert (te{1},[3,7]) | |
1323 %! assert (m{1}, 'firetruck') | |
1324 %! assert (t{1}{1}, 'iretr') | |
1325 | |
1326 %!test | |
1327 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*'); | |
1328 %! assert (s,[1,12]) | |
1329 %! assert (e,[5,17]) | |
1330 %! assert (size(te), [1,2]) | |
1331 %! assert (isempty(te{1})) | |
1332 %! assert (isempty(te{2})) | |
1333 %! assert (m{1},'ShoRt') | |
1334 %! assert (m{2},'String') | |
1335 %! assert (size(t), [1,2]) | |
1336 %! assert (isempty(t{1})) | |
1337 %! assert (isempty(t{2})) | |
1338 | |
1339 %!test | |
1340 %! [s, e, te, m, t] = regexpi('ShoRt Test String','\w*r\w*','once'); | |
1341 %! assert (s,1) | |
1342 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1343 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1344 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1345 %! assert (isempty(t)) |
5582 | 1346 |
1347 %!test | |
1348 %! [m, te, e, s, t] = regexpi('ShoRt Test String','\w*r\w*','once', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1349 %! assert (s,1) | |
1350 %! assert (e,5) | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1351 %! assert (isempty(te)) |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1352 %! assert (m,'ShoRt') |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1353 %! assert (isempty(t)) |
5582 | 1354 |
7242 | 1355 %!testif HAVE_PCRE |
1356 %! [s, e, te, m, t, nm] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)'); | |
1357 %! assert (s,1) | |
1358 %! assert (e,10) | |
1359 %! assert (size(te), [1,1]) | |
1360 %! assert (te{1}, [1 5; 7, 10]) | |
1361 %! assert (m{1},'ShoRt Test') | |
1362 %! assert (size(t),[1,1]) | |
1363 %! assert (t{1}{1},'ShoRt') | |
1364 %! assert (t{1}{2},'Test') | |
1365 %! assert (size(nm), [1,1]) | |
1366 %! assert (!isempty(fieldnames(nm))) | |
1367 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1368 %! assert (nm.word1,'ShoRt') | |
1369 %! assert (nm.word2,'Test') | |
5582 | 1370 |
7242 | 1371 %!testif HAVE_PCRE |
1372 %! [nm, m, te, e, s, t] = regexpi('ShoRt Test String','(?<word1>\w*t)\s*(?<word2>\w*t)', 'names', 'match', 'tokenExtents', 'end', 'start', 'tokens'); | |
1373 %! assert (s,1) | |
1374 %! assert (e,10) | |
1375 %! assert (size(te), [1,1]) | |
1376 %! assert (te{1}, [1 5; 7, 10]) | |
1377 %! assert (m{1},'ShoRt Test') | |
1378 %! assert (size(t),[1,1]) | |
1379 %! assert (t{1}{1},'ShoRt') | |
1380 %! assert (t{1}{2},'Test') | |
1381 %! assert (size(nm), [1,1]) | |
1382 %! assert (!isempty(fieldnames(nm))) | |
1383 %! assert (sort(fieldnames(nm)),{'word1';'word2'}) | |
1384 %! assert (nm.word1,'ShoRt') | |
1385 %! assert (nm.word2,'Test') | |
5582 | 1386 |
5779 | 1387 %!assert(regexpi("abc\nabc",'.'),[1:7]) |
1388 %!assert(regexpi("abc\nabc",'.','dotall'),[1:7]) | |
7242 | 1389 %!testif HAVE_PCRE |
1390 %! assert(regexpi("abc\nabc",'(?s).'),[1:7]) | |
1391 %! assert(regexpi("abc\nabc",'.','dotexceptnewline'),[1,2,3,5,6,7]) | |
1392 %! assert(regexpi("abc\nabc",'(?-s).'),[1,2,3,5,6,7]) | |
5779 | 1393 |
1394 %!assert(regexpi("caseCaSe",'case'),[1,5]) | |
1395 %!assert(regexpi("caseCaSe",'case',"matchcase"),1) | |
1396 %!assert(regexpi("caseCaSe",'case',"ignorecase"),[1,5]) | |
7242 | 1397 %!testif HAVE_PCRE |
1398 %! assert(regexpi("caseCaSe",'(?-i)case'),1) | |
1399 %! assert(regexpi("caseCaSe",'(?i)case'),[1,5]) | |
5779 | 1400 |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1401 %!assert (regexpi("abc\nabc",'C$'),7) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1402 %!assert (regexpi("abc\nabc",'C$',"stringanchors"),7) |
7242 | 1403 %!testif HAVE_PCRE |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1404 %! assert (regexpi("abc\nabc",'(?-m)C$'),7) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1405 %! assert (regexpi("abc\nabc",'C$',"lineanchors"),[3,7]) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1406 %! assert (regexpi("abc\nabc",'(?m)C$'),[3,7]) |
5779 | 1407 |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1408 %!assert (regexpi("this word",'S w'),4) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1409 %!assert (regexpi("this word",'S w','literalspacing'),4) |
7242 | 1410 %!testif HAVE_PCRE |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1411 %! assert (regexpi("this word",'(?-x)S w','literalspacing'),4) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1412 %! assert (regexpi("this word",'S w','freespacing'),zeros(1,0)) |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1413 %! assert (regexpi("this word",'(?x)S w'),zeros(1,0)) |
5779 | 1414 |
5582 | 1415 %!error regexpi('string', 'tri', 'BadArg'); |
1416 %!error regexpi('string'); | |
1417 | |
6361 | 1418 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},'-'),{6;[1,5,9];zeros(1,0)}) |
1419 %!assert(regexpi({'asdfg-dfd','-dfd-dfd-','qasfdfdaq'},'-'),{6,[1,5,9],zeros(1,0)}) | |
1420 %!assert(regexpi({'asdfg-dfd';'-dfd-dfd-';'qasfdfdaq'},{'-';'f';'q'}),{6;[3,7];[1,9]}) | |
1421 %!assert(regexpi('Strings',{'t','s'}),{2,[1,7]}) | |
1422 | |
5582 | 1423 */ |
1424 | |
6361 | 1425 |
1426 static octave_value | |
1427 octregexprep (const octave_value_list &args, const std::string &nm) | |
5785 | 1428 { |
6361 | 1429 octave_value retval; |
5785 | 1430 int nargin = args.length(); |
1431 | |
1432 // Make sure we have string,pattern,replacement | |
1433 const std::string buffer = args(0).string_value (); | |
1434 if (error_state) return retval; | |
1435 const std::string pattern = args(1).string_value (); | |
1436 if (error_state) return retval; | |
1437 const std::string replacement = args(2).string_value (); | |
1438 if (error_state) return retval; | |
1439 | |
1440 // Pack options excluding 'tokenize' and various output | |
1441 // reordering strings into regexp arg list | |
1442 octave_value_list regexpargs(nargin-1,octave_value()); | |
1443 regexpargs(0) = args(0); | |
1444 regexpargs(1) = args(1); | |
1445 int len=2; | |
1446 for (int i = 3; i < nargin; i++) | |
1447 { | |
1448 const std::string opt = args(i).string_value(); | |
1449 if (opt != "tokenize" && opt != "start" && opt != "end" | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1450 && opt != "tokenextents" && opt != "match" && opt != "tokens" |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1451 && opt != "names" && opt != "warnings") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1452 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1453 regexpargs(len++) = args(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1454 } |
5785 | 1455 } |
1456 regexpargs.resize(len); | |
1457 | |
1458 // Identify replacement tokens; build a vector of group numbers in | |
1459 // the replacement string so that we can quickly calculate the size | |
1460 // of the replacement. | |
1461 int tokens = 0; | |
1462 for (size_t i=1; i < replacement.size(); i++) | |
1463 { | |
1464 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1465 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1466 tokens++, i++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1467 } |
5785 | 1468 } |
1469 std::vector<int> token(tokens); | |
1470 int kk = 0; | |
1471 for (size_t i = 1; i < replacement.size(); i++) | |
1472 { | |
1473 if (replacement[i-1]=='$' && isdigit(replacement[i])) | |
10154
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 token[kk++] = replacement[i]-'0'; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1476 i++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1477 } |
5785 | 1478 } |
1479 | |
1480 // Perform replacement | |
1481 std::string rep; | |
1482 if (tokens > 0) | |
1483 { | |
1484 std::list<regexp_elem> lst; | |
1485 string_vector named; | |
1486 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1487 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1488 int sz = octregexp_list (regexpargs, nm , false, lst, named, nopts, once); |
5785 | 1489 |
1490 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1491 return retval; |
5785 | 1492 if (sz == 0) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1493 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1494 retval = args(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1495 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1496 } |
5785 | 1497 |
1498 // Determine replacement length | |
1499 const size_t replen = replacement.size() - 2*tokens; | |
1500 int delta = 0; | |
1501 const_iterator p = lst.begin(); | |
1502 for (int i = 0; i < sz; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1503 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1504 OCTAVE_QUIT; |
5785 | 1505 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1506 const Matrix pairs(p->te); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1507 size_t pairlen = 0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1508 for (int j = 0; j < tokens; j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1509 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1510 if (token[j] == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1511 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
|
1512 else if (token[j] <= pairs.rows()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1513 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
|
1514 pairs(token[j]-1,0)) + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1515 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1516 delta += static_cast<int>(replen + pairlen) - |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1517 static_cast<int>(p->e - p->s + 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1518 p++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1519 } |
5785 | 1520 |
1521 // Build replacement string | |
1522 rep.reserve(buffer.size()+delta); | |
1523 size_t from = 0; | |
1524 p = lst.begin(); | |
1525 for (int i=0; i < sz; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1526 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1527 OCTAVE_QUIT; |
5785 | 1528 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1529 const Matrix pairs(p->te); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1530 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
|
1531 from = static_cast<size_t>(p->e - 1) + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1532 for (size_t j = 1; j < replacement.size(); j++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1533 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1534 if (replacement[j-1]=='$' && isdigit(replacement[j])) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1535 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1536 int k = replacement[j]-'0'; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1537 if (k == 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1538 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1539 // replace with entire match |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1540 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
|
1541 static_cast<size_t>(p->e - p->s) + 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1542 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1543 else if (k <= pairs.rows()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1544 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1545 // replace with group capture |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1546 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
|
1547 static_cast<size_t>(pairs(k-1,1) - |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1548 pairs(k-1,0))+1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1549 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1550 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1551 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1552 // replace with nothing |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1553 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1554 j++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1555 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1556 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1557 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1558 rep.append(1,replacement[j-1]); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1559 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1560 if (j+1 == replacement.size()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1561 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1562 rep.append(1,replacement[j]); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1563 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1564 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1565 p++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1566 } |
5785 | 1567 rep.append(&buffer[from],buffer.size()-from); |
1568 } | |
1569 else | |
1570 { | |
1571 std::list<regexp_elem> lst; | |
1572 string_vector named; | |
1573 int nopts; | |
7893
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1574 bool once; |
eb9ccb44ea41
make regexp(...,'once') matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7520
diff
changeset
|
1575 int sz = octregexp_list (regexpargs, nm, false, lst, named, nopts, once); |
5785 | 1576 |
1577 if (error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1578 return retval; |
5785 | 1579 if (sz == 0) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1580 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1581 retval = args(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1582 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1583 } |
5785 | 1584 |
1585 // Determine replacement length | |
1586 const size_t replen = replacement.size(); | |
1587 int delta = 0; | |
1588 const_iterator p = lst.begin(); | |
1589 for (int i = 0; i < sz; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1590 { |
5785 | 1591 OCTAVE_QUIT; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1592 delta += static_cast<int>(replen) - |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1593 static_cast<int>(p->e - p->s + 1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1594 p++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1595 } |
5785 | 1596 |
1597 // Build replacement string | |
1598 rep.reserve(buffer.size()+delta); | |
1599 size_t from = 0; | |
1600 p = lst.begin(); | |
1601 for (int i=0; i < sz; i++) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1602 { |
5785 | 1603 OCTAVE_QUIT; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1604 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
|
1605 from = static_cast<size_t>(p->e - 1) + 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1606 rep.append(replacement); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1607 p++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1608 } |
5785 | 1609 rep.append(&buffer[from],buffer.size()-from); |
1610 } | |
1611 | |
6361 | 1612 retval = rep; |
1613 return retval; | |
1614 } | |
1615 | |
6549 | 1616 DEFUN_DLD (regexprep, args, , |
6361 | 1617 "-*- texinfo -*-\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1618 @deftypefn {Loadable Function} {@var{outstr} =} regexprep (@var{string}, @var{pat}, @var{repstr})\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1619 @deftypefnx {Loadable Function} {@var{outstr} =} regexprep (@var{string}, @var{pat}, @var{repstr}, \"@var{opt1}\", @dots{})\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1620 Replace occurrences of pattern @var{pat} in @var{string} with @var{repstr}.\n\ |
6361 | 1621 \n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1622 The pattern is a regular expression as documented for @code{regexp}.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1623 @xref{doc-regexp,,regexp}.\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1624 \n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1625 The replacement string may contain @code{$i}, which substitutes\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1626 for the ith set of parentheses in the match string. For example,\n\ |
10840 | 1627 \n\ |
6361 | 1628 @example\n\ |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1629 regexprep(\"Bill Dunn\",'(\\w+) (\\w+)','$2, $1')\n\ |
6361 | 1630 @end example\n\ |
10840 | 1631 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
1632 @noindent\n\ |
6361 | 1633 returns \"Dunn, Bill\"\n\ |
1634 \n\ | |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1635 Options in addition to those of @code{regexp} are\n\ |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1636 \n\ |
6361 | 1637 @table @samp\n\ |
1638 \n\ | |
1639 @item once\n\ | |
7001 | 1640 Replace only the first occurrence of @var{pat} in the result.\n\ |
6361 | 1641 \n\ |
1642 @item warnings\n\ | |
1643 This option is present for compatibility but is ignored.\n\ | |
1644 \n\ | |
1645 @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
|
1646 @seealso{regexp,regexpi,strrep}\n\ |
6361 | 1647 @end deftypefn") |
1648 { | |
1649 octave_value_list retval; | |
1650 int nargin = args.length(); | |
1651 | |
1652 if (nargin < 3) | |
1653 { | |
1654 print_usage (); | |
1655 return retval; | |
1656 } | |
1657 | |
1658 if (args(0).is_cell() || args(1).is_cell() || args(2).is_cell()) | |
1659 { | |
1660 Cell str; | |
1661 Cell pat; | |
1662 Cell rep; | |
6495 | 1663 dim_vector dv0; |
1664 dim_vector dv1(1,1); | |
6361 | 1665 |
1666 if (args(0).is_cell()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1667 str = args(0).cell_value(); |
6361 | 1668 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1669 str = Cell (args(0)); |
6361 | 1670 |
1671 if (args(1).is_cell()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1672 pat = args(1).cell_value(); |
6361 | 1673 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1674 pat = Cell (args(1)); |
6361 | 1675 |
1676 if (args(2).is_cell()) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1677 rep = args(2).cell_value(); |
6361 | 1678 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1679 rep = Cell (args(2)); |
6361 | 1680 |
6495 | 1681 dv0 = str.dims(); |
1682 if (pat.numel() != 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1683 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1684 dv1 = pat.dims(); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1685 if (rep.numel() != 1 && dv1 != rep.dims()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1686 error ("regexprep: Inconsistent cell array dimensions"); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1687 } |
6361 | 1688 else if (rep.numel() != 1) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1689 dv1 = rep.dims(); |
6361 | 1690 |
1691 if (!error_state) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1692 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1693 Cell ret (dv0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1694 octave_value_list new_args = args; |
6361 | 1695 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1696 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
|
1697 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1698 new_args(0) = str(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1699 if (pat.numel() == 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1700 new_args(1) = pat(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1701 if (rep.numel() == 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1702 new_args(2) = rep(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1703 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
|
1704 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1705 if (pat.numel() != 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1706 new_args(1) = pat(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1707 if (rep.numel() != 1) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1708 new_args(2) = rep(j); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1709 new_args(0) = octregexprep (new_args, "regexprep"); |
6361 | 1710 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1711 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1712 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1713 } |
6361 | 1714 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1715 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1716 break; |
6495 | 1717 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1718 ret(i) = new_args(0); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1719 } |
6361 | 1720 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1721 if (!error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1722 retval = octave_value (ret); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9064
diff
changeset
|
1723 } |
6361 | 1724 } |
1725 else | |
1726 retval = octregexprep (args, "regexprep"); | |
1727 | |
5785 | 1728 return retval; |
1729 } | |
1730 | |
1731 /* | |
1732 %!test # Replace with empty | |
1733 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1734 %! t = regexprep(xml,'<[!?][^>]*>',''); | |
1735 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1736 | |
1737 %!test # Replace with non-empty | |
1738 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1739 %! t = regexprep(xml,'<[!?][^>]*>','?'); | |
1740 %! assert(t,'? <tag v="hello">some stuff?</tag>') | |
1741 | |
1742 %!test # Check that 'tokenize' is ignored | |
1743 %! xml = '<!-- This is some XML --> <tag v="hello">some stuff<!-- sample tag--></tag>'; | |
1744 %! t = regexprep(xml,'<[!?][^>]*>','','tokenize'); | |
1745 %! assert(t,' <tag v="hello">some stuff</tag>') | |
1746 | |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1747 ## Test capture replacement |
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1748 %!testif HAVE_PCRE |
7242 | 1749 %! data = "Bob Smith\nDavid Hollerith\nSam Jenkins"; |
1750 %! result = "Smith, Bob\nHollerith, David\nJenkins, Sam"; | |
1751 %! t = regexprep(data,'(?m)^(\w+)\s+(\w+)$','$2, $1'); | |
1752 %! assert(t,result) | |
5785 | 1753 |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
11025
diff
changeset
|
1754 ## Return the original if no match |
5785 | 1755 %!assert(regexprep('hello','world','earth'),'hello') |
1756 | |
1757 ## Test a general replacement | |
1758 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_"), "a_b_c_d_e_f_g"); | |
1759 | |
1760 ## Make sure it works at the beginning and end | |
1761 %!assert(regexprep("a[b]c{d}e-f=g", "a", "_"), "_[b]c{d}e-f=g"); | |
1762 %!assert(regexprep("a[b]c{d}e-f=g", "g", "_"), "a[b]c{d}e-f=_"); | |
1763 | |
1764 ## Options | |
1765 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Za-z0-9_]", "_", "once"), "a_b]c{d}e-f=g"); | |
1766 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "ignorecase"), "a_b_c_d_e_f_g"); | |
1767 | |
1768 ## Option combinations | |
1769 %!assert(regexprep("a[b]c{d}e-f=g", "[^A-Z0-9_]", "_", "once", "ignorecase"), "a_b]c{d}e-f=g"); | |
1770 | |
1771 ## End conditions on replacement | |
1772 %!assert(regexprep("abc","(b)",".$1"),"a.bc"); | |
1773 %!assert(regexprep("abc","(b)","$1"),"abc"); | |
1774 %!assert(regexprep("abc","(b)","$1."),"ab.c"); | |
1775 %!assert(regexprep("abc","(b)","$1.."),"ab..c"); | |
1776 | |
6361 | 1777 ## Test cell array arguments |
6503 | 1778 %!assert(regexprep("abc",{"b","a"},"?"),{"??c"}) |
6361 | 1779 %!assert(regexprep({"abc","cba"},"b","?"),{"a?c","c?a"}) |
6503 | 1780 %!assert(regexprep({"abc","cba"},{"b","a"},{"?","!"}),{"!?c","c?!"}) |
6361 | 1781 |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1782 # Nasty lookbehind expression |
11025
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1783 %!testif HAVE_PCRE |
df2152514429
Update docstrings for regular expression functions
Rik <octave@nomad.inbox5.com>
parents:
11018
diff
changeset
|
1784 %! assert(regexprep('x^(-1)+y(-1)+z(-1)=0','(?<=[a-z]+)\(\-[1-9]*\)','_minus1'),'x^(-1)+y_minus1+z_minus1=0') |
8093
dcc31f473596
Treat PCRE lookbehind operators in a manner that is approximately correct
David Bateman <dbateman@free.fr>
parents:
8021
diff
changeset
|
1785 |
5785 | 1786 */ |