Move away from regexes for basic String matching

This leverages simpler tests to determine matches over regular
expressions
This commit is contained in:
Joshua Clayton 2016-06-11 06:58:28 -04:00
parent 9b58030110
commit 07c0fb0d3f
3 changed files with 22 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -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 == '-'