From 850d19d038ef7c6fb65dcd431efefd841af0132b Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Fri, 23 Nov 2018 11:24:05 -0400 Subject: [PATCH] add dropFromEnd --- Git/Remote.hs | 3 +-- Types/Export.hs | 3 ++- Utility/Scheduled.hs | 3 ++- Utility/Split.hs | 4 ++++ Utility/Url.hs | 3 +-- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Git/Remote.hs b/Git/Remote.hs index ce741a0d0a..28e8b0741f 100644 --- a/Git/Remote.hs +++ b/Git/Remote.hs @@ -26,8 +26,7 @@ isRemoteKey k = "remote." `isPrefixOf` k && ".url" `isSuffixOf` k {- Get a remote's name from the config key that specifies its location. -} remoteKeyToRemoteName :: String -> RemoteName -remoteKeyToRemoteName k = intercalate "." $ - reverse $ drop 1 $ reverse $ drop 1 $ splitc '.' k +remoteKeyToRemoteName k = intercalate "." $ dropFromEnd 1 $ drop 1 $ splitc '.' k {- Construct a legal git remote name out of an arbitrary input string. - diff --git a/Types/Export.hs b/Types/Export.hs index 0e86f96848..35b3404240 100644 --- a/Types/Export.hs +++ b/Types/Export.hs @@ -16,6 +16,7 @@ module Types.Export ( ) where import Git.FilePath +import Utility.Split import qualified System.FilePath.Posix as Posix @@ -50,4 +51,4 @@ exportDirectories (ExportLocation f) = subs ps (d:ds) = (d:ps) : subs (d:ps) ds dirs = map Posix.dropTrailingPathSeparator $ - reverse $ drop 1 $ reverse $ Posix.splitPath f + dropFromEnd 1 $ Posix.splitPath f diff --git a/Utility/Scheduled.hs b/Utility/Scheduled.hs index b68ff901ca..12ead42521 100644 --- a/Utility/Scheduled.hs +++ b/Utility/Scheduled.hs @@ -30,6 +30,7 @@ import Utility.Data import Utility.PartialPrelude import Utility.Misc import Utility.Tuple +import Utility.Split import Data.List import Data.Time.Clock @@ -265,7 +266,7 @@ toRecurrance s = case words s of constructor "month" = Just Monthly constructor "year" = Just Yearly constructor u - | "s" `isSuffixOf` u = constructor $ reverse $ drop 1 $ reverse u + | "s" `isSuffixOf` u = constructor $ dropFromEnd 1 u | otherwise = Nothing withday sd u = do c <- constructor u diff --git a/Utility/Split.hs b/Utility/Split.hs index decfe7d396..ffea5d3f07 100644 --- a/Utility/Split.hs +++ b/Utility/Split.hs @@ -28,3 +28,7 @@ splitc c s = case break (== c) s of -- | same as Data.List.Utils.replace replace :: Eq a => [a] -> [a] -> [a] -> [a] replace old new = intercalate new . split old + +-- | Only traverses the list once while dropping the last n characters. +dropFromEnd :: Int -> [a] -> [a] +dropFromEnd n l = zipWith const l (drop n l) diff --git a/Utility/Url.hs b/Utility/Url.hs index c29db74352..a9c46b4326 100644 --- a/Utility/Url.hs +++ b/Utility/Url.hs @@ -278,8 +278,7 @@ getUrlInfo url uo = case parseURIRelaxed url of contentDispositionFilename :: String -> Maybe FilePath contentDispositionFilename s | "attachment; filename=\"" `isPrefixOf` s && "\"" `isSuffixOf` s = - Just $ reverse $ drop 1 $ reverse $ - drop 1 $ dropWhile (/= '"') s + Just $ dropFromEnd 1 $ drop 1 $ dropWhile (/= '"') s | otherwise = Nothing headRequest :: Request -> Request