Merge pull request #21174 from 3x380V/fix_21172

Base: fix Unit scheme management regressions
This commit is contained in:
Benjamin Nauck
2025-05-14 19:43:59 +02:00
committed by GitHub
5 changed files with 33 additions and 25 deletions

View File

@@ -135,7 +135,7 @@ PyObject* UnitsApi::sGetSchema(PyObject* /*self*/, PyObject* args)
return nullptr;
}
return Py_BuildValue("i", count());
return Py_BuildValue("i", schemas->currentSchema()->getNum());
}
PyObject* UnitsApi::sSetSchema(PyObject* /*self*/, PyObject* args)
@@ -164,7 +164,7 @@ PyObject* UnitsApi::sSchemaTranslate(PyObject* /*self*/, PyObject* args)
if (index < 0 || index >= static_cast<int>(count())) {
PyErr_SetString(PyExc_ValueError,
std::string {"invalid schema index:" + std::to_string(index)}.c_str());
std::string {"invalid schema index: " + std::to_string(index)}.c_str());
return nullptr;
}
@@ -172,7 +172,8 @@ PyObject* UnitsApi::sSchemaTranslate(PyObject* /*self*/, PyObject* args)
double factor {};
std::string unitStr;
const std::string unitStrLocalised = schemaTranslate(quant, factor, unitStr);
auto schema = std::make_unique<UnitsSchema>(schemas->spec(index));
const std::string unitStrLocalised = schema->translate(quant, factor, unitStr);
Py::Tuple res {3};
res[0] = Py::String {unitStrLocalised, "utf-8"};

View File

@@ -103,12 +103,13 @@ UnitsSchema::toLocale(const Quantity& quant, const double factor, const std::str
std::string valueString =
Lc.toString((quant.getValue() / factor), format.toFormat(), format.precision).toStdString();
return fmt::format(
"{}{}{}",
valueString,
unitString.empty() || unitString == "°" || unitString == "" || unitString == "" ? ""
: " ",
unitString);
return fmt::format("{}{}{}",
valueString,
unitString.empty() || unitString == "°" || unitString == ""
|| unitString == "" || unitString == "\"" || unitString == "'"
? ""
: " ",
unitString);
}
bool UnitsSchema::isMultiUnitLength() const

View File

@@ -53,7 +53,11 @@ size_t UnitsSchemas::count() const
std::vector<std::string> UnitsSchemas::getVec(const std::function<std::string(UnitsSchemaSpec)>& fn)
{
std::vector<std::string> vec;
std::transform(pack.specs.begin(), pack.specs.end(), std::back_inserter(vec), fn);
auto specs = pack.specs;
std::sort(specs.begin(), specs.end(), [](const UnitsSchemaSpec& a, const UnitsSchemaSpec& b) {
return a.num < b.num;
});
std::transform(specs.begin(), specs.end(), std::back_inserter(vec), fn);
return vec;
}

View File

