From 96fd2142aaf2c521b5396bb37ca06ee9a1dd2a64 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 29 May 2009 11:31:51 +0000 Subject: [PATCH] move some unrelated things out of AccountName, drop unused pattern match code --- Ledger/AccountName.hs | 56 ------------------------------------------- Ledger/RawLedger.hs | 15 ++++++++++++ Ledger/Utils.hs | 5 ++++ 3 files changed, 20 insertions(+), 56 deletions(-) diff --git a/Ledger/AccountName.hs b/Ledger/AccountName.hs index 5b91f4f19..3d7db9889 100644 --- a/Ledger/AccountName.hs +++ b/Ledger/AccountName.hs @@ -23,11 +23,6 @@ accountNameFromComponents = concat . intersperse [acctsepchar] accountLeafName :: AccountName -> String accountLeafName = last . accountNameComponents -unbracket :: String -> String -unbracket s - | (head s == '[' && last s == ']') || (head s == '(' && last s == ')') = init $ tail s - | otherwise = s - accountNameLevel :: AccountName -> Int accountNameLevel "" = 0 accountNameLevel a = (length $ filter (==acctsepchar) a) + 1 @@ -100,54 +95,3 @@ elideAccountName width s = | otherwise = done++ss --- | Check if a set of ledger account/description patterns matches the --- given account name or entry description. Patterns are case-insensitive --- regular expression strings; those beginning with - are anti-patterns. -matchpats :: [String] -> String -> Bool -matchpats pats str = - (null positives || any match positives) && (null negatives || not (any match negatives)) - where - (negatives,positives) = partition isnegativepat pats - match "" = True - match pat = matchregex (abspat pat) str - --- | Similar to matchpats, but follows the special behaviour of ledger --- 2.6's balance command: positive patterns which do not contain : match --- the account leaf name, other patterns match the full account name. -matchpats_balance :: [String] -> String -> Bool -matchpats_balance pats str = match_positive_pats pats str && (not $ match_negative_pats pats str) --- (null positives || any match positives) && (null negatives || not (any match negatives)) --- where --- (negatives,positives) = partition isnegativepat pats --- match "" = True --- match pat = matchregex (abspat pat) matchee --- where --- matchee = if not (':' `elem` pat) && not (isnegativepat pat) --- then accountLeafName str --- else str - --- | Do the positives in these patterns permit a match for this string ? -match_positive_pats :: [String] -> String -> Bool -match_positive_pats pats str = (null ps) || (any match ps) - where - ps = positivepats pats - match "" = True - match p = matchregex (abspat p) matchee - where - matchee | ':' `elem` p = str - | otherwise = accountLeafName str - --- | Do the negatives in these patterns prevent a match for this string ? -match_negative_pats :: [String] -> String -> Bool -match_negative_pats pats str = (not $ null ns) && (any match ns) - where - ns = map abspat $ negativepats pats - match "" = True - match p = matchregex (abspat p) str - -negateprefix = "not:" -isnegativepat pat = negateprefix `isPrefixOf` pat -abspat pat = if isnegativepat pat then drop (length negateprefix) pat else pat -positivepats = filter (not . isnegativepat) -negativepats = filter isnegativepat -matchregex pat str = null pat || containsRegex (mkRegexWithOpts pat True False) str diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index e457950c7..aadb23f74 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -172,3 +172,18 @@ rawLedgerDateSpan rl | otherwise = DateSpan (Just $ ltdate $ head ts) (Just $ addDays 1 $ ltdate $ last ts) where ts = sortBy (comparing ltdate) $ ledger_txns rl + +-- | Check if a set of ledger account/description patterns matches the +-- given account name or entry description. Patterns are case-insensitive +-- regular expression strings; those beginning with - are anti-patterns. +matchpats :: [String] -> String -> Bool +matchpats pats str = + (null positives || any match positives) && (null negatives || not (any match negatives)) + where + (negatives,positives) = partition isnegativepat pats + match "" = True + match pat = matchregex (abspat pat) str + negateprefix = "not:" + isnegativepat pat = negateprefix `isPrefixOf` pat + abspat pat = if isnegativepat pat then drop (length negateprefix) pat else pat + matchregex pat str = null pat || containsRegex (mkRegexWithOpts pat True False) str diff --git a/Ledger/Utils.hs b/Ledger/Utils.hs index 49b7226e3..18b7a654e 100644 --- a/Ledger/Utils.hs +++ b/Ledger/Utils.hs @@ -69,6 +69,11 @@ underline s = s' ++ replicate (length s) '-' ++ "\n" | last s == '\n' = s | otherwise = s ++ "\n" +unbracket :: String -> String +unbracket s + | (head s == '[' && last s == ']') || (head s == '(' && last s == ')') = init $ tail s + | otherwise = s + -- | Join multi-line strings as side-by-side rectangular strings of the same height, top-padded. concatTopPadded :: [String] -> String concatTopPadded strs = intercalate "\n" $ map concat $ transpose padded