Skip to content

Commit

Permalink
Added Path.Internal.{Posix,Windows}.isWindows and removed some CPP fr…
Browse files Browse the repository at this point in the history
…om Path include file
  • Loading branch information
mmhat committed Jun 25, 2024
1 parent e73559b commit 32c140e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/OsPath/Internal/Include.hs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ isWindows = True
#else
isWindows = False
#endif
{-# INLINE isWindows #-}

--------------------------------------------------------------------------------
-- Orphan instances
Expand Down
19 changes: 6 additions & 13 deletions src/Path/Include.hs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/Path/Internal/Include.hs
Original file line number Diff line number Diff line change
@@ -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 #-}
Expand All @@ -15,6 +15,7 @@ module Path.Internal.PLATFORM_NAME
, relRootFP
, toFilePath
, hasParentDir
, isWindows
)
where

Expand Down Expand Up @@ -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 #-}
2 changes: 1 addition & 1 deletion src/Path/Internal/Posix.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE CPP #-}
#define PLATFORM_NAME Posix
#define IS_WINDOWS False
#define IS_WINDOWS 0
#include "Include.hs"
2 changes: 1 addition & 1 deletion src/Path/Internal/Windows.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE CPP #-}
#define PLATFORM_NAME Windows
#define IS_WINDOWS True
#define IS_WINDOWS 1
#include "Include.hs"
1 change: 0 additions & 1 deletion src/Path/Posix.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
#define PLATFORM_NAME Posix
#define IS_WINDOWS 0
#include "Include.hs"
1 change: 0 additions & 1 deletion src/Path/Windows.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
#define PLATFORM_NAME Windows
#define IS_WINDOWS 1
#include "Include.hs"

0 comments on commit 32c140e

Please sign in to comment.