Skip to content

Instantly share code, notes, and snippets.

@obfusk
Created October 12, 2019 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obfusk/0a8368d41b67a36d42966d8f39874aaa to your computer and use it in GitHub Desktop.
Save obfusk/0a8368d41b67a36d42966d8f39874aaa to your computer and use it in GitHub Desktop.
import System.Environment
import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as UTF8
escapeURL :: String -> String
escapeURL = UTF8.toString . BS.concatMap (BS.pack . f) . UTF8.fromString
where
f c | alnum c || c `elem` [45,95,46,126] {- "-_.~" -} = [c]
| otherwise = h c
h c | c > 0xf = let (a,b) = c `divMod` 16 in [37,i a,i b]
| otherwise = [37,48,i c] -- %,0
i c | c >= 0xa = 65 + c - 10
| otherwise = 48 + c
alnum c = (48 <= c && c <= 57) -- 0-9
|| (65 <= c && c <= 90) -- A-Z
|| (97 <= c && c <= 122) -- a-z
main = getArgs >>= mapM_ (putStrLn . escapeURL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment