How-To work with URL encoding
When working with the liberate::net::url
class, it may be useful
to encode and decode strings using the percent encoding defined in RFC3986.
For the most part, this amounts to replacing some reserved characters with
a percent sign followed by the hexadecimal value of the ASCII code representing
the character – so that ` ` becomes %20, etc.
Using URL encoding
1#include <liberate/string/urlencode.h>
2
3auto res = liberate::string::urlencode("foo bar");
4assert ("foo%20bar" == res);
Note that the urlencode
function only encodes some reserved characters,
while the urldecode
function decodes any percent-encoded character. This
means they do not provide the exact inverse function of each other.
Using URL encoding
1auto res = liberate::string::urldecode("foo%2Fbar");
2assert("foo/bar" == res);
3
4auto back = liberate::string::urlencode(res);
5assert("foo/bar" == back);
6assert(res != back);
See also