Split QString generated from QGeoCoordinate.
-
I download coordinates from file created with QGeoCoordinate::toString(QGeoCoordinate::Degrees). It looks like lines with numbers, sometimes without altitude:
-27.46758°, 153.02789°, 28.1m
-27.46758°, 153.02789°
...
I need QStringList with just only string doubles for conversion:QStringList list = s.split( QRegularExpression(thisHatedRegExpString), QString::SkipEmptyParts ); return QGeoCoordinate( list.at(0).toDouble(), list.at(1).toDouble(), list.size() == 2 ? 0.0 : list.at(2).toDouble() );
To get toDouble() working I need clear string real values without "°" and "m". But how thisHatedRegExpString must look like?... The ", " keeps unneeded parts. I always hated this regular expression syntax.
-
Simply don't use a regex...
QVector<QStringRef> parts = s.splitRef(QLatin1String(", ")); if (parts.size() < 2 || parts.size() > 3) return QGeoCoordinate(); if (parts[0].endsWith('°') parts[0] = parts[0].chop(1); if (parts[1].endsWith('°') parts[1] = parts[1].chop(1); if (parts.size() == 3 && parts[2].endsWith('m')) parts[2] = parts[2].chop(1); return QGeoCoordinate(parts.at(0).toDouble(), parts.at(1).toDouble(), parts.size() == 2 ? 0.0 : parts.at(2).toDouble() );
-
@Christian-Ehrlicher in this case simpler use QStringList and QRegularExpression(", "). The code
QStringList list = s.split( QRegularExpression(", "), QString::SkipEmptyParts );
gives list == {"-27.46758°", "153.02789°", "28.1m"} or {"-27.46758°", "153.02789°"}. No endsWith() checking needed - if line exists it always ends with '°' or 'm'. But I wanted perform all in one split operation.
-
@Gourmet said in Split QString generated from QGeoCoordinate.:
But I wanted perform all in one split operation.
Why? To make it slower? To be someone who wrote a regexp?
-
@Christian-Ehrlicher to make code shorter...
-
@Gourmet said in Split QString generated from QGeoCoordinate.:
to make code shorter...
Readable code is more important than short code.
https://softwareengineering.stackexchange.com/questions/203684/is-fewer-lines-of-code-always-better
-
@Gourmet said in Split QString generated from QGeoCoordinate.:
less to read means easer to read.
So you say me you can read a regexp (which you are not aware to create on your own so I guess you won't understand it later on) is easier to understand than simple string modifications? Nice try...
-
@Christian-Ehrlicher regexp has one specific feature - it is much more easer to read and understand than to write. This is because of it's "encapsulation" - when you see it you already know what it means. Often it's not needed parse it when reading - just treat as whole thing.
-
I'll go a completely different way
QGeoCoordinate result(0.0,0.0,0.0); const QRegularExpression dobleMatchingRegExp(QStringLiteral("[-+]?\\d*\\.?\\d+")); QRegularExpressionMatchIterator i = dobleMatchingRegExp.globalMatch(s); if(i.hasNext()) result.setLatitude(i.next().capturedRef(0).toDouble()); if(i.hasNext()) result.setLongitude(i.next().capturedRef(0).toDouble()); if(i.hasNext()) result.setAltitude(i.next().capturedRef(0).toDouble()); return result;
Sources: