VirtualBox

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

Last change on this file since 16823 was 13549, checked in by vboxsync, 16 years ago

Just RTSTR_MAX.

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