summaryrefslogtreecommitdiffhomepage
path: root/url.cpp
blob: 5ad820ab5d8a3ccb7172efa6de9181b746828479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "url.h"

std::string Reichwein::URL::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;
}