| 1 |
// -*- related-file-name: "../include/lcdf/globmatch.hh" -*- |
| 2 |
|
| 3 |
/* globmatch.{cc,hh} -- glob_match() function for shell globbing |
| 4 |
* |
| 5 |
* Copyright (c) 2000-2023 Eddie Kohler |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify it |
| 8 |
* under the terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2 of the License, or (at your option) |
| 10 |
* any later version. This program is distributed in the hope that it will be |
| 11 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
| 13 |
* Public License for more details. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifdef HAVE_CONFIG_H |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
#include <lcdf/globmatch.hh> |
| 20 |
#include <lcdf/string.hh> |
| 21 |
#include <lcdf/vector.hh> |
| 22 |
|
| 23 |
bool |
| 24 |
glob_match(const String& str, const String& pattern) |
| 25 |
{ |
| 26 |
const char* sdata = str.data(); |
| 27 |
const char* pdata = pattern.data(); |
| 28 |
int slen = str.length(); |
| 29 |
int plen = pattern.length(); |
| 30 |
int spos = 0, ppos = 0; |
| 31 |
Vector<int> glob_ppos, glob_spos1, glob_spos2; |
| 32 |
|
| 33 |
while (1) { |
| 34 |
while (ppos < plen) |
| 35 |
switch (pdata[ppos]) { |
| 36 |
|
| 37 |
case '?': |
| 38 |
if (spos >= slen) |
| 39 |
goto done; |
| 40 |
spos++; |
| 41 |
ppos++; |
| 42 |
break; |
| 43 |
|
| 44 |
case '*': |
| 45 |
glob_ppos.push_back(ppos + 1); |
| 46 |
glob_spos1.push_back(spos); |
| 47 |
glob_spos2.push_back(slen); |
| 48 |
spos = slen; |
| 49 |
ppos++; |
| 50 |
break; |
| 51 |
|
| 52 |
case '[': { |
| 53 |
if (spos >= slen) |
| 54 |
goto done; |
| 55 |
|
| 56 |
// find end of character class |
| 57 |
int p = ppos + 1; |
| 58 |
bool negated = false; |
| 59 |
if (p < plen && pdata[p] == '^') { |
| 60 |
negated = true; |
| 61 |
p++; |
| 62 |
} |
| 63 |
int first = p; |
| 64 |
if (p < plen && pdata[p] == ']') |
| 65 |
p++; |
| 66 |
while (p < plen && pdata[p] != ']') |
| 67 |
p++; |
| 68 |
if (p >= plen) // not a character class at all |
| 69 |
goto ordinary; |
| 70 |
|
| 71 |
// parse character class |
| 72 |
bool in = false; |
| 73 |
for (int i = first; i < p && !in; i++) { |
| 74 |
int c1 = pdata[i]; |
| 75 |
int c2 = c1; |
| 76 |
if (i < p - 2 && pdata[i+1] == '-') { |
| 77 |
c2 = pdata[i+2]; |
| 78 |
i += 2; |
| 79 |
} |
| 80 |
if (sdata[spos] >= c1 && sdata[spos] <= c2) |
| 81 |
in = true; |
| 82 |
} |
| 83 |
|
| 84 |
if ((negated && in) || (!negated && !in)) |
| 85 |
goto done; |
| 86 |
ppos = p + 1; |
| 87 |
spos++; |
| 88 |
break; |
| 89 |
} |
| 90 |
|
| 91 |
default: |
| 92 |
ordinary: |
| 93 |
if (spos >= slen || sdata[spos] != pdata[ppos]) |
| 94 |
goto done; |
| 95 |
spos++; |
| 96 |
ppos++; |
| 97 |
break; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
done: |
| 102 |
if (spos == slen && ppos == plen) |
| 103 |
return true; |
| 104 |
while (glob_ppos.size() && glob_spos1.back() == glob_spos2.back()) { |
| 105 |
glob_ppos.pop_back(); |
| 106 |
glob_spos1.pop_back(); |
| 107 |
glob_spos2.pop_back(); |
| 108 |
} |
| 109 |
if (glob_ppos.size()) { |
| 110 |
glob_spos2.back()--; |
| 111 |
spos = glob_spos2.back(); |
| 112 |
ppos = glob_ppos.back(); |
| 113 |
} else |
| 114 |
return false; |
| 115 |
} |
| 116 |
} |