r38991 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38990‎ | r38991 | r38992 >
Date:11:16, 9 August 2008
Author:river
Status:old
Tags:
Comment:
better checking for correct # of arguments
Modified paths:
  • /trunk/extensions/AbuseFilter/parser_native/affunctions.cpp (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/parser_native/affunctions.cpp
@@ -15,18 +15,51 @@
1616 #include <ios>
1717 #include <iostream>
1818
 19+#include <unicode/uchar.h>
 20+
 21+#include <boost/format.hpp>
 22+
1923 #include "utf8.h"
2024 #include "equiv.h"
2125 #include "affunctions.h"
2226
23 -#include <unicode/uchar.h>
 27+namespace {
2428
 29+struct too_many_arguments_exception : afp::exception {
 30+ too_many_arguments_exception(char const *what)
 31+ : afp::exception(what) {}
 32+};
 33+
 34+struct too_few_arguments_exception : afp::exception {
 35+ too_few_arguments_exception(char const *what)
 36+ : afp::exception(what) {}
 37+};
 38+
 39+void
 40+check_args(std::string const &fname, int args, int min, int max = 0)
 41+{
 42+ if (max == 0)
 43+ max = min;
 44+ if (args < min) {
 45+ std::string s = str(boost::format(
 46+ "too few arguments for function %s (got %d, expected %d)")
 47+ % fname % args % min);
 48+ throw too_few_arguments_exception(s.c_str());
 49+ } else if (args > max) {
 50+ std::string s = str(boost::format(
 51+ "too many arguments for function %s (got %d, expected %d)")
 52+ % fname % args % min);
 53+ throw too_many_arguments_exception(s.c_str());
 54+ }
 55+}
 56+
 57+} // anonymous namespace
 58+
2559 namespace afp {
2660
2761 datum
2862 af_count(std::vector<datum> const &args) {
29 - if (args.empty())
30 - throw exception( "Not enough arguments to count" );
 63+ check_args("count", args.size(), 1, 2);
3164
3265 std::string needle, haystack;
3366
@@ -55,8 +88,7 @@
5689
5790 datum
5891 af_norm(std::vector<datum> const &args) {
59 - if (args.empty())
60 - throw exception( "Not enough arguments to norm" );
 92+ check_args("norm", args.size(), 1);
6193
6294 std::string orig = args[0].toString();
6395
@@ -97,8 +129,7 @@
98130
99131 datum
100132 af_specialratio(std::vector<datum> const &args) {
101 - if (args.empty())
102 - throw exception( "Not enough arguments to specialratio" );
 133+ check_args("specialratio", args.size(), 1);
103134
104135 std::string orig = args[0].toString();
105136 int len = 0;
@@ -118,9 +149,7 @@
119150
120151 datum
121152 af_rmspecials(std::vector<datum> const &args) {
122 - if (args.empty())
123 - throw exception( "Not enough arguments to rmspecials" );
124 -
 153+ check_args("rmspecials", args.size(), 1);
125154 return datum(rmspecials(args[0].toString()));
126155 }
127156
@@ -139,33 +168,25 @@
140169
141170 datum
142171 af_ccnorm(std::vector<datum> const &args) {
143 - if (args.empty())
144 - throw exception( "Not enough arguments to ccnorm" );
145 -
 172+ check_args("ccnorm", args.size(), 1);
146173 return datum( confusable_character_normalise( args[0].toString() ) );
147174 }
148175
149176 datum
150177 af_rmdoubles(std::vector<datum> const &args) {
151 - if (args.empty())
152 - throw exception( "Not enough arguments to rmdoubles" );
153 -
 178+ check_args("ccnorm", args.size(), 1);
154179 return datum(rmdoubles(args[0].toString()));
155180 }
156181
157182 datum
158183 af_length(std::vector<datum> const &args) {
159 - if (args.empty())
160 - throw exception( "Not enough arguments to lcase" );
161 -
 184+ check_args("ccnorm", args.size(), 1);
162185 return datum( (long int)utf8::utf8_strlen(args[0].toString()) );
163186 }
164187
165188 datum
166189 af_lcase(std::vector<datum> const &args) {
167 - if (args.empty())
168 - throw exception( "Not enough arguments to lcase" );
169 -
 190+ check_args("ccnorm", args.size(), 1);
170191 return datum(utf8::utf8_tolower(args[0].toString()));
171192 }
172193

Status & tagging log