fork(3) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::string query_encode(const std::string &s)
  5. {
  6. std::string ret;
  7.  
  8. #define IS_BETWEEN(ch, low, high) (ch >= low && ch <= high)
  9. #define IS_ALPHA(ch) (IS_BETWEEN(ch, 'A', 'Z') || IS_BETWEEN(ch, 'a', 'z'))
  10. #define IS_DIGIT(ch) IS_BETWEEN(ch, '0', '9')
  11. #define IS_HEXDIG(ch) (IS_DIGIT(ch) || IS_BETWEEN(ch, 'A', 'F') || IS_BETWEEN(ch, 'a', 'f'))
  12.  
  13. for(size_t i = 0; i < s.size();)
  14. {
  15. char ch = s[i++];
  16.  
  17. if (IS_ALPHA(ch) || IS_DIGIT(ch))
  18. {
  19. ret += ch;
  20. }
  21. else if ((ch == '%') && IS_HEXDIG(s[i+0]) && IS_HEXDIG(s[i+1]))
  22. {
  23. ret += s.substr(i-1, 3);
  24. i += 2;
  25. }
  26. else
  27. {
  28. switch (ch)
  29. {
  30. case '-':
  31. case '.':
  32. case '_':
  33. case '~':
  34. case '!':
  35. case '$':
  36. case '&':
  37. case '\'':
  38. case '(':
  39. case ')':
  40. case '*':
  41. case '+':
  42. case ',':
  43. case ';':
  44. case '=':
  45. case ':':
  46. case '@':
  47. case '/':
  48. case '?':
  49. case '[':
  50. case ']':
  51. ret += ch;
  52. break;
  53.  
  54. default:
  55. {
  56. static const char hex[] = "0123456789ABCDEF";
  57. char pct[] = "% ";
  58. pct[1] = hex[(ch >> 4) & 0xF];
  59. pct[2] = hex[ch & 0xF];
  60. ret.append(pct, 3);
  61. break;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. return ret;
  68. }
  69.  
  70. int main()
  71. {
  72. std::string d = "https://w...content-available-to-author-only...i.de/api/interpreter?data=" + query_encode("area[\"name\"=\"Nicaragua\"][\"admin_level\"=\"2\"]->.boundaryarea;(node[\"type\"=\"route\"][\"route\"=\"bus\"](area.boundaryarea);way[\"type\"=\"route\"][\"route\"=\"bus\"](area.boundaryarea);>;relation[\"type\"=\"route\"][\"route\"=\"bus\"](area.boundaryarea);>>;);out meta;");
  73. std::cout << "Encoded: " + d + "\n";
  74. return 0;
  75. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Encoded: https://w...content-available-to-author-only...i.de/api/interpreter?data=area[%22name%22=%22Nicaragua%22][%22admin_level%22=%222%22]-%3E.boundaryarea;(node[%22type%22=%22route%22][%22route%22=%22bus%22](area.boundaryarea);way[%22type%22=%22route%22][%22route%22=%22bus%22](area.boundaryarea);%3E;relation[%22type%22=%22route%22][%22route%22=%22bus%22](area.boundaryarea);%3E%3E;);out%20meta;