GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-regexp.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2005-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_lo_regexp_h)
27 #define octave_lo_regexp_h 1
28 
29 #include "octave-config.h"
30 
31 #include <list>
32 #include <sstream>
33 #include <string>
34 
35 #include "Array.h"
36 #include "Matrix.h"
37 #include "base-list.h"
38 #include "str-vec.h"
39 
41 
42 class
44 regexp
45 {
46 public:
47 
48  class opts;
49  class match_data;
50 
51  regexp (const std::string& pat = "",
52  const regexp::opts& opt = regexp::opts (),
53  const std::string& w = "regexp")
54  : m_pattern (pat), m_options (opt), m_code (nullptr), m_named_pats (),
55  m_names (0), m_named_idx (), m_who (w)
56  {
57  compile_internal ();
58  }
59 
60  regexp (const regexp&) = default;
61 
62  regexp& operator = (const regexp& rx) = default;
63 
64  ~regexp (void) { free (); }
65 
66  void compile (const std::string& pat,
67  const regexp::opts& opt = regexp::opts ())
68  {
69  m_pattern = pat;
70  m_options = opt;
71  compile_internal ();
72  }
73 
74  match_data match (const std::string& buffer) const;
75 
76  bool is_match (const std::string& buffer) const;
77 
78  Array<bool> is_match (const string_vector& buffer) const;
79 
80  std::string replace (const std::string& buffer,
81  const std::string& replacement) const;
82 
83  static match_data
84  match (const std::string& pat, const std::string& buffer,
85  const regexp::opts& opt = regexp::opts (),
86  const std::string& who = "regexp")
87  {
88  regexp rx (pat, opt, who);
89 
90  return rx.match (buffer);
91  }
92 
93  static bool
94  is_match (const std::string& pat, const std::string& buffer,
95  const regexp::opts& opt = regexp::opts (),
96  const std::string& who = "regexp")
97  {
98  regexp rx (pat, opt, who);
99 
100  return rx.is_match (buffer);
101  }
102 
103  static Array<bool>
104  is_match (const std::string& pat, const string_vector& buffer,
105  const regexp::opts& opt = regexp::opts (),
106  const std::string& who = "regexp")
107  {
108  regexp rx (pat, opt, who);
109 
110  return rx.is_match (buffer);
111  }
112 
113  static std::string
114  replace (const std::string& pat, const std::string& buffer,
115  const std::string& replacement,
116  const regexp::opts& opt = regexp::opts (),
117  const std::string& who = "regexp")
118  {
119  regexp rx (pat, opt, who);
120 
121  return rx.replace (buffer, replacement);
122  }
123 
124  class opts
125  {
126  public:
127 
128  opts (void)
129  : m_case_insensitive (false), m_dotexceptnewline (false),
130  m_emptymatch (false), m_freespacing (false), m_lineanchors (false),
131  m_once (false) { }
132 
133  opts (const opts&) = default;
134 
135  opts& operator = (const opts&) = default;
136 
137  ~opts (void) = default;
138 
139  void case_insensitive (bool val) { m_case_insensitive = val; }
140  void dotexceptnewline (bool val) { m_dotexceptnewline = val; }
141  void emptymatch (bool val) { m_emptymatch = val; }
142  void freespacing (bool val) { m_freespacing = val; }
143  void lineanchors (bool val) { m_lineanchors = val; }
144  void once (bool val) { m_once = val; }
145 
146  bool case_insensitive (void) const { return m_case_insensitive; }
147  bool dotexceptnewline (void) const { return m_dotexceptnewline; }
148  bool emptymatch (void) const { return m_emptymatch; }
149  bool freespacing (void) const { return m_freespacing; }
150  bool lineanchors (void) const { return m_lineanchors; }
151  bool once (void) const { return m_once; }
152 
153  private:
154 
160  bool m_once;
161  };
162 
164  {
165  public:
166 
168  const std::string& ms, const Matrix& te,
169  double s, double e)
170  : m_match_string (ms), m_named_tokens (nt), m_tokens (t),
171  m_token_extents (te), m_start (s), m_end (e)
172  { }
173 
174  match_element (const match_element&) = default;
175 
176  match_element& operator = (const match_element&) = default;
177 
178  std::string match_string (void) const { return m_match_string; }
179  string_vector named_tokens (void) const { return m_named_tokens; }
180  string_vector tokens (void) const { return m_tokens; }
181  Matrix token_extents (void) const { return m_token_extents; }
182  double start (void) const { return m_start; }
183  double end (void) const { return m_end; }
184 
185  private:
186 
187  std::string m_match_string;
191 
192  // FIXME: Are these values declared as double because that's what
193  // Octave interpreter functions will store? Should they be int or
194  // size_t instead?
195  double m_start;
196  double m_end;
197  };
198 
199  class match_data : public base_list<match_element>
200  {
201  public:
202 
203  match_data (void)
204  : base_list<match_element> (), m_named_pats ()
205  { }
206 
207  match_data (const std::list<match_element>& l, const string_vector& np)
208  : base_list<match_element> (l), m_named_pats (np)
209  { }
210 
211  match_data (const match_data&) = default;
212 
213  match_data& operator = (const match_data&) = default;
214 
215  ~match_data (void) = default;
216 
217  string_vector named_patterns (void) const { return m_named_pats; }
218 
219  private:
220 
222  };
223 
224 private:
225 
226  // The pattern we've been asked to match.
227  std::string m_pattern;
228 
230 
231  // Internal data describing the regular expression.
232  void *m_code;
233 
235  int m_names;
237  std::string m_who;
238 
239  void free (void);
240 
241  void compile_internal (void);
242 };
243 
245 
246 #endif
OCTAVE_END_NAMESPACE(octave)
Definition: dMatrix.h:42
~match_data(void)=default
match_data(const std::list< match_element > &l, const string_vector &np)
Definition: lo-regexp.h:207
string_vector m_named_pats
Definition: lo-regexp.h:221
string_vector named_patterns(void) const
Definition: lo-regexp.h:217
match_data(const match_data &)=default
double start(void) const
Definition: lo-regexp.h:182
string_vector m_named_tokens
Definition: lo-regexp.h:188
match_element(const match_element &)=default
match_element(const string_vector &nt, const string_vector &t, const std::string &ms, const Matrix &te, double s, double e)
Definition: lo-regexp.h:167
double end(void) const
Definition: lo-regexp.h:183
std::string m_match_string
Definition: lo-regexp.h:187
Matrix token_extents(void) const
Definition: lo-regexp.h:181
string_vector tokens(void) const
Definition: lo-regexp.h:180
std::string match_string(void) const
Definition: lo-regexp.h:178
string_vector named_tokens(void) const
Definition: lo-regexp.h:179
string_vector m_tokens
Definition: lo-regexp.h:189
void dotexceptnewline(bool val)
Definition: lo-regexp.h:140
void lineanchors(bool val)
Definition: lo-regexp.h:143
bool m_lineanchors
Definition: lo-regexp.h:159
void case_insensitive(bool val)
Definition: lo-regexp.h:139
~opts(void)=default
bool freespacing(void) const
Definition: lo-regexp.h:149
bool case_insensitive(void) const
Definition: lo-regexp.h:146
bool dotexceptnewline(void) const
Definition: lo-regexp.h:147
bool m_freespacing
Definition: lo-regexp.h:158
void freespacing(bool val)
Definition: lo-regexp.h:142
bool once(void) const
Definition: lo-regexp.h:151
bool m_dotexceptnewline
Definition: lo-regexp.h:156
opts(const opts &)=default
bool lineanchors(void) const
Definition: lo-regexp.h:150
bool emptymatch(void) const
Definition: lo-regexp.h:148
bool m_emptymatch
Definition: lo-regexp.h:157
void emptymatch(bool val)
Definition: lo-regexp.h:141
bool m_case_insensitive
Definition: lo-regexp.h:155
void once(bool val)
Definition: lo-regexp.h:144
opts(void)
Definition: lo-regexp.h:128
void compile(const std::string &pat, const regexp::opts &opt=regexp::opts())
Definition: lo-regexp.h:66
int m_names
Definition: lo-regexp.h:235
static std::string replace(const std::string &pat, const std::string &buffer, const std::string &replacement, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:114
Array< int > m_named_idx
Definition: lo-regexp.h:236
~regexp(void)
Definition: lo-regexp.h:64
std::string m_who
Definition: lo-regexp.h:237
std::string replace(const std::string &buffer, const std::string &replacement) const
Definition: lo-regexp.cc:608
opts m_options
Definition: lo-regexp.h:229
std::string m_pattern
Definition: lo-regexp.h:227
regexp(const std::string &pat="", const regexp::opts &opt=regexp::opts(), const std::string &w="regexp")
Definition: lo-regexp.h:51
static bool is_match(const std::string &pat, const std::string &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:94
string_vector m_named_pats
Definition: lo-regexp.h:234
regexp(const regexp &)=default
bool is_match(const std::string &buffer) const
Definition: lo-regexp.cc:580
match_data match(const std::string &buffer) const
Definition: lo-regexp.cc:328
void * m_code
Definition: lo-regexp.h:232
static Array< bool > is_match(const std::string &pat, const string_vector &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:104
static match_data match(const std::string &pat, const std::string &buffer, const regexp::opts &opt=regexp::opts(), const std::string &who="regexp")
Definition: lo-regexp.h:84
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.in.cc:55
std::complex< double > w(std::complex< double > z, double relerr=0)
void free(void *)