r38994 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38993‎ | r38994 | r38995 >
Date:12:14, 9 August 2008
Author:river
Status:old
Tags:
Comment:
remove cast / ctors for basic types from aftypes, too prone to error.
(side effect: time for the test case goes from 6:14 to 5:26)
Modified paths:
  • /trunk/extensions/AbuseFilter/parser_native/affunctions.cpp (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/aftypes.cpp (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/aftypes.h (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/check.cpp (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/filter_evaluator.cpp (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/parser.cpp (modified) (history)
  • /trunk/extensions/AbuseFilter/parser_native/request.cpp (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/parser_native/filter_evaluator.cpp
@@ -31,7 +31,7 @@
3232 filter_evaluator::evaluate(std::string const &filter) const
3333 {
3434 try {
35 - return (bool) e.evaluate(filter);
 35+ return e.evaluate(filter).toBool();
3636 } catch (std::exception &e) {
3737 std::cerr << "can't evaluate filter: " << e.what() << '\n';
3838 return false;
Index: trunk/extensions/AbuseFilter/parser_native/aftypes.cpp
@@ -22,59 +22,30 @@
2323
2424 namespace afp {
2525
26 -datum::datum(std::string const &var) {
27 - _init_from_string(var);
 26+datum::datum() {
2827 }
2928
30 -datum::datum(char const *var)
 29+datum::datum(datum const &other)
 30+ : value_(other.value_)
3131 {
32 - _init_from_string(var);
3332 }
3433
35 -void
36 -datum::_init_from_string(std::string const &var)
 34+datum
 35+datum::from_string_convert(std::string const &var)
3736 {
3837 // Try integer
3938 try {
40 - value_ = boost::lexical_cast<long int>(var);
 39+ return from_int(boost::lexical_cast<long int>(var));
4140 } catch (boost::bad_lexical_cast &e) {
4241 try {
43 - value_ = boost::lexical_cast<double>(var);
 42+ return from_double(boost::lexical_cast<double>(var));
4443 } catch (boost::bad_lexical_cast &e) {
4544 /* If it's nothing else, it's a string */
46 - value_ = var;
 45+ return from_string(var);
4746 }
4847 }
4948 }
5049
51 -datum::datum() {
52 -}
53 -
54 -datum::datum(datum const &other)
55 - : value_(other.value_)
56 -{
57 -}
58 -
59 -datum::datum(long int var)
60 - : value_(var)
61 -{
62 -}
63 -
64 -datum::datum(double var)
65 - : value_(var)
66 -{
67 -}
68 -
69 -datum::datum(float var)
70 - : value_(var)
71 -{
72 -}
73 -
74 -datum::datum(bool var)
75 - : value_((long int) var)
76 -{
77 -}
78 -
7950 datum
8051 datum::from_string(std::string const &v)
8152 {
@@ -99,6 +70,18 @@
10071 return d;
10172 }
10273
 74+template<> datum datum::from<std::string>(std::string const &v) {
 75+ return from_string(v);
 76+}
 77+
 78+template<> datum datum::from<long int>(long int const &v) {
 79+ return from_int(v);
 80+}
 81+
 82+template<> datum datum::from<double>(double const &v) {
 83+ return from_double(v);
 84+}
 85+
10386 datum & datum::operator= (datum const &other) {
10487 // Protect against self-assignment
10588 if (this == &other) {
@@ -259,9 +242,9 @@
260243 typedef typename from_string_converter<U>::type b_type;
261244
262245 Operator<typename preferred_type<a_type, b_type>::type> op;
263 - return op(
 246+ return datum::from<typename preferred_type<a_type, b_type>::type>(op(
264247 from_string_converter<T>::convert(a),
265 - from_string_converter<U>::convert(b));
 248+ from_string_converter<U>::convert(b)));
266249 }
267250
268251 /*
@@ -272,7 +255,8 @@
273256 typedef typename from_string_converter<T>::type a_type;
274257
275258 Operator<typename preferred_type<a_type, a_type>::type> op;
276 - return op(from_string_converter<T>::convert(a));
 259+ return datum::from<typename preferred_type<a_type, a_type>::type>(
 260+ op(from_string_converter<T>::convert(a)));
277261 }
278262
279263 };
@@ -319,7 +303,7 @@
320304 * For comparisons that only work on integers - strings will be converted.
321305 */
322306 template<template<typename V> class Operator>
323 -struct arith_compare_visitor : boost::static_visitor<datum> {
 307+struct arith_compare_visitor : boost::static_visitor<bool> {
324308 template<typename T, typename U>
325309 bool operator() (T const &a, U const &b) const {
326310 typedef typename from_string_converter<T>::type a_type;
@@ -420,7 +404,7 @@
421405
422406 datum
423407 pow(datum const &a, datum const &b) {
424 - datum result = datum(std::pow(a.toFloat(),b.toFloat()));
 408+ datum result = datum::from_double(std::pow(a.toFloat(),b.toFloat()));
425409
426410 return result;
427411 }
@@ -475,7 +459,7 @@
476460
477461 bool
478462 datum::operator! () const {
479 - return !(int) *this;
 463+ return !toBool();
480464 }
481465
482466 } // namespace afp
Index: trunk/extensions/AbuseFilter/parser_native/check.cpp
@@ -19,7 +19,7 @@
2020
2121 for(int i=0;i<=100;i++) {
2222 try {
23 - f.add_variable("foo", afp::datum("love"));
 23+ f.add_variable("foo", afp::datum::from_string("love"));
2424 result = f.evaluate( "specialratio('foo;') == 0.25" );
2525 } catch (afp::exception* excep) {
2626 printf( "Exception: %s\n", excep->what() );
Index: trunk/extensions/AbuseFilter/parser_native/affunctions.cpp
@@ -83,7 +83,7 @@
8484 if (args.size() >= 2)
8585 count--;
8686
87 - return datum((long int)count);
 87+ return datum::from_int((long int)count);
8888 }
8989
9090 datum
@@ -108,7 +108,7 @@
109109 lastchr = chr;
110110 }
111111
112 - return datum(result);
 112+ return datum::from_string(result);
113113 }
114114
115115 std::string
@@ -144,13 +144,13 @@
145145
146146 double ratio = (float)specialcount / len;
147147
148 - return datum(ratio);
 148+ return datum::from_double(ratio);
149149 }
150150
151151 datum
152152 af_rmspecials(std::vector<datum> const &args) {
153153 check_args("rmspecials", args.size(), 1);
154 - return datum(rmspecials(args[0].toString()));
 154+ return datum::from_string(rmspecials(args[0].toString()));
155155 }
156156
157157 std::string
@@ -169,25 +169,25 @@
170170 datum
171171 af_ccnorm(std::vector<datum> const &args) {
172172 check_args("ccnorm", args.size(), 1);
173 - return datum( confusable_character_normalise( args[0].toString() ) );
 173+ return datum::from_string(confusable_character_normalise( args[0].toString()));
174174 }
175175
176176 datum
177177 af_rmdoubles(std::vector<datum> const &args) {
178178 check_args("ccnorm", args.size(), 1);
179 - return datum(rmdoubles(args[0].toString()));
 179+ return datum::from_string(rmdoubles(args[0].toString()));
180180 }
181181
182182 datum
183183 af_length(std::vector<datum> const &args) {
184184 check_args("ccnorm", args.size(), 1);
185 - return datum( (long int)utf8::utf8_strlen(args[0].toString()) );
 185+ return datum::from_int((long int)utf8::utf8_strlen(args[0].toString()));
186186 }
187187
188188 datum
189189 af_lcase(std::vector<datum> const &args) {
190190 check_args("ccnorm", args.size(), 1);
191 - return datum(utf8::utf8_tolower(args[0].toString()));
 191+ return datum::from_string(utf8::utf8_tolower(args[0].toString()));
192192 }
193193
194194 std::string
Index: trunk/extensions/AbuseFilter/parser_native/aftypes.h
@@ -50,31 +50,22 @@
5151 class datum {
5252 public:
5353 datum();
54 -
55 - /*
56 - * Generic ctor tries to convert to an int.
57 - */
58 - template<typename T>
59 - datum(T const &v)
60 - : value_(boost::lexical_cast<long int>(v))
61 - {
62 - }
63 -
64 - // Specific type constructors
65 - datum( std::string const &var );
66 - datum( char const *var );
67 - datum( long int var );
68 - datum( float var );
69 - datum( double var );
70 - datum( bool var );
71 -
72 - datum( const datum & oldData );
 54+ datum(datum const &oldData);
7355
74 - // Type forcing helpers
 56+ // Type forcing construction functions
7557 static datum from_string(std::string const &v);
 58+ static datum from_string_convert(std::string const &v);
7659 static datum from_int(long int v);
7760 static datum from_double(double v);
7861
 62+ /*
 63+ * Template versions of the above. See below for the actual
 64+ * implementations.
 65+ */
 66+ template<typename T> static datum from(T const &v) {
 67+ return from_int(0);
 68+ }
 69+
7970 // Assignment operator
8071 datum &operator= (const datum & other);
8172
@@ -98,22 +89,6 @@
9990 return (bool) toInt();
10091 }
10192
102 - operator long int(void) const {
103 - return toInt();
104 - }
105 -
106 - operator double(void) const {
107 - return toFloat();
108 - }
109 -
110 - operator std::string(void) const {
111 - return toString();
112 - }
113 -
114 - operator bool(void) const {
115 - return (bool) toInt();
116 - }
117 -
11893 template<typename char_type, typename traits>
11994 void
12095 print_to(std::basic_ostream<char_type, traits> &s) const {
@@ -141,6 +116,11 @@
142117 std::string what_;
143118 };
144119
 120+
 121+template<> datum datum::from<std::string>(std::string const &v);
 122+template<> datum datum::from<long int>(long int const &v);
 123+template<> datum datum::from<long int>(long int const &v);
 124+
145125 datum operator+(datum const &a, datum const &b);
146126 datum operator-(datum const &a, datum const &b);
147127 datum operator*(datum const &a, datum const &b);
Index: trunk/extensions/AbuseFilter/parser_native/request.cpp
@@ -95,7 +95,7 @@
9696 return false;
9797 value = *str;
9898
99 - f.add_variable(key, datum(value));
 99+ f.add_variable(key, datum::from_string_convert(value));
100100 }
101101
102102 return true;
Index: trunk/extensions/AbuseFilter/parser_native/parser.cpp
@@ -101,8 +101,8 @@
102102 datum
103103 f_in(datum const &a, datum const &b)
104104 {
105 - std::string sa = a, sb = b;
106 - return datum(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end());
 105+ std::string sa = a.toString(), sb = b.toString();
 106+ return datum::from_int(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end());
107107 }
108108
109109 datum
@@ -115,13 +115,13 @@
116116 f_regex(datum const &str, datum const &pattern)
117117 {
118118 boost::u32regex r = boost::make_u32regex(pattern.toString());
119 - return boost::u32regex_match(str.toString(), r);
 119+ return datum::from_int(boost::u32regex_match(str.toString(), r));
120120 }
121121
122122 datum
123123 f_ternary(datum const &v, datum const &iftrue, datum const &iffalse)
124124 {
125 - return (bool)v ? iftrue : iffalse;
 125+ return v.toInt() ? iftrue : iffalse;
126126 }
127127
128128 datum
@@ -165,8 +165,32 @@
166166 return datum::from_string(s);
167167 }
168168
 169+datum
 170+datum_and(datum const &a, datum const &b)
 171+{
 172+ return datum::from_int(a.toInt() && b.toInt());
169173 }
170174
 175+datum
 176+datum_or(datum const &a, datum const &b)
 177+{
 178+ return datum::from_int(a.toInt() || b.toInt());
 179+}
 180+
 181+datum
 182+datum_xor(datum const &a, datum const &b)
 183+{
 184+ return datum::from_int((bool)a.toInt() ^ (bool)b.toInt());
 185+}
 186+
 187+datum
 188+datum_negate(datum const &a)
 189+{
 190+ return datum::from_int(!(a.toBool()));
 191+}
 192+
 193+}
 194+
171195 /*
172196 * This is the closure types for functions. 'val' stores the final result of
173197 * the function call; func and args store the function object and the parsed
@@ -280,7 +304,7 @@
281305 * *(c_escape_ch_p[x]) into (*c_escape_ch_p)[x]
282306 */
283307 | (
284 - ch_p('"')[value.val = ""]
 308+ ch_p('"')[value.val = bind(&datum::from_string)("")]
285309 >> *((c_escape_ch_p[value.val = bind(&f_append)(value.val, arg1)] - '"'))
286310 >> ch_p('"')[value.val = bind(&f_strip_last)(value.val)]
287311 )
@@ -295,7 +319,7 @@
296320 */
297321 variable =
298322 self.variables[variable.val = arg1]
299 - | (+ (upper_p | '_') )[variable.val = ""]
 323+ | (+ (upper_p | '_') )[variable.val = bind(&datum::from_string)("")]
300324 ;
301325
302326 /*
@@ -317,7 +341,7 @@
318342 */
319343 basic =
320344 ( '(' >> tern_expr[basic.val = arg1] >> ')' )
321 - | ch_p('!') >> tern_expr[basic.val = !arg1]
 345+ | ch_p('!') >> tern_expr[basic.val = bind(&datum_negate)(arg1)]
322346 | ch_p('+') >> tern_expr[basic.val = arg1]
323347 | ch_p('-') >> tern_expr[basic.val = -arg1]
324348 | value[basic.val = arg1]
@@ -381,10 +405,10 @@
382406 ord_expr =
383407 plus_expr[ord_expr.val = arg1]
384408 >> *(
385 - "<" >> plus_expr[ord_expr.val = ord_expr.val < arg1]
386 - | ">" >> plus_expr[ord_expr.val = ord_expr.val > arg1]
387 - | "<=" >> plus_expr[ord_expr.val = ord_expr.val <= arg1]
388 - | ">=" >> plus_expr[ord_expr.val = ord_expr.val >= arg1]
 409+ "<" >> plus_expr[ord_expr.val = bind(&datum::from_int)(ord_expr.val < arg1)]
 410+ | "<=" >> plus_expr[ord_expr.val = bind(&datum::from_int)(ord_expr.val <= arg1)]
 411+ | ">" >> plus_expr[ord_expr.val = bind(&datum::from_int)(ord_expr.val > arg1)]
 412+ | ">=" >> plus_expr[ord_expr.val = bind(&datum::from_int)(ord_expr.val >= arg1)]
389413 )
390414 ;
391415
@@ -394,14 +418,14 @@
395419 eq_expr =
396420 ord_expr[eq_expr.val = arg1]
397421 >> *(
398 - "=" >> eq_expr[eq_expr.val = eq_expr.val == arg1]
399 - | "==" >> eq_expr[eq_expr.val = eq_expr.val == arg1]
400 - | "!=" >> eq_expr[eq_expr.val = eq_expr.val != arg1]
401 - | "/=" >> eq_expr[eq_expr.val = eq_expr.val != arg1]
 422+ "=" >> eq_expr[eq_expr.val = bind(&datum::from_int)(eq_expr.val == arg1)]
 423+ | "==" >> eq_expr[eq_expr.val = bind(&datum::from_int)(eq_expr.val == arg1)]
 424+ | "!=" >> eq_expr[eq_expr.val = bind(&datum::from_int)(eq_expr.val != arg1)]
 425+ | "/=" >> eq_expr[eq_expr.val = bind(&datum::from_int)(eq_expr.val != arg1)]
402426 | "===" >> eq_expr[eq_expr.val =
403 - bind(&datum::compare_with_type)(eq_expr.val, arg1)]
 427+ bind(&datum::from_int)(bind(&datum::compare_with_type)(eq_expr.val, arg1))]
404428 | "!==" >> eq_expr[eq_expr.val =
405 - !bind(&datum::compare_with_type)(eq_expr.val, arg1)]
 429+ bind(&datum::from_int)(!bind(&datum::compare_with_type)(eq_expr.val, arg1))]
406430 )
407431 ;
408432
@@ -411,11 +435,9 @@
412436 bool_expr =
413437 eq_expr[bool_expr.val = arg1]
414438 >> *(
415 - '&' >> eq_expr[bool_expr.val = bool_expr.val && arg1]
416 - | '|' >> eq_expr[bool_expr.val = bool_expr.val || arg1]
417 - | '^' >> eq_expr[bool_expr.val =
418 - ((bool_expr.val || arg1)
419 - && !(bool_expr.val && arg1)) ]
 439+ '&' >> eq_expr[bool_expr.val = bind(datum_and)(bool_expr.val, arg1)]
 440+ | '|' >> eq_expr[bool_expr.val = bind(datum_or)(bool_expr.val, arg1)]
 441+ | '^' >> eq_expr[bool_expr.val = bind(datum_xor)(bool_expr.val, arg1)]
420442 )
421443 ;
422444
@@ -458,8 +480,8 @@
459481 /*
460482 * We provide a couple of standard variables everyone wants.
461483 */
462 - add_variable("true", afp::datum(true));
463 - add_variable("false", afp::datum(false));
 484+ add_variable("true", afp::datum::from_int(true));
 485+ add_variable("false", afp::datum::from_int(false));
464486
465487 /*
466488 * The cast functions.
@@ -672,9 +694,9 @@
673695 }
674696
675697 afp::expressor e;
676 - e.add_variable("ONE", 1);
677 - e.add_variable("TWO", 2);
678 - e.add_variable("THREE", 3);
 698+ e.add_variable("ONE", afp::datum::from_int(1));
 699+ e.add_variable("TWO", afp::datum::from_int(2));
 700+ e.add_variable("THREE", afp::datum::from_int(3));
679701 e.add_function("add", f_add);
680702 e.add_function("norm", f_norm);
681703

Status & tagging log