[13472] | 1 | /* $Id: simplepattern.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - RTStrSimplePattern.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[13472] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[13472] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[13472] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[13472] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[13472] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[13472] | 41 | #include <iprt/string.h>
|
---|
[21337] | 42 | #include "internal/iprt.h"
|
---|
| 43 |
|
---|
[13472] | 44 | #include <iprt/assert.h>
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString)
|
---|
| 48 | {
|
---|
| 49 | #if 0
|
---|
[13549] | 50 | return RTStrSimplePatternNMatch(pszPattern, RTSTR_MAX, pszString, RTSTR_MAX);
|
---|
[13472] | 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 | {
|
---|
[33540] | 65 | /* collapse '*' and '?', they are superfluous */
|
---|
[13472] | 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 | }
|
---|
[21337] | 100 | RT_EXPORT_SYMBOL(RTStrSimplePatternMatch);
|
---|
[13472] | 101 |
|
---|
| 102 |
|
---|
| 103 | RTDECL(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 | {
|
---|
[33540] | 122 | /* Collapse '*' and '?', they are superfluous. End of the pattern == match. */
|
---|
[13472] | 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 | }
|
---|
[21337] | 164 | RT_EXPORT_SYMBOL(RTStrSimplePatternNMatch);
|
---|
[13472] | 165 |
|
---|
| 166 |
|
---|
| 167 | RTDECL(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 | */
|
---|
[13513] | 177 | unsigned char ch = '\0';
|
---|
[13472] | 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)
|
---|
[13549] | 200 | *poffMatchingPattern = RTSTR_MAX;
|
---|
[13472] | 201 | return false;
|
---|
| 202 | }
|
---|
[21337] | 203 | RT_EXPORT_SYMBOL(RTStrSimplePatternMultiMatch);
|
---|
[13472] | 204 |
|
---|