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