bring back OsPath changes

I hope that the windows test suite failure on appveyor was fixed by
updating to a newer windows there. I have not been able to reproduce
that failure in a windows 11 VM run locally.
This commit is contained in:
Joey Hess 2025-01-30 14:34:21 -04:00
parent f0ab439c95
commit 84291b6014
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
119 changed files with 1003 additions and 647 deletions

View file

@ -1,20 +1,24 @@
{- misc utility functions
-
- Copyright 2010-2011 Joey Hess <id@joeyh.name>
- Copyright 2010-2025 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-tabs #-}
module Utility.Misc (
hGetContentsStrict,
readFileStrict,
separate,
separate',
separateEnd',
firstLine,
firstLine',
fileLines,
fileLines',
linesFile,
linesFile',
segment,
segmentDelim,
massReplace,
@ -32,6 +36,9 @@ import Data.List
import System.Exit
import Control.Applicative
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Char8 as L8
import Prelude
{- A version of hgetContents that is not lazy. Ensures file is
@ -39,10 +46,6 @@ import Prelude
hGetContentsStrict :: Handle -> IO String
hGetContentsStrict = hGetContents >=> \s -> length s `seq` return s
{- A version of readFile that is not lazy. -}
readFileStrict :: FilePath -> IO String
readFileStrict = readFile >=> \s -> length s `seq` return s
{- Like break, but the item matching the condition is not included
- in the second result list.
-
@ -78,6 +81,51 @@ firstLine' = S.takeWhile (/= nl)
where
nl = fromIntegral (ord '\n')
-- On windows, readFile does NewlineMode translation,
-- stripping CR before LF. When converting to ByteString,
-- use this to emulate that.
fileLines :: L.ByteString -> [L.ByteString]
#ifdef mingw32_HOST_OS
fileLines = map stripCR . L8.lines
where
stripCR b = case L8.unsnoc b of
Nothing -> b
Just (b', e)
| e == '\r' -> b'
| otherwise -> b
#else
fileLines = L8.lines
#endif
fileLines' :: S.ByteString -> [S.ByteString]
#ifdef mingw32_HOST_OS
fileLines' = map stripCR . S8.lines
where
stripCR b = case S8.unsnoc b of
Nothing -> b
Just (b', e)
| e == '\r' -> b'
| otherwise -> b
#else
fileLines' = S8.lines
#endif
-- One windows, writeFile does NewlineMode translation,
-- adding CR before LF. When converting to ByteString, use this to emulate that.
linesFile :: L.ByteString -> L.ByteString
#ifndef mingw32_HOST_OS
linesFile = id
#else
linesFile = L8.concat . concatMap (\x -> [x, L8.pack "\r\n"]) . fileLines
#endif
linesFile' :: S.ByteString -> S.ByteString
#ifndef mingw32_HOST_OS
linesFile' = id
#else
linesFile' = S8.concat . concatMap (\x -> [x, S8.pack "\r\n"]) . fileLines'
#endif
{- Splits a list into segments that are delimited by items matching
- a predicate. (The delimiters are not included in the segments.)
- Segments may be empty. -}