From b1d6a41c65967b54912fd9621d581f051b1524a2 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 27 Jun 2018 14:29:26 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20a=20bug=20in=20lat-lon=20parsing=20-=20ac?= =?UTF-8?q?cept=20=E2=80=98*=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, trailing * symbols confused the parser. --- simgear/misc/strutils.cxx | 14 ++++++++++---- simgear/misc/strutils_test.cxx | 10 ++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) 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));