Calculator: improve superscript handling.

This fixes the problem of the simple case, but handling, say, expressions
in parens would require a lot more thought (a lexer would help!) I'll
leave that problem for another day.

Fixes #483.
master
Matt Miller 2014-06-10 08:34:11 +08:00
parent 5e6c1abb9a
commit 2c855b6dad
2 changed files with 9 additions and 4 deletions

View File

@ -53,7 +53,7 @@ foreach my $style (@known_styles) {
# Luckily it will someday be able to be tokenized so this won't apply.
my $all_seps = join('', map { $_->{decimal} . $_->{thousands} } @known_styles);
my $numbery = qr/^[\d$all_seps]+$/;
my $numbery = qr/[\d$all_seps]+/;
my $funcy = qr/[[a-z]+\(|log[_]?\d{1,3}\(|\^/; # Stuff that looks like functions.
my %named_operations = (
@ -114,7 +114,7 @@ handle query_nowhitespace => sub {
$tmp_expr =~ s#\b$name\b# $constant #ig;
}
my @numbers = grep { $_ =~ $numbery } (split /\s+/, $tmp_expr);
my @numbers = grep { $_ =~ /^$numbery$/ } (split /\s+/, $tmp_expr);
my $style = display_style(@numbers);
return unless $style;
@ -154,8 +154,7 @@ handle query_nowhitespace => sub {
$results_no_html = $results_html = $tmp_q;
# Superscript (before spacing).
$results_html =~ s/\^([^\)]+)/<sup>$1<\/sup>/g;
$results_html =~ s/\^(\d+|\b(?:$ored_constants)\b)/<sup>$1<\/sup>/g;
$results_html =~ s/\^($numbery|\b$ored_constants\b)/<sup>$1<\/sup>/g;
($results_no_html, $results_html) = map { spacing($_) } ($results_no_html, $results_html);
return if $results_no_html =~ /^\s/;

View File

@ -310,6 +310,12 @@ ddg_goodie_test(
heading => 'Calculator',
html => qq(<div>2,2 squared = <a href="javascript:;" onClick="document.x.q.value='4,8';document.x.q.focus();">4,8</a></div>),
),
'0.8^2 + 0.6^2' => test_zci(
'0.8 ^ 2 + 0.6 ^ 2 = 1',
heading => 'Calculator',
html =>
qq(<div>0.8<sup>2</sup> + 0.6<sup>2</sup> = <a href="javascript:;" onClick="document.x.q.value='1';document.x.q.focus();">1</a></div>),
),
'sin(1.0) + 1,05' => undef,
'4,24,334+22,53,828' => undef,
'5234534.34.54+1' => undef,