00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
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;
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 }
00243
00244
00245 #endif