Skip to content

Commit 2fb4a16

Browse files
Do not return empty paths in splitRoot
1 parent 5c1175c commit 2fb4a16

2 files changed

Lines changed: 39 additions & 30 deletions

File tree

core/src/Streamly/Internal/FileSystem/PosixPath.hs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ import Streamly.Internal.Data.Path
194194
{- $setup
195195
>>> :m
196196
>>> :set -XQuasiQuotes
197-
>>> import Data.Maybe (fromJust, isJust)
197+
>>> import Data.Maybe (fromJust, isNothing, isJust)
198198
>>> import qualified Streamly.Data.Stream as Stream
199199
200200
For APIs that have not been released yet.
@@ -675,46 +675,50 @@ unsafeJoinPaths = undefined
675675

676676
#ifndef IS_WINDOWS
677677
-- | If a path is rooted then separate the root and the remaining path,
678-
-- otherwise root is returned as empty. If the path is rooted then the non-root
679-
-- part is guaranteed to not start with a separator.
678+
-- otherwise return 'Nothing'. If the path is rooted then the non-root
679+
-- part is guaranteed to NOT start with a separator.
680680
--
681681
-- Some filepath package equivalent idioms:
682682
--
683683
-- >>> splitDrive = Path.splitRoot
684684
-- >>> joinDrive = Path.unsafeAppend
685-
-- >>> takeDrive = fst . Path.splitRoot
686-
-- >>> dropDrive = snd . Path.splitRoot
685+
-- >>> takeDrive = fmap fst . Path.splitRoot
686+
-- >>> dropDrive x = Path.splitRoot x >>= snd
687+
-- >>> hasDrive = isJust . Path.splitRoot
688+
-- >>> isDrive = isNothing . dropDrive
687689
--
688-
-- >> hasDrive = not . null . takeDrive -- TODO
689-
-- >> isDrive = null . dropDrive -- TODO
690-
--
691-
-- >>> toList (a,b) = (Path.toString a, Path.toString b)
692-
-- >>> split = toList . Path.splitRoot . pack
690+
-- >>> toList (a,b) = (Path.toString a, fmap Path.toString b)
691+
-- >>> split = fmap toList . Path.splitRoot . pack
693692
--
694693
-- >>> split "/"
695-
-- ("/","")
694+
-- Just ("/",Nothing)
696695
--
697696
-- >>> split "."
698-
-- (".","")
697+
-- Just (".",Nothing)
699698
--
700699
-- >>> split "./"
701-
-- ("./","")
700+
-- Just ("./",Nothing)
702701
--
703702
-- >>> split "/home"
704-
-- ("/","home")
703+
-- Just ("/",Just "home")
705704
--
706705
-- >>> split "//"
707-
-- ("//","")
706+
-- Just ("//",Nothing)
708707
--
709708
-- >>> split "./home"
710-
-- ("./","home")
709+
-- Just ("./",Just "home")
711710
--
712711
-- >>> split "home"
713-
-- ("","home")
712+
-- Nothing
714713
--
715-
splitRoot :: OS_PATH -> (OS_PATH, OS_PATH)
716-
splitRoot (OS_PATH a) =
717-
bimap OS_PATH OS_PATH $ Common.splitRoot Common.OS_NAME a
714+
splitRoot :: OS_PATH -> Maybe (OS_PATH, Maybe OS_PATH)
715+
splitRoot (OS_PATH x) =
716+
let (a,b) = Common.splitRoot Common.OS_NAME x
717+
in if Array.null a
718+
then Nothing
719+
else if Array.null b
720+
then Just (OS_PATH a, Nothing)
721+
else Just (OS_PATH a, Just (OS_PATH b))
718722

719723
-- | Split the path components keeping separators between path components
720724
-- attached to the dir part. Redundant separators are removed, only the first

core/src/Streamly/Internal/FileSystem/WindowsPath.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -385,24 +385,29 @@ eqPath (OS_PATH a) (OS_PATH b) =
385385
-- See "Streamly.Internal.FileSystem.PosixPath" module for common examples. We
386386
-- provide some Windows specific examples here.
387387
--
388-
-- >>> toList (a,b) = (Path.toString a, Path.toString b)
389-
-- >>> split = toList . Path.splitRoot . pack
388+
-- >>> toList (a,b) = (Path.toString a, fmap Path.toString b)
389+
-- >>> split = fmap toList . Path.splitRoot . pack
390390
--
391391
-- >>> split "c:"
392-
-- ("c:","")
392+
-- Just ("c:",Nothing)
393393
--
394394
-- >>> split "c:/"
395-
-- ("c:/","")
395+
-- Just ("c:/",Nothing)
396396
--
397397
-- >>> split "//x/"
398-
-- ("//x/","")
398+
-- Just ("//x/",Nothing)
399399
--
400400
-- >>> split "//x/y"
401-
-- ("//x/","y")
402-
--
403-
splitRoot :: OS_PATH -> (OS_PATH, OS_PATH)
404-
splitRoot (OS_PATH a) =
405-
bimap OS_PATH OS_PATH $ Common.splitRoot Common.OS_NAME a
401+
-- Just ("//x/",Just "y")
402+
--
403+
splitRoot :: OS_PATH -> Maybe (OS_PATH, Maybe OS_PATH)
404+
splitRoot (OS_PATH x) =
405+
let (a,b) = Common.splitRoot Common.OS_NAME x
406+
in if Array.null a
407+
then Nothing
408+
else if Array.null b
409+
then Just (OS_PATH a, Nothing)
410+
else Just (OS_PATH a, Just (OS_PATH b))
406411

407412
-- | Split a path into components separated by the path separator. "."
408413
-- components in the path are ignored except when in the leading position.

0 commit comments

Comments
 (0)