forked from organicmaps/organicmaps
[search] Add search::MatchLatLon().
This commit is contained in:
parent
42a0ee0812
commit
bf986ed042
5 changed files with 119 additions and 0 deletions
54
search/latlon_match.cpp
Normal file
54
search/latlon_match.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "latlon_match.hpp"
|
||||
#include "../std/cstdlib.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template <typename CharT> void Skip(CharT * & s)
|
||||
{
|
||||
// 0xC2 - commond Unicode first byte for ANSI (not ASCII!) symbols.
|
||||
// 0xC2,0xB0 - degree symbol.
|
||||
while (*s == ' ' || *s == ',' || *s == ';' || *s == ':' || *s == '.' || *s == '(' || *s == ')' ||
|
||||
*s == char(0xB0) || *s == char(0xC2))
|
||||
++s;
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
bool search::MatchLatLon(string const & str, double & latRes, double & lonRes)
|
||||
{
|
||||
char const * s = str.c_str();
|
||||
|
||||
Skip(s);
|
||||
if (!*s)
|
||||
return false;
|
||||
|
||||
char * s1;
|
||||
double const lat = strtod(s, &s1);
|
||||
if (!s1 || !*s1)
|
||||
return false;
|
||||
|
||||
Skip(s1);
|
||||
if (!*s1)
|
||||
return false;
|
||||
|
||||
char * s2;
|
||||
double lon = strtod(s1, &s2);
|
||||
if (!s2)
|
||||
return false;
|
||||
|
||||
Skip(s2);
|
||||
if (*s2)
|
||||
return false;
|
||||
|
||||
if (lat < -90 || lat > 90)
|
||||
return false;
|
||||
if (lon < -180 || lon > 360)
|
||||
return false;
|
||||
if (lon > 180)
|
||||
lon = lon - 360;
|
||||
|
||||
latRes = lat;
|
||||
lonRes = lon;
|
||||
return true;
|
||||
}
|
10
search/latlon_match.hpp
Normal file
10
search/latlon_match.hpp
Normal file
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
#include "../base/base.hpp"
|
||||
#include "../std/string.hpp"
|
||||
|
||||
namespace search
|
||||
{
|
||||
|
||||
bool MatchLatLon(string const & s, double & lat, double & lon);
|
||||
|
||||
} // namespace search
|
|
@ -17,6 +17,7 @@ HEADERS += \
|
|||
query.hpp \
|
||||
result.hpp \
|
||||
string_match.hpp \
|
||||
latlon_match.hpp
|
||||
|
||||
SOURCES += \
|
||||
delimiters.cpp \
|
||||
|
@ -26,3 +27,4 @@ SOURCES += \
|
|||
query.cpp \
|
||||
result.cpp \
|
||||
string_match.cpp \
|
||||
latlon_match.cpp
|
||||
|
|
52
search/search_tests/latlon_match_test.cpp
Normal file
52
search/search_tests/latlon_match_test.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
#include "../../testing/testing.hpp"
|
||||
#include "../latlon_match.hpp"
|
||||
#include "../../std/utility.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
pair<double, double> TestLatLonMatchSuccessful(string const & s)
|
||||
{
|
||||
double lat = -3500;
|
||||
double lon = -3500;
|
||||
TEST(search::MatchLatLon(s, lat, lon), (s, lat, lon));
|
||||
return make_pair(lat, lon);
|
||||
}
|
||||
|
||||
void TestLatLonFailed(string const & s)
|
||||
{
|
||||
double lat = -3500;
|
||||
double lon = -3500;
|
||||
TEST(!search::MatchLatLon(s, lat, lon), (s, lat, lon));
|
||||
TEST_EQUAL(lat, -3500, ());
|
||||
TEST_EQUAL(lon, -3500, ());
|
||||
}
|
||||
|
||||
} // unnamed namespace
|
||||
|
||||
UNIT_TEST(LatLonMatch)
|
||||
{
|
||||
TestLatLonFailed("");
|
||||
TestLatLonFailed("0.0");
|
||||
TestLatLonFailed("2.0 sadas 2.0");
|
||||
TestLatLonFailed("90.1 0.0");
|
||||
TestLatLonFailed("-90.1 0.0");
|
||||
TestLatLonFailed("0.0 361");
|
||||
TestLatLonFailed("0.0 -181.0");
|
||||
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2.0 3.0"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2.0, 3.0"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2.0;3.0"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2;3.0"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.03232424), TestLatLonMatchSuccessful("2.0;3.03232424"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2 3"), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("2 3."), ());
|
||||
TEST_EQUAL(make_pair(2.0, 3.0), TestLatLonMatchSuccessful("(2.0, 3.0)"), ());
|
||||
TEST_EQUAL(make_pair(0.0, 0.0), TestLatLonMatchSuccessful("0.0 0.0"), ());
|
||||
TEST_EQUAL(make_pair(0.0, 180.0), TestLatLonMatchSuccessful("0.0 180"), ());
|
||||
TEST_EQUAL(make_pair(0.0, -179.0), TestLatLonMatchSuccessful("0.0 181"), ());
|
||||
TEST_EQUAL(make_pair(0.0, -170.0), TestLatLonMatchSuccessful("0.0 -170"), ());
|
||||
TEST_EQUAL(make_pair(0.0, -180.0), TestLatLonMatchSuccessful("0.0 -180"), ());
|
||||
TEST_EQUAL(make_pair(0.0, -160.0), TestLatLonMatchSuccessful("0.0 200"), ());
|
||||
TEST_EQUAL(make_pair(0.0, 0.0), TestLatLonMatchSuccessful("0.0 360"), ());
|
||||
}
|
|
@ -22,6 +22,7 @@ SOURCES += \
|
|||
keyword_matcher_test.cpp \
|
||||
query_test.cpp \
|
||||
string_match_test.cpp \
|
||||
latlon_match_test.cpp \
|
||||
|
||||
HEADERS += \
|
||||
match_cost_mock.hpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue