From: Michael Orlitzky Date: Sun, 2 May 2010 21:06:31 +0000 (-0400) Subject: Removed the Text.Regex.Posix import. X-Git-Tag: 0.0.1~75 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=1e7731c84546a25daed6bbfb3d9c733b334667bd;p=hath.git Removed the Text.Regex.Posix import. Removed the is_valid_cidr function. Send each octet/maskbit string to the appropriate module for parsing. Return Cidr.None if either the octets or maskbits are invalid. --- diff --git a/src/Cidr.hs b/src/Cidr.hs index 4bb3d48..ac24278 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -1,13 +1,11 @@ module Cidr ( Cidr(..), - cidr_from_string, - is_valid_cidr + cidr_from_string ) where -import Text.Regex.Posix - import IPv4Address import ListUtils +import Maskbits import Octet @@ -15,33 +13,31 @@ data Cidr = None | Cidr { ipv4address :: IPv4Address, maskbits :: Maskbits } deriving (Eq, Show) --- Will return True if the passed String is in CIDR notation, False --- otherwise. -is_valid_cidr :: String -> Bool -is_valid_cidr cidr = cidr =~ "([0-9]{1,3}\\.){3}[0-9]{1,3}/[0-9]{1,2}" - -- Returns the mask portion of a CIDR address. That is, everything -- after the trailing slash. -maskbits_from_string :: String -> Maskbits -maskbits_from_string s = read ((splitWith (`elem` "/") s) !! 1) +maskbits_from_cidr_string :: String -> Maskbits +maskbits_from_cidr_string s = + maskbits_from_string ((splitWith (`elem` "/") s) !! 1) -- Takes an IP address String in CIDR notation, and returns a list of -- its octets (as Ints). -octets_from_string :: String -> [Int] -octets_from_string s = map read (take 4 (splitWith (`elem` "./") s)) +octets_from_cidr_string :: String -> [Octet] +octets_from_cidr_string s = + map octet_from_string (take 4 (splitWith (`elem` "./") s)) cidr_from_string :: String -> Cidr cidr_from_string s - | addr == IPv4Address.None = Cidr.None + | addr == IPv4Address.None = Cidr.None + | mbits == Maskbits.None = Cidr.None | otherwise = Cidr addr mbits where addr = ipv4address_from_octets (oct1) (oct2) (oct3) (oct4) - oct1 = octet_from_int (octs !! 0) - oct2 = octet_from_int (octs !! 1) - oct3 = octet_from_int (octs !! 2) - oct4 = octet_from_int (octs !! 3) - octs = octets_from_string s - mbits = maskbits_from_string s + oct1 = (octs !! 0) + oct2 = (octs !! 1) + oct3 = (octs !! 2) + oct4 = (octs !! 3) + octs = octets_from_cidr_string s + mbits = maskbits_from_cidr_string s