VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/simplepattern.cpp@ 106579

Last change on this file since 106579 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: simplepattern.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - RTStrSimplePattern.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/string.h>
42#include "internal/iprt.h"
43
44#include <iprt/assert.h>
45
46
47RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString)
48{
49#if 0
50 return RTStrSimplePatternNMatch(pszPattern, RTSTR_MAX, pszString, RTSTR_MAX);
51#else
52 /* ASSUMES ASCII / UTF-8 */
53 for (;;)
54 {
55 char chPat = *pszPattern;
56 switch (chPat)
57 {
58 default:
59 if (*pszString != chPat)
60 return false;
61 break;
62
63 case '*':
64 {
65 /* collapse '*' and '?', they are superfluous */
66 while ((chPat = *++pszPattern) == '*' || chPat == '?')
67 /* nothing */;
68
69 /* if no more pattern, we're done now. */
70 if (!chPat)
71 return true;
72
73 /* find chPat in the string and try get a match on the remaining pattern. */
74 for (;;)
75 {
76 char chStr = *pszString++;
77 if ( chStr == chPat
78 && RTStrSimplePatternMatch(pszPattern + 1, pszString))
79 return true;
80 if (!chStr)
81 return false;
82 }
83 /* won't ever get here */
84 break;
85 }
86
87 case '?':
88 if (!*pszString)
89 return false;
90 break;
91
92 case '\0':
93 return !*pszString;
94 }
95 pszString++;
96 pszPattern++;
97 }
98#endif
99}
100RT_EXPORT_SYMBOL(RTStrSimplePatternMatch);
101
102
103RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
104 const char *pszString, size_t cchString)
105{
106 /* ASSUMES ASCII / UTF-8 */
107 for (;;)
108 {
109 char chPat = cchPattern ? *pszPattern : '\0';
110 switch (chPat)
111 {
112 default:
113 {
114 char chStr = cchString ? *pszString : '\0';
115 if (chStr != chPat)
116 return false;
117 break;
118 }
119
120 case '*':
121 {
122 /* Collapse '*' and '?', they are superfluous. End of the pattern == match. */
123 do
124 {
125 if (!--cchPattern)
126 return true;
127 chPat = *++pszPattern;
128 } while (chPat == '*' || chPat == '?');
129 if (!chPat)
130 return true;
131
132 /* Find chPat in the string and try get a match on the remaining pattern. */
133 for (;;)
134 {
135 if (!cchString--)
136 return false;
137 char chStr = *pszString++;
138 if ( chStr == chPat
139 && RTStrSimplePatternNMatch(pszPattern + 1, cchPattern - 1, pszString, cchString))
140 return true;
141 if (!chStr)
142 return false;
143 }
144 /* won't ever get here */
145 break;
146 }
147
148 case '?':
149 if (!cchString || !*pszString)
150 return false;
151 break;
152
153 case '\0':
154 return cchString == 0 || !*pszString;
155 }
156
157 /* advance */
158 pszString++;
159 cchString--;
160 pszPattern++;
161 cchPattern--;
162 }
163}
164RT_EXPORT_SYMBOL(RTStrSimplePatternNMatch);
165
166
167RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
168 const char *pszString, size_t cchString,
169 size_t *poffMatchingPattern)
170{
171 const char *pszCur = pszPatterns;
172 while (*pszCur && cchPatterns)
173 {
174 /*
175 * Find the end of the current pattern.
176 */
177 unsigned char ch = '\0';
178 const char *pszEnd = pszCur;
179 while (cchPatterns && (ch = *pszEnd) != '\0' && ch != '|')
180 cchPatterns--, pszEnd++;
181
182 /*
183 * Try match it.
184 */
185 if (RTStrSimplePatternNMatch(pszCur, pszEnd - pszCur, pszString, cchString))
186 {
187 if (poffMatchingPattern)
188 *poffMatchingPattern = pszCur - pszPatterns;
189 return true;
190 }
191
192 /* advance */
193 if (!ch || !cchPatterns)
194 break;
195 cchPatterns--;
196 pszCur = pszEnd + 1;
197 }
198
199 if (poffMatchingPattern)
200 *poffMatchingPattern = RTSTR_MAX;
201 return false;
202}
203RT_EXPORT_SYMBOL(RTStrSimplePatternMultiMatch);
204
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette