import Data Char import System IO isUnreserved elem -_ urldecode urlde

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import Data.Char
import System.IO
isUnreserved c =
c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9'
|| c `elem` "-_.~"
urldecode [] = []
urldecode ('%':tail) = urldecode1 tail
urldecode ('+':tail) = ' ' : urldecode tail
urldecode (c:tail)
| isUnreserved c = c : urldecode tail
| otherwise = undefined
urldecode1 (c1:c2:tail)
| isHexDigit c1 && isHexDigit c2 =
(chr $ (digitToInt c1) * 16 + digitToInt c2) : urldecode tail
| otherwise = undefined
urldecode1 _ = undefined
main = hSetBinaryMode stdout True >> interact urldecode