Skip to content

Commit

Permalink
Coerce functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Oct 15, 2023
1 parent e11289e commit b0e7018
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 75 deletions.
101 changes: 51 additions & 50 deletions System/OsString/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ import System.OsString.Internal.Types (
#endif
)

import Data.Coerce
import Data.Char
import Control.Monad.Catch
( MonadThrow, throwM )
Expand Down Expand Up @@ -532,9 +533,9 @@ length (PosixString s) = BS.length s
-- @since 1.4.200.0
map :: (PLATFORM_WORD -> PLATFORM_WORD) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
map f (WindowsString s) = WindowsString (BS16.map (getWindowsChar . f . WindowsChar) s)
map f (WindowsString s) = WindowsString (BS16.map (getWindowsChar . coerce f) s)
#else
map f (PosixString s) = PosixString (BS.map (getPosixChar . f . PosixChar) s)
map f (PosixString s) = PosixString (BS.map (getPosixChar . coerce f) s)
#endif

-- | /O(n)/ 'reverse' @xs@ efficiently returns the elements of @xs@ in reverse order.
Expand Down Expand Up @@ -566,9 +567,9 @@ intercalate (PosixString s) xs = PosixString (BS.intercalate s (fmap getPosixStr
-- @since 1.4.200.0
foldl :: (a -> PLATFORM_WORD -> a) -> a -> PLATFORM_STRING -> a
#ifdef WINDOWS
foldl f a (WindowsString s) = BS16.foldl (\a' c -> f a' (WindowsChar c)) a s
foldl f a (WindowsString s) = BS16.foldl (coerce f) a s
#else
foldl f a (PosixString s) = BS.foldl (\a' c -> f a' (PosixChar c)) a s
foldl f a (PosixString s) = BS.foldl (coerce f) a s
#endif

-- | 'foldl'' is like 'foldl', but strict in the accumulator.
Expand All @@ -577,9 +578,9 @@ foldl f a (PosixString s) = BS.foldl (\a' c -> f a' (PosixChar c)) a s
foldl'
:: (a -> PLATFORM_WORD -> a) -> a -> PLATFORM_STRING -> a
#ifdef WINDOWS
foldl' f a (WindowsString s) = BS16.foldl' (\a' c -> f a' (WindowsChar c)) a s
foldl' f a (WindowsString s) = BS16.foldl' (coerce f) a s
#else
foldl' f a (PosixString s) = BS.foldl' (\a' c -> f a' (PosixChar c)) a s
foldl' f a (PosixString s) = BS.foldl' (coerce f) a s
#endif

-- | 'foldl1' is a variant of 'foldl' that has no starting value
Expand All @@ -589,9 +590,9 @@ foldl' f a (PosixString s) = BS.foldl' (\a' c -> f a' (PosixChar c)) a s
-- @since 1.4.200.0
foldl1 :: (PLATFORM_WORD -> PLATFORM_WORD -> PLATFORM_WORD) -> PLATFORM_STRING -> PLATFORM_WORD
#ifdef WINDOWS
foldl1 f (WindowsString s) = WindowsChar $ BS16.foldl1 (\a' c -> getWindowsChar $ f (WindowsChar a') (WindowsChar c)) s
foldl1 f (WindowsString s) = WindowsChar $ BS16.foldl1 (\a' c -> getWindowsChar $ coerce f a' c) s
#else
foldl1 f (PosixString s) = PosixChar $ BS.foldl1 (\a' c -> getPosixChar $ f (PosixChar a') (PosixChar c)) s
foldl1 f (PosixString s) = PosixChar $ BS.foldl1 (\a' c -> getPosixChar $ coerce f a' c) s
#endif

