Skip to content

Instantly share code, notes, and snippets.

@btfak
Forked from hnaohiro/go-urlencode
Created July 22, 2013 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btfak/6051512 to your computer and use it in GitHub Desktop.
Save btfak/6051512 to your computer and use it in GitHub Desktop.
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