Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Calculator: More fixes -- Trigger regex contained a bug (#3898)
Browse files Browse the repository at this point in the history
* Restrict some other stuff

* Don't require digit

* Restrict multiple ops to two or less

* reject empty parens/multiple ops

* No @, or $ not followed by a number or .

* combine char class

* add more negative tests

* Add ops back, case-insensitive subs, require two ops again

* Case-insensitive on multiple ops too

* Allow leading negatives

* Move the leading neg sub

* Better regex for hex input

* update tests
  • Loading branch information
moollaza authored Jan 28, 2017
1 parent 9322e7b commit 3309758
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/DDG/Goodie/Calculator.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ zci answer_type => 'calc';
zci is_cached => 1;

triggers query_nowhitespace => qr'
(?: [() x X × ∙ ⋅ * % + \- ÷ / \^ \$ 0-9 \. ,] |
(?: [0-9 () x × ∙ ⋅ * % + \- ÷ / \^ \$ \. ,]+ |
times | divided by | plus | minus | fact | factorial | cos |
sin | tan | cotan | log | ln | log_?\d{1,3} | exp | tanh |
sec | csc | squared | sqrt | pi | e | gross | dozen | pi |
| score){2,}
sec | csc | squared | sqrt | gross | dozen | pi |
score){2,}
'xi;

my $number_re = number_style_regex();
Expand Down Expand Up @@ -82,13 +82,18 @@ $safe->share_from('main', [qw'
handle query_nowhitespace => sub {
my $query = $_;

return if ($query =~ /\b0x/); # Probably attempt to express a hexadecimal number, query_nowhitespace makes this overreach a bit.
# regex source: http://perldoc.perl.org/functions/hex.html
return if ($req->query_lc =~ /\A(?:0?[xX])?(?:_?[0-9a-fA-F])*\z/); # Probably attempt to express a hexadecimal number, query_nowhitespace makes this overreach a bit.
return if ($query =~ $network); # Probably want to talk about addresses, not calculations.
return if ($query =~ qr/(?:(?<pcnt>\d+)%(?<op>(\+|\-|\*|\/))(?<num>\d+)) | (?:(?<num>\d+)(?<op>(\+|\-|\*|\/))(?<pcnt>\d+)%)/); # Probably want to calculate a percent ( will be used PercentOf )
return if ($query =~ /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/); # Probably are searching for a phone number, not making a calculation
return if $query =~ /[":\@]/;
return if $query =~ m{[x × ∙ ⋅ * % + \- ÷ / \^ \$ \. ,]{3,}}i;
return if $query =~ /\$[^\d\.]/;
return if $query =~ /\(\)/;

$query =~ s/^(?:whatis|calculate|solve|math)//;
$query =~ s/factorial/fact/; #replace factorial with fact
$query =~ s/^(?:whatis|calculate|solve|math)//i;
$query =~ s/factorial/fact/i; #replace factorial with fact

# Grab expression.
my $tmp_expr = spacing($query, 1);
Expand Down Expand Up @@ -166,6 +171,8 @@ sub prepare_for_display {
}

my $spaced_query = spacing($query);
$spaced_query =~ s/^ - /-/;

return +{
text => $spaced_query . ' = ' . $result,
structured => {
Expand Down
31 changes: 29 additions & 2 deletions t/Calculator.t
Original file line number Diff line number Diff line change
Expand Up @@ -832,9 +832,27 @@ ddg_goodie_test(
result => re(qr/>6</)
}
),
'-10 * 3' => test_zci(
'-10 * 3 = -30',
heading => 'Calculator',
structured_answer => {
input => ['-10 * 3'],
operation => 'Calculate',
result => re(qr/>-30</)
}
),
'-10x3' => test_zci(
'-10 * 3 = -30',
heading => 'Calculator',
structured_answer => {
input => ['-10 * 3'],
operation => 'Calculate',
result => re(qr/>-30</)
}
),
'123.123.123.123/255.255.255.255' => undef,
'83.166.167.160/27' => undef,
'9 + 0 x 07' => undef,
'9 + 0 x 0xbf7' => undef,
'0x07' => undef,
'sin(1.0) + 1,05' => undef,
'4,24,334+22,53,828' => undef,
Expand Down Expand Up @@ -862,7 +880,16 @@ ddg_goodie_test(
'warn "hi"; 1 + 1' => undef,
'die "killed"; 1 + 3' => undef,
'1 + 1; die' => undef,
'`ls -al /`; 3 * 4' => undef
'`ls -al /`; 3 * 4' => undef,
'1()' => undef,
'1^()' => undef,
'1^($)' => undef,
'1/*-+' => undef,
'http://' => undef,
'1(-2)' => undef,
'word+word' => undef,
'word + word' => undef,
'mxtoolbox' => undef,
);

done_testing;

0 comments on commit 3309758

Please sign in to comment.