Claw  1.7.3
impl/glob.tpp
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00031 /*----------------------------------------------------------------------------*/
00043 template<typename InputIterator1, typename InputIterator2>
00044 bool claw::glob_match
00045 ( InputIterator1 pattern_first, InputIterator1 pattern_last,
00046   InputIterator2 first, InputIterator2 last,
00047   typename InputIterator1::value_type any_sequence,
00048   typename InputIterator1::value_type zero_or_one,
00049   typename InputIterator1::value_type any )
00050 {
00051   bool result(false);
00052 
00053   if ( (pattern_first == pattern_last) || (first == last) )
00054     {
00055       result = (first == last);
00056 
00057       for ( ; result && (pattern_first != pattern_last); ++ pattern_first )
00058   result =
00059     (*pattern_first == any_sequence) || (*pattern_first == zero_or_one);
00060     }
00061   else if ( *pattern_first == any_sequence )
00062     result =
00063       glob_match
00064       ( pattern_first + 1, pattern_last, first, last, any_sequence, zero_or_one,
00065   any)
00066       || glob_match
00067       ( pattern_first, pattern_last, first + 1, last, any_sequence, zero_or_one,
00068   any );
00069   else if ( *pattern_first == zero_or_one )
00070     result =
00071       glob_match
00072       ( pattern_first + 1, pattern_last, first, last, any_sequence, zero_or_one,
00073   any)
00074       || glob_match
00075       ( pattern_first + 1, pattern_last, first + 1, last, any_sequence,
00076   zero_or_one, any );
00077   else if ( (*pattern_first == zero_or_one) || (*pattern_first == *first) )
00078     result =
00079       glob_match
00080       ( pattern_first + 1, pattern_last, first + 1, last, any_sequence,
00081   zero_or_one, any );
00082   else
00083     result = false;
00084 
00085   return result;
00086 } // glob_match()
00087 
00088 /*----------------------------------------------------------------------------*/
00100 template<typename InputIterator1, typename InputIterator2>
00101 bool claw::glob_potential_match
00102 ( InputIterator1 pattern_first, InputIterator1 pattern_last,
00103   InputIterator2 first, InputIterator2 last,
00104   typename InputIterator1::value_type any_sequence,
00105   typename InputIterator1::value_type zero_or_one,
00106   typename InputIterator1::value_type any )
00107 {
00108   bool result(true);
00109   bool stop(false);
00110 
00111   while ( !stop && (pattern_first != pattern_last) && (first != last) )
00112     if ( (*pattern_first == any_sequence) || (*pattern_first == zero_or_one) )
00113       stop = true;
00114     else if ( *pattern_first == any )
00115       {
00116   ++pattern_first;
00117   ++first;
00118       }
00119     else if ( *pattern_first == *first )
00120       {
00121   ++pattern_first;
00122   ++first;
00123       }
00124     else
00125       {
00126   result = false;
00127   stop = true;
00128       }
00129 
00130   return result;
00131 } // glob_potential_match()