aboutsummaryrefslogtreecommitdiff
path: root/ch08
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-04-22 16:25:42 +0200
committerJan Sucan <jan@jansucan.com>2023-04-22 16:25:42 +0200
commitd5bdc79002348178a6376387316505c708b77b06 (patch)
tree1a36e8b2fedf18ac9e2f5b5447b1ebaf29ec5bcb /ch08
parent7b6a8bf1ba4c24fe212ddae6c9202a6a3513c6ba (diff)
ch08: Copy a needed file from the examples
Diffstat (limited to 'ch08')
-rw-r--r--ch08/GlobRegex.hs55
1 files changed, 55 insertions, 0 deletions
diff --git a/ch08/GlobRegex.hs b/ch08/GlobRegex.hs
new file mode 100644
index 0000000..1b847f4
--- /dev/null
+++ b/ch08/GlobRegex.hs
@@ -0,0 +1,55 @@
+{-- snippet type --}
+module GlobRegex
+ (
+ globToRegex
+ , matchesGlob
+ ) where
+
+import Text.Regex.Posix ((=~))
+
+globToRegex :: String -> String
+{-- /snippet type --}
+
+{-- snippet rooted --}
+globToRegex cs = '^' : globToRegex' cs ++ "$"
+{-- /snippet rooted --}
+
+{-- snippet asterisk --}
+globToRegex' :: String -> String
+globToRegex' "" = ""
+
+globToRegex' ('*':cs) = ".*" ++ globToRegex' cs
+
+globToRegex' ('?':cs) = '.' : globToRegex' cs
+
+globToRegex' ('[':'!':c:cs) = "[^" ++ c : charClass cs
+globToRegex' ('[':c:cs) = '[' : c : charClass cs
+globToRegex' ('[':_) = error "unterminated character class"
+
+globToRegex' (c:cs) = escape c ++ globToRegex' cs
+{-- /snippet asterisk --}
+
+{-
+{-- snippet last --}
+globToRegex' (c:cs) = escape c ++ globToRegex' cs
+{-- /snippet last --}
+-}
+
+{-- snippet escape --}
+escape :: Char -> String
+escape c | c `elem` regexChars = '\\' : [c]
+ | otherwise = [c]
+ where regexChars = "\\+()^$.{}]|"
+{-- /snippet escape --}
+
+{-- snippet charClass --}
+charClass :: String -> String
+charClass (']':cs) = ']' : globToRegex' cs
+charClass (c:cs) = c : charClass cs
+charClass [] = error "unterminated character class"
+{-- /snippet charClass --}
+
+{-- snippet matchesGlob --}
+matchesGlob :: FilePath -> String -> Bool
+name `matchesGlob` pat = name =~ globToRegex pat
+{-- /snippet matchesGlob --}