mirror of
https://github.com/joshuaclayton/unused.git
synced 2024-09-11 12:55:54 +03:00
Move away from regexes for basic String matching
This leverages simpler tests to determine matches over regular expressions
This commit is contained in:
parent
9b58030110
commit
07c0fb0d3f
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 == '-'
|
||||
|
Loading…
Reference in New Issue
Block a user