diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 54b129d3..cb51fff7 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -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)) { diff --git a/simgear/misc/strutils_test.cxx b/simgear/misc/strutils_test.cxx index 8d17e0ed..00fef42c 100644 --- a/simgear/misc/strutils_test.cxx +++ b/simgear/misc/strutils_test.cxx @@ -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));