-- | 'foldl1'' is like 'foldl1', but strict in the accumulator.
Expand All @@ -601,9 +602,9 @@ foldl1 f (PosixString s) = PosixChar $ BS.foldl1 (\a' c -> getPosixChar $ f (Pos
foldl1'
:: (PLATFORM_WORD -> PLATFORM_WORD -> PLATFORM_WORD) -> PLATFORM_STRING -> PLATFORM_WORD
#ifdef WINDOWS
foldl1' f (WindowsString s) = WindowsChar $ BS16.foldl1' (\a' c -> getWindowsChar $ f (WindowsChar a') (WindowsChar c)) s
foldl1' f (WindowsString s) = WindowsChar $ BS16.foldl1' (\a' c -> getWindowsChar $ coerce f a' c) s
#else
foldl1' f (PosixString s) = PosixChar $ BS.foldl1' (\a' c -> getPosixChar $ f (PosixChar a') (PosixChar c)) s
foldl1' f (PosixString s) = PosixChar $ BS.foldl1' (\a' c -> getPosixChar $ coerce f a' c) s
#endif

-- | 'foldr', applied to a binary operator, a starting value
Expand All @@ -613,9 +614,9 @@ foldl1' f (PosixString s) = PosixChar $ BS.foldl1' (\a' c -> getPosixChar $ f (P
-- @since 1.4.200.0
foldr :: (PLATFORM_WORD -> a -> a) -> a -> PLATFORM_STRING -> a
#ifdef WINDOWS
foldr f a (WindowsString s) = BS16.foldr (\c a' -> f (WindowsChar c) a') a s
foldr f a (WindowsString s) = BS16.foldr (coerce f) a s
#else
foldr f a (PosixString s) = BS.foldr (\c a' -> f (PosixChar c) a') a s
foldr f a (PosixString s) = BS.foldr (coerce f) a s
#endif

-- | 'foldr'' is like 'foldr', but strict in the accumulator.
Expand All @@ -624,9 +625,9 @@ foldr f a (PosixString s) = BS.foldr (\c a' -> f (PosixChar c) a') a s
foldr'
:: (PLATFORM_WORD -> a -> a) -> a -> PLATFORM_STRING -> a
#ifdef WINDOWS
foldr' f a (WindowsString s) = BS16.foldr' (\c a' -> f (WindowsChar c) a') a s
foldr' f a (WindowsString s) = BS16.foldr' (coerce f) a s
#else
foldr' f a (PosixString s) = BS.foldr' (\c a' -> f (PosixChar c) a') a s
foldr' f a (PosixString s) = BS.foldr' (coerce f) a s
#endif

-- | 'foldr1' is a variant of 'foldr' that has no starting value argument,
Expand All @@ -636,9 +637,9 @@ foldr' f a (PosixString s) = BS.foldr' (\c a' -> f (PosixChar c) a') a s
-- @since 1.4.200.0
foldr1 :: (PLATFORM_WORD -> PLATFORM_WORD -> PLATFORM_WORD) -> PLATFORM_STRING -> PLATFORM_WORD
#ifdef WINDOWS
foldr1 f (WindowsString s) = WindowsChar $ BS16.foldr1 (\c a' -> getWindowsChar $ f (WindowsChar c) (WindowsChar a')) s
foldr1 f (WindowsString s) = WindowsChar $ BS16.foldr1 (\c a' -> getWindowsChar $ coerce f c a') s
#else
foldr1 f (PosixString s) = PosixChar $ BS.foldr1 (\c a' -> getPosixChar $ f (PosixChar c) (PosixChar a')) s
foldr1 f (PosixString s) = PosixChar $ BS.foldr1 (\c a' -> getPosixChar $ coerce f c a') s
#endif

-- | 'foldr1'' is a variant of 'foldr1', but is strict in the
Expand All @@ -650,9 +651,9 @@ foldr1 f (PosixString s) = PosixChar $ BS.foldr1 (\c a' -> getPosixChar $ f (Pos
foldr1'
:: (PLATFORM_WORD -> PLATFORM_WORD -> PLATFORM_WORD) -> PLATFORM_STRING -> PLATFORM_WORD
#ifdef WINDOWS
foldr1' f (WindowsString s) = WindowsChar $ BS16.foldr1' (\c a' -> getWindowsChar $ f (WindowsChar c) (WindowsChar a')) s
foldr1' f (WindowsString s) = WindowsChar $ BS16.foldr1' (\c a' -> getWindowsChar $ coerce f c a') s
#else
foldr1' f (PosixString s) = PosixChar $ BS.foldr1' (\c a' -> getPosixChar $ f (PosixChar c) (PosixChar a')) s
foldr1' f (PosixString s) = PosixChar $ BS.foldr1' (\c a' -> getPosixChar $ coerce f c a') s
#endif

