-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.hs
71 lines (62 loc) · 2.11 KB
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{-# LANGUAGE Safe #-}
{- |
Module : Util
Description : Utility functions for Yasgheld
Copyright : (c) Andrew Ray, 2017
License : MIT
Utility functions for Yasgheld.
-}
module Util (
strip,
replace,
removeComment,
dropBlanks,
(??),
(?)
) where
import Data.Char (isSpace)
import Data.List (dropWhileEnd, isPrefixOf, isSubsequenceOf)
import Data.List.Split (splitOn)
import Data.Maybe (isNothing)
-- | Strips leading and trailing whitespace from the given String.
strip :: String -- ^ Input String
-> String -- ^ Output String, whitespace removed
strip = dropWhileEnd isSpace.dropWhile isSpace
-- | Performs substitution on a string
replace :: Eq a
=> [a] -- ^ The search value
-> [a] -- ^ The replace value
-> [a] -- ^ The string to search in
-> [a] -- ^ The string, after substitutions
replace [] _ str = str
replace _ _ [] = []
replace find repl str
| find `isPrefixOf` str = repl ++ replace find repl (drop (length find) str)
| otherwise = head str : replace find repl (tail str)
-- | Removes comments from strings
removeComment :: String -- ^ String to remove comments from
-> String -- ^ String with comments removed
removeComment str
| ";" `isSubsequenceOf` str = head $ splitOn ";" str
| otherwise = str
dropBlanks :: [String] -> [String]
dropBlanks = filter (\a -> strip a /= "" && not (";" `isPrefixOf` strip a))
-- | Finds the list referenced by the given first term
infixl 9 ?
(?) :: (Eq a)
=> [(a, b, c)] -- ^ The list of three-tuples to search in
-> a -- ^ The first term to search for
-> Maybe (b, c) -- ^ Just the last two terms, or Nothing if no matches
(?) [] _ = Nothing
(?) ((a, b, c):rest) find
| a == find = Just (b, c)
| otherwise = rest ? find
-- | Finds the second term referenced by the first given term
infixl 9 ??
(??) :: (Eq a)
=> [(a, b, c)] -- ^ The list of three-tuples to search in
-> a -- ^ The first term to search for
-> Maybe b -- ^ Just the second term, or Nothing if no matches
(??) list find
| isNothing (list ? find) = Nothing
| otherwise = Just $ (\(Just (b, _)) -> b) (list ? find)