GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
lo-regexp.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2005-2025 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 "str-vec.h"
38
40
42{
43public:
44
45 class opts;
46 class match_data;
47
48 regexp (const std::string& pat = "",
49 const regexp::opts& opt = regexp::opts (),
50 const std::string& w = "regexp")
51 : m_pattern (pat), m_options (opt), m_code (nullptr), m_named_pats (),
52 m_names (0), m_named_idx (), m_who (w)
53 {
54 compile_internal ();
55 }
56
57 regexp (const regexp&) = default;
58
59 regexp& operator = (const regexp& rx) = default;
60
61 ~regexp () { free (); }
62
63 void compile (const std::string& pat,
64 const regexp::opts& opt = regexp::opts ())
65 {
66 m_pattern = pat;
67 m_options = opt;
68 compile_internal ();
69 }
70
71 match_data match (const std::string& buffer) const;
72
73 bool is_match (const std::string& buffer) const;
74
75 Array<bool> is_match (const string_vector& buffer) const;
76
77 std::string replace (const std::string& buffer,
78 const std::string& replacement) const;
79
80 static match_data
81 match (const std::string& pat, const std::string& buffer,
82 const regexp::opts& opt = regexp::opts (),
83 const std::string& who = "regexp")
84 {
85 regexp rx (pat, opt, who);
86
87 return rx.match (buffer);
88 }
89
90 static bool
91 is_match (const std::string& pat, const std::string& buffer,
92 const regexp::opts& opt = regexp::opts (),
93 const std::string& who = "regexp")
94 {
95 regexp rx (pat, opt, who);
96
97 return rx.is_match (buffer);
98 }
99
100 static Array<bool>
101 is_match (const std::string& pat, const string_vector& buffer,
102 const regexp::opts& opt = regexp::opts (),
103 const std::string& who = "regexp")
104 {
105 regexp rx (pat, opt, who);
106
107 return rx.is_match (buffer);
108 }
109
110 static std::string
111 replace (const std::string& pat, const std::string& buffer,
112 const std::string& replacement,
113 const regexp::opts& opt = regexp::opts (),
114 const std::string& who = "regexp")
115 {
116 regexp rx (pat, opt, who);
117
118 return rx.replace (buffer, replacement);
119 }
120
121 class opts
122 {
123 public:
124
126 : m_case_insensitive (false), m_dotexceptnewline (false),
127 m_emptymatch (false), m_freespacing (false), m_lineanchors (false),
128 m_once (false) { }
129
130 opts (const opts&) = default;
131
132 opts& operator = (const opts&) = default;
133
134 ~opts () = default;
135
136 void case_insensitive (bool val) { m_case_insensitive = val; }
137 void dotexceptnewline (bool val) { m_dotexceptnewline = val; }
138 void emptymatch (bool val) { m_emptymatch = val; }
139 void freespacing (bool val) { m_freespacing = val; }
140 void lineanchors (bool val) { m_lineanchors = val; }
141 void once (bool val) { m_once = val; }
142
143 bool case_insensitive () const { return m_case_insensitive; }
144 bool dotexceptnewline () const { return m_dotexceptnewline; }
145 bool emptymatch () const { return m_emptymatch; }
146 bool freespacing () const { return m_freespacing; }
147 bool lineanchors () const { return m_lineanchors; }
148 bool once () const { return m_once; }
149
150 private:
151
152 bool m_case_insensitive;
153 bool m_dotexceptnewline;
154 bool m_emptymatch;
155 bool m_freespacing;
156 bool m_lineanchors;
157 bool m_once;
158 };
159
161 {
162 public:
163
164 match_element () = delete;
165
167 const std::string& ms, const Matrix& te,
168 double s, double e)
169 : m_match_string (ms), m_named_tokens (nt), m_tokens (t),
170 m_token_extents (te), m_start (s), m_end (e)
171 { }
172
173 OCTAVE_DEFAULT_COPY_MOVE_DELETE (match_element)
174
175 std::string match_string () const { return m_match_string; }
176 string_vector named_tokens () const { return m_named_tokens; }
177 string_vector tokens () const { return m_tokens; }
178 Matrix token_extents () const { return m_token_extents; }
179 double start () const { return m_start; }
180 double end () const { return m_end; }
181
182 private:
183
184 std::string m_match_string;
185 string_vector m_named_tokens;
186 string_vector m_tokens;
187 Matrix m_token_extents;
188
189 // FIXME: Are these values declared as double because that's what
190 // Octave interpreter functions will store? Should they be int or
191 // size_t instead?
192 double m_start;
193 double m_end;
194 };
195
196 class match_data : public std::list<match_element>
197 {
198 public:
199
201 : std::list<match_element> (), m_named_pats ()
202 { }
203
204 match_data (const std::list<match_element>& l, const string_vector& np)
205 : std::list<match_element> (l), m_named_pats (np)
206 { }
207
208 OCTAVE_DEFAULT_COPY_MOVE_DELETE (match_data)
209
210 string_vector named_patterns () const { return m_named_pats; }
211
212 private:
213
214 string_vector m_named_pats;
215 };
216
217private:
218
219 // The pattern we've been asked to match.
220 std::string m_pattern;
221
222 opts m_options;
223
224 // Internal data describing the regular expression.
225 void *m_code;
226
227 string_vector m_named_pats;
228 int m_names;
229 Array<int> m_named_idx;
230 std::string m_who;
231
232 void free ();
233
234 void compile_internal ();
235};
236
237OCTAVE_END_NAMESPACE(octave)
238
239#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
match_data(const std::list< match_element > &l, const string_vector &np)
Definition lo-regexp.h:204
Matrix token_extents() const
Definition lo-regexp.h:178
double end() const
Definition lo-regexp.h:180
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:166
double start() const
Definition lo-regexp.h:179
string_vector tokens() const
Definition lo-regexp.h:177
string_vector named_tokens() const
Definition lo-regexp.h:176
void dotexceptnewline(bool val)
Definition lo-regexp.h:137
void lineanchors(bool val)
Definition lo-regexp.h:140
bool case_insensitive() const
Definition lo-regexp.h:143
bool freespacing() const
Definition lo-regexp.h:146
void case_insensitive(bool val)
Definition lo-regexp.h:136
~opts()=default
void freespacing(bool val)
Definition lo-regexp.h:139
opts(const opts &)=default
bool dotexceptnewline() const
Definition lo-regexp.h:144
bool emptymatch() const
Definition lo-regexp.h:145
void emptymatch(bool val)
Definition lo-regexp.h:138
bool lineanchors() const
Definition lo-regexp.h:147
void once(bool val)
Definition lo-regexp.h:141
bool once() const
Definition lo-regexp.h:148
void compile(const std::string &pat, const regexp::opts &opt=regexp::opts())
Definition lo-regexp.h:63
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:111
std::string replace(const std::string &buffer, const std::string &replacement) const
Definition lo-regexp.cc:610
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:101
regexp(const std::string &pat="", const regexp::opts &opt=regexp::opts(), const std::string &w="regexp")
Definition lo-regexp.h:48
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:91
regexp(const regexp &)=default
bool is_match(const std::string &buffer) const
Definition lo-regexp.cc:582
match_data match(const std::string &buffer) const
Definition lo-regexp.cc:327
~regexp()
Definition lo-regexp.h:61
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:81
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55
void free(void *)