Skip to content

Commit a5843b4

Browse files
committed
Improve performance of fromTarPathInternal and fromFilePathToNative
1 parent 6dd1a8d commit a5843b4

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

Codec/Archive/Tar/Types.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,15 @@ fromTarPathToWindowsPath :: TarPath -> FilePath
384384
fromTarPathToWindowsPath = BS.Char8.unpack . fromTarPathInternal FilePath.Windows.pathSeparator
385385

386386
fromTarPathInternal :: Char -> TarPath -> BS.ByteString
387-
fromTarPathInternal sep (TarPath name prefix)
388-
| BS.null prefix = adjustSeps name
389-
| BS.null name = adjustSeps prefix
390-
| sep == FilePath.Posix.pathSeparator = prefix <> BS.Char8.cons sep name
391-
| otherwise = adjustSeps prefix <> BS.Char8.cons sep (adjustSeps name)
387+
fromTarPathInternal sep = go
392388
where
393-
adjustSeps = BS.Char8.map $ \c -> if c == FilePath.Posix.pathSeparator then sep else c
389+
posixSep = FilePath.Posix.pathSeparator
390+
adjustSeps = if sep == posixSep then id else
391+
BS.Char8.map $ \c -> if c == posixSep then sep else c
392+
go (TarPath name prefix)
393+
| BS.null prefix = adjustSeps name
394+
| BS.null name = adjustSeps prefix
395+
| otherwise = adjustSeps prefix <> BS.Char8.cons sep (adjustSeps name)
394396
{-# INLINE fromTarPathInternal #-}
395397

396398
-- | Convert a native 'FilePath' to a 'TarPath'.
@@ -528,21 +530,20 @@ fromLinkTargetToWindowsPath (LinkTarget pathbs) =
528530

529531
-- | Convert a unix FilePath to a native 'FilePath'.
530532
fromFilePathToNative :: FilePath -> FilePath
531-
fromFilePathToNative path = adjustDirectory $
532-
FilePath.Native.joinPath $ FilePath.Posix.splitDirectories path
533-
where
534-
adjustDirectory | FilePath.Posix.hasTrailingPathSeparator path
535-
= FilePath.Native.addTrailingPathSeparator
536-
| otherwise = id
533+
fromFilePathToNative =
534+
fromFilePathInternal FilePath.Posix.pathSeparator FilePath.Native.pathSeparator
537535

538536
-- | Convert a unix FilePath to a Windows 'FilePath'.
539537
fromFilePathToWindowsPath :: FilePath -> FilePath
540-
fromFilePathToWindowsPath path = adjustDirectory $
541-
FilePath.Windows.joinPath $ FilePath.Posix.splitDirectories path
538+
fromFilePathToWindowsPath =
539+
fromFilePathInternal FilePath.Posix.pathSeparator FilePath.Windows.pathSeparator
540+
541+
fromFilePathInternal :: Char -> Char -> FilePath -> FilePath
542+
fromFilePathInternal fromSep toSep = adjustSeps
542543
where
543-
adjustDirectory | FilePath.Posix.hasTrailingPathSeparator path
544-
= FilePath.Windows.addTrailingPathSeparator
545-
| otherwise = id
544+
adjustSeps = if fromSep == toSep then id else
545+
map $ \c -> if c == fromSep then toSep else c
546+
{-# INLINE fromFilePathInternal #-}
546547

547548
--
548549
-- * Entries type

0 commit comments

Comments
 (0)