mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-05-04 17:30:12 +08:00
refs #212, fixed parsing for PQs
* trimmed strings * renamed to parseToVariant methods avos ambiguity with template method
This commit is contained in:
@@ -90,14 +90,17 @@ namespace BlackMisc
|
|||||||
qDBusRegisterMetaType<CPqString>();
|
qDBusRegisterMetaType<CPqString>();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant CPqString::parse(const QString &value)
|
/*
|
||||||
|
* Parse
|
||||||
|
*/
|
||||||
|
QVariant CPqString::parseToVariant(const QString &value)
|
||||||
{
|
{
|
||||||
QVariant v;
|
QVariant v;
|
||||||
if (value.isEmpty()) return v;
|
if (value.isEmpty()) return v;
|
||||||
QRegExp rx("^([-+]?[0-9]*\\.?[0-9]+)\\s*(\\D*)$");
|
QRegExp rx("^([-+]?[0-9]*\\.?[0-9]+)\\s*(\\D*)$");
|
||||||
if (rx.indexIn(value) < 0) return v;
|
if (rx.indexIn(value) < 0) return v;
|
||||||
QString number = rx.cap(1);
|
QString number = rx.cap(1).trimmed();
|
||||||
QString unit = rx.cap(2);
|
QString unit = rx.cap(2).trimmed();
|
||||||
if (unit.isEmpty() || number.isEmpty()) return v;
|
if (unit.isEmpty() || number.isEmpty()) return v;
|
||||||
bool success;
|
bool success;
|
||||||
double numberD = number.toDouble(&success);
|
double numberD = number.toDouble(&success);
|
||||||
|
|||||||
@@ -74,15 +74,16 @@ namespace BlackMisc
|
|||||||
//! \brief Register metadata
|
//! \brief Register metadata
|
||||||
static void registerMetadata();
|
static void registerMetadata();
|
||||||
|
|
||||||
//! \brief parse a string value like "100m", "10.3Mhz"
|
//! Parse a string value like "100m", "10.3Mhz"
|
||||||
static QVariant parse(const QString &value);
|
static QVariant parseToVariant(const QString &value);
|
||||||
|
|
||||||
//! \brief parse into concrete type
|
//! Parse into concrete type
|
||||||
template <class PQ> static const PQ parse(const QString &symbol)
|
template <class PQ> static PQ parse(const QString &value)
|
||||||
{
|
{
|
||||||
PQ invalid;
|
PQ invalid;
|
||||||
if (symbol.isEmpty()) return invalid;
|
invalid.setNull();
|
||||||
QVariant qv = CPqString::parse(symbol);
|
if (value.isEmpty()) return invalid;
|
||||||
|
QVariant qv = CPqString::parseToVariant(value);
|
||||||
if (!qv.isNull() && qv.canConvert<PQ>())
|
if (!qv.isNull() && qv.canConvert<PQ>())
|
||||||
{
|
{
|
||||||
return qv.value<PQ>();
|
return qv.value<PQ>();
|
||||||
|
|||||||
Reference in New Issue
Block a user