shadowm

Untitled

Jan 3rd, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. std::string urlencode(const std::string &str)
  2. {
  3.     static std::string nonresv =
  4.         "-."
  5.         "0123456789"
  6.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  7.         "_"
  8.         "abcdefghijklmnopqrstuvwxyz"
  9.         "~";
  10.  
  11.     std::string res;
  12.  
  13.     for(size_t n = 0; n < str.length(); ++n) {
  14.         const char& c = str[n];
  15.  
  16.         if(nonresv.find(c) != std::string::npos) {
  17.             res += c;
  18.             continue;
  19.         }
  20.  
  21.         char buf[4];
  22.         snprintf(buf, sizeof(buf), "%%%02X", c);
  23.         res += buf;
  24.     }
  25.  
  26.     return res;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment