Created
January 24, 2013 20:57
-
-
Save hnaohiro/4627658 to your computer and use it in GitHub Desktop.
Golang URL Encode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func urlencode(s string) (result string){ | |
for _, c := range(s) { | |
if c <= 0x7f { // single byte | |
result += fmt.Sprintf("%%%X", c) | |
} else if c > 0x1fffff {// quaternary byte | |
result += fmt.Sprintf("%%%X%%%X%%%X%%%X", | |
0xf0 + ((c & 0x1c0000) >> 18), | |
0x80 + ((c & 0x3f000) >> 12), | |
0x80 + ((c & 0xfc0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} else if c > 0x7ff { // triple byte | |
result += fmt.Sprintf("%%%X%%%X%%%X", | |
0xe0 + ((c & 0xf000) >> 12), | |
0x80 + ((c & 0xfc0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} else { // double byte | |
result += fmt.Sprintf("%%%X%%%X", | |
0xc0 + ((c & 0x7c0) >> 6), | |
0x80 + (c & 0x3f), | |
) | |
} | |
} | |
return result | |
} |
Why is this required? Golang already has QueryEscape()
as well as PathEscape()
functions. I've written about URL Encoding in Golang and URL Decoding in Golang. Also, there are tools to URL Encode online and URL Decode online.
It's needed because Golang does this:
- use
url.PathEscape
and get space as%20
, but leave:
unencoded (should be%3A
), or - use
url.QueryEscape
and get space as+
, but encode:
as%3A
So, if you integrate with something that goes by RFC in a certain manner and encodes %20
and %3A
, you are out of luck.
P.S. Of course, :
is semi-legal and perhaps shouldn't be encoded, but tell that to all kinds of Java/Python libs...
To add a cherry on top: QueryEscape
incorrectly escapes already escaped strings over and over again, which is straight up ridiculous.
It's 2022 and it still does this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if you need a path component with a slash? In Perl:
This lets me specify each path part separately.