Claw  1.7.3
glob.tpp
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
25 /**
26  * \file string_algorithm.tpp
27  * \brief Implementation of the globalization algorithms.
28  * \author Julien Jorge
29  */
30 
31 /*----------------------------------------------------------------------------*/
32 /**
33  * \brief Check if a sequence matches a given pattern.
34  * \param pattern_first Iterator on the beginning of the pattern.
35  * \param pattern_last Iterator just past the end of the pattern.
36  * \param first Iterator on the beginning of the sequence.
37  * \param last Iterator just last the end of the sequence.
38  * \param any_sequence A value representing any sequence of values, empty or
39  * not.
40  * \param zero_or_one A value representing any value or no value.
41  * \param any A value representing any value.
42  */
43 template<typename InputIterator1, typename InputIterator2>
44 bool claw::glob_match
45 ( InputIterator1 pattern_first, InputIterator1 pattern_last,
46  InputIterator2 first, InputIterator2 last,
47  typename InputIterator1::value_type any_sequence,
48  typename InputIterator1::value_type zero_or_one,
49  typename InputIterator1::value_type any )
50 {
51  bool result(false);
52 
53  if ( (pattern_first == pattern_last) || (first == last) )
54  {
55  result = (first == last);
56 
57  for ( ; result && (pattern_first != pattern_last); ++ pattern_first )
58  result =
59  (*pattern_first == any_sequence) || (*pattern_first == zero_or_one);
60  }
61  else if ( *pattern_first == any_sequence )
62  result =
63  glob_match
64  ( pattern_first + 1, pattern_last, first, last, any_sequence, zero_or_one,
65  any)
66  || glob_match
67  ( pattern_first, pattern_last, first + 1, last, any_sequence, zero_or_one,
68  any );
69  else if ( *pattern_first == zero_or_one )
70  result =
71  glob_match
72  ( pattern_first + 1, pattern_last, first, last, any_sequence, zero_or_one,
73  any)
74  || glob_match
75  ( pattern_first + 1, pattern_last, first + 1, last, any_sequence,
76  zero_or_one, any );
77  else if ( (*pattern_first == zero_or_one) || (*pattern_first == *first) )
78  result =
79  glob_match
80  ( pattern_first + 1, pattern_last, first + 1, last, any_sequence,
81  zero_or_one, any );
82  else
83  result = false;
84 
85  return result;
86 } // glob_match()
87 
88 /*----------------------------------------------------------------------------*/
89 /**
90  * \brief Check if a sequence may match a given pattern.
91  * \param pattern_first Iterator on the beginning of the pattern.
92  * \param pattern_last Iterator just past the end of the pattern.
93  * \param first Iterator on the beginning of the sequence.
94  * \param last Iterator just last the end of the sequence.
95  * \param any_sequence A value representing any sequence of values, empty or
96  * not.
97  * \param zero_or_one A value representing any value or no value.
98  * \param any A value representing any value.
99  */
100 template<typename InputIterator1, typename InputIterator2>
101 bool claw::glob_potential_match
102 ( InputIterator1 pattern_first, InputIterator1 pattern_last,
103  InputIterator2 first, InputIterator2 last,
104  typename InputIterator1::value_type any_sequence,
105  typename InputIterator1::value_type zero_or_one,
106  typename InputIterator1::value_type any )
107 {
108  bool result(true);
109  bool stop(false);
110 
111  while ( !stop && (pattern_first != pattern_last) && (first != last) )
112  if ( (*pattern_first == any_sequence) || (*pattern_first == zero_or_one) )
113  stop = true;
114  else if ( *pattern_first == any )
115  {
116  ++pattern_first;
117  ++first;
118  }
119  else if ( *pattern_first == *first )
120  {
121  ++pattern_first;
122  ++first;
123  }
124  else
125  {
126  result = false;
127  stop = true;
128  }
129 
130  return result;
131 } // glob_potential_match()