GNU Octave 7.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-2022 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
40namespace octave
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_data (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 double m_start;
192 double m_end;
193 };
194
195 class match_data : public base_list<match_element>
196 {
197 public:
198
200 : base_list<match_element> (), m_named_pats ()
201 { }
202
203 match_data (const std::list<match_element>& l, const string_vector& np)
204 : base_list<match_element> (l), m_named_pats (np)
205 { }
206
207 match_data (const match_data&) = default;
208
209 match_data& operator = (const match_data&) = default;
210
211 ~match_data (void) = default;
212
213 string_vector named_patterns (void) const { return m_named_pats; }
214
215 private:
216
218 };
219
220 private:
221
222 // The pattern we've been asked to match.
223 std::string m_pattern;
224
226
227 // Internal data describing the regular expression.
228 void *m_data;
229
233 std::string m_who;
234
235 void free (void);
236
237 void compile_internal (void);
238 };
239}
240
241#endif
Definition: dMatrix.h:42
string_vector named_patterns(void) const
Definition: lo-regexp.h:213
match_data(const match_data &)=default
string_vector m_named_pats
Definition: lo-regexp.h:217
match_data(const std::list< match_element > &l, const string_vector &np)
Definition: lo-regexp.h:203
match_element(const match_element &)=default
Matrix token_extents(void) const
Definition: lo-regexp.h:181
string_vector tokens(void) const
Definition: lo-regexp.h:180
string_vector m_named_tokens
Definition: lo-regexp.h:188
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 start(void) const
Definition: lo-regexp.h:182
double end(void) const
Definition: lo-regexp.h:183
string_vector named_tokens(void) const
Definition: lo-regexp.h:179
std::string match_string(void) const
Definition: lo-regexp.h:178
void lineanchors(bool val)
Definition: lo-regexp.h:143
bool once(void) const
Definition: lo-regexp.h:151
void case_insensitive(bool val)
Definition: lo-regexp.h:139
bool emptymatch(void) const
Definition: lo-regexp.h:148
bool lineanchors(void) const
Definition: lo-regexp.h:150
void emptymatch(bool val)
Definition: lo-regexp.h:141
void once(bool val)
Definition: lo-regexp.h:144
bool freespacing(void) const
Definition: lo-regexp.h:149
void freespacing(bool val)
Definition: lo-regexp.h:142
void dotexceptnewline(bool val)
Definition: lo-regexp.h:140
~opts(void)=default
bool dotexceptnewline(void) const
Definition: lo-regexp.h:147
bool case_insensitive(void) const
Definition: lo-regexp.h:146
opts(const opts &)=default
std::string m_pattern
Definition: lo-regexp.h:223
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
regexp(const std::string &pat="", const regexp::opts &opt=regexp::opts(), const std::string &w="regexp")
Definition: lo-regexp.h:51
std::string replace(const std::string &buffer, const std::string &replacement) const
Definition: lo-regexp.cc:468
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
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
match_data match(const std::string &buffer) const
Definition: lo-regexp.cc:250
~regexp(void)
Definition: lo-regexp.h:64
bool is_match(const std::string &buffer) const
Definition: lo-regexp.cc:440
string_vector m_named_pats
Definition: lo-regexp.h:230
Array< int > m_named_idx
Definition: lo-regexp.h:232
regexp(const regexp &)=default
void compile(const std::string &pat, const regexp::opts &opt=regexp::opts())
Definition: lo-regexp.h:66
void * m_data
Definition: lo-regexp.h:228
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
std::string m_who
Definition: lo-regexp.h:233
#define OCTAVE_API
Definition: main.in.cc:55
std::complex< double > w(std::complex< double > z, double relerr=0)
void free(void *)