Skip to content

Commit

Permalink
[ge0] Pack the Parser's return values into a struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpimenov authored and tatiana-yan committed Feb 17, 2020
1 parent d1ab27b commit 292febc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 49 deletions.
45 changes: 18 additions & 27 deletions ge0/ge0_tests/parser_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,25 @@ double GetLonEpsilon(size_t coordBytes)
void TestSuccess(char const * s, double lat, double lon, double zoom, char const * name)
{
Ge0Parser parser;
double parsedLat;
double parsedLon;
string parsedName;
double parsedZoomLevel;
bool const result = parser.Parse(s, parsedLat, parsedLon, parsedName, parsedZoomLevel);
Ge0Parser::Result parseResult;
bool const success = parser.Parse(s, parseResult);

TEST(result, (s, zoom, lat, lon, name));
TEST(success, (s, parseResult));

TEST_EQUAL(parsedName, string(name), (s));
TEST_EQUAL(parseResult.m_name, string(name), (s));
double const latEps = GetLatEpsilon(9);
double const lonEps = GetLonEpsilon(9);
TEST_ALMOST_EQUAL_ABS(parsedLat, lat, latEps, (s, zoom, lat, lon, name));
TEST_ALMOST_EQUAL_ABS(parsedLon, lon, lonEps, (s, zoom, lat, lon, name));
TEST_ALMOST_EQUAL_ABS(parsedZoomLevel, zoom, kZoomEps, (s, zoom, lat, lon, name));
double const lonEps = GetLonEpsilon(9);
TEST_ALMOST_EQUAL_ABS(parseResult.m_lat, lat, latEps, (s, parseResult));
TEST_ALMOST_EQUAL_ABS(parseResult.m_lon, lon, lonEps, (s, parseResult));
TEST_ALMOST_EQUAL_ABS(parseResult.m_zoomLevel, zoom, kZoomEps, (s, parseResult));
}

void TestFailure(char const * s)
{
Ge0Parser parser;
string name;
double lat;
double lon;
double zoomLevel;
bool const result = parser.Parse(s, lat, lon, name, zoomLevel);
TEST(!result, (s));
Ge0Parser::Result parseResult;
bool const success = parser.Parse(s, parseResult);
TEST(!success, (s, parseResult));
}

bool ConvergenceTest(double lat, double lon, double latEps, double lonEps)
Expand Down Expand Up @@ -225,21 +219,18 @@ UNIT_TEST(NameDecoding)
"d0%bd%d0%b8%d1%8e%3F";

Ge0Parser parser;
double parsedLat;
double parsedLon;
string parsedName;
double parsedZoomLevel;
bool const result = parser.Parse(url.c_str(), parsedLat, parsedLon, parsedName, parsedZoomLevel);
Ge0Parser::Result parseResult;
bool const success = parser.Parse(url.c_str(), parseResult);

TEST(result, (url, zoom, lat, lon, name));
TEST(success, (url, parseResult));

// Name would be valid but is too long.
TEST_NOT_EQUAL(parsedName, string(name), (url));
TEST_NOT_EQUAL(parseResult.m_name, string(name), (url));
double const latEps = GetLatEpsilon(9);
double const lonEps = GetLonEpsilon(9);
TEST_ALMOST_EQUAL_ABS(parsedLat, lat, latEps, (url, zoom, lat, lon, name));
TEST_ALMOST_EQUAL_ABS(parsedLon, lon, lonEps, (url, zoom, lat, lon, name));
TEST_ALMOST_EQUAL_ABS(parsedZoomLevel, zoom, kZoomEps, (url, zoom, lat, lon, name));
TEST_ALMOST_EQUAL_ABS(parseResult.m_lat, lat, latEps, (url, parseResult));
TEST_ALMOST_EQUAL_ABS(parseResult.m_lon, lon, lonEps, (url, parseResult));
TEST_ALMOST_EQUAL_ABS(parseResult.m_zoomLevel, zoom, kZoomEps, (url, parseResult));
}
}

Expand Down
29 changes: 20 additions & 9 deletions ge0/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/string_utils.hpp"

#include <algorithm>
#include <sstream>

using namespace std;

Expand All @@ -27,7 +28,7 @@ Ge0Parser::Ge0Parser()
}
}

