]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/Main.hs
List regex alternates in reverse order to avoid matching single digits before the...
[hath.git] / src / Main.hs
index 45705be673f087d76d8e64ea61f16c086ac98632..deb902297c45d3fac0990854f7a471c80f09a9b2 100644 (file)
@@ -49,7 +49,8 @@ add_barriers x = non_addr_char ++ x ++ non_addr_char
 --   3. Generate a regex matching every value between those min and
 --      max values.
 --   4. Join the regexes from step 3 with regexes matching periods.
---   5. Stick an address boundary on either side of the result.
+--   5. Stick an address boundary on either side of the result if
+--      use_barriers is True.
 --
 cidr_to_regex :: Bool -> Cidr.Cidr -> String
 cidr_to_regex use_barriers cidr =
@@ -79,9 +80,15 @@ alternate terms = "(" ++ (intercalate "|" terms) ++ ")"
 
 -- | Take two Ints as parameters, and return a regex matching any
 --   integer between them (inclusive).
+--
+--   IMPORTANT: we match from max to min so that if e.g. the last
+--   octet is '255', we want '255' to match before '2' in the regex
+--   (255|254|...|3|2|1) which does not happen if we use
+--   (1|2|3|...|254|255).
+--
 numeric_range :: Int -> Int -> String
 numeric_range x y =
-    alternate (map show [lower..upper])
+    alternate (map show $ reverse [lower..upper])
      where
        lower = minimum [x,y]
        upper = maximum [x,y]