import Data Char import System IO isUnreserved elem -_ urlencode urlen

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import Data.Char
import System.IO
isUnreserved c =
c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9'
|| c `elem` "-_.~"
urlencode [] = []
urlencode (' ':tail) = '+' : urlencode tail
urlencode (c:tail)
| isUnreserved c = c : urlencode tail
| otherwise = '%' : intToDigit d1 : intToDigit d2 : urlencode tail where (d1, d2) = ord c `divMod` 16
main = hSetBinaryMode stdin True >> interact urlencode