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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://go.dev/play/p/Lwu-ixIwMB3