Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/coding
diff options
context:
space:
mode:
authorAlex Zolotarev <deathbaba@gmail.com>2011-02-04 07:43:51 +0300
committerAlex Zolotarev <alex@maps.me>2015-09-23 01:11:46 +0300
commitdb10d5fbfc3401a1b6598ca735eb01d4eb59d305 (patch)
tree8553b9646157f0c94b6ed6245b969d549d36d12a /coding
parent666c8b45be8271e129efa9a55f4bc7a3fba6a931 (diff)
Added coding/strutil.hpp:UrlEncode() and UrlDecode
Fixed not downloaded countries with spaces on iPhone
Diffstat (limited to 'coding')
-rw-r--r--coding/coding_tests/coding_tests.pro4
-rw-r--r--coding/coding_tests/strutil_test.cpp34
-rw-r--r--coding/strutil.hpp37
3 files changed, 73 insertions, 2 deletions
diff --git a/coding/coding_tests/coding_tests.pro b/coding/coding_tests/coding_tests.pro
index c6dad8d4f4..9d2f423deb 100644
--- a/coding/coding_tests/coding_tests.pro
+++ b/coding/coding_tests/coding_tests.pro
@@ -27,7 +27,7 @@ SOURCES += ../../testing/testingmain.cpp \
file_sort_test.cpp \
reader_cache_test.cpp \
file_container_test.cpp \
+ strutil_test.cpp \
HEADERS += \
- reader_test.hpp
-
+ reader_test.hpp \
diff --git a/coding/coding_tests/strutil_test.cpp b/coding/coding_tests/strutil_test.cpp
new file mode 100644
index 0000000000..ebfe83f971
--- /dev/null
+++ b/coding/coding_tests/strutil_test.cpp
@@ -0,0 +1,34 @@
+#include "../../testing/testing.hpp"
+
+#include "../strutil.hpp"
+
+char const * orig1 = "http://google.com/main_index.php";
+char const * enc1 = "http%3A%2F%2Fgoogle.com%2Fmain_index.php";
+char const * orig2 = "Some File Name.ext";
+char const * enc2 = "Some%20File%20Name.ext";
+char const * orig3 = "Wow, two spaces?!";
+char const * enc3 = "Wow%2C%20%20two%20spaces%3F%21";
+char const * orig4 = "#$%^&@~[]{}()|*+`\"\'";
+char const * enc4 = "%23%24%25%5E%26%40~%5B%5D%7B%7D%28%29%7C%2A%2B%60%22%27";
+
+UNIT_TEST(UrlEncode)
+{
+ TEST_EQUAL(UrlEncode(""), "", ());
+ TEST_EQUAL(UrlEncode(" "), "%20", ());
+ TEST_EQUAL(UrlEncode("20"), "20", ());
+ TEST_EQUAL(UrlEncode(orig1), enc1, ());
+ TEST_EQUAL(UrlEncode(orig2), enc2, ());
+ TEST_EQUAL(UrlEncode(orig3), enc3, ());
+ TEST_EQUAL(UrlEncode(orig4), enc4, ());
+}
+
+UNIT_TEST(UrlDecode)
+{
+ TEST_EQUAL(UrlDecode(""), "", ());
+ TEST_EQUAL(UrlDecode("%20"), " ", ());
+ TEST_EQUAL(UrlDecode("20"), "20", ());
+ TEST_EQUAL(UrlDecode(enc1), orig1, ());
+ TEST_EQUAL(UrlDecode(enc2), orig2, ());
+ TEST_EQUAL(UrlDecode(enc3), orig3, ());
+ TEST_EQUAL(UrlDecode(enc4), orig4, ());
+}
diff --git a/coding/strutil.hpp b/coding/strutil.hpp
index a43868cbe6..ded0d0c727 100644
--- a/coding/strutil.hpp
+++ b/coding/strutil.hpp
@@ -1,6 +1,9 @@
#pragma once
+#include "hex.hpp"
+
#include "../base/base.hpp"
+#include "../base/macros.hpp"
#include "../std/string.hpp"
#include "../std/memcpy.hpp"
@@ -69,4 +72,38 @@ inline bool IsPrefixOf(string const & s1, string const & s2)
return true;
}
+inline string UrlEncode(string const & rawUrl)
+{
+ string result(rawUrl);
+ for (size_t i = 0; i < result.size(); ++i)
+ {
+ char const c = result[i];
+ if (c < '.' || c == '/' || (c > '9' && c < 'A') || (c > 'Z' && c < '_')
+ || c == '`' || (c > 'z' && c < '~') || c > '~')
+ {
+ string hexStr("%");
+ hexStr += NumToHex(c);
+ result.replace(i, 1, hexStr);
+ i += 2;
+ }
+ }
+ return result;
+}
+
+inline string UrlDecode(string const & encodedUrl)
+{
+ string result;
+ for (size_t i = 0; i < encodedUrl.size(); ++i)
+ {
+ if (encodedUrl[i] == '%')
+ {
+ result += FromHex(encodedUrl.substr(i + 1, 2));
+ i += 2;
+ }
+ else
+ result += encodedUrl[i];
+ }
+ return result;
+}
+
#include "../base/stop_mem_debug.hpp"