Skip to content

Commit 428b58b

Browse files
authored
Include the non-prefixed BaseUnits in the BaseUnitPrefixes (#1492)
Include the non-prefixed `BaseUnits` in the `BaseUnitPrefixes`: - the `BaseUnitPrefixes` now also handles the case where prefixing an already prefixed base unit results in a non-prefixed base unit - regenerating the quantities resulted in a number of new `BaseUnits` to become available (as well as some which got their scales reduced)
1 parent 7556b7c commit 428b58b

24 files changed

+44
-32
lines changed

CodeGen/Helpers/PrefixBuilder/BaseUnitPrefix.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
namespace CodeGen.Helpers.PrefixBuilder;
77

88
/// <summary>
9-
/// Represents a unique key that combines a base unit and a prefix.
9+
/// Represents a unique combination of a base unit and an optional prefix, used to identify a prefixed unit.
1010
/// </summary>
1111
/// <param name="BaseUnit">
12-
/// The base unit associated with the prefix. For example, "Gram".
12+
/// The base unit associated with the prefix, such as "Gram".
1313
/// </param>
1414
/// <param name="Prefix">
15-
/// The prefix applied to the base unit. For example, <see cref="JsonTypes.Prefix.Kilo" />.
15+
/// The prefix applied to the base unit, such as <see cref="JsonTypes.Prefix.Kilo" />.
16+
/// <para>If the prefix exponent value is 0, this parameter will be <c>null</c>.</para>
1617
/// </param>
17-
internal readonly record struct BaseUnitPrefix(string BaseUnit, Prefix Prefix);
18+
internal readonly record struct BaseUnitPrefix(string BaseUnit, Prefix? Prefix);

CodeGen/Helpers/PrefixBuilder/BaseUnitPrefixes.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static BaseUnitPrefixes FromBaseUnits(IEnumerable<Unit> baseUnits)
7777
{
7878
var unitName = baseUnit.SingularName;
7979
prefixedStringFactors[unitName] = new PrefixScaleFactor(unitName, 0);
80+
baseUnitPrefixConversions[new BaseUnitPrefix(unitName, null)] = unitName;
8081
foreach (Prefix prefix in baseUnit.Prefixes)
8182
{
8283
var prefixedUnitName = prefix + unitName.ToCamelCase();
@@ -123,10 +124,20 @@ internal bool TryGetMatchingPrefix(string unitName, int exponent, Prefix prefix,
123124
{
124125
var (quotient, remainder) = int.DivRem(prefixFactor, exponent);
125126
// Ensure the prefix factor is divisible by the exponent without a remainder and that there is a valid prefix matching the target scale
126-
if (remainder == 0 && TryGetPrefixWithScale(targetPrefixFactor.ScaleFactor + quotient, out Prefix calculatedPrefix))
127+
if (remainder == 0)
127128
{
128-
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, calculatedPrefix);
129-
return true;
129+
if (targetPrefixFactor.ScaleFactor + quotient == 0)
130+
{
131+
// when the resulting exponent is 0: return the non-prefixed BaseUnit
132+
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, null);
133+
return true;
134+
}
135+
136+
if (TryGetPrefixWithScale(targetPrefixFactor.ScaleFactor + quotient, out Prefix calculatedPrefix))
137+
{
138+
matchingPrefix = new BaseUnitPrefix(targetPrefixFactor.BaseUnit, calculatedPrefix);
139+
return true;
140+
}
130141
}
131142
}
132143

UnitsNet/GeneratedCode/Quantities/ElectricAdmittance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricApparentPower.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricCapacitance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricConductance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricImpedance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricInductance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricPotential.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricPotentialChangeRate.g.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/GeneratedCode/Quantities/ElectricReactance.g.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)