]> git.tdb.fi Git - libs/core.git/blob - source/strings/glob.cpp
Move files around to prepare for assimilation into core
[libs/core.git] / source / strings / glob.cpp
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #include "glob.h"
9
10 using namespace std;
11
12 namespace {
13
14 template<bool icase>
15 bool cmp(char c, char h);
16
17 template<>
18 bool cmp<true>(char c, char h)
19 { return tolower(c)==tolower(h); }
20
21 template<>
22 bool cmp<false>(char c, char h)
23 { return c==h; }
24
25 template<bool icase>
26 bool globmatch(string::const_iterator pat_i, const string::const_iterator &pat_e, string::const_iterator str_i, const string::const_iterator &str_e)
27 {
28         while(pat_i!=pat_e && str_i!=str_e)
29         {
30                 if(*pat_i=='?' || cmp<icase>(*str_i, *pat_i))
31                 {
32                         ++pat_i;
33                         ++str_i;
34                 }
35                 else if(*pat_i=='*')
36                 {
37                         ++pat_i;
38                         if(pat_i==pat_e)
39                                 return true;
40                         
41                         for(; str_i!=str_e; ++str_i)
42                                 if(cmp<icase>(*str_i, *pat_i) && globmatch<icase>(pat_i, pat_e, str_i, str_e))
43                                         return true;
44                         
45                         return false;
46                 }
47                 else
48                         return false;
49         }
50
51         return pat_i==pat_e && str_i==str_e;
52 }
53
54 }
55
56 namespace Msp {
57
58 bool globmatch(const string &pat, const string &str)
59 {
60         return ::globmatch<false>(pat.begin(), pat.end(), str.begin(), str.end());
61 }
62
63 bool globcasematch(const string &pat, const string &str)
64 {
65         return ::globmatch<true>(pat.begin(), pat.end(), str.begin(), str.end());
66 }
67
68 } // namespace Msp