Skip to content

Commit 7f1ca8c

Browse files
authored
Merge pull request #1194 from dpvc/fix-unit-aliases
Mark units that are aliases, and use alias string for uString comparisons so sameUnits will allow plurals, etc. (#1193)
2 parents 59bc19c + fc48f05 commit 7f1ca8c

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

lib/Units.pm

+9-1
Original file line numberDiff line numberDiff line change
@@ -786,11 +786,19 @@ our %known_units = (
786786
},
787787
);
788788

789+
# A map from unit name to its aliases
790+
our %unit_aliases;
791+
792+
# A map from units name to what it is an alias for
793+
our %unit_aliased_to;
794+
789795
# Process aliases.
790796
for my $unit (keys %known_units) {
791797
if (ref $known_units{$unit}{aliases} eq 'ARRAY') {
792798
my $aliases = delete $known_units{$unit}{aliases};
793-
$known_units{$_} = $known_units{$unit} for @$aliases;
799+
$known_units{$_} = $known_units{$unit} for @$aliases;
800+
$unit_aliased_to{$_} = $unit for @$aliases;
801+
$unit_aliases{$unit} = $aliases;
794802
}
795803
}
796804

macros/contexts/contextUnits.pl

+14-8
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,11 @@ package context::Units::Context;
685685
# The units from the original Units package
686686
#
687687
our %UNITS = (%Units::known_units);
688-
$UNITS{$_} = $UNITS{L} for ('litre', 'litres', 'litre', 'litres'); # add these extras
688+
our %ALIAS = (%Units::unit_aliased_to);
689+
for ('litre', 'litres', 'liter', 'liters') {
690+
$UNITS{$_} = $UNITS{L};
691+
$ALIAS{$_} = 'L';
692+
}
689693

690694
#
691695
# The categories of units that can be selected.
@@ -782,6 +786,7 @@ sub addUnit {
782786
isConstant => 1
783787
}
784788
);
789+
$constants->set($name => { ustring => $ALIAS{$name} }) if $ALIAS{$name};
785790
$constants->add(map { $_ => { alias => $name } } @$aliases) if $aliases;
786791
$self->addUnitAliases($name) unless $options{noaliases};
787792
} else {
@@ -1326,25 +1331,26 @@ sub fString {
13261331
#
13271332
# Get the string version using original units using:
13281333
# The original order and powers if $exact is set, or
1329-
# Alphabetic order and fractions otherwise.
1334+
# Alphabetic order and fractions otherwise. Use
1335+
# alias strings to normalize the result.
13301336
#
13311337
sub uString {
13321338
my ($self, $exact) = @_;
1333-
return $self->stringFor('nunits', 'dunits', $exact ? $self->{order} : undef, !$exact);
1339+
return $self->stringFor('nunits', 'dunits', $exact ? $self->{order} : undef, !$exact, 0, 1);
13341340
}
13351341

13361342
#
13371343
# Creates the string version using the given order and power settings
13381344
#
13391345
sub stringFor {
1340-
my ($self, $key1, $key2, $order, $noNegativePowers, $allowEmptyNumerator) = @_;
1346+
my ($self, $key1, $key2, $order, $noNegativePowers, $allowEmptyNumerator, $useAlias) = @_;
13411347
my ($nunits, $dunits) = ({ %{ $self->{$key1} } }, { %{ $self->{$key2} } });
13421348
$order = [ main::lex_sort(keys %$nunits, keys %$dunits) ] unless $order;
13431349
my ($ns, $ds) = ([], []);
13441350
my $constants = $self->context->constants;
13451351
for my $u (@$order) {
1346-
$self->pushUnitString($ns, $ds, $u, $nunits->{$u}, $noNegativePowers);
1347-
$self->pushUnitString($ds, $ns, $u, $dunits->{$u}, $noNegativePowers);
1352+
$self->pushUnitString($ns, $ds, $u, $nunits->{$u}, $noNegativePowers, $useAlias);
1353+
$self->pushUnitString($ds, $ns, $u, $dunits->{$u}, $noNegativePowers, $useAlias);
13481354
$nunits->{$u} = $dunits->{$u} = 0; # don't include them again
13491355
}
13501356
my ($num, $den) = (join(' ', @$ns), join(' ', @$ds));
@@ -1359,10 +1365,10 @@ sub stringFor {
13591365
# a negative power or not
13601366
#
13611367
sub pushUnitString {
1362-
my ($self, $units, $invert, $u, $n, $noNegativePowers) = @_;
1368+
my ($self, $units, $invert, $u, $n, $noNegativePowers, $useAlias) = @_;
13631369
return unless $n;
13641370
my $def = $self->context->constants->get($u);
1365-
my $unit = ($def->{string} || $u);
1371+
my $unit = (($useAlias ? $def->{ustring} : '') || $def->{string} || $u);
13661372
if (!$noNegativePowers && ($self->{negativePowers}{$u} || $self->getFlag('useNegativePowers'))) {
13671373
push(@$invert, $unit . "^-$n");
13681374
} else {

0 commit comments

Comments
 (0)