From 07c0fb0d3ffade4279b69219265ce6dde3749a37 Mon Sep 17 00:00:00 2001 From: Joshua Clayton Date: Sat, 11 Jun 2016 06:58:28 -0400 Subject: [PATCH] Move away from regexes for basic String matching This leverages simpler tests to determine matches over regular expressions --- src/Unused/ResponseFilter.hs | 22 +++++++++++++--------- src/Unused/TagsSource.hs | 11 +++++------ src/Unused/TermSearch/Internal.hs | 6 ++++-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/Unused/ResponseFilter.hs b/src/Unused/ResponseFilter.hs index e2b5f60..374bf6a 100644 --- a/src/Unused/ResponseFilter.hs +++ b/src/Unused/ResponseFilter.hs @@ -10,7 +10,7 @@ module Unused.ResponseFilter import qualified Data.Map.Strict as Map import Data.List (isInfixOf, isPrefixOf, isSuffixOf) -import Unused.Regex (matchRegex) +import qualified Data.Char as C import Unused.Types import Unused.ResultsClassifier @@ -35,7 +35,11 @@ includesLikelihood :: [RemovalLikelihood] -> TermResults -> Bool includesLikelihood l = (`elem` l) . rLikelihood . trRemoval isClassOrModule :: TermResults -> Bool -isClassOrModule = matchRegex "^[A-Z]" . trTerm +isClassOrModule = + startsWithUpper . trTerm + where + startsWithUpper [] = False + startsWithUpper (a:_) = C.isUpper a autoLowLikelihood :: LanguageConfiguration -> TermResults -> Bool autoLowLikelihood l r = @@ -55,15 +59,15 @@ classOrModuleFunction True = isClassOrModule classOrModuleFunction False = const True matcherToBool :: Matcher -> TermResults -> Bool -matcherToBool (Path p v) = any (positionToRegex p v) . paths -matcherToBool (Term p v) = positionToRegex p v . trTerm +matcherToBool (Path p v) = any (positionToTest p v) . paths +matcherToBool (Term p v) = positionToTest p v . trTerm matcherToBool (AppOccurrences i) = (== i) . appOccurrenceCount -matcherToBool (AllowedTerms ts) = flip isAllowedTerm ts +matcherToBool (AllowedTerms ts) = (`isAllowedTerm` ts) -positionToRegex :: Position -> (String -> String -> Bool) -positionToRegex StartsWith = isPrefixOf -positionToRegex EndsWith = isSuffixOf -positionToRegex Equals = (==) +positionToTest :: Position -> (String -> String -> Bool) +positionToTest StartsWith = isPrefixOf +positionToTest EndsWith = isSuffixOf +positionToTest Equals = (==) paths :: TermResults -> [String] paths r = tmPath <$> trMatches r diff --git a/src/Unused/TagsSource.hs b/src/Unused/TagsSource.hs index ee38977..4aacf25 100644 --- a/src/Unused/TagsSource.hs +++ b/src/Unused/TagsSource.hs @@ -6,9 +6,8 @@ module Unused.TagsSource , loadTagsFromPipe ) where -import Data.List (nub) +import Data.List (isPrefixOf, nub) import System.Directory (findFile) -import Unused.Regex (matchRegex) import qualified Data.Text as T data TagSearchOutcome @@ -22,13 +21,13 @@ loadTagsFromFile = fmap (fmap tokensFromTags) tagsContent tokensFromTags :: String -> [String] tokensFromTags = - filter tagRemovalRegex . nub . map token . tokenLocations + filter validTokens . nub . tokenLocations where - tokenLocations = map (T.splitOn "\t" . T.pack) . lines + tokenLocations = map (token . T.splitOn "\t" . T.pack) . lines token = T.unpack . head -tagRemovalRegex :: String -> Bool -tagRemovalRegex = not . matchRegex "^!_TAG" +validTokens :: String -> Bool +validTokens = not . isPrefixOf "!_TAG" tagsContent :: IO (Either TagSearchOutcome String) tagsContent = findFile possibleTagsFileDirectories "tags" >>= eitherReadFile diff --git a/src/Unused/TermSearch/Internal.hs b/src/Unused/TermSearch/Internal.hs index 9b67f60..e53fe11 100644 --- a/src/Unused/TermSearch/Internal.hs +++ b/src/Unused/TermSearch/Internal.hs @@ -7,8 +7,8 @@ module Unused.TermSearch.Internal import Data.Maybe (fromMaybe) import qualified Data.Text as T +import qualified Data.Char as C import Unused.Types (TermMatch(..)) -import Unused.Regex import Unused.Util (stringToInt) commandLineOptions :: String -> [String] @@ -29,4 +29,6 @@ parseSearchResult term s = regexSafeTerm :: String -> Bool regexSafeTerm = - matchRegex "^[[:word:]]+$" + all regexSafeChar + where + regexSafeChar c = C.isAlphaNum c || c == '_' || c == '-'