From a2ae14f3bb387e4de693fe5a62105e06d4200f2d Mon Sep 17 00:00:00 2001 From: River Tarnell Date: Sat, 9 Aug 2008 11:16:45 +0000 Subject: [PATCH] better checking for correct # of arguments --- parser_native/affunctions.cpp | 65 +++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/parser_native/affunctions.cpp b/parser_native/affunctions.cpp index 50ee17281..9a3d1c009 100644 --- a/parser_native/affunctions.cpp +++ b/parser_native/affunctions.cpp @@ -15,18 +15,51 @@ #include #include +#include + +#include + #include "utf8.h" #include "equiv.h" #include "affunctions.h" -#include +namespace { + +struct too_many_arguments_exception : afp::exception { + too_many_arguments_exception(char const *what) + : afp::exception(what) {} +}; + +struct too_few_arguments_exception : afp::exception { + too_few_arguments_exception(char const *what) + : afp::exception(what) {} +}; + +void +check_args(std::string const &fname, int args, int min, int max = 0) +{ + if (max == 0) + max = min; + if (args < min) { + std::string s = str(boost::format( + "too few arguments for function %s (got %d, expected %d)") + % fname % args % min); + throw too_few_arguments_exception(s.c_str()); + } else if (args > max) { + std::string s = str(boost::format( + "too many arguments for function %s (got %d, expected %d)") + % fname % args % min); + throw too_many_arguments_exception(s.c_str()); + } +} + +} // anonymous namespace namespace afp { datum af_count(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to count" ); + check_args("count", args.size(), 1, 2); std::string needle, haystack; @@ -55,8 +88,7 @@ af_count(std::vector const &args) { datum af_norm(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to norm" ); + check_args("norm", args.size(), 1); std::string orig = args[0].toString(); @@ -97,8 +129,7 @@ rmdoubles(std::string const &orig) { datum af_specialratio(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to specialratio" ); + check_args("specialratio", args.size(), 1); std::string orig = args[0].toString(); int len = 0; @@ -118,9 +149,7 @@ af_specialratio(std::vector const &args) { datum af_rmspecials(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to rmspecials" ); - + check_args("rmspecials", args.size(), 1); return datum(rmspecials(args[0].toString())); } @@ -139,33 +168,25 @@ rmspecials(std::string const &orig) { datum af_ccnorm(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to ccnorm" ); - + check_args("ccnorm", args.size(), 1); return datum( confusable_character_normalise( args[0].toString() ) ); } datum af_rmdoubles(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to rmdoubles" ); - + check_args("ccnorm", args.size(), 1); return datum(rmdoubles(args[0].toString())); } datum af_length(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to lcase" ); - + check_args("ccnorm", args.size(), 1); return datum( (long int)utf8::utf8_strlen(args[0].toString()) ); } datum af_lcase(std::vector const &args) { - if (args.empty()) - throw exception( "Not enough arguments to lcase" ); - + check_args("ccnorm", args.size(), 1); return datum(utf8::utf8_tolower(args[0].toString())); }