1
// -*- related-file-name: "../include/lcdf/globmatch.hh" -*-
3
/* globmatch.{cc,hh} -- glob_match() function for shell globbing
5
* Copyright (c) 2000-2006 Eddie Kohler
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.
19
#include <lcdf/globmatch.hh>
20
#include <lcdf/string.hh>
21
#include <lcdf/vector.hh>
24
glob_match(const String& str, const String& pattern)
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;
35
switch (pdata[ppos]) {
45
glob_ppos.push_back(ppos + 1);
46
glob_spos1.push_back(spos);
47
glob_spos2.push_back(slen);
56
// find end of character class
59
if (p < plen && pdata[p] == '^') {
64
if (p < plen && pdata[p] == ']')
66
while (p < plen && pdata[p] != ']')
68
if (p >= plen) // not a character class at all
71
// parse character class
73
for (int i = first; i < p && !in; i++) {
76
if (i < p - 2 && pdata[i+1] == '-') {
80
if (sdata[spos] >= c1 && sdata[spos] <= c2)
84
if ((negated && in) || (!negated && !in))
93
if (spos >= slen || sdata[spos] != pdata[ppos])
102
if (spos == slen && ppos == plen)
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();
109
if (glob_ppos.size()) {
111
spos = glob_spos2.back();
112
ppos = glob_ppos.back();