annotate liboctave/regex-match.cc @ 12312:b10ea6efdc58 release-3-4-x ss-3-3-91

version is now 3.3.91
author John W. Eaton <jwe@octave.org>
date Mon, 31 Jan 2011 08:36:58 -0500
parents 12df7854fa7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
1 /*
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
2
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 10350
diff changeset
3 Copyright (C) 2008-2011 David Bateman
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
4
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 This file is part of Octave.
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
6
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 option) any later version.
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
11
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 for more details.
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
16
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
20
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
21 */
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
22
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
24 #include <config.h>
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
25 #endif
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
26
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
27 #include <vector>
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
28 #include <iostream>
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
29 #include <string>
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
30
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 #include "regex-match.h"
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 #include "str-vec.h"
8377
25bc2d31e1bf improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents: 7955
diff changeset
33 #include "oct-locbuf.h"
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
34
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
35 regex_match&
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 regex_match::operator = (const regex_match& gm)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
37 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
38 if (this != &gm)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
39 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 #if HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 for (int i = 0; i < pat.length (); i++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
42 regfree (compiled +i);
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 delete [] compiled;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 #endif
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 pat = gm.pat;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 case_insen = gm.case_insen;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 init ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
48 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
49 return *this;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
51
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 regex_match::~regex_match (void)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 #if HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 for (int i = 0; i < pat.length (); i++)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
56 regfree (compiled +i);
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 delete [] compiled;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 #endif
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
59 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
60
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
61
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
62 void
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
63 regex_match::set_pattern (const std::string& p)
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
64 {
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 #if HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 for (int i = 0; i < pat.length (); i++)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 regfree (compiled +i);
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 delete [] compiled;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
70 pat = p;
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
71 init ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
73
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
74 void
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
75 regex_match::set_pattern (const string_vector& p)
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
76 {
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
77 #if HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
78 for (int i = 0; i < pat.length (); i++)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
79 regfree (compiled +i);
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
80 delete [] compiled;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
81 #endif
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
82 pat = p;
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
83 init ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
84 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
85
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
86 void
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
87 regex_match::init (void)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
88 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
89 #ifdef HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
90 int npat = pat.length ();
7955
645ced9fa838 regex-match.cc (regex_match::init): initialize err to 0
John W. Eaton <jwe@octave.org>
parents: 7784
diff changeset
91 int err = 0;
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
92 int i;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
93
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
94 compiled = new regex_t [npat];
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
95
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
96 for (i = 0; i < npat; i++)
7784
d3a7882fa0b3 style fixes
John W. Eaton <jwe@octave.org>
parents: 7779
diff changeset
97 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
98 err = regcomp (compiled + i, pat(i).c_str (),
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
99 (REG_NOSUB | REG_EXTENDED |
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
100 (case_insen ? REG_ICASE : 0)));
7784
d3a7882fa0b3 style fixes
John W. Eaton <jwe@octave.org>
parents: 7779
diff changeset
101 if (err)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
102 break;
7784
d3a7882fa0b3 style fixes
John W. Eaton <jwe@octave.org>
parents: 7779
diff changeset
103 }
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
104
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
105 if (err)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
106 {
7784
d3a7882fa0b3 style fixes
John W. Eaton <jwe@octave.org>
parents: 7779
diff changeset
107 int len = regerror (err, compiled + i, 0, 0);
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
108 OCTAVE_LOCAL_BUFFER (char, errmsg, len);
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
109 regerror(err, compiled + i, errmsg, len);
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
110 (*current_liboctave_error_handler) ("%s in pattern (%s)", errmsg,
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
111 pat(i).c_str());
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
112
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
113 for (int j = 0; j < i + 1; j++)
10314
07ebe522dac2 untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents: 10158
diff changeset
114 regfree (compiled + j);
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
115 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
116 #else
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
117 (*current_liboctave_error_handler)
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
118 ("regex not available in this version of Octave");
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
119 #endif
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
120 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
121
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
122 bool
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
123 regex_match::match (const std::string& s)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
124 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
125 #if HAVE_REGEX
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
126 int npat = pat.length ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
127
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
128 const char *str = s.c_str ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
129
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
130 for (int i = 0; i < npat; i++)
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11570
diff changeset
131 if (regexec (compiled + i, str, 0, 0, 0) == 0)
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
132 return true;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
133 #endif
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
134
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
135 return false;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
136 }
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
137
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
138 Array<bool>
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
139 regex_match::match (const string_vector& s)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
140 {
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
141 int n = s.length ();
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
142
11570
57632dea2446 attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
143 Array<bool> retval (dim_vector (n, 1));
7779
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
144
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
145 for (int i = 0; i < n; i++)
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
146 retval(i) = match (s[i]);
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
147
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
148 return retval;
791231dac333 Add regexp matching to Fwho and Fclear
David Bateman <dbateman@free.fr>
parents:
diff changeset
149 }