packages feed

ip 0.6.1 → 0.6.2

raw patch · 14 files changed

+69/−43 lines, 14 files

Files

ip.cabal view
@@ -1,5 +1,5 @@ name:                ip-version:             0.6.1+version:             0.6.2 synopsis:            Library for IP and MAC addresses description:         Please see README.md homepage:            https://github.com/andrewthad/haskell-ip#readme@@ -19,10 +19,11 @@     Net.Mac.Text     Net.Mac.ByteString.Char8     Net.IPv4+    Net.IPv4.String     Net.IPv4.Text     Net.IPv4.ByteString.Char8     Net.Internal-  build-depends:       +  build-depends:       base        >= 4.8  && < 5     , attoparsec     , aeson       >= 0.9  && < 0.12@@ -36,7 +37,7 @@   type:                exitcode-stdio-1.0   hs-source-dirs:      test   main-is:             Test.hs-  build-depends:       +  build-depends:       base     , ip     , test-framework
src/Net/IPv4.hs view
@@ -2,20 +2,20 @@  {-# LANGUAGE GeneralizedNewtypeDeriving #-}  {-| An IPv4 data type-    +     This module provides the IPv4 data type and functions for working     with it. There are also encoding and decoding functions provided-    in this module, but they should be imported from +    in this module, but they should be imported from     @Net.IPv4.Text@ and @Net.IPv4.ByteString.Char8@ instead. They are     defined here so that the 'FromJSON' and 'ToJSON' instances can     use them.-    +     At some point, a highly efficient IPv4-to-ByteString function needs     to be added to this module to take advantage of @aeson@'s new     @toEncoding@ method. -}-    -module Net.IPv4 ++module Net.IPv4   ( -- * Types     IPv4(..)   , IPv4Range(..)@@ -62,7 +62,7 @@ newtype IPv4 = IPv4 { getIPv4 :: Word32 }   deriving (Eq,Ord,Show,Read,Enum,Bounded,Hashable,Generic) -data IPv4Range = IPv4Range +data IPv4Range = IPv4Range   { ipv4RangeBase   :: {-# UNPACK #-} !IPv4   , ipv4RangeLength :: {-# UNPACK #-} !Int8   } deriving (Eq,Ord,Show,Read,Generic)@@ -79,7 +79,7 @@   toJSON addrRange = Aeson.String (rangeToDotDecimalText addrRange)  instance FromJSON IPv4Range where-  parseJSON (Aeson.String t) = +  parseJSON (Aeson.String t) =     case rangeFromDotDecimalText' t of       Left err  -> fail err       Right res -> return res@@ -89,14 +89,14 @@ -- mask w = IPv4 $ complement $ 0xffffffff `shiftR` w  fromDotDecimalText' :: Text -> Either String IPv4-fromDotDecimalText' t = +fromDotDecimalText' t =   AT.parseOnly (dotDecimalParser <* AT.endOfInput) t  fromDotDecimalText :: Text -> Maybe IPv4 fromDotDecimalText = rightToMaybe . fromDotDecimalText'  rangeFromDotDecimalText' :: Text -> Either String IPv4Range-rangeFromDotDecimalText' t = +rangeFromDotDecimalText' t =   AT.parseOnly (dotDecimalRangeParser <* AT.endOfInput) t  rangeFromDotDecimalText :: Text -> Maybe IPv4Range@@ -108,7 +108,7 @@   <*  AT.char '/'   <*> (AT.decimal >>= limitSize)   where-  limitSize i = +  limitSize i =     if i > 32       then fail "An IP range length must be between 0 and 32"       else return i@@ -125,19 +125,19 @@   <*  AT.char '.'   <*> (AT.decimal >>= limitSize)   where-  limitSize i = -    if i > 255 +  limitSize i =+    if i > 255       then fail "All octets in an ip address must be between 0 and 255"       else return i  fromOctets :: Word8 -> Word8 -> Word8 -> Word8 -> IPv4-fromOctets a b c d = fromOctets' +fromOctets a b c d = fromOctets'   (fromIntegral a) (fromIntegral b) (fromIntegral c) (fromIntegral d)  -- | This is sort of a misnomer. It takes Word32 to make --   dotDecimalParser probably perform better. fromOctets' :: Word32 -> Word32 -> Word32 -> Word32 -> IPv4-fromOctets' a b c d = IPv4 +fromOctets' a b c d = IPv4     ( shiftL a 24   .|. shiftL b 16   .|. shiftL c 8@@ -164,7 +164,7 @@ rangeToDotDecimalText = LText.toStrict . TBuilder.toLazyText . rangeToDotDecimalBuilder  rangeToDotDecimalBuilder :: IPv4Range -> TBuilder.Builder-rangeToDotDecimalBuilder (IPv4Range addr len) = +rangeToDotDecimalBuilder (IPv4Range addr len) =   toDotDecimalBuilder addr   <> TBuilder.singleton '/'   <> decimal len@@ -198,7 +198,7 @@         theArr <- TArray.unsafeFreeze marr         return (theArr,i4 + n3')   in Text arr 0 len-    + putAndCount :: Int -> Word8 -> TArray.MArray s -> ST s Int putAndCount pos w marr   | w < 10 = TArray.unsafeWrite marr pos (i2w w) >> return 1@@ -234,7 +234,7 @@   \8081828384858687888990919293949596979899"  threeDigits :: ByteString-threeDigits = +threeDigits =   ByteString.replicate 300 0 <> BC8.pack   "100101102103104105106107108109110111112\   \113114115116117118119120121122123124125\
src/Net/IPv4/ByteString/Char8.hs view
@@ -79,7 +79,7 @@   \8081828384858687888990919293949596979899"  threeDigits :: ByteString-threeDigits = +threeDigits =   Data.ByteString.replicate 300 0 <> BC8.pack   "100101102103104105106107108109110111112\   \113114115116117118119120121122123124125\
+ src/Net/IPv4/String.hs view
@@ -0,0 +1,25 @@+-- | This module exists for the convenience of those who need a+-- 'String' representation of an 'IPv4' address. Using this module+-- is discouraged unless the end user is working with a library+-- that can only use 'String' to deal with textual data (such as+-- @pandoc@ or @hxr@).+--+module Net.IPv4.String+  ( encode+  , decode+  , decodeEither+  ) where++import Net.IPv4+import qualified Data.Text as Text+import qualified Net.IPv4.Text as N++encode :: IPv4 -> String+encode = Text.unpack . N.encode++decode :: String -> Maybe IPv4+decode = N.decode . Text.pack++decodeEither :: String -> Either String IPv4+decodeEither = N.decodeEither . Text.pack+
src/Net/IPv4/Text.hs view
@@ -1,4 +1,4 @@-module Net.IPv4.Text +module Net.IPv4.Text   ( encode   , decode   , decodeEither
src/Net/Internal.hs view
@@ -4,9 +4,9 @@ import qualified Data.Aeson.Types as Aeson  attoparsecParseJSON :: AT.Parser a -> Aeson.Value -> Aeson.Parser a-attoparsecParseJSON p v = +attoparsecParseJSON p v =   case v of-    Aeson.String t -> +    Aeson.String t ->       case AT.parseOnly p t of         Left err  -> fail err         Right res -> return res
src/Net/Mac.hs view
@@ -6,7 +6,7 @@ import Data.Text (Text) import Data.Hashable (Hashable) import GHC.Generics (Generic)-import Data.Word +import Data.Word import Data.Aeson (ToJSON(..),FromJSON(..)) import qualified Data.Attoparsec.Text as AT import qualified Data.Attoparsec.ByteString.Char8 as AB@@ -42,7 +42,7 @@ fromText' t = AT.parseOnly (textParser <* AT.endOfInput) t  toTextBuilder :: Mac -> TBuilder.Builder-toTextBuilder (Mac a b) = +toTextBuilder (Mac a b) =   hexadecimal (255 .&. shiftR a 8 )   <> colon   <> hexadecimal (255 .&. a )@@ -71,8 +71,8 @@   <*  AT.char ':'   <*> (AT.hexadecimal >>= limitSize)   where-  limitSize i = -    if i > 255 +  limitSize i =+    if i > 255       then fail "All octets in a mac address must be between 00 and FF"       else return i @@ -90,8 +90,8 @@   <*  AB.char ':'   <*> (AB.hexadecimal >>= limitSize)   where-  limitSize i = -    if i > 255 +  limitSize i =+    if i > 255       then fail "All octets in a mac address must be between 00 and FF"       else return i 
src/Net/Mac/ByteString/Char8.hs view
@@ -41,8 +41,8 @@   <*  AB.char ':'   <*> (AB.hexadecimal >>= limitSize)   where-  limitSize i = -    if i > 255 +  limitSize i =+    if i > 255       then fail "All octets in a mac address must be between 00 and FF"       else return i 
src/Net/Mac/Text.hs view
@@ -1,4 +1,4 @@-module Net.Mac.Text +module Net.Mac.Text   ( encode   , decode   , decodeEither
test/Bench.hs view
@@ -27,13 +27,13 @@ main :: IO () main = do   let ipAddr = IPv4 1000000009-  defaultMain -    [ bgroup "IPv4 to Text" +  defaultMain+    [ bgroup "IPv4 to Text"       [ bench "Naive" $ whnf Naive.encodeText ipAddr       , bench "Text Builder" $ whnf IPv4Text2.encode ipAddr       , bench "Preallocated" $ whnf IPv4Text1.encode ipAddr       ]-    , bgroup "IPv4 to ByteString" +    , bgroup "IPv4 to ByteString"       [ bench "Naive" $ whnf Naive.encodeByteString ipAddr       , bench "Preallocated: No Lookup Tables" $ whnf IPv4ByteString1.encode ipAddr       , bench "Preallocated" $ whnf NIPBS.encode ipAddr
test/IPv4Text1.hs view
@@ -54,7 +54,7 @@         theArr <- TArray.unsafeFreeze marr         return (theArr,i4 + n3')   in Text arr 0 len-    + putAndCount :: Int -> Word8 -> TArray.MArray s -> ST s Int putAndCount pos w marr   | w < 10 = TArray.unsafeWrite marr pos (i2w w) >> return 1@@ -92,7 +92,7 @@   \8081828384858687888990919293949596979899"  threeDigits :: ByteString-threeDigits = +threeDigits =   ByteString.replicate 300 0 <> BC8.pack   "100101102103104105106107108109110111112\   \113114115116117118119120121122123124125\
test/IPv4Text2.hs view
@@ -30,7 +30,7 @@ encode = LText.toStrict . TBuilder.toLazyText . toDotDecimalBuilder  toDotDecimalBuilder :: IPv4 -> TBuilder.Builder-toDotDecimalBuilder (IPv4 w) = +toDotDecimalBuilder (IPv4 w) =   decimal (255 .&. shiftR w 24 )   <> dot   <> decimal (255 .&. shiftR w 16 )
test/Naive.hs view
@@ -37,7 +37,7 @@   where (a,b,c,d) = IPv4.toOctets i  decodeText :: Text -> Maybe IPv4-decodeText t = +decodeText t =   case mapM (readMaybe . Text.unpack) (Text.splitOn (Text.pack ".") t) of     Just [a,b,c,d] -> Just (IPv4.fromOctets a b c d)     _ -> Nothing
test/Test.hs view
@@ -27,8 +27,8 @@  tests :: [Test] tests =-  [ testGroup "Naive IPv4 encode/decode" -    [ testProperty "Isomorphism" +  [ testGroup "Naive IPv4 encode/decode"+    [ testProperty "Isomorphism"         $ propEncodeDecodeIso Naive.encodeText Naive.decodeText     ]   , testGroup "Text Builder IPv4 Text encode/decode"@@ -48,7 +48,7 @@         $ propMatching IPv4_ByteString.encode Naive.encodeByteString     ]   , testGroup "Raw byte array MAC Text encode/decode"-    [ testProperty "Isomorphism" +    [ testProperty "Isomorphism"         $ propEncodeDecodeIso Mac_Text.encode Mac_Text.decode     ]   ]