1 /*
2 *
3 * Copyright (c) 2002
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12 #include "./regex_comparison.hpp"
13 #include <map>
14 #include <cassert>
15 #include <boost/timer.hpp>
16 #include <boost/xpressive/xpressive.hpp>
17
18 namespace sxpr
19 {
20
21 using namespace boost::xpressive;
22
23 // short matches
24 char const * sz1 = "^([0-9]+)(\\-| |$)(.*)$";
25 sregex rx1 = bol >> (s1= +range('0','9')) >> (s2= as_xpr('-')|' '|eol) >> (s3= *_) >> eol;
26
27 char const * sz2 = "([[:digit:]]{4}[- ]){3}[[:digit:]]{3,4}";
28 sregex rx2 = bol >> repeat<3>(s1= repeat<4>(set[digit]) >> (set='-',' ')) >> repeat<3,4>(set[digit]);
29
30 char const * sz3 = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
31 sregex rx3
32 = bol
33 >> (s1= +set[ range('a','z') | range('A','Z') | range('0','9') | '_' | '-' | '.' ])
34 >> '@'
35 >> (s2=
36 (s3= '[' >> repeat<1,3>(range('0','9')) >> '.' >> repeat<1,3>(range('0','9'))
37 >> '.' >> repeat<1,3>(range('0','9')) >> '.'
38 )
39 |
40 (s4= +(s5= +set[ range('a','z') | range('A','Z') | range('0','9') | '-' ] >> '.' ) )
41 )
42 >> (s6= repeat<2,4>(set[ range('a','z') | range('A','Z')]) | repeat<1,3>(range('0','9')))
43 >> (s7= !as_xpr(']'))
44 >> eol
45 ;
46
47 char const * sz4 = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
48 sregex rx4 = rx3;
49
50 char const * sz5 = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
51 sregex rx5 = rx3;
52
53 char const * sz6 = "^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$";
54 sregex rx6 = bol >> repeat<1,2>(set[ range('a','z') | range('A','Z') ])
55 >> range('0','9') >> repeat<0,1>(set[range('0','9')|range('A','Z')|range('a','z')])
56 >> repeat<0,1>(as_xpr(' ')) >> range('0','9')
57 >> repeat<2>(set[range('A','Z')|range('a','z')]) >> eol;
58
59 char const * sz7 = "^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$";
60 sregex rx7 = rx6;
61
62 char const * sz8 = "^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$";
63 sregex rx8 = rx6;
64
65 char const * sz9 = "^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$";
66 sregex rx9 = bol >> repeat<1,2>(set[digit]) >> '/' >> repeat<1,2>(set[digit]) >> '/' >> repeat<4>(set[digit]) >> eol;
67
68 char const * sz10 = "^[[:digit:]]{1,2}/[[:digit:]]{1,2}/[[:digit:]]{4}$";
69 sregex rx10 = rx9;
70
71 char const * sz11 = "^[-+]?[[:digit:]]*\\.?[[:digit:]]*$";
72 sregex rx11 = bol >> !(set= '-','+') >> *set[digit] >> !as_xpr('.') >> *set[digit] >> eol ;
73
74 char const * sz12 = "^[-+]?[[:digit:]]*\\.?[[:digit:]]*$";
75 sregex rx12 = rx11;
76
77 char const * sz13 = "^[-+]?[[:digit:]]*\\.?[[:digit:]]*$";
78 sregex rx13 = rx11;
79
80 // long matches
81 char const * sz14 = "Twain";
82 boost::xpressive::sregex rx14 = as_xpr("Twain");
83
84 char const * sz15 = "Huck[[:alpha:]]+";
85 boost::xpressive::sregex rx15 = "Huck" >> +set[alpha];
86
87 char const * sz16 = "[[:alpha:]]+ing";
88 boost::xpressive::sregex rx16 = +set[alpha] >> "ing";
89
90 char const * sz17 = "^[^\n]*?Twain";
91 boost::xpressive::sregex rx17 = bol >> -*~as_xpr('\n') >> "Twain";
92
93 char const * sz18 = "Tom|Sawyer|Huckleberry|Finn";
94 boost::xpressive::sregex rx18 = ( as_xpr("Tom") | "Sawyer" | "Huckleberry" | "Finn" );
95
96 //char const * sz18 = "Tom|Sawyer|.uckleberry|Finn";
97 //boost::xpressive::sregex rx18 = ( as_xpr("Tom") | "Sawyer" | _ >> "uckleberry" | "Finn" );
98
99 char const * sz19 = "(Tom|Sawyer|Huckleberry|Finn).{0,30}river|river.{0,30}(Tom|Sawyer|Huckleberry|Finn)";
100 boost::xpressive::sregex rx19 =
101 (s1= as_xpr("Tom") | "Sawyer" | "Huckleberry" | "Finn" )
102 >> repeat<0,30>(_)
103 >> "river"
104 |
105 "river"
106 >> repeat<0,30>(_)
107 >> (s2= as_xpr("Tom") | "Sawyer" | "Huckleberry" | "Finn" );
108
109 std::map< std::string, sregex > rxmap;
110
111 struct map_init
112 {
map_initsxpr::map_init113 map_init()
114 {
115 rxmap[ sz1 ] = rx1;
116 rxmap[ sz2 ] = rx2;
117 rxmap[ sz3 ] = rx3;
118 rxmap[ sz4 ] = rx4;
119 rxmap[ sz5 ] = rx5;
120 rxmap[ sz6 ] = rx6;
121 rxmap[ sz7 ] = rx7;
122 rxmap[ sz8 ] = rx8;
123 rxmap[ sz9 ] = rx9;
124 rxmap[ sz10 ] = rx10;
125 rxmap[ sz11 ] = rx11;
126 rxmap[ sz12 ] = rx12;
127 rxmap[ sz13 ] = rx13;
128 rxmap[ sz14 ] = rx14;
129 rxmap[ sz15 ] = rx15;
130 rxmap[ sz16 ] = rx16;
131 rxmap[ sz17 ] = rx17;
132 rxmap[ sz18 ] = rx18;
133 rxmap[ sz19 ] = rx19;
134 }
135 };
136
137 static map_init const i = map_init();
138
time_match(const std::string & re,const std::string & text)139 double time_match(const std::string& re, const std::string& text)
140 {
141 boost::xpressive::sregex const &e = rxmap[ re ];
142 boost::xpressive::smatch what;
143 boost::timer tim;
144 int iter = 1;
145 int counter, repeats;
146 double result = 0;
147 double run;
148 assert(boost::xpressive::regex_match( text, what, e ));
149 do
150 {
151 tim.restart();
152 for(counter = 0; counter < iter; ++counter)
153 {
154 boost::xpressive::regex_match( text, what, e );
155 }
156 result = tim.elapsed();
157 iter *= 2;
158 } while(result < 0.5);
159 iter /= 2;
160
161 // repeat test and report least value for consistency:
162 for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
163 {
164 tim.restart();
165 for(counter = 0; counter < iter; ++counter)
166 {
167 boost::xpressive::regex_match( text, what, e );
168 }
169 run = tim.elapsed();
170 result = (std::min)(run, result);
171 }
172 return result / iter;
173 }
174
175 struct noop
176 {
operator ()sxpr::noop177 void operator()( boost::xpressive::smatch const & ) const
178 {
179 }
180 };
181
time_find_all(const std::string & re,const std::string & text)182 double time_find_all(const std::string& re, const std::string& text)
183 {
184 boost::xpressive::sregex const &e = rxmap[ re ];
185 boost::xpressive::smatch what;
186 boost::timer tim;
187 int iter = 1;
188 int counter, repeats;
189 double result = 0;
190 double run;
191 do
192 {
193 tim.restart();
194 for(counter = 0; counter < iter; ++counter)
195 {
196 boost::xpressive::sregex_iterator begin( text.begin(), text.end(), e ), end;
197 std::for_each( begin, end, noop() );
198 }
199 result = tim.elapsed();
200 iter *= 2;
201 }while(result < 0.5);
202 iter /= 2;
203
204 if(result >10)
205 return result / iter;
206
207 // repeat test and report least value for consistency:
208 for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
209 {
210 tim.restart();
211 for(counter = 0; counter < iter; ++counter)
212 {
213 boost::xpressive::sregex_iterator begin( text.begin(), text.end(), e ), end;
214 std::for_each( begin, end, noop() );
215 }
216 run = tim.elapsed();
217 result = (std::min)(run, result);
218 }
219 return result / iter;
220 }
221
222 }
223
224
225