]>
Dogcows Code - chaz/yoink/blob - src/stlplus/portability/wildcard.cpp
c15252e670209b91a2e469906acad2fc506f8291
1 ////////////////////////////////////////////////////////////////////////////////
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
8 // Simple wildcard matching function.
10 // WARNING: wheel re-invention follows
11 // Given that all shells perform wildcard matching, why don't the library writers put it in the C run-time????????
13 ////////////////////////////////////////////////////////////////////////////////
14 #include "wildcard.hpp"
19 // function for testing whether a character matches a set
20 // I can't remember the exact rules and I have no definitive references but:
21 // a set contains characters, escaped characters (I think) and ranges in the form a-z
22 // The character '-' can only appear at the start of the set where it is not interpreted as a range
23 // This is a horrible mess - blame the Unix folks for making a hash of wildcards
24 // first expand any ranges and remove escape characters to make life more palatable
26 static bool match_set (const std::string
& set
, char match
)
28 std::string simple_set
;
29 for (std::string::const_iterator i
= set
.begin(); i
!= set
.end(); ++i
)
39 else if (i
+1 == set
.end())
45 // found a set. The first character is already in the result, so first remove it (the set might be empty)
46 simple_set
.erase(simple_set
.end()-1);
48 for (char ch
= *(i
-2); ch
<= last
; ch
++)
56 if (i
+1 == set
.end()) {return false;}
64 std::string::size_type result
= simple_set
.find(match
);
65 return result
!= std::string::npos
;
68 // the recursive bit - basically whenever a * is found you recursively call this for each candidate substring match
69 // until either it succeeds or you run out of string to match
70 // for each * in the wildcard another level of recursion is created
72 static bool match_remainder (const std::string
& wild
, std::string::const_iterator wildi
, const std::string
& match
, std::string::const_iterator matchi
)
74 //cerr << "match_remainder called at " << *matchi << " with wildcard " << *wildi << endl;
75 while (wildi
!= wild
.end() && matchi
!= match
.end())
77 //cerr << "trying to match " << *matchi << " with wildcard " << *wildi << endl;
84 for (std::string::const_iterator i
= matchi
; i
!= match
.end(); ++i
)
86 // deal with * at the end of the wildcard - there is no remainder then
87 if (wildi
== wild
.end())
89 if (i
== match
.end()-1)
92 else if (match_remainder(wild
, wildi
, match
, i
))
101 // scan for the end of the set using a similar method for avoiding escaped characters
103 std::string::const_iterator end
= wildi
+ 1;
104 for (; !found
&& end
!= wild
.end(); ++end
)
110 // found the set, now match with its contents excluding the brackets
111 if (!match_set(wild
.substr(wildi
- wild
.begin() + 1, end
- wildi
- 1), *matchi
))
117 if (end
== wild
.end()-1)
136 if (wildi
== wild
.end()-1)
139 if (*wildi
!= *matchi
)
145 if (*wildi
!= *matchi
)
152 bool result
= wildi
== wild
.end() && matchi
== match
.end();
156 // like all recursions the exported function has a simpler interface than the
157 // recursive function and is just a 'seed' to the recursion itself
159 bool wildcard(const std::string
& wild
, const std::string
& match
)
161 return match_remainder(wild
, wild
.begin(), match
, match
.begin());
164 } // end namespace stlplus
This page took 0.042045 seconds and 4 git commands to generate.