GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
glob-match.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-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_glob_match_h)
27#define octave_glob_match_h 1
28
29#include "octave-config.h"
30
31#include <string>
32
33#include "Array.h"
34#include "str-vec.h"
35
37{
38public:
39
40 enum opts
41 {
42 pathname = 1, // No wildcard can ever match '/'.
43 noescape = 2, // Backslashes don't quote special chars.
44 period = 4 // Leading '.' is matched only explicitly.
45 };
46
47 glob_match (const std::string& p,
48 unsigned int xopts = pathname | noescape | period)
49 : m_pat (p), m_fnmatch_flags (opts_to_fnmatch_flags (xopts))
50 { }
51
53 unsigned int xopts = pathname | noescape | period)
54 : m_pat (p), m_fnmatch_flags (opts_to_fnmatch_flags (xopts))
55 { }
56
57 glob_match (const glob_match& gm) = default;
58
59 glob_match& operator = (const glob_match& gm) = default;
60
61 ~glob_match () = default;
62
63 void set_pattern (const std::string& p) { m_pat = p; }
64
65 void set_pattern (const string_vector& p) { m_pat = p; }
66
67 bool match (const std::string& str) const;
68
69 Array<bool> match (const string_vector& str) const
70 {
71 int n = str.numel ();
72
73 Array<bool> retval (dim_vector (n, 1));
74
75 for (int i = 0; i < n; i++)
76 retval(i) = match (str[i]);
77
78 return retval;
79 }
80
81 // We forward to glob_internal here to avoid problems with gnulib's
82 // glob.h defining glob to be rpl_glob.
83
84 string_vector glob () const;
85
86private:
87
88 // Globbing pattern(s).
89 string_vector m_pat;
90
91 // Option flags.
92 int m_fnmatch_flags;
93
94 int opts_to_fnmatch_flags (unsigned int xopts) const;
95};
96
98{
99
100// This class is meant to provide a performant implementation for symbol
101// matching on all platforms. For Windows, that is done by manually
102// implementing matching rules for '*' and '?' wildcards. On other platforms,
103// the matching is deferred to `fnmatch`. That means that the matching rules
104// differ depending on the platform. To write cross-platform compatible code
105// with this class, do not use [] groups or ranges, named character classes,
106// collating symbols, or equivalence class expressions.
107
108public:
109
110 symbol_match (const std::string& pattern);
111
112 symbol_match (const symbol_match&);
113
114 symbol_match& operator = (const symbol_match& in)
115 {
116 if (this == &in)
117 return *this;
118
119 m_pat = in.m_pat;
120
121 if (m_glob)
122 m_glob->set_pattern (m_pat);
123
124 return *this;
125 }
126
127 ~symbol_match () = default;
128
129 void set_pattern (const std::string& p)
130 {
131 m_pat = p;
132
133 if (m_glob)
134 m_glob->set_pattern (m_pat);
135 }
136
137 bool match (const std::string& sym);
138
139private:
140
141 std::string m_pat;
142
143 std::unique_ptr<glob_match> m_glob;
144};
145
146#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
~glob_match()=default
glob_match(const std::string &p, unsigned int xopts=pathname|noescape|period)
Definition glob-match.h:47
glob_match(const glob_match &gm)=default
void set_pattern(const std::string &p)
Definition glob-match.h:63
Array< bool > match(const string_vector &str) const
Definition glob-match.h:69
glob_match(const string_vector &p=string_vector(), unsigned int xopts=pathname|noescape|period)
Definition glob-match.h:52
void set_pattern(const string_vector &p)
Definition glob-match.h:65
octave_idx_type numel() const
Definition str-vec.h:98
~symbol_match()=default
void set_pattern(const std::string &p)
Definition glob-match.h:129
#define OCTAVE_API
Definition main.in.cc:55
string_vector glob(const string_vector &pat)
Definition oct-glob.cc:73