url.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. namespace nall::Decode {
  3. //returns empty string on malformed content
  4. inline auto URL(string_view input) -> string {
  5. string output;
  6. for(uint n = 0; n < input.size();) {
  7. char c = input[n];
  8. //unreserved characters
  9. if(c >= 'A' && c <= 'Z') { output.append(c); n++; continue; }
  10. if(c >= 'a' && c <= 'z') { output.append(c); n++; continue; }
  11. if(c >= '0' && c <= '9') { output.append(c); n++; continue; }
  12. if(c == '-' || c == '_' || c == '.' || c == '~') { output.append(c); n++; continue; }
  13. //special characters
  14. if(c == '+') { output.append(' '); n++; continue; }
  15. //reserved characters
  16. if(c != '%' || n + 2 >= input.size()) return "";
  17. char hi = input[n + 1];
  18. char lo = input[n + 2];
  19. if(hi >= '0' && hi <= '9') hi -= '0';
  20. else if(hi >= 'A' && hi <= 'F') hi -= 'A' - 10;
  21. else if(hi >= 'a' && hi <= 'f') hi -= 'a' - 10;
  22. else return "";
  23. if(lo >= '0' && lo <= '9') lo -= '0';
  24. else if(lo >= 'A' && lo <= 'F') lo -= 'A' - 10;
  25. else if(lo >= 'a' && lo <= 'f') lo -= 'a' - 10;
  26. else return "";
  27. char byte = hi * 16 + lo;
  28. output.append(byte);
  29. n += 3;
  30. }
  31. return output;
  32. }
  33. }