GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
lo-regexp.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012 John W. Eaton
4 Copyright (C) 2005-2013 David Bateman
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_lo_regexp_h)
25 #define octave_lo_regexp_h 1
26 
27 #include <list>
28 #include <sstream>
29 #include <string>
30 
31 #include "Array.h"
32 #include "Matrix.h"
33 #include "base-list.h"
34 #include "str-vec.h"
35 
36 class
37 OCTAVE_API
38 regexp
39 {
40 public:
41 
42  class opts;
43  class match_data;
44 
45  regexp (const std::string& pat = "",
46  const regexp::opts& opt = regexp::opts (),
47  const std::string& w = "regexp")
48  : pattern (pat), options (opt), data (0), named_pats (),
49  nnames (0), named_idx (), who (w)
50  {
51  compile_internal ();
52  }
53 
54  regexp (const regexp& rx)
55  : pattern (rx.pattern), data (rx.data), named_pats (rx.named_pats),
56  nnames (rx.nnames), named_idx (rx.named_idx)
57  { }
58 
59  regexp& operator = (const regexp& rx)
60  {
61  if (this != &rx)
62  {
63  pattern = rx.pattern;
64  data = rx.data;
65  named_pats = rx.named_pats;
66  nnames = rx.nnames;
67  named_idx = rx.named_idx;
68  }
69 
70  return *this;
71  }
72 
73  ~regexp (void) { free (); }
74 
75  void compile (const std::string& pat,
76  const regexp::opts& opt = regexp::opts ())
77  {
78  pattern = pat;
79  options = opt;
80  compile_internal ();
81  }
82 
83  match_data match (const std::string& buffer);
84 
85  bool is_match (const std::string& buffer);
86 
87  Array<bool> is_match (const string_vector& buffer);
88 
89  std::string replace (const std::string& buffer,
90  const std::string& replacement);
91 
92  class opts
93  {
94  public:
95 
96  opts (void)
97  : x_case_insensitive (false), x_dotexceptnewline (false),
98  x_emptymatch (false), x_freespacing (false), x_lineanchors (false),
99  x_once (false) { }
100 
101  opts (const opts& o)
102  : x_case_insensitive (o.x_case_insensitive),
103  x_dotexceptnewline (o.x_dotexceptnewline),
104  x_emptymatch (o.x_emptymatch),
105  x_freespacing (o.x_freespacing),
106  x_lineanchors (o.x_lineanchors),
107  x_once (o.x_once)
108  { }
109 
110  opts& operator = (const opts& o)
111  {
112  if (this != &o)
113  {
114  x_case_insensitive = o.x_case_insensitive;
115  x_dotexceptnewline = o.x_dotexceptnewline;
116  x_emptymatch = o.x_emptymatch;
117  x_freespacing = o.x_freespacing;
118  x_lineanchors = o.x_lineanchors;
119  x_once = o.x_once;
120  }
121 
122  return *this;
123  }
124 
125  ~opts (void) { }
126 
127  void case_insensitive (bool val) { x_case_insensitive = val; }
128  void dotexceptnewline (bool val) { x_dotexceptnewline = val; }
129  void emptymatch (bool val) { x_emptymatch = val; }
130  void freespacing (bool val) { x_freespacing = val; }
131  void lineanchors (bool val) { x_lineanchors = val; }
132  void once (bool val) { x_once = val; }
133 
134  bool case_insensitive (void) const { return x_case_insensitive; }
135  bool dotexceptnewline (void) const { return x_dotexceptnewline; }
136  bool emptymatch (void) const { return x_emptymatch; }
137  bool freespacing (void) const { return x_freespacing; }
138  bool lineanchors (void) const { return x_lineanchors; }
139  bool once (void) const { return x_once; }
140 
141  private:
142 
148  bool x_once;
149  };
150 
152  {
153  public:
154 
156  const std::string& ms, const Matrix& te,
157  double s, double e)
158  : x_match_string (ms), x_named_tokens (nt), x_tokens (t),
159  x_token_extents (te), x_start (s), x_end (e)
160  { }
161 
163  : x_match_string (a.x_match_string),
164  x_named_tokens (a.x_named_tokens), x_tokens (a.x_tokens),
165  x_token_extents (a.x_token_extents),
166  x_start (a.x_start), x_end (a.x_end)
167  { }
168 
169  std::string match_string (void) const { return x_match_string; }
170  string_vector named_tokens (void) const { return x_named_tokens; }
171  string_vector tokens (void) const { return x_tokens; }
172  Matrix token_extents (void) const { return x_token_extents; }
173  double start (void) const { return x_start; }
174  double end (void) const { return x_end; }
175 
176  private:
177 
178  std::string x_match_string;
182  double x_start;
183  double x_end;
184  };
185 
186  class match_data : public octave_base_list<match_element>
187  {
188  public:
189 
190  match_data (void)
191  : octave_base_list<match_element> (), named_pats ()
192  { }
193 
194  match_data (const std::list<match_element>& l, const string_vector& np)
195  : octave_base_list<match_element> (l), named_pats (np)
196  { }
197 
198  match_data (const match_data& rx_lst)
199  : octave_base_list<match_element> (rx_lst),
200  named_pats (rx_lst.named_pats)
201  { }
202 
203  match_data& operator = (const match_data& rx_lst)
204  {
205  if (this != &rx_lst)
206  {
208  named_pats = rx_lst.named_pats;
209  }
210 
211  return *this;
212  }
213 
214  ~match_data (void) { }
215 
216  string_vector named_patterns (void) { return named_pats; }
217 
218  private:
219 
221  };
222 
223 private:
224 
225  // The pattern we've been asked to match.
226  std::string pattern;
227 
229 
230  // Internal data describing the regular expression.
231  void *data;
232 
233  std::string m;
235  int nnames;
237  std::string who;
238 
239  void free (void);
240 
241  void compile_internal (void);
242 };
243 
244 inline regexp::match_data
245 regexp_match (const std::string& pat,
246  const std::string& buffer,
247  const regexp::opts& opt = regexp::opts (),
248  const std::string& who = "regexp")
249 {
250  regexp rx (pat, opt, who);
251 
252  return rx.match (buffer);
253 }
254 
255 inline bool
256 is_regexp_match (const std::string& pat,
257  const std::string& buffer,
258  const regexp::opts& opt = regexp::opts (),
259  const std::string& who = "regexp")
260 {
261  regexp rx (pat, opt, who);
262 
263  return rx.is_match (buffer);
264 }
265 
266 inline Array<bool>
267 is_regexp_match (const std::string& pat,
268  const string_vector& buffer,
269  const regexp::opts& opt = regexp::opts (),
270  const std::string& who = "regexp")
271 {
272  regexp rx (pat, opt, who);
273 
274  return rx.is_match (buffer);
275 }
276 
277 inline std::string
278 regexp_replace (const std::string& pat,
279  const std::string& buffer,
280  const std::string& replacement,
281  const regexp::opts& opt = regexp::opts (),
282  const std::string& who = "regexp")
283 {
284  regexp rx (pat, opt, who);
285 
286  return rx.replace (buffer, replacement);
287 }
288 
289 #endif