-- | /O(n)/ Applied to a predicate and a 'OsString', 'all' determines
Expand All @@ -661,9 +662,9 @@ foldr1' f (PosixString s) = PosixChar $ BS.foldr1' (\c a' -> getPosixChar $ f (P
-- @since 1.4.200.0
all :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> Bool
#ifdef WINDOWS
all f (WindowsString s) = BS16.all (f . WindowsChar) s
all f (WindowsString s) = BS16.all (coerce f) s
#else
all f (PosixString s) = BS.all (f . PosixChar) s
all f (PosixString s) = BS.all (coerce f) s
#endif

-- | /O(n)/ Applied to a predicate and a 'OsString', 'any' determines if
Expand All @@ -672,9 +673,9 @@ all f (PosixString s) = BS.all (f . PosixChar) s
-- @since 1.4.200.0
any :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> Bool
#ifdef WINDOWS
any f (WindowsString s) = BS16.any (f . WindowsChar) s
any f (WindowsString s) = BS16.any (coerce f) s
#else
any f (PosixString s) = BS.any (f . PosixChar) s
any f (PosixString s) = BS.any (coerce f) s
#endif

-- /O(n)/ Concatenate a list of OsStrings.
Expand Down Expand Up @@ -776,9 +777,9 @@ takeEnd n (PosixString s) = PosixString $ BS.takeEnd n s
-- @since 1.4.200.0
takeWhileEnd :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
takeWhileEnd f (WindowsString s) = WindowsString $ BS16.takeWhileEnd (f . WindowsChar) s
takeWhileEnd f (WindowsString s) = WindowsString $ BS16.takeWhileEnd (coerce f) s
#else
takeWhileEnd f (PosixString s) = PosixString $ BS.takeWhileEnd (f . PosixChar) s
takeWhileEnd f (PosixString s) = PosixString $ BS.takeWhileEnd (coerce f) s
#endif

-- | Similar to 'Prelude.takeWhile',
Expand All @@ -788,9 +789,9 @@ takeWhileEnd f (PosixString s) = PosixString $ BS.takeWhileEnd (f . PosixChar) s
-- @since 1.4.200.0
takeWhile :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
takeWhile f (WindowsString s) = WindowsString $ BS16.takeWhile (f . WindowsChar) s
takeWhile f (WindowsString s) = WindowsString $ BS16.takeWhile (coerce f) s
#else
takeWhile f (PosixString s) = PosixString $ BS.takeWhile (f . PosixChar) s
takeWhile f (PosixString s) = PosixString $ BS.takeWhile (coerce f) s
#endif

-- | /O(n)/ 'drop' @n@ @xs@ returns the suffix of @xs@ after the first n elements, or 'empty' if @n > 'length' xs@.
Expand Down Expand Up @@ -828,9 +829,9 @@ dropEnd n (PosixString s) = PosixString $ BS.dropEnd n s
-- @since 1.4.200.0
dropWhile :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
dropWhile f (WindowsString s) = WindowsString $ BS16.dropWhile (f . WindowsChar) s
dropWhile f (WindowsString s) = WindowsString $ BS16.dropWhile (coerce f) s
#else
dropWhile f (PosixString s) = PosixString $ BS.dropWhile (f . PosixChar) s
dropWhile f (PosixString s) = PosixString $ BS.dropWhile (coerce f) s
#endif

-- | Similar to 'Prelude.dropWhileEnd',
Expand All @@ -842,9 +843,9 @@ dropWhile f (PosixString s) = PosixString $ BS.dropWhile (f . PosixChar) s
-- @since 1.4.200.0
dropWhileEnd :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
dropWhileEnd f (WindowsString s) = WindowsString $ BS16.dropWhileEnd (f . WindowsChar) s
dropWhileEnd f (WindowsString s) = WindowsString $ BS16.dropWhileEnd (coerce f) s
#else
dropWhileEnd f (PosixString s) = PosixString $ BS.dropWhileEnd (f . PosixChar) s
dropWhileEnd f (PosixString s) = PosixString $ BS.dropWhileEnd (coerce f) s
#endif

-- | Returns the longest (possibly empty) suffix of elements which __do not__
Expand All @@ -855,9 +856,9 @@ dropWhileEnd f (PosixString s) = PosixString $ BS.dropWhileEnd (f . PosixChar) s
-- @since 1.4.200.0
breakEnd :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> (PLATFORM_STRING, PLATFORM_STRING)
#ifdef WINDOWS
breakEnd f (WindowsString s) = bimap WindowsString WindowsString $ BS16.breakEnd (f . WindowsChar) s
breakEnd f (WindowsString s) = bimap WindowsString WindowsString $ BS16.breakEnd (coerce f) s
#else
breakEnd f (PosixString s) = bimap PosixString PosixString $ BS.breakEnd (f . PosixChar) s
breakEnd f (PosixString s) = bimap PosixString PosixString $ BS.breakEnd (coerce f) s
#endif

-- | Similar to 'Prelude.break',
Expand All @@ -869,9 +870,9 @@ breakEnd f (PosixString s) = bimap PosixString PosixString $ BS.breakEnd (f . Po
-- @since 1.4.200.0
break :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> (PLATFORM_STRING, PLATFORM_STRING)
#ifdef WINDOWS
break f (WindowsString s) = bimap WindowsString WindowsString $ BS16.break (f . WindowsChar) s
break f (WindowsString s) = bimap WindowsString WindowsString $ BS16.break (coerce f) s
#else
break f (PosixString s) = bimap PosixString PosixString $ BS.break (f . PosixChar) s
break f (PosixString s) = bimap PosixString PosixString $ BS.break (coerce f) s
#endif

-- | Similar to 'Prelude.span',
Expand All @@ -883,9 +884,9 @@ break f (PosixString s) = bimap PosixString PosixString $ BS.break (f . PosixCha
-- @since 1.4.200.0
span :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> (PLATFORM_STRING, PLATFORM_STRING)
#ifdef WINDOWS
span f (WindowsString s) = bimap WindowsString WindowsString $ BS16.span (f . WindowsChar) s
span f (WindowsString s) = bimap WindowsString WindowsString $ BS16.span (coerce f) s
#else
span f (PosixString s) = bimap PosixString PosixString $ BS.span (f . PosixChar) s
span f (PosixString s) = bimap PosixString PosixString $ BS.span (coerce f) s
#endif

-- | Returns the longest (possibly empty) suffix of elements
Expand All @@ -906,9 +907,9 @@ span f (PosixString s) = bimap PosixString PosixString $ BS.span (f . PosixChar)
-- @since 1.4.200.0
spanEnd :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> (PLATFORM_STRING, PLATFORM_STRING)
#ifdef WINDOWS
spanEnd f (WindowsString s) = bimap WindowsString WindowsString $ BS16.spanEnd (f . WindowsChar) s
spanEnd f (WindowsString s) = bimap WindowsString WindowsString $ BS16.spanEnd (coerce f) s
#else
spanEnd f (PosixString s) = bimap PosixString PosixString $ BS.spanEnd (f . PosixChar) s
spanEnd f (PosixString s) = bimap PosixString PosixString $ BS.spanEnd (coerce f) s
#endif

-- | /O(n)/ 'splitAt' @n sbs@ is equivalent to @('take' n sbs, 'drop' n sbs)@.
Expand Down Expand Up @@ -953,9 +954,9 @@ split (PosixChar w) (PosixString s) = PosixString <$> BS.split w s
-- @since 1.4.200.0
splitWith :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> [PLATFORM_STRING]
#ifdef WINDOWS
splitWith f (WindowsString s) = WindowsString <$> BS16.splitWith (f . WindowsChar) s
splitWith f (WindowsString s) = WindowsString <$> BS16.splitWith (coerce f) s
#else
splitWith f (PosixString s) = PosixString <$> BS.splitWith (f . PosixChar) s
splitWith f (PosixString s) = PosixString <$> BS.splitWith (coerce f) s
#endif

-- | /O(n)/ The 'stripSuffix' function takes two OsStrings and returns 'Just'
Expand Down Expand Up @@ -1070,9 +1071,9 @@ elem (PosixChar w) (PosixString s) = BS.elem w s
-- @since 1.4.200.0
find :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> Maybe PLATFORM_WORD
#ifdef WINDOWS
find f (WindowsString s) = WindowsChar <$> BS16.find (f . WindowsChar) s
find f (WindowsString s) = WindowsChar <$> BS16.find (coerce f) s
#else
find f (PosixString s) = PosixChar <$> BS.find (f . PosixChar) s
find f (PosixString s) = PosixChar <$> BS.find (coerce f) s
#endif

-- | /O(n)/ 'filter', applied to a predicate and a OsString,
Expand All @@ -1082,9 +1083,9 @@ find f (PosixString s) = PosixChar <$> BS.find (f . PosixChar) s
-- @since 1.4.200.0
filter :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> PLATFORM_STRING
#ifdef WINDOWS
filter f (WindowsString s) = WindowsString $ BS16.filter (f . WindowsChar) s
filter f (WindowsString s) = WindowsString $ BS16.filter (coerce f) s
#else
filter f (PosixString s) = PosixString $ BS.filter (f . PosixChar) s
filter f (PosixString s) = PosixString $ BS.filter (coerce f) s
#endif

-- | /O(n)/ The 'partition' function takes a predicate a OsString and returns
Expand All @@ -1096,9 +1097,9 @@ filter f (PosixString s) = PosixString $ BS.filter (f . PosixChar) s
-- @since 1.4.200.0
partition :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> (PLATFORM_STRING, PLATFORM_STRING)
#ifdef WINDOWS
partition f (WindowsString s) = bimap WindowsString WindowsString $ BS16.partition (f . WindowsChar) s
partition f (WindowsString s) = bimap WindowsString WindowsString $ BS16.partition (coerce f) s
#else
partition f (PosixString s) = bimap PosixString PosixString $ BS.partition (f . PosixChar) s
partition f (PosixString s) = bimap PosixString PosixString $ BS.partition (coerce f) s
#endif

-- | /O(1)/ 'OsString' index (subscript) operator, starting from 0.
Expand Down Expand Up @@ -1171,9 +1172,9 @@ count (PosixChar w) (PosixString s) = BS.count w s
-- @since 1.4.200.0
findIndex :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> Maybe Int
#ifdef WINDOWS
findIndex f (WindowsString s) = BS16.findIndex (f . WindowsChar) s
findIndex f (WindowsString s) = BS16.findIndex (coerce f) s
#else
findIndex f (PosixString s) = BS.findIndex (f . PosixChar) s
findIndex f (PosixString s) = BS.findIndex (coerce f) s
#endif

-- | /O(n)/ The 'findIndices' function extends 'findIndex', by returning the
Expand All @@ -1182,7 +1183,7 @@ findIndex f (PosixString s) = BS.findIndex (f . PosixChar) s
-- @since 1.4.200.0
findIndices :: (PLATFORM_WORD -> Bool) -> PLATFORM_STRING -> [Int]
#ifdef WINDOWS
findIndices f (WindowsString s) = BS16.findIndices (f . WindowsChar) s
findIndices f (WindowsString s) = BS16.findIndices (coerce f) s
#else
findIndices f (PosixString s) = BS.findIndices (f . PosixChar) s
findIndices f (PosixString s) = BS.findIndices (coerce f) s
#endif
Loading

0 comments on commit b0e7018

Please sign in to comment.