1+ {-# LANGUAGE CPP #-}
12{-# LANGUAGE TypeApplications #-}
23{-# LANGUAGE BangPatterns #-}
34{-# LANGUAGE ViewPatterns #-}
@@ -8,7 +9,7 @@ module System.File.OsPath.Internal where
89
910import qualified System.File.Platform as P
1011
11- import Prelude ((.) , ($) , String , IO , ioError , pure , either , const , flip , Maybe (.. ), fmap , (<$>) , id , Bool (.. ), FilePath , (++) , return , show , (>>=) , (==) , otherwise , errorWithoutStackTrace , userError )
12+ import Prelude ((.) , ($) , String , IO , ioError , pure , either , const , flip , Maybe (.. ), fmap , (<$>) , id , Bool (.. ), FilePath , (++) , return , show , (>>=) , (==) , otherwise , errorWithoutStackTrace , userError , mempty )
1213import GHC.IO (catchException )
1314import GHC.IO.Exception (IOException (.. ))
1415import GHC.IO.Handle (hClose_help )
@@ -20,14 +21,16 @@ import Control.DeepSeq (force)
2021import Control.Exception (SomeException , try , evaluate , mask , onException , throwIO )
2122import System.IO (IOMode (.. ), hSetBinaryMode , hClose )
2223import System.IO.Unsafe (unsafePerformIO )
23- import System.OsString (osstr )
2424import System.OsPath as OSP
2525import System.OsString.Internal.Types
2626
2727import qualified Data.ByteString as BS
2828import qualified Data.ByteString.Lazy as BSL
2929import qualified System.OsString as OSS
3030import System.Posix.Types (CMode )
31+ #if !MIN_VERSION_filepath(1, 5, 0)
32+ import Data.Coerce
33+ #endif
3134
3235-- | Like 'openFile', but open the file in binary mode.
3336-- On Windows, reading a file in text mode (which is the default)
@@ -232,7 +235,7 @@ augmentError str osfp = flip catchException (ioError . addFilePathToIOError str
232235openTempFile' :: String -> OsPath -> OsString -> Bool -> CMode
233236 -> IO (OsPath , Handle )
234237openTempFile' loc (OsString tmp_dir) template@ (OsString tmpl) binary mode
235- | OSS. any (== OSP. pathSeparator) template
238+ | any_ (== OSP. pathSeparator) template
236239 = throwIO $ userError $ " openTempFile': Template string must not contain path separator characters: " ++ P. lenientDecode tmpl
237240 | otherwise = do
238241 (fp, hdl) <- P. findTempName (prefix, suffix) loc tmp_dir mode
@@ -243,18 +246,45 @@ openTempFile' loc (OsString tmp_dir) template@(OsString tmpl) binary mode
243246 -- for temporary files (hidden on Unix OSes). Unfortunately we're
244247 -- below filepath in the hierarchy here.
245248 (OsString prefix, OsString suffix) =
246- case OSS. break (== OSS. unsafeFromChar ' .' ) $ OSS. reverse template of
249+ case break_ (== OSS. unsafeFromChar ' .' ) $ reverse_ template of
247250 -- First case: template contains no '.'s. Just re-reverse it.
248- (rev_suffix, [osstr ||]) -> (OSS. reverse rev_suffix, OSS. empty)
251+ (rev_suffix, xs)
252+ | xs == mempty -> (reverse_ rev_suffix, mempty )
249253 -- Second case: template contains at least one '.'. Strip the
250254 -- dot from the prefix and prepend it to the suffix (if we don't
251255 -- do this, the unique number will get added after the '.' and
252256 -- thus be part of the extension, which is wrong.)
253257 (rev_suffix, xs)
254258 | (h: rest) <- OSS. unpack xs
255- , h == unsafeFromChar ' .' -> (OSS. reverse (OSS. pack rest), OSS. cons (unsafeFromChar ' .' ) $ OSS. reverse rev_suffix)
259+ , h == unsafeFromChar ' .' -> (reverse_ (OSS. pack rest), cons_ (unsafeFromChar ' .' ) $ reverse_ rev_suffix)
256260 -- Otherwise, something is wrong, because (break (== '.')) should
257261 -- always return a pair with either the empty string or a string
258262 -- beginning with '.' as the second component.
259263 _ -> errorWithoutStackTrace " bug in System.IO.openTempFile"
260264
265+ #if MIN_VERSION_filepath(1, 5, 0)
266+ any_ :: (OsChar -> Bool ) -> OsString -> Bool
267+ any_ = OSS. any
268+
269+ cons_ :: OsChar -> OsString -> OsString
270+ cons_ = OSS. cons
271+
272+ break_ :: (OsChar -> Bool ) -> OsString -> (OsString , OsString )
273+ break_ = OSS. break
274+
275+ reverse_ :: OsString -> OsString
276+ reverse_ = OSS. reverse
277+ #else
278+ any_ :: (OsChar -> Bool ) -> OsString -> Bool
279+ any_ = coerce P. any_
280+
281+ cons_ :: OsChar -> OsString -> OsString
282+ cons_ = coerce P. cons_
283+
284+ break_ :: (OsChar -> Bool ) -> OsString -> (OsString , OsString )
285+ break_ = coerce P. break_
286+
287+ reverse_ :: OsString -> OsString
288+ reverse_ = coerce P. reverse_
289+ #endif
290+
0 commit comments