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.cc
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 (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include "glob-match.h"
31#include "oct-glob.h"
32#include "glob-wrappers.h"
33
34bool
35glob_match::match (const std::string& str) const
36{
37 return octave::sys::fnmatch (m_pat, str, m_fnmatch_flags);
38}
39
42{
43 return octave::sys::glob (m_pat);
44}
45
46int
47glob_match::opts_to_fnmatch_flags (unsigned int xopts) const
48{
49 int retval = 0;
50
51 if (xopts & pathname)
52 retval |= octave_fnm_pathname_wrapper ();
53
54 if (xopts & noescape)
55 retval |= octave_fnm_noescape_wrapper ();
56
57 if (xopts & period)
58 retval |= octave_fnm_period_wrapper ();
59
60 return retval;
61}
62
63symbol_match::symbol_match (const std::string& pattern)
64{
65 m_pat = pattern;
66
67#if defined (OCTAVE_USE_WINDOWS_API)
68 m_glob = nullptr;
69#else
70 m_glob = std::unique_ptr<glob_match> {new glob_match {pattern}};
71#endif
72}
73
75{
76 m_pat = in.m_pat;
77
78#if defined (OCTAVE_USE_WINDOWS_API)
79 m_glob = nullptr;
80#else
81 m_glob = std::unique_ptr<glob_match> {new glob_match {m_pat}};
82#endif
83}
84
85bool
86symbol_match::match (const std::string& sym)
87{
88#if defined (OCTAVE_USE_WINDOWS_API)
89
90 // gnulib's fnmatch replacement is slow on Windows.
91 // We don't need full POSIX compatibility to match symbol patterns.
92 // Glob patterns with '*' or '?' should be good enough.
93 // We also do not need to worry about multi-byte characters because symbols
94 // are ASCII-only.
95 octave_idx_type pat_len = m_pat.length ();
96 octave_idx_type pat_idx = 0;
97 octave_idx_type pat_wildc_idx = -1;
98 octave_idx_type sym_len = sym.length ();
99 octave_idx_type sym_idx = 0;
100 octave_idx_type sym_wildc_idx = 0;
101
102 while (sym_idx < sym_len)
103 {
104 if (pat_idx < pat_len
105 && (m_pat[pat_idx] == '?' || m_pat[pat_idx] == sym[sym_idx]))
106 {
107 // match to '?' or exact match
108 pat_idx++;
109 sym_idx++;
110 }
111 else if (pat_idx < pat_len && m_pat[pat_idx] == '*')
112 {
113 // remember position in pattern and symbol
114 pat_wildc_idx = pat_idx;
115 sym_wildc_idx = sym_idx;
116 pat_idx++;
117 }
118 else if (pat_wildc_idx != -1)
119 {
120 // no match but previous wildcard '*'
121 // revert pat_idx to previous position
122 pat_idx = pat_wildc_idx + 1;
123 // but proceed to next character in symbol and try to match again
124 sym_wildc_idx++;
125 sym_idx = sym_wildc_idx;
126 }
127 else
128 // no exact match and no wildcard
129 return false;
130 }
131
132 // consume potentially trailing '*' in pattern
133 while (pat_idx < pat_len && m_pat[pat_idx] == '*')
134 pat_idx++;
135
136 // check for remaining (unmatched) characters in pattern
137 return pat_idx == pat_len;
138
139#else
140
141 return m_glob->match (sym);
142
143#endif
144}
145
string_vector glob() const
Definition glob-match.cc:41
bool match(const std::string &str) const
Definition glob-match.cc:35
symbol_match(const std::string &pattern)
Definition glob-match.cc:63
bool match(const std::string &sym)
Definition glob-match.cc:86
int octave_fnm_period_wrapper(void)
int octave_fnm_pathname_wrapper(void)
int octave_fnm_noescape_wrapper(void)