match.h

Go to the documentation of this file.
00001 /*
00002  * ASTL - the Automaton Standard Template Library.
00003  * C++ generic components for Finite State Automata handling.
00004  * Copyright (C) 2000-2006 Vincent Le Maout (vincent.lemaout@chello.fr).
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00024 #ifndef ASTL_MATCH_H
00025 #define ASTL_MATCH_H
00026 
00027 namespace astl {
00028 
00029 template <typename ForwardI, typename Cursor>
00030 inline
00031 ForwardI 
00032 first_match(Cursor &c, ForwardI first, ForwardI last)
00033 {
00034   ForwardI start = first;
00035   for(; first != last && c.forward(*first); ++first)
00036     if (c.src_final()) return ++first;
00037   return start;  // no match found
00038 }
00039 
00040 template <typename Cursor>
00041 inline
00042 const char*
00043 first_match(Cursor &c, const char *text)
00044 {
00045   const char *start = text;
00046   for(; *text != '\0' && c.forward(*text); ++text)
00047     if (c.src_final()) return ++text;
00048   return start;  // no match found
00049 }
00050 
00051 template <typename ForwardI, typename Cursor>
00052 inline
00053 ForwardI 
00054 longest_match(Cursor &c, ForwardI first, ForwardI last)
00055 {
00056   ForwardI last_match_position = first;
00057   while(first != last && c.forward(*first)) {
00058     ++first;
00059     if (c.src_final()) 
00060       last_match_position = first;
00061   }
00062   return last_match_position;
00063 }
00064 
00065 template <typename Cursor>
00066 inline
00067 const char*
00068 longest_match(Cursor &c, const char *text)
00069 {
00070   const char *last_match_position = text;
00071   while(*text != '\0' && c.forward(*text)) {
00072     ++text;
00073     if (c.src_final()) 
00074       last_match_position = text;
00075   }
00076   return last_match_position;
00077 }
00078 
00079 template <typename InputI, typename Cursor>
00080 inline
00081 int match_count(Cursor &c, InputI first, InputI last)
00082 {
00083   int r = 0;
00084   for(; first != last && c.forward(*first); ++first)
00085     if (c.src_final()) ++r;
00086   return r;
00087 }
00088 
00101 template <typename InputI, typename Cursor>
00102 inline
00103 bool match(Cursor &c, InputI first, InputI last)
00104 {
00105   for(; first != last && c.forward(*first); ++first);
00106   return first == last && c.src_final();
00107 }
00108 
00109 template <typename Cursor>
00110 inline
00111 bool match(Cursor& c, const char *w)
00112 {
00113   for(; *w != '\0' && c.forward(*w); ++w);
00114   return *w == '\0' && c.src_final();
00115 }
00116 
00117 template <typename ForwardI, typename ExtractCursor>
00118 inline
00119 std::vector<std::pair<int, int> > 
00120 split_match(ExtractCursor &c, ForwardI first, ForwardI last)
00121 {
00122   std::vector<std::pair<int, int> > tmp;
00123   for(; first != last && c.forward(*first); ++first)
00124     if (c.src_final()) tmp = c.submatches();
00125   return tmp;
00126 }
00127 
00242 } // namespace astl
00243 
00244 
00245 #endif

Generated on Sun Mar 8 02:41:34 2009 for ASTL by  doxygen 1.5.7.1