Commits

Jesper Axelsson committed 80d879b

Code clean up and added Compiled new type

Comments (0)

Files changed (1)

src/Data/Text/Wildcards.hs

 {-# LANGUAGE OverloadedStrings #-}
-module Data.Text.Wildcards (match, compile) where
+module Data.Text.Wildcards (match, matchC, compile) where
 
-import Data.Text ()
-import qualified Data.Text as T
-import Data.Attoparsec.Text as AT
 
 
+import           Data.Attoparsec.Text as AT
+import qualified Data.Text            as T
 
-newtype Compiled = Parser Bool
 
-nn = many1 digit
 
-tt = match "c" "c"
-t1 = match "c" "c2"
-t2 = match "c" "d"
-t3 = match "*" "foo"
-t4 = match "*o" "hello"
-t5 = match "*o" "ffff"
-t6 = match "?" "f"
-t7 = match "?" "fo"
-t8 = match "???" "foo"
-t9 = match "*foo?" "dudefoop"
-t10 = match "*foo?" "dudefoopo"
-t11 = match "*foo?" "dudefoo"
+
+type Compiled = Parser Bool
+
+
 
 match :: T.Text -> T.Text -> Bool
-match pat txt = -- parseOnly (compile pat) txt
+match pat txt =
     case parseOnly (compile pat) txt of
          Right res -> res
          Left  _   -> False
 
---compile :: T.Text -> Compiled
-compile :: T.Text -> Parser Bool
-compile pat = 
-    let pc  = mhead pat 
+
+
+matchC :: Compiled -> T.Text -> Bool
+matchC pat txt = 
+    case parseOnly pat txt of
+         Right res -> res
+         Left  _   -> False
+
+
+
+compile :: T.Text -> Compiled
+compile pat =
+    let pc  = mhead pat
         rst = T.tail pat in
     case pc of
        Just '?' -> let (cnt, rst') = countC '?' pat in AT.take cnt >> compile rst'
        Just '*' -> starFunc rst >> compile rst
-       Just c   -> let (pat', rst') = T.break (contains "*?") pat in  
+       Just _   -> let (pat', rst') = T.break (contains "*?") pat in
                        string pat' >> compile rst'
        Nothing  -> atEnd
 
+
+
+starFunc :: T.Text ->  Parser ()
 starFunc pat | T.null pat         = takeText >> return ()
-             | T.head pat == '*'  = starFunc  (T.tail pat)
-             | otherwise          = skipWhile (T.head pat /=) 
+             | T.head pat == '*'   = starFunc  (T.tail pat)
+             | otherwise          = skipWhile (T.head pat /=)
 
 
-foo c pat = T.break (contains "*?") (T.pack pat)
 
+countC :: Char -> T.Text -> (Int, T.Text)
 countC c txt = (cnt, T.drop cnt txt)
     where cnt = T.length (T.takeWhile (c ==) txt)
 
+
+
 mhead :: T.Text -> Maybe Char
 mhead s | T.null s  = Nothing
         | otherwise = Just (T.head s)
 
+
+
 contains :: T.Text -> Char -> Bool
 contains txt c = case T.find (c ==) txt of
                      Just _  -> True
                      Nothing -> False
+
+