mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 09:15:34 +08:00
refs #77, used locale aware number parsing for command parser
This commit is contained in:
committed by
Roland Winklmeier
parent
c173a30a94
commit
eb13c9d4ae
@@ -44,23 +44,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
if (unit.isEmpty() || number.isEmpty()) return v;
|
if (unit.isEmpty() || number.isEmpty()) return v;
|
||||||
bool success;
|
bool success;
|
||||||
double numberD;
|
double numberD = parseNumber(number, success, mode);
|
||||||
switch (mode)
|
|
||||||
{
|
|
||||||
case SeparatorsLocale:
|
|
||||||
numberD = QLocale::system().toDouble(number, &success);
|
|
||||||
break;
|
|
||||||
case SeparatorsCLocale:
|
|
||||||
numberD = number.toDouble(&success);
|
|
||||||
break;
|
|
||||||
case SeparatorsBestGuess:
|
|
||||||
numberD = number.toDouble(&success);
|
|
||||||
if (!success) numberD = QLocale::system().toDouble(number, &success);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
qFatal("Wrong mode");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (!success) return v;
|
if (!success) return v;
|
||||||
|
|
||||||
if (CMeasurementUnit::isValidUnitSymbol<CAccelerationUnit>(unit))
|
if (CMeasurementUnit::isValidUnitSymbol<CAccelerationUnit>(unit))
|
||||||
@@ -119,5 +103,27 @@ namespace BlackMisc
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
double CPqString::parseNumber(const QString &number, bool &success, CPqString::SeparatorMode mode)
|
||||||
|
{
|
||||||
|
double numberD = -1;
|
||||||
|
switch (mode)
|
||||||
|
{
|
||||||
|
case SeparatorsLocale:
|
||||||
|
numberD = QLocale::system().toDouble(number, &success);
|
||||||
|
break;
|
||||||
|
case SeparatorsCLocale:
|
||||||
|
numberD = number.toDouble(&success);
|
||||||
|
break;
|
||||||
|
case SeparatorsBestGuess:
|
||||||
|
numberD = number.toDouble(&success);
|
||||||
|
if (!success) numberD = QLocale::system().toDouble(number, &success);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qFatal("Wrong mode");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return numberD;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ namespace BlackMisc
|
|||||||
PQ invalid;
|
PQ invalid;
|
||||||
invalid.setNull();
|
invalid.setNull();
|
||||||
if (value.isEmpty()) return invalid;
|
if (value.isEmpty()) return invalid;
|
||||||
QVariant qv = CPqString::parseToVariant(value, mode);
|
QVariant qv = parseToVariant(value, mode);
|
||||||
if (!qv.isNull() && qv.canConvert<PQ>())
|
if (!qv.isNull() && qv.canConvert<PQ>())
|
||||||
{
|
{
|
||||||
return qv.value<PQ>();
|
return qv.value<PQ>();
|
||||||
@@ -71,6 +71,9 @@ namespace BlackMisc
|
|||||||
return invalid;
|
return invalid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Locale aware parsing
|
||||||
|
static double parseNumber(const QString &number, bool &success, SeparatorMode mode = SeparatorsCLocale);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BLACK_ENABLE_TUPLE_CONVERSION(CPqString)
|
BLACK_ENABLE_TUPLE_CONVERSION(CPqString)
|
||||||
QString m_string;
|
QString m_string;
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "simplecommandparser.h"
|
#include "simplecommandparser.h"
|
||||||
|
#include "pqstring.h"
|
||||||
|
|
||||||
|
using namespace BlackMisc::PhysicalQuantities;
|
||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
@@ -78,7 +81,7 @@ namespace BlackMisc
|
|||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) return false;
|
if (p.isEmpty()) return false;
|
||||||
bool ok;
|
bool ok;
|
||||||
p.toDouble(&ok);
|
CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +99,7 @@ namespace BlackMisc
|
|||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) return def;
|
if (p.isEmpty()) return def;
|
||||||
bool ok;
|
bool ok;
|
||||||
double d = p.toDouble(&ok);
|
double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||||
return ok ? d : def;
|
return ok ? d : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user