Base: fix UnitsSchema::translate

Schema translation previously returned factor 0 and unit string
of input quantity if there was no translation match. Restore
that behaviour as well as returned factor and unit string of
imperial schemas.

Fixes: 1155f0d752 ("Base: simplify UnitsSchemas management")
This commit is contained in:
Ladislav Michl
2025-05-27 12:08:05 +02:00
parent e5e251df8f
commit bcd23743f4
2 changed files with 22 additions and 14 deletions

View File

@@ -733,17 +733,27 @@ inline std::string toDms(const double value)
* Special functions caller
*/
inline const std::map<std::string, std::function<std::string(double)>> specials // clang-format off
// clang-format off
inline const std::map<std::string, std::function<std::string(double, double&, std::string&)>> specials
{
{
{ "toDMS" , [](const double val) { return toDms(val); }},
{ "toFractional" , [](const double val) { return toFractional(val); }}
{ "toDMS" , [](const double val, double& factor, std::string& unitString) {
factor = 1.0;
unitString = "deg";
return toDms(val);
}},
{ "toFractional" , [](const double val, double& factor, std::string& unitString) {
factor = 25.4;
unitString = "in";
return toFractional(val);
}}
}
}; // clang-format on
inline std::string runSpecial(const std::string& name, const double value)
inline std::string
runSpecial(const std::string& name, const double value, double& factor, std::string& unitString)
{
return specials.contains(name) ? specials.at(name)(value) : "";
return specials.contains(name) ? specials.at(name)(value, factor, unitString) : "";
}