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
parent
65d6f76978
commit
1676300aed
|
@ -93,7 +93,7 @@ handle query_lc => sub {
|
||||||
return unless scalar @matches == 2; # conversion requires two triggers
|
return unless scalar @matches == 2; # conversion requires two triggers
|
||||||
|
|
||||||
# normalize the whitespace, "25cm" should work for example
|
# 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:
|
# fix precision and rounding:
|
||||||
my $precision = 3;
|
my $precision = 3;
|
||||||
|
|
|
@ -28,10 +28,12 @@ my @known_styles = (
|
||||||
|
|
||||||
# This is not as good an idea as I might think.
|
# 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.
|
# 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 {
|
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
|
# Takes an array of numbers and returns which style to use for parse and display
|
||||||
|
|
Loading…
Reference in New Issue