diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2018-10-17 16:28:31 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2018-10-23 12:23:40 +0200 |
commit | c87c25cc8407c33e4d88390836a9a52a1626c19e (patch) | |
tree | 25bc1f903144ad754116037f04de8c2e5cbbec7c | |
parent | 2766e64973b5f9eadf2e8a1cbbe43334e3e76d76 (diff) | |
download | qtlocation-mapboxgl-c87c25cc8407c33e4d88390836a9a52a1626c19e.tar.gz |
[core] don't use <sstream> and <iomanip> for string construction
-rw-r--r-- | src/mbgl/programs/program_parameters.cpp | 30 | ||||
-rw-r--r-- | src/mbgl/shaders/shaders.cpp | 15 | ||||
-rw-r--r-- | src/mbgl/util/url.cpp | 20 | ||||
-rw-r--r-- | test/util/url.test.cpp | 8 |
4 files changed, 43 insertions, 30 deletions
diff --git a/src/mbgl/programs/program_parameters.cpp b/src/mbgl/programs/program_parameters.cpp index e76ec4be71..2c70062e08 100644 --- a/src/mbgl/programs/program_parameters.cpp +++ b/src/mbgl/programs/program_parameters.cpp @@ -1,7 +1,5 @@ #include <mbgl/programs/program_parameters.hpp> - -#include <iomanip> -#include <sstream> +#include <mbgl/util/string.hpp> namespace mbgl { @@ -9,14 +7,15 @@ ProgramParameters::ProgramParameters(const float pixelRatio, const bool overdraw, optional<std::string> cacheDir_) : defines([&] { - std::ostringstream ss; - ss.imbue(std::locale("C")); - ss.setf(std::ios_base::showpoint); - ss << "#define DEVICE_PIXEL_RATIO " << pixelRatio << std::endl; + std::string result; + result.reserve(32); + result += "#define DEVICE_PIXEL_RATIO "; + result += util::toString(pixelRatio, true); + result += "\n"; if (overdraw) { - ss << "#define OVERDRAW_INSPECTOR" << std::endl; + result += "#define OVERDRAW_INSPECTOR\n"; } - return ss.str(); + return result; }()), cacheDir(std::move(cacheDir_)) { } @@ -29,10 +28,15 @@ optional<std::string> ProgramParameters::cachePath(const char* name) const { if (!cacheDir) { return {}; } else { - std::ostringstream ss; - ss << *cacheDir << "/com.mapbox.gl.shader." << name << "." << std::setfill('0') - << std::setw(sizeof(size_t) * 2) << std::hex << std::hash<std::string>()(defines) << ".pbf"; - return ss.str(); + std::string result; + result.reserve(cacheDir->length() + 64); + result += *cacheDir; + result += "/com.mapbox.gl.shader."; + result += name; + result += "."; + result += util::toHex(std::hash<std::string>()(defines)); + result += ".pbf"; + return result; } } diff --git a/src/mbgl/shaders/shaders.cpp b/src/mbgl/shaders/shaders.cpp index 2e5a318024..04cfda35cc 100644 --- a/src/mbgl/shaders/shaders.cpp +++ b/src/mbgl/shaders/shaders.cpp @@ -1,10 +1,9 @@ #include <mbgl/shaders/shaders.hpp> #include <mbgl/shaders/preludes.hpp> #include <mbgl/programs/program_parameters.hpp> +#include <mbgl/util/string.hpp> #include <cassert> -#include <sstream> -#include <iomanip> namespace mbgl { namespace shaders { @@ -18,12 +17,12 @@ std::string vertexSource(const ProgramParameters& parameters, const char* vertex } std::string programIdentifier(const std::string& vertexSource, const std::string& fragmentSource) { - std::ostringstream ss; - ss << std::setfill('0') << std::setw(sizeof(size_t) * 2) << std::hex; - ss << std::hash<std::string>()(vertexSource); - ss << std::hash<std::string>()(fragmentSource); - ss << "v2"; - return ss.str(); + std::string result; + result.reserve(sizeof(size_t) * 2 * 2 + 2 + 1); + result += util::toHex(std::hash<std::string>()(vertexSource)); + result += util::toHex(std::hash<std::string>()(fragmentSource)); + result += "v2"; + return result; } } // namespace shaders diff --git a/src/mbgl/util/url.cpp b/src/mbgl/util/url.cpp index a4263502ef..37a70007ca 100644 --- a/src/mbgl/util/url.cpp +++ b/src/mbgl/util/url.cpp @@ -1,8 +1,6 @@ #include <mbgl/util/url.hpp> #include <mbgl/util/token.hpp> -#include <iomanip> -#include <sstream> #include <string> #include <cstdlib> #include <algorithm> @@ -24,26 +22,30 @@ inline bool isSchemeCharacter(char c) { return isAlphaNumericCharacter(c) || c == '-' || c == '+' || c == '.'; } +inline char toLowerHex(char c) { + c &= 0x0F; + return '0' + c + (c > 9 ? 7 : 0); +} + } // namespace namespace mbgl { namespace util { std::string percentEncode(const std::string& input) { - std::ostringstream encoded; - - encoded.fill('0'); - encoded << std::hex; + std::string encoded; for (auto c : input) { if (isAlphaNumericCharacter(c) || c == '-' || c == '_' || c == '.' || c == '~') { - encoded << c; + encoded += c; } else { - encoded << '%' << std::setw(2) << int(c); + encoded += '%'; + encoded += toLowerHex(c >> 4); + encoded += toLowerHex(c); } } - return encoded.str(); + return encoded; } std::string percentDecode(const std::string& input) { diff --git a/test/util/url.test.cpp b/test/util/url.test.cpp index 55d8af2811..24697c42fa 100644 --- a/test/util/url.test.cpp +++ b/test/util/url.test.cpp @@ -6,6 +6,14 @@ using namespace mbgl::util; +TEST(URL, percentEncode) { + EXPECT_EQ("%22%C3%A9nc%C3%B8%C3%B0ing%22", percentEncode("\"éncøðing\"")); +} + +TEST(URL, percentDecode) { + EXPECT_EQ("\"éncøðing\"", percentDecode("%22%C3%A9nc%C3%B8%C3%B0ing%22")); +} + TEST(URL, Scheme) { EXPECT_EQ(URL::Segment({ 0, 4 }), URL("http://example.com/test?query=foo").scheme); EXPECT_EQ(URL::Segment({ 0, 4 }), URL("http://127.0.0.1:8080/test?query=foo").scheme); |