Created
October 12, 2019 23:40
-
-
Save obfusk/0a8368d41b67a36d42966d8f39874aaa to your computer and use it in GitHub Desktop.
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
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