@@ -685,7 +685,11 @@ package context::Units::Context;
685
685
# The units from the original Units package
686
686
#
687
687
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
+ }
689
693
690
694
#
691
695
# The categories of units that can be selected.
@@ -782,6 +786,7 @@ sub addUnit {
782
786
isConstant => 1
783
787
}
784
788
);
789
+ $constants -> set($name => { ustring => $ALIAS {$name } }) if $ALIAS {$name };
785
790
$constants -> add(map { $_ => { alias => $name } } @$aliases ) if $aliases ;
786
791
$self -> addUnitAliases($name ) unless $options {noaliases };
787
792
} else {
@@ -1326,25 +1331,26 @@ sub fString {
1326
1331
#
1327
1332
# Get the string version using original units using:
1328
1333
# 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.
1330
1336
#
1331
1337
sub uString {
1332
1338
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 );
1334
1340
}
1335
1341
1336
1342
#
1337
1343
# Creates the string version using the given order and power settings
1338
1344
#
1339
1345
sub stringFor {
1340
- my ($self , $key1 , $key2 , $order , $noNegativePowers , $allowEmptyNumerator ) = @_ ;
1346
+ my ($self , $key1 , $key2 , $order , $noNegativePowers , $allowEmptyNumerator , $useAlias ) = @_ ;
1341
1347
my ($nunits , $dunits ) = ({ %{ $self -> {$key1 } } }, { %{ $self -> {$key2 } } });
1342
1348
$order = [ main::lex_sort(keys %$nunits , keys %$dunits ) ] unless $order ;
1343
1349
my ($ns , $ds ) = ([], []);
1344
1350
my $constants = $self -> context-> constants;
1345
1351
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 );
1348
1354
$nunits -> {$u } = $dunits -> {$u } = 0; # don't include them again
1349
1355
}
1350
1356
my ($num , $den ) = (join (' ' , @$ns ), join (' ' , @$ds ));
@@ -1359,10 +1365,10 @@ sub stringFor {
1359
1365
# a negative power or not
1360
1366
#
1361
1367
sub pushUnitString {
1362
- my ($self , $units , $invert , $u , $n , $noNegativePowers ) = @_ ;
1368
+ my ($self , $units , $invert , $u , $n , $noNegativePowers , $useAlias ) = @_ ;
1363
1369
return unless $n ;
1364
1370
my $def = $self -> context-> constants-> get($u );
1365
- my $unit = ($def -> {string } || $u );
1371
+ my $unit = (( $useAlias ? $def -> { ustring } : ' ' ) || $def -> {string } || $u );
1366
1372
if (!$noNegativePowers && ($self -> {negativePowers }{$u } || $self -> getFlag(' useNegativePowers' ))) {
1367
1373
push (@$invert , $unit . " ^-$n " );
1368
1374
} else {
0 commit comments