fork(1) download
  1. #include <string>
  2. #include <sstream>
  3. #include <iostream>
  4.  
  5. std::string hex( unsigned int c )
  6. {
  7. std::ostringstream stm ;
  8. stm << '%' << std::hex << std::uppercase << c ;
  9. return stm.str() ;
  10. }
  11.  
  12. std::string url_encode( const std::string& str )
  13. {
  14. static const std::string unreserved = "0123456789"
  15. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  16. "abcdefghijklmnopqrstuvwxyz"
  17. "-_.~" ;
  18. std::string result ;
  19.  
  20. for( unsigned char c : str )
  21. {
  22. if( unreserved.find(c) != std::string::npos ) result += c ;
  23. else result += hex(c) ;
  24. }
  25.  
  26. return result ;
  27. }
  28.  
  29. int main()
  30. {
  31. std::string test = u8"čau" ;
  32. std::cout << test;
  33. std::cout << test << '\n'
  34. << url_encode(test) << '\n' ;
  35. }
  36.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
čaučau
%C4%8Dau