Fix a bug in lat-lon parsing - accept ‘*’

Previously, trailing * symbols confused the parser.
This commit is contained in:
James Turner
2018-06-27 14:29:26 +01:00
parent a4e2fdfad2
commit b1d6a41c65
2 changed files with 20 additions and 4 deletions

View File

@@ -1137,7 +1137,7 @@ bool matchPropPathToTemplate(const std::string& path, const std::string& templat
bool parseStringAsLatLonValue(const std::string& s, double& degrees)
{
string ss = simplify(s);
auto spacePos = ss.find(' ');
auto spacePos = ss.find_first_of(" *");
if (spacePos == std::string::npos) {
degrees = std::stof(ss);
@@ -1149,10 +1149,16 @@ bool parseStringAsLatLonValue(const std::string& s, double& degrees)
// check for minutes marker
auto quotePos = ss.find('\'');
if (quotePos == std::string::npos) {
minutes = std::stof(ss.substr(spacePos));
const auto minutesStr = ss.substr(spacePos+1);
if (!minutesStr.empty()) {
minutes = std::stof(minutesStr);
}
} else {
minutes = std::stof(ss.substr(spacePos, quotePos - spacePos));
seconds = std::stof(ss.substr(quotePos+1));
minutes = std::stof(ss.substr(spacePos+1, quotePos - spacePos));
const auto secondsStr = ss.substr(quotePos+1);
if (!secondsStr.empty()) {
seconds = std::stof(secondsStr);
}
}
if ((seconds < 0.0) || (minutes < 0.0)) {

View File

@@ -627,10 +627,20 @@ void test_parseGeod()
SG_CHECK_EQUAL_EP(a.getLongitudeDeg(), -3.0);
SG_CHECK_EQUAL_EP2(a.getLatitudeDeg(), 56.12, 1e-4);
// trailing degrees
SG_VERIFY(strutils::parseStringAsGeod("56.12*,-3.0*", &a));
SG_CHECK_EQUAL_EP(a.getLongitudeDeg(), -3.0);
SG_CHECK_EQUAL_EP2(a.getLatitudeDeg(), 56.12, 1e-4);
// embedded whitepace, DMS notation, NSEW notation
SG_VERIFY(strutils::parseStringAsGeod("\t40 30'50\"S, 12 34'56\"W ", &a));
SG_CHECK_EQUAL_EP2(a.getLongitudeDeg(), -12.58222222, 1e-4);
SG_CHECK_EQUAL_EP2(a.getLatitudeDeg(), -40.5138888, 1e-4);
// embedded whitepace, DMS notation, NSEW notation, degrees symbol
SG_VERIFY(strutils::parseStringAsGeod("\t40*30'50\"S, 12*34'56\"W ", &a));
SG_CHECK_EQUAL_EP2(a.getLongitudeDeg(), -12.58222222, 1e-4);
SG_CHECK_EQUAL_EP2(a.getLatitudeDeg(), -40.5138888, 1e-4);
// signed degrees-minutes
SG_VERIFY(strutils::parseStringAsGeod("-45 27.89,-12 34.56", &a));