diff --git a/bin/offline.cpp b/bin/offline.cpp index 5123e7b92..0d0fe5728 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -53,6 +53,27 @@ mapbox::geometry::geometry parseGeometry(const std::string& json) { }); } +std::ostream& operator<<(std::ostream& os, mbgl::Response::Error::Reason r) { + switch (r) { + case mbgl::Response::Error::Reason::Success: + return os << "Response::Error::Reason::Success"; + case mbgl::Response::Error::Reason::NotFound: + return os << "Response::Error::Reason::NotFound"; + case mbgl::Response::Error::Reason::Server: + return os << "Response::Error::Reason::Server"; + case mbgl::Response::Error::Reason::Connection: + return os << "Response::Error::Reason::Connection"; + case mbgl::Response::Error::Reason::RateLimit: + return os << "Response::Error::Reason::RateLimit"; + case mbgl::Response::Error::Reason::Other: + return os << "Response::Error::Reason::Other"; + } + + // The above switch is exhaustive, but placate GCC nonetheless: + assert(false); + return os; +} + int main(int argc, char *argv[]) { args::ArgumentParser argumentParser("Mapbox GL offline tool"); args::HelpFlag helpFlag(argumentParser, "help", "Display this help menu", {'h', "help"}); diff --git a/cmake/core-files.txt b/cmake/core-files.txt index 9e83ceb66..e7fd7d81c 100644 --- a/cmake/core-files.txt +++ b/cmake/core-files.txt @@ -752,6 +752,7 @@ src/mbgl/util/math.hpp src/mbgl/util/offscreen_texture.cpp src/mbgl/util/offscreen_texture.hpp src/mbgl/util/premultiply.cpp +src/mbgl/util/rapidjson.cpp src/mbgl/util/rapidjson.hpp src/mbgl/util/rect.hpp src/mbgl/util/std.hpp diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp index 508400141..07b059e43 100644 --- a/include/mbgl/storage/response.hpp +++ b/include/mbgl/storage/response.hpp @@ -69,6 +69,4 @@ public: Error(Reason, std::string = "", optional = {}); }; -std::ostream& operator<<(std::ostream&, Response::Error::Reason); - } // namespace mbgl diff --git a/platform/android/src/conversion/constant.cpp b/platform/android/src/conversion/constant.cpp index eaff434db..a609dc7c6 100644 --- a/platform/android/src/conversion/constant.cpp +++ b/platform/android/src/conversion/constant.cpp @@ -1,7 +1,7 @@ #include "constant.hpp" #include "collection.hpp" -#include +#include namespace mbgl { namespace android { @@ -24,9 +24,18 @@ Result>> Converter>, std::stri } Result>> Converter>, Color>::operator()(jni::JNIEnv& env, const Color& value) const { - std::stringstream sstream; - sstream << "rgba(" << value.r << ", " << value.g << ", " << value.b << ", " << value.a << ")"; - return jni::Make(env, sstream.str()); + std::string result; + result.reserve(32); + result += "rgba("; + result += util::toString(value.r); + result += ", "; + result += util::toString(value.g); + result += ", "; + result += util::toString(value.b); + result += ", "; + result += util::toString(value.a); + result += ")"; + return jni::Make(env, result); } Result>> Converter>, style::expression::Formatted>::operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const { diff --git a/platform/node/test/js/map.test.js b/platform/node/test/js/map.test.js index 4a71d7204..b21c1519e 100644 --- a/platform/node/test/js/map.test.js +++ b/platform/node/test/js/map.test.js @@ -317,7 +317,7 @@ test('Map', function(t) { t.throws(function() { map.load('foo bar'); - }, /Failed to parse style: 1 - Invalid value./); + }, /Failed to parse style: Invalid value. at offset 1/); t.throws(function() { map.load('""'); @@ -349,7 +349,7 @@ test('Map', function(t) { t.throws(function() { map.load('invalid'); - }, /Failed to parse style: 0 - Invalid value./); + }, /Failed to parse style: Invalid value. at offset 0/); }); t.test('accepts an empty stylesheet string', function(t) { diff --git a/src/mbgl/algorithm/generate_clip_ids.cpp b/src/mbgl/algorithm/generate_clip_ids.cpp index 287d2a408..aefa55b92 100644 --- a/src/mbgl/algorithm/generate_clip_ids.cpp +++ b/src/mbgl/algorithm/generate_clip_ids.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index ba14655ca..1bd6f987e 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -4,7 +4,6 @@ #include #include -#include #include namespace mbgl { diff --git a/src/mbgl/renderer/sources/render_raster_dem_source.cpp b/src/mbgl/renderer/sources/render_raster_dem_source.cpp index fbf2c09d1..2283a0e78 100644 --- a/src/mbgl/renderer/sources/render_raster_dem_source.cpp +++ b/src/mbgl/renderer/sources/render_raster_dem_source.cpp @@ -4,7 +4,6 @@ #include #include #include -#include namespace mbgl { diff --git a/src/mbgl/sprite/sprite_parser.cpp b/src/mbgl/sprite/sprite_parser.cpp index 1a36e3e99..99e2b0c8c 100644 --- a/src/mbgl/sprite/sprite_parser.cpp +++ b/src/mbgl/sprite/sprite_parser.cpp @@ -9,7 +9,6 @@ #include #include -#include namespace mbgl { @@ -91,9 +90,7 @@ std::vector> parseSprite(const std::string& encode JSDocument doc; doc.Parse<0>(json.c_str()); if (doc.HasParseError()) { - std::stringstream message; - message << "Failed to parse JSON: " << rapidjson::GetParseError_En(doc.GetParseError()) << " at offset " << doc.GetErrorOffset(); - throw std::runtime_error(message.str()); + throw std::runtime_error("Failed to parse JSON: " + formatJSONParseError(doc)); } else if (!doc.IsObject()) { throw std::runtime_error("Sprite JSON root must be an object"); } else { diff --git a/src/mbgl/storage/response.cpp b/src/mbgl/storage/response.cpp index 222f55db8..ba075c326 100644 --- a/src/mbgl/storage/response.cpp +++ b/src/mbgl/storage/response.cpp @@ -1,8 +1,4 @@ #include -#include - -#include -#include namespace mbgl { @@ -26,25 +22,4 @@ Response::Error::Error(Reason reason_, std::string message_, optional : reason(reason_), message(std::move(message_)), retryAfter(std::move(retryAfter_)) { } -std::ostream& operator<<(std::ostream& os, Response::Error::Reason r) { - switch (r) { - case Response::Error::Reason::Success: - return os << "Response::Error::Reason::Success"; - case Response::Error::Reason::NotFound: - return os << "Response::Error::Reason::NotFound"; - case Response::Error::Reason::Server: - return os << "Response::Error::Reason::Server"; - case Response::Error::Reason::Connection: - return os << "Response::Error::Reason::Connection"; - case Response::Error::Reason::RateLimit: - return os << "Response::Error::Reason::RateLimit"; - case Response::Error::Reason::Other: - return os << "Response::Error::Reason::Other"; - } - - // The above switch is exhaustive, but placate GCC nonetheless: - assert(false); - return os; -} - } // namespace mbgl diff --git a/src/mbgl/style/conversion/json.hpp b/src/mbgl/style/conversion/json.hpp index 3a7bf2b55..503f60a38 100644 --- a/src/mbgl/style/conversion/json.hpp +++ b/src/mbgl/style/conversion/json.hpp @@ -4,7 +4,6 @@ #include #include -#include namespace mbgl { namespace style { @@ -16,9 +15,7 @@ optional convertJSON(const std::string& json, Error& error, Args&&...args) { document.Parse<0>(json.c_str()); if (document.HasParseError()) { - std::stringstream message; - message << document.GetErrorOffset() << " - " << rapidjson::GetParseError_En(document.GetParseError()); - error = { message.str() }; + error = { formatJSONParseError(document) }; return {}; } diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp index 114a666f0..77cf23bad 100644 --- a/src/mbgl/style/parser.cpp +++ b/src/mbgl/style/parser.cpp @@ -18,7 +18,6 @@ #include #include -#include namespace mbgl { namespace style { @@ -30,11 +29,7 @@ StyleParseResult Parser::parse(const std::string& json) { document.Parse<0>(json.c_str()); if (document.HasParseError()) { - std::stringstream message; - message << document.GetErrorOffset() << " - " - << rapidjson::GetParseError_En(document.GetParseError()); - - return std::make_exception_ptr(std::runtime_error(message.str())); + return std::make_exception_ptr(std::runtime_error(formatJSONParseError(document))); } if (!document.IsObject()) { diff --git a/src/mbgl/text/shaping.cpp b/src/mbgl/text/shaping.cpp index 6d10085ad..3a6335955 100644 --- a/src/mbgl/text/shaping.cpp +++ b/src/mbgl/text/shaping.cpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace mbgl { diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index 9743cd3f7..80b3e970b 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -19,8 +19,6 @@ #include #include -#include - namespace mbgl { using namespace style; diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index b823b727a..6c36c152c 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -6,7 +6,6 @@ #include #include -#include #include namespace { diff --git a/src/mbgl/util/rapidjson.cpp b/src/mbgl/util/rapidjson.cpp new file mode 100644 index 000000000..32a3c0b92 --- /dev/null +++ b/src/mbgl/util/rapidjson.cpp @@ -0,0 +1,13 @@ +#include +#include + +namespace mbgl { + +std::string formatJSONParseError(const JSDocument& doc) { + return std::string{ rapidjson::GetParseError_En(doc.GetParseError()) } + " at offset " + + util::toString(doc.GetErrorOffset()); +} + +} // namespace mbgl + + diff --git a/src/mbgl/util/rapidjson.hpp b/src/mbgl/util/rapidjson.hpp index 7262e8545..2fb2a07c9 100644 --- a/src/mbgl/util/rapidjson.hpp +++ b/src/mbgl/util/rapidjson.hpp @@ -8,4 +8,6 @@ namespace mbgl { using JSDocument = rapidjson::GenericDocument, rapidjson::CrtAllocator>; using JSValue = rapidjson::GenericValue, rapidjson::CrtAllocator>; +std::string formatJSONParseError(const JSDocument&); + } // namespace mbgl diff --git a/src/mbgl/util/stopwatch.cpp b/src/mbgl/util/stopwatch.cpp index 3883d8535..6db9da07e 100644 --- a/src/mbgl/util/stopwatch.cpp +++ b/src/mbgl/util/stopwatch.cpp @@ -4,7 +4,6 @@ #include #include -#include #include namespace mbgl { diff --git a/src/mbgl/util/stopwatch.hpp b/src/mbgl/util/stopwatch.hpp index 0c91342a5..daa313f70 100644 --- a/src/mbgl/util/stopwatch.hpp +++ b/src/mbgl/util/stopwatch.hpp @@ -4,7 +4,10 @@ #include #include + +#ifdef MBGL_TIMING #include +#endif namespace mbgl { namespace util { diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index cd15c5c27..a21f9c7e7 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -237,7 +237,7 @@ TEST(Map, SetStyleInvalidJSON) { auto observer = Log::removeObserver(); auto flo = static_cast(observer.get()); EXPECT_EQ(1u, flo->count({ EventSeverity::Error, Event::ParseStyle, -1, - "Failed to parse style: 0 - Invalid value." })); + "Failed to parse style: Invalid value. at offset 0" })); auto unchecked = flo->unchecked(); EXPECT_TRUE(unchecked.empty()) << unchecked; } diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index 6cb01146f..abd435023 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -127,7 +127,7 @@ TEST(Source, LoadingCorrupt) { test.styleObserver.sourceError = [&] (Source& source, std::exception_ptr error) { EXPECT_EQ("source", source.getID()); - EXPECT_EQ("0 - Invalid value.", util::toString(error)); + EXPECT_EQ("Invalid value. at offset 0", util::toString(error)); test.end(); };