bool Ge0Parser::Parse(string const & url, double & outLat, double & outLon, std::string & outName, double & outZoomLevel)
bool Ge0Parser::Parse(string const & url, Result & result)
{
// Original URL format:
//
Expand All @@ -43,14 +44,13 @@ bool Ge0Parser::Parse(string const & url, double & outLat, double & outLon, std:
for (string const & prefix : {"ge0://", "http://ge0.me/", "https://ge0.me/"})
{
if (strings::StartsWith(url, prefix))
return ParseAfterPrefix(url, prefix.size(), outLat, outLon, outName, outZoomLevel);
return ParseAfterPrefix(url, prefix.size(), result);
}

return false;
}

bool Ge0Parser::ParseAfterPrefix(string const & url, size_t from, double & outLat, double & outLon,
std::string & outName, double & outZoomLevel)
bool Ge0Parser::ParseAfterPrefix(string const & url, size_t from, Result & result)
{
size_t const kEncodedZoomAndCoordinatesLength = 10;
if (url.size() < from + kEncodedZoomAndCoordinatesLength)
Expand All @@ -66,20 +66,20 @@ bool Ge0Parser::ParseAfterPrefix(string const & url, size_t from, double & outLa
uint8_t const zoomI = DecodeBase64Char(url[posZoom]);
if (zoomI >= 64)
return false;
outZoomLevel = DecodeZoom(zoomI);
result.m_zoomLevel = DecodeZoom(zoomI);

if (!DecodeLatLon(url.substr(posLatLon, lengthLatLon), outLat, outLon))
if (!DecodeLatLon(url.substr(posLatLon, lengthLatLon), result.m_lat, result.m_lon))
return false;

ASSERT(mercator::ValidLon(outLon), (outLon));
ASSERT(mercator::ValidLat(outLat), (outLat));
ASSERT(mercator::ValidLat(result.m_lat), (result.m_lat));
ASSERT(mercator::ValidLon(result.m_lon), (result.m_lon));

if (url.size() >= posName)
{
CHECK_GREATER(posName, 0, ());
if (url[posName - 1] != '/')
return false;
outName = DecodeName(url.substr(posName, min(url.size() - posName, kMaxNameLength)));
result.m_name = DecodeName(url.substr(posName, min(url.size() - posName, kMaxNameLength)));
}

return true;
Expand Down Expand Up @@ -179,4 +179,15 @@ bool Ge0Parser::IsHexChar(char const a)
{
return ((a >= '0' && a <= '9') || (a >= 'A' && a <= 'F') || (a >= 'a' && a <= 'f'));
}

string DebugPrint(Ge0Parser::Result const & r)
{
ostringstream oss;
oss << "ParseResult [";
oss << "zoom=" << r.m_zoomLevel << ", ";
oss << "lat=" << r.m_lat << ", ";
oss << "lon=" << r.m_lon << ", ";
oss << "name=" << r.m_name << "]";
return oss.str();
}
} // namespace ge0
15 changes: 12 additions & 3 deletions ge0/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ namespace ge0
class Ge0Parser
{
public:
struct Result
{
double m_zoomLevel = 0.0;
double m_lat = 0.0;
double m_lon = 0.0;
std::string m_name;
};

Ge0Parser();

bool Parse(std::string const & url, double & outLat, double & outLon, std::string & outName, double & outZoomLevel);
bool Parse(std::string const & url, Result & result);

protected:
bool ParseAfterPrefix(std::string const & url, size_t from, double & outLat, double & outLon,
std::string & outName, double & outZoomLevel);
bool ParseAfterPrefix(std::string const & url, size_t from, Result & result);

uint8_t DecodeBase64Char(char const c);
static double DecodeZoom(uint8_t const zoomByte);
Expand All @@ -31,4 +38,6 @@ class Ge0Parser
private:
uint8_t m_base64ReverseCharTable[256];
};

std::string DebugPrint(Ge0Parser::Result const & r);
} // namespace ge0
9 changes: 5 additions & 4 deletions map/framework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2155,12 +2155,13 @@ bool Framework::ShowMapForURL(string const & url)
if (strings::StartsWith(url, "ge0"))
{
ge0::Ge0Parser parser;
double lat, lon, zoom;
ge0::Ge0Parser::Result parseResult;

if (parser.Parse(url, lat, lon, name, zoom))
if (parser.Parse(url, parseResult))
{
point = mercator::FromLatLon(lat, lon);
rect = df::GetRectForDrawScale(zoom, point);
point = mercator::FromLatLon(parseResult.m_lat, parseResult.m_lon);
rect = df::GetRectForDrawScale(parseResult.m_zoomLevel, point);
name = move(parseResult.m_name);
result = NEED_CLICK;
}
}
Expand Down
9 changes: 3 additions & 6 deletions search/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,10 @@ void Processor::SearchCoordinates()
string token;
while (iss >> token)
{
double lat;
double lon;
string unusedName;
double unusedZoomLevel;
ge0::Ge0Parser parser;
if (parser.Parse(token, lat, lon, unusedName, unusedZoomLevel))
emitUnique(lat, lon);
ge0::Ge0Parser::Result r;
if (parser.Parse(token, r))
emitUnique(r.m_lat, r.m_lon);

url::GeoURLInfo info(token);
if (info.IsValid())
Expand Down

0 comments on commit 292febc

Please sign in to comment.