mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +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;
|
||||
bool success;
|
||||
double numberD;
|
||||
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;
|
||||
}
|
||||
double numberD = parseNumber(number, success, mode);
|
||||
if (!success) return v;
|
||||
|
||||
if (CMeasurementUnit::isValidUnitSymbol<CAccelerationUnit>(unit))
|
||||
@@ -119,5 +103,27 @@ namespace BlackMisc
|
||||
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
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace BlackMisc
|
||||
PQ invalid;
|
||||
invalid.setNull();
|
||||
if (value.isEmpty()) return invalid;
|
||||
QVariant qv = CPqString::parseToVariant(value, mode);
|
||||
QVariant qv = parseToVariant(value, mode);
|
||||
if (!qv.isNull() && qv.canConvert<PQ>())
|
||||
{
|
||||
return qv.value<PQ>();
|
||||
@@ -71,6 +71,9 @@ namespace BlackMisc
|
||||
return invalid;
|
||||
}
|
||||
|
||||
//! Locale aware parsing
|
||||
static double parseNumber(const QString &number, bool &success, SeparatorMode mode = SeparatorsCLocale);
|
||||
|
||||
private:
|
||||
BLACK_ENABLE_TUPLE_CONVERSION(CPqString)
|
||||
QString m_string;
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
*/
|
||||
|
||||
#include "simplecommandparser.h"
|
||||
#include "pqstring.h"
|
||||
|
||||
using namespace BlackMisc::PhysicalQuantities;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -78,7 +81,7 @@ namespace BlackMisc
|
||||
const QString p = this->part(index);
|
||||
if (p.isEmpty()) return false;
|
||||
bool ok;
|
||||
p.toDouble(&ok);
|
||||
CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -96,7 +99,7 @@ namespace BlackMisc
|
||||
const QString p = this->part(index);
|
||||
if (p.isEmpty()) return def;
|
||||
bool ok;
|
||||
double d = p.toDouble(&ok);
|
||||
double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||
return ok ? d : def;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user