summaryrefslogtreecommitdiffhomepage
path: root/libcommon/url.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcommon/url.cpp')
-rw-r--r--libcommon/url.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/libcommon/url.cpp b/libcommon/url.cpp
new file mode 100644
index 0000000..5baf603
--- /dev/null
+++ b/libcommon/url.cpp
@@ -0,0 +1,32 @@
+#include "url.h"
+
+std::string urlDecode(std::string s)
+{
+ std::string result;
+
+ size_t pos = 0;
+ while (pos < s.size()) {
+ char c {s[pos]};
+ if (c == '+') {
+ result += ' ';
+ } else if (c == '%' && pos + 2 < s.size()) {
+ try {
+ int i = stoi(s.substr(pos + 1, 2), 0, 16);
+ if (i < 0 || i > 255)
+ return result;
+
+ result += static_cast<char>(i);
+ } catch (...) {
+ return result;
+ }
+
+ pos += 2;
+ } else {
+ result += c;
+ }
+ pos++;
+ }
+
+ return result;
+}
+