NumberStyler: improve number-matching regex.

The major change is matching the exponentials more carefully.  They only
count if they have <number>E<integer> otherwise they won't work.

Also include a suggested change from @mintsoft to better handle the
spacing of numbers and units in Conversions, by separating them using
our defined regexes.

All-in-all, much more resilient!
master
Matt Miller 2014-07-28 08:17:49 +08:00
parent 65d6f76978
commit 1676300aed
2 changed files with 5 additions and 3 deletions

View File

@ -93,7 +93,7 @@ handle query_lc => sub {
return unless scalar @matches == 2; # conversion requires two triggers
# normalize the whitespace, "25cm" should work for example
$_ =~ s/([0-9])([a-df-zA-DF-Z])/$1 $2/; # Skip 'e' to handle exponentials.
$_ =~ s/($number_re)($keys)/$1 $2/g;
# fix precision and rounding:
my $precision = 3;

View File

@ -28,10 +28,12 @@ my @known_styles = (
# This is not as good an idea as I might think.
# Luckily it will someday be able to be tokenized so this won't apply.
my $all_seps = join('', map { $_->decimal . $_->thousands . $_->exponential } @known_styles);
my $all_seps = join('', map { $_->decimal . $_->thousands } @known_styles);
my $numbers = '[\d' . $all_seps . ']+';
my $re_text = join('|', $numbers, map { $numbers . $_->exponential . '\d+' } @known_styles);
sub number_style_regex {
return qr/[\d$all_seps]+/;
return qr/$re_text/;
}
# Takes an array of numbers and returns which style to use for parse and display