Base: Quantity: return std::string

This commit is contained in:
Ladislav Michl
2024-07-13 13:07:27 +02:00
committed by Yorik van Havre
parent c11b37e312
commit 2ea8a633ac
58 changed files with 573 additions and 592 deletions

View File

@@ -28,6 +28,7 @@
#include <array>
#endif
#include <fmt/format.h>
#include <Base/Tools.h>
#include "Quantity.h"
#include "Exception.h"
@@ -81,10 +82,10 @@ Quantity::Quantity(double value, const Unit& unit)
, myUnit {unit}
{}
Quantity::Quantity(double value, const QString& unit)
Quantity::Quantity(double value, const std::string& unit)
: myValue {0.0}
{
if (unit.isEmpty()) {
if (unit.empty()) {
this->myValue = value;
this->myUnit = Unit();
return;
@@ -236,29 +237,34 @@ Quantity Quantity::operator-() const
return Quantity(-(this->myValue), this->myUnit);
}
QString Quantity::getUserString(double& factor, QString& unitString) const
std::string Quantity::getUserString(double& factor, std::string& unitString) const
{
return Base::UnitsApi::schemaTranslate(*this, factor, unitString);
QString str = QString::fromStdString(unitString);
QString ret = Base::UnitsApi::schemaTranslate(*this, factor, str);
unitString = str.toStdString();
return ret.toStdString();
}
QString Quantity::getUserString(UnitsSchema* schema, double& factor, QString& unitString) const
std::string
Quantity::getUserString(UnitsSchema* schema, double& factor, std::string& unitString) const
{
return schema->schemaTranslate(*this, factor, unitString);
QString str = QString::fromStdString(unitString);
QString ret = schema->schemaTranslate(*this, factor, str);
unitString = str.toStdString();
return ret.toStdString();
}
QString Quantity::getSafeUserString() const
std::string Quantity::getSafeUserString() const
{
auto retString = getUserString();
if (Q_LIKELY(this->myValue != 0)) {
auto feedbackQty = parse(retString);
auto ret = getUserString();
if (this->myValue) {
auto feedbackQty = parse(ret);
auto feedbackVal = feedbackQty.getValue();
if (feedbackVal == 0) {
retString = QStringLiteral("%1 %2").arg(this->myValue).arg(QString::fromStdString(this->getUnit().getString()));
ret = fmt::format("{} {}", this->myValue, this->getUnit().getString());
}
}
retString =
QString::fromStdString(Base::Tools::escapeQuotesFromString(retString.toStdString()));
return retString;
return Base::Tools::escapeQuotesFromString(ret);
}
/// true if it has a number without a unit
@@ -562,11 +568,11 @@ private:
#pragma GCC diagnostic pop
#endif
Quantity Quantity::parse(const QString& string)
Quantity Quantity::parse(const std::string& string)
{
// parse from buffer
QuantityParser::YY_BUFFER_STATE my_string_buffer =
QuantityParser::yy_scan_string(string.toUtf8().data());
QuantityParser::yy_scan_string(string.c_str());
QuantityParser::StringBufferCleaner cleaner(my_string_buffer);
// set the global return variables
QuantResult = Quantity(DOUBLE_MIN);

View File

@@ -25,7 +25,7 @@
#define BASE_Quantity_H
#include "Unit.h"
#include <QString>
#include <string>
// NOLINTBEGIN
#ifndef DOUBLE_MAX
@@ -130,7 +130,7 @@ public:
Quantity(const Quantity&) = default;
Quantity(Quantity&&) = default;
explicit Quantity(double value, const Unit& unit = Unit());
explicit Quantity(double value, const QString& unit);
explicit Quantity(double value, const std::string& unit);
/// Destruction
~Quantity() = default;
@@ -166,17 +166,17 @@ public:
myFormat = fmt;
}
/// transfer to user preferred unit/potence
QString getUserString(double& factor, QString& unitString) const;
QString getUserString() const
std::string getUserString(double& factor, std::string& unitString) const;
std::string getUserString() const
{ // to satisfy GCC
double dummy1 {};
QString dummy2 {};
std::string dummy2 {};
return getUserString(dummy1, dummy2);
}
QString getUserString(UnitsSchema* schema, double& factor, QString& unitString) const;
QString getSafeUserString() const;
std::string getUserString(UnitsSchema* schema, double& factor, std::string& unitString) const;
std::string getSafeUserString() const;
static Quantity parse(const QString& string);
static Quantity parse(const std::string& string);
/// returns the unit of the quantity
const Unit& getUnit() const

View File

@@ -127,10 +127,10 @@ int QuantityPy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyErr_Clear(); // set by PyArg_ParseTuple()
char* string {};
if (PyArg_ParseTuple(args, "et", "utf-8", &string)) {
QString qstr = QString::fromUtf8(string);
std::string str(string);
PyMem_Free(string);
try {
*self = Quantity::parse(qstr);
*self = Quantity::parse(str);
}
catch (const Base::ParserError& e) {
PyErr_SetString(PyExc_ValueError, e.what());
@@ -142,7 +142,7 @@ int QuantityPy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyErr_Clear(); // set by PyArg_ParseTuple()
if (PyArg_ParseTuple(args, "det", &f, "utf-8", &string)) {
QString unit = QString::fromUtf8(string);
std::string unit(string);
PyMem_Free(string);
try {
*self = Quantity(f, unit);
@@ -161,15 +161,15 @@ int QuantityPy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyObject* QuantityPy::getUserPreferred(PyObject* /*args*/)
{
QString uus;
std::string uus;
double factor {};
Py::Tuple res(3);
QString uss = getQuantityPtr()->getUserString(factor, uus);
auto uss = getQuantityPtr()->getUserString(factor, uus);
res[0] = Py::String(uss.toUtf8(), "utf-8");
res[0] = Py::String(uss, "utf-8");
res[1] = Py::Float(factor);
res[2] = Py::String(uus.toUtf8(), "utf-8");
res[2] = Py::String(uus, "utf-8");
return Py::new_reference_to(res);
}
@@ -236,9 +236,9 @@ PyObject* QuantityPy::getValueAs(PyObject* args)
PyErr_Clear();
char* string {};
if (PyArg_ParseTuple(args, "et", "utf-8", &string)) {
QString qstr = QString::fromUtf8(string);
std::string str(string);
PyMem_Free(string);
quant = Quantity::parse(qstr);
quant = Quantity::parse(str);
}
}
@@ -633,7 +633,7 @@ void QuantityPy::setUnit(Py::Object arg)
Py::String QuantityPy::getUserString() const
{
return {getQuantityPtr()->getUserString().toUtf8(), "utf-8"};
return {getQuantityPtr()->getUserString(), "utf-8"};
}
Py::Dict QuantityPy::getFormat() const

View File

@@ -128,7 +128,7 @@ Unit::Unit() //NOLINT
Unit::Unit(const std::string& expr) // NOLINT
{
try {
*this = Quantity::parse(QString::fromStdString(expr)).getUnit();
*this = Quantity::parse(expr).getUnit();
}
catch (const Base::ParserError&) {
Val = 0;

View File

@@ -84,10 +84,10 @@ int UnitPy::PyInit(PyObject* args, PyObject* /*kwd*/)
// get string
char* string {};
if (PyArg_ParseTuple(args, "et", "utf-8", &string)) {
QString qstr = QString::fromUtf8(string);
std::string str(string);
PyMem_Free(string);
try {
*self = Quantity::parse(qstr).getUnit();
*self = Quantity::parse(str).getUnit();
return 0;
}
catch (const Base::ParserError& e) {

View File

@@ -184,7 +184,7 @@ QString UnitsApi::schemaTranslate(const Base::Quantity& quant, double& factor, Q
double UnitsApi::toDouble(PyObject* args, const Base::Unit& u)
{
if (PyUnicode_Check(args)) {
QString str = QString::fromUtf8(PyUnicode_AsUTF8(args));
std::string str(PyUnicode_AsUTF8(args));
// Parse the string
Quantity q = Quantity::parse(str);
if (q.getUnit() == u) {
@@ -207,7 +207,7 @@ Quantity UnitsApi::toQuantity(PyObject* args, const Base::Unit& u)
{
double d {};
if (PyUnicode_Check(args)) {
QString str = QString::fromUtf8(PyUnicode_AsUTF8(args));
std::string str(PyUnicode_AsUTF8(args));
// Parse the string
Quantity q = Quantity::parse(str);
d = q.getValue();

View File

@@ -87,10 +87,10 @@ PyObject* UnitsApi::sParseQuantity(PyObject* /*self*/, PyObject* args)
}
Quantity rtn;
QString qstr = QString::fromUtf8(pstr);
std::string str(pstr);
PyMem_Free(pstr);
try {
rtn = Quantity::parse(qstr);
rtn = Quantity::parse(str);
}
catch (const Base::ParserError&) {
PyErr_Format(PyExc_ValueError, "invalid unit expression \n");