From 32c140ef34b6128bdd30b322f03e012235256459 Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Wed, 26 Jun 2024 00:50:32 +0200 Subject: [PATCH] Added Path.Internal.{Posix,Windows}.isWindows and removed some CPP from Path include file --- src/OsPath/Internal/Include.hs | 1 + src/Path/Include.hs | 19 ++++++------------- src/Path/Internal/Include.hs | 11 ++++++++++- src/Path/Internal/Posix.hs | 2 +- src/Path/Internal/Windows.hs | 2 +- src/Path/Posix.hs | 1 - src/Path/Windows.hs | 1 - 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/OsPath/Internal/Include.hs b/src/OsPath/Internal/Include.hs index 9ac9b2b..7adf4ed 100644 --- a/src/OsPath/Internal/Include.hs +++ b/src/OsPath/Internal/Include.hs @@ -295,6 +295,7 @@ isWindows = True #else isWindows = False #endif +{-# INLINE isWindows #-} -------------------------------------------------------------------------------- -- Orphan instances diff --git a/src/Path/Include.hs b/src/Path/Include.hs index 1ca20ae..76ae14e 100644 --- a/src/Path/Include.hs +++ b/src/Path/Include.hs @@ -1,6 +1,5 @@ -- This template expects CPP definitions for: -- PLATFORM_NAME = Posix | Windows --- IS_WINDOWS = 0 | 1 -- | This library provides a well-typed representation of paths in a filesystem -- directory tree. @@ -482,11 +481,9 @@ splitExtension (Path fpath) = trailingSeps = takeWhile isSep rstr xtn = (takeWhile notSep . dropWhile isSep) rstr in (reverse name, reverse xtn ++ trailingSeps) -#if IS_WINDOWS - normalizeDrive = normalizeTrailingSeps -#else - normalizeDrive = id -#endif + normalizeDrive + | isWindows = normalizeTrailingSeps + | otherwise = id (drv, pth) = FilePath.splitDrive fpath (dir, file) = splitLast FilePath.isPathSeparator pth @@ -826,7 +823,6 @@ normalizeLeadingSeps path = normLeadingSep ++ rest where (leadingSeps, rest) = span FilePath.isPathSeparator path normLeadingSep = replicate (min 1 (length leadingSeps)) FilePath.pathSeparator -#if IS_WINDOWS -- | Normalizes seps only at the end of a path. normalizeTrailingSeps :: FilePath -> FilePath normalizeTrailingSeps = reverse . normalizeLeadingSeps . reverse @@ -846,15 +842,12 @@ normalizeWindowsSeps :: FilePath -> FilePath normalizeWindowsSeps path = normLeadingSeps ++ normalizeAllSeps rest where (leadingSeps, rest) = span FilePath.isPathSeparator path normLeadingSeps = replicate (min 2 (length leadingSeps)) FilePath.pathSeparator -#endif -- | Applies platform-specific sep normalization following @FilePath.normalise@. normalizeFilePath :: FilePath -> FilePath -#if IS_WINDOWS -normalizeFilePath = normalizeWindowsSeps . FilePath.normalise -#else -normalizeFilePath = normalizeLeadingSeps . FilePath.normalise -#endif +normalizeFilePath + | isWindows = normalizeWindowsSeps . FilePath.normalise + | otherwise = normalizeLeadingSeps . FilePath.normalise -- | Path of some type. @t@ represents the type, whether file or -- directory. Pattern match to find whether the path is absolute or diff --git a/src/Path/Internal/Include.hs b/src/Path/Internal/Include.hs index f91fd23..aa9035b 100644 --- a/src/Path/Internal/Include.hs +++ b/src/Path/Internal/Include.hs @@ -1,6 +1,6 @@ -- This template expects CPP definitions for: -- PLATFORM_NAME = Posix | Windows --- IS_WINDOWS = False | True +-- IS_WINDOWS = 0 | 1 {-# LANGUAGE CPP #-} {-# LANGUAGE DeriveGeneric #-} @@ -15,6 +15,7 @@ module Path.Internal.PLATFORM_NAME , relRootFP , toFilePath , hasParentDir + , isWindows ) where @@ -136,3 +137,11 @@ instance forall b t. (Typeable b, Typeable t) => TH.Lift (Path b t) where #elif MIN_VERSION_template_haskell(2,16,0) liftTyped = TH.unsafeTExpCoerce . TH.lift #endif + +isWindows :: Bool +#if IS_WINDOWS +isWindows = True +#else +isWindows = False +#endif +{-# INLINE isWindows #-} diff --git a/src/Path/Internal/Posix.hs b/src/Path/Internal/Posix.hs index 25e35e1..23a1b40 100644 --- a/src/Path/Internal/Posix.hs +++ b/src/Path/Internal/Posix.hs @@ -1,4 +1,4 @@ {-# LANGUAGE CPP #-} #define PLATFORM_NAME Posix -#define IS_WINDOWS False +#define IS_WINDOWS 0 #include "Include.hs" diff --git a/src/Path/Internal/Windows.hs b/src/Path/Internal/Windows.hs index a8b5cbb..95b16e4 100644 --- a/src/Path/Internal/Windows.hs +++ b/src/Path/Internal/Windows.hs @@ -1,4 +1,4 @@ {-# LANGUAGE CPP #-} #define PLATFORM_NAME Windows -#define IS_WINDOWS True +#define IS_WINDOWS 1 #include "Include.hs" diff --git a/src/Path/Posix.hs b/src/Path/Posix.hs index 23a1b40..49b09ea 100644 --- a/src/Path/Posix.hs +++ b/src/Path/Posix.hs @@ -1,4 +1,3 @@ {-# LANGUAGE CPP #-} #define PLATFORM_NAME Posix -#define IS_WINDOWS 0 #include "Include.hs" diff --git a/src/Path/Windows.hs b/src/Path/Windows.hs index 95b16e4..5a09ca7 100644 --- a/src/Path/Windows.hs +++ b/src/Path/Windows.hs @@ -1,4 +1,3 @@ {-# LANGUAGE CPP #-} #define PLATFORM_NAME Windows -#define IS_WINDOWS 1 #include "Include.hs"