@@ -82,7 +82,7 @@ inline const UnitsSchemaSpec s2
};
inline const UnitsSchemaSpec s3
{ 0, "Internal", "m", false, false, QT_TRANSLATE_NOOP("UnitsApi", "Internal (m, m², m³)"), true,
{ 0, "Internal", "mm", false, false, QT_TRANSLATE_NOOP("UnitsApi", "Standard (mm, kg, s, °)"), true,
{
{ "Length", {
{ 1e-6 , "mm" , 1.0 },
@@ -581,8 +581,8 @@ inline const UnitsSchemaSpec s7
{ "Length", {
{ 0.00000254 , "in" , 25.4 },
{ 2.54 , "thou" , 0.0254 },
{ 304.8 , "" , 25.4 },
{ 914.4 , "" , 304.8 },
{ 304.8 , "\"" , 25.4 },
{ 914.4 , "'" , 304.8 },
{ 1'609'344.0 , "yd" , 914.4 },
{ 1'609'344'000.0 , "mi" , 1'609'344.0 },
{ 0 , "in" , 25.4 }}
@@ -649,7 +649,7 @@ inline std::size_t greatestCommonDenominator(const std::size_t a, const std::siz
}
/**
* double -> [feet][inches[-fraction]], e.g.: 34-1/4
* double -> [feet'][inches[-fraction]"], e.g.: 3'4-1/4"
*/
inline std::string toFractional(const double value)
{
@@ -676,19 +676,19 @@ inline std::string toFractional(const double value)
if (inches > 0) {
resultParts.push_back(fmt::format("{}", inches));
if (numerator == 0) {
resultParts.emplace_back("");
resultParts.emplace_back("\"");
}
}
if (numerator > 0) {
if (inches > 0) {
resultParts.emplace_back("-");
}
resultParts.push_back(fmt::format("{}/{}", numerator, denominator));
resultParts.push_back(fmt::format("{}/{}\"", numerator, denominator));
}
return fmt::format("{}{}{}",
value < 0 ? "-" : "",
feet > 0 ? fmt::format("{}", feet) : "",
feet > 0 ? fmt::format("{}'", feet) : "",
fmt::join(resultParts, ""));
}

View File

@@ -21,6 +21,7 @@
#include <gtest/gtest.h>
#include "Base/Exception.h"
#include "Base/Tools.h"
#include "Base/Unit.h"
#include "Base/Quantity.h"
#include "Base/UnitsApi.h"
@@ -33,6 +34,7 @@
using Base::Quantity;
using Base::QuantityFormat;
using Base::RuntimeError;
using Base::Tools;
using Base::Unit;
using Base::UnitsApi;
using Base::UnitsSchema;
@@ -263,7 +265,7 @@ TEST_F(SchemaTest, imperial_safe_user_str_same)
{
constexpr auto val {304.8};
const auto result = set("Imperial", Unit::Length, val);
const auto expect {"1.00"};
const auto expect = Tools::escapeQuotesFromString("1.00'");
EXPECT_EQ(result, expect);
}
@@ -272,7 +274,7 @@ TEST_F(SchemaTest, imperial_safe_user_str_more)
{
constexpr auto val {310.0};
const auto result = set("Imperial", Unit::Length, val);
const auto expect {"1.02"};
const auto expect = Tools::escapeQuotesFromString("1.02'");
EXPECT_EQ(result, expect);
}
@@ -281,7 +283,7 @@ TEST_F(SchemaTest, imperial_safe_user_str_less)
{
constexpr auto val {300.0};
const auto result = set("Imperial", Unit::Length, val);
const auto expect {"11.81"};
const auto expect = Tools::escapeQuotesFromString("11.81\"");
EXPECT_EQ(result, expect);
}
@@ -290,7 +292,7 @@ TEST_F(SchemaTest, imperial_safe_user_str_one_inch)
{
constexpr auto val {25.4};
const auto result = set("Imperial", Unit::Length, val);
const auto expect {"1.00"};
const auto expect = Tools::escapeQuotesFromString("1.00\"");
EXPECT_EQ(result, expect);
}
@@ -299,7 +301,7 @@ TEST_F(SchemaTest, imperial_building_special_function_length_inch)
{
constexpr auto val {25.4};
const auto result = set("ImperialBuilding", Unit::Length, val);
const auto expect {"1"};
const auto expect = Tools::escapeQuotesFromString("1\"");
EXPECT_EQ(result, expect);
}
@@ -308,7 +310,7 @@ TEST_F(SchemaTest, imperial_building_special_function_length_foot)
{
constexpr auto val {25.4 * 12};
const auto result = set("ImperialBuilding", Unit::Length, val);
const auto expect {"1"};
const auto expect = Tools::escapeQuotesFromString("1'");
EXPECT_EQ(result, expect);
}
@@ -317,7 +319,7 @@ TEST_F(SchemaTest, imperial_building_special_function_length)
{
constexpr auto val {360.6};
const auto result = set("ImperialBuilding", Unit::Length, val);
const auto expect {"12-1/4"};
const auto expect = Tools::escapeQuotesFromString("1'2-1/4\"");
EXPECT_EQ(result, expect);
}
@@ -326,7 +328,7 @@ TEST_F(SchemaTest, imperial_building_special_function_length_neg)
{
constexpr auto val {-360.6};
const auto result = set("ImperialBuilding", Unit::Length, val);
const auto expect {"-12-1/4"};
const auto expect = Tools::escapeQuotesFromString("-1'2-1/4\"");
EXPECT_EQ(result, expect);
}