Skip to content

Commit

Permalink
Remove code duplication and unify definition of 'trim' function. (#7795)
Browse files Browse the repository at this point in the history
* Remove code duplication and unify definition of 'trim' function.

* Attempt to refactor 'trimEnd' function.

* Remove unused import.

* Add @SInCE label.
  • Loading branch information
kczulko authored Nov 4, 2021
1 parent cd5dd66 commit 5b4258c
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cabal/Cabal.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ library
Distribution.Utils.LogProgress
Distribution.Utils.MapAccum
Distribution.Utils.MD5
Distribution.Utils.String
Distribution.Utils.Structured
Distribution.Compat.CreatePipe
Distribution.Compat.Directory
Expand Down Expand Up @@ -333,7 +334,6 @@ library
Distribution.Compat.SnocList
Distribution.GetOpt
Distribution.Lex
Distribution.Utils.String
Distribution.Simple.Build.Macros.Z
Distribution.Simple.Build.PathsModule.Z
Distribution.Simple.GHC.EnvironmentParser
Expand Down
3 changes: 2 additions & 1 deletion Cabal/src/Distribution/Compat/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module Distribution.Compat.Prelude (
sort, sortBy,
nub, nubBy,
partition,
dropWhileEnd,

-- * Data.List.NonEmpty
NonEmpty((:|)), nonEmpty, foldl1, foldr1,
Expand Down Expand Up @@ -186,7 +187,7 @@ import Data.Either (partitionEithers)
import Data.Function (on)
import Data.Functor.Identity (Identity (..))
import Data.Int (Int16, Int32, Int64, Int8)
import Data.List (intercalate, intersperse, isPrefixOf, isSuffixOf, nub, nubBy, partition, sort, sortBy, unfoldr)
import Data.List (dropWhileEnd, intercalate, intersperse, isPrefixOf, isSuffixOf, nub, nubBy, partition, sort, sortBy, unfoldr)
import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty, head, init, last, tail)
import Data.Map (Map)
import Data.Maybe (catMaybes, fromMaybe, isJust, isNothing, listToMaybe, mapMaybe, maybeToList)
Expand Down
4 changes: 1 addition & 3 deletions Cabal/src/Distribution/FieldGrammar/FieldDescrs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module Distribution.FieldGrammar.FieldDescrs (
import Distribution.Compat.Prelude
import Prelude ()

import Data.List (dropWhileEnd)
import Distribution.Compat.Lens (aview, cloneLens)
import Distribution.Utils.String (trim)
import Distribution.Compat.Newtype
import Distribution.FieldGrammar
import Distribution.Pretty (Pretty (..), showFreeText)
Expand Down Expand Up @@ -114,8 +114,6 @@ parsecFreeText = dropDotLines <$ C.spaces <*> many C.anyChar
trim' :: String -> String
trim' = dropWhileEnd (`elem` (" \t" :: String))

trim :: String -> String
trim = dropWhile isSpace . dropWhileEnd isSpace

class (P.Parsec a, Pretty a) => ParsecPretty a
instance (P.Parsec a, Pretty a) => ParsecPretty a
7 changes: 1 addition & 6 deletions Cabal/src/Distribution/FieldGrammar/Parsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ module Distribution.FieldGrammar.Parsec (
fieldLinesToStream,
) where

import Data.List (dropWhileEnd)
import Distribution.Compat.Newtype
import Distribution.Compat.Prelude
import Distribution.Simple.Utils (fromUTF8BS)
import Distribution.Utils.String (trim)
import Prelude ()

import qualified Data.ByteString as BS
Expand Down Expand Up @@ -271,8 +271,6 @@ instance FieldGrammar Parsec ParsecFieldGrammar where
]
-- hack: recover the order of prefixed fields
reorder = map snd . sortBy (comparing fst)
trim :: String -> String
trim = dropWhile isSpace . dropWhileEnd isSpace

availableSince vs def (ParsecFG names prefixes parser) = ParsecFG names prefixes parser'
where
Expand Down Expand Up @@ -391,9 +389,6 @@ fieldlinesToFreeText fls = intercalate "\n" (map go fls)
where
s = trim (fromUTF8BS bs)

trim :: String -> String
trim = dropWhile isSpace . dropWhileEnd isSpace

fieldlinesToFreeText3 :: Position -> [FieldLine Position] -> String
fieldlinesToFreeText3 _ [] = ""
fieldlinesToFreeText3 _ [FieldLine _ bs] = fromUTF8BS bs
Expand Down
2 changes: 1 addition & 1 deletion Cabal/src/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import Data.ByteString.Lazy ( ByteString )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as BLC8
import Data.List
( (\\), inits, stripPrefix, intersect, dropWhileEnd )
( (\\), inits, stripPrefix, intersect)
import qualified Data.Map as Map
import System.Directory
( canonicalizePath, createDirectoryIfMissing, doesFileExist
Expand Down
2 changes: 1 addition & 1 deletion Cabal/src/Distribution/Simple/UHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ getGlobalPackageDir verbosity progdb = do
let pkgdir = trimEnd output
return pkgdir
where
trimEnd = reverse . dropWhile isSpace . reverse
trimEnd = dropWhileEnd isSpace

getUserPackageDir :: IO FilePath
getUserPackageDir = do
Expand Down
7 changes: 7 additions & 0 deletions Cabal/src/Distribution/Utils/String.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ module Distribution.Utils.String
( -- * Encode to/from UTF8
decodeStringUtf8
, encodeStringUtf8
, trim
) where

import Data.Word
import Data.Bits
import Data.Char (chr,ord)
import Data.List (dropWhileEnd)
import GHC.Unicode (isSpace)

-- | Decode 'String' from UTF8-encoded octets.
--
Expand Down Expand Up @@ -91,3 +94,7 @@ encodeStringUtf8 (c:cs)
w8 = fromIntegral (ord c) :: Word8
w8ShiftR :: Int -> Word8
w8ShiftR = fromIntegral . shiftR (ord c)

-- @since 3.8.0.0
trim :: String -> String
trim = dropWhile isSpace . dropWhileEnd isSpace
7 changes: 1 addition & 6 deletions cabal-install/src/Distribution/Client/HttpUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import qualified Control.Exception as Exception
import Distribution.Simple.Utils
( die', info, warn, debug, notice
, copyFileVerbose, withTempFile, IOData (..) )
import Distribution.Utils.String (trim)
import Distribution.Client.Utils
( withTempFileName )
import Distribution.Client.Version
Expand Down Expand Up @@ -890,12 +891,6 @@ statusParseFail verbosity uri r =
die' verbosity $ "Failed to download " ++ show uri ++ " : "
++ "No Status Code could be parsed from response: " ++ r

-- Trim
trim :: String -> String
trim = f . f
where f = reverse . dropWhile isSpace


------------------------------------------------------------------------------
-- Multipart stuff partially taken from cgi package.
--
Expand Down
6 changes: 1 addition & 5 deletions cabal-install/src/Distribution/Client/Init/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import qualified Distribution.Package as P
import qualified Distribution.Types.PackageName as PN
import Distribution.Simple.PackageIndex (InstalledPackageIndex, moduleNameIndex)
import Distribution.Simple.Setup (Flag(..))
import Distribution.Utils.String (trim)
import Distribution.Version
import Distribution.Client.Init.Defaults
import Distribution.Client.Init.Types
Expand Down Expand Up @@ -203,11 +204,6 @@ takeWhile' p = takeWhile p . trim
dropWhile' :: (Char -> Bool) -> String -> String
dropWhile' p = dropWhile p . trim

trim :: String -> String
trim = removeLeadingSpace . reverse . removeLeadingSpace . reverse
where
removeLeadingSpace = dropWhile isSpace

-- | Check whether a potential source file is located in one of the
-- source directories.
isSourceFile :: Maybe [FilePath] -> SourceFileEntry -> Bool
Expand Down
5 changes: 1 addition & 4 deletions cabal-install/src/Distribution/Client/Upload.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Distribution.Client.Setup
( IsCandidate(..), RepoContext(..) )

import Distribution.Simple.Utils (notice, warn, info, die', toUTF8BS)
import Distribution.Utils.String (trim)
import Distribution.Client.Config

import qualified Distribution.Client.BuildReports.Anonymous as BuildReport
Expand Down Expand Up @@ -228,7 +229,3 @@ handlePackage transport verbosity uri packageUri auth isCandidate path =
formatWarnings :: String -> String
formatWarnings x = "Warnings:\n" ++ (unlines . map ("- " ++) . lines) x

-- Trim
trim :: String -> String
trim = f . f
where f = reverse . dropWhile isSpace

0 comments on commit 5b4258c

Please sign in to comment.