annotate src/DLD-FUNCTIONS/regexp.cc @ 10154:40dfc0c99116

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