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