Permalink
Browse files

fixed leading space bug in and simplified Read AddrPort

  • Loading branch information...
Cetin Sert
Cetin Sert committed May 31, 2012
1 parent c6716b1 commit 22234ecadd0eee541472c0cf41489cca8bafb5ee
Showing with 6 additions and 8 deletions.
  1. +6 −8 src/Main.hs
View
@@ -33,6 +33,7 @@ import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.StablePtr
import Data.Word
import Data.Char
import System.IO.Unsafe
import qualified Data.Vector.Storable.Mutable as SVM
@@ -137,22 +138,19 @@ type Port = PortNumber
instance Read Port where readsPrec p s = map (\(i,r) -> (fromInteger i,r)) $ readsPrec p s
data AddrPort = !Host :@: !Port
instance Show AddrPort where
show (a:@:p) = if B.null a then show p else f a ++ ":" ++ show p
where f a = if B.elem ':' a then "["++show (LS a)++"]" else show (LS a)
instance Read AddrPort where
readsPrec p s =
case reverse $ elemIndices ':' s of { [] -> all s; (0:_) -> all $ drop 1 s; (i:_) -> one i s }
where all s = readsPrec p s >>= \(p, s') -> return $ ("" :@: p, s')
one i s = do
(a,_) <- readsPrec p $ if elem '[' x
then map (\c -> case c of '[' -> '"'; ']' -> '"' ; c -> c) x
else "\"" ++ x ++ "\""
(p,r) <- readsPrec p $ tail y
where all s = readsPrec p s >>= \(p, s') -> return $ ("" :@: p, s')
one i s = do
let (x,y) = splitAt i s // \(a,b) -> (dropWhile isSpace a, b)
(a,_) <- readsPrec p $! "\"" ++ filter (\c -> c /= '[' && ']' /= c) x ++ "\""
(p,r) <- readsPrec p $! tail y
return $ (a :@: p, r)
where (x,y) = splitAt i s
faf :: Family -> LiteralString
faf x = LS $! case x of { AF_INET6 -> "IPv6(+4?)"; AF_INET -> "IPv4"; _ -> B.pack $ show x }

0 comments on commit 22234ec

Please sign in to comment.