Skip to content

Commit

Permalink
Add path comparison operations
Browse files Browse the repository at this point in the history
  • Loading branch information
harendra-kumar committed Jan 8, 2025
1 parent 72fa645 commit bc759a1
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions core/src/Streamly/Internal/FileSystem/Path/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ module Streamly.Internal.FileSystem.Path.Common
, unsafeJoinPaths
-- , processParentRefs

, eqPathBytes
, eqPathStrict
, eqPathLax

-- * Utilities
, wordToChar
, charToWord
Expand Down Expand Up @@ -819,3 +823,84 @@ unsafeJoinPaths os =
-- the leading separator from the first path. But it is not necessary.
-- Instead we can avoid adding a separator if it is already present.
Array.fromStream . Array.concatSepBy (charToWord $ primarySeparator os)

-- | Check two paths for byte level equality. This is the most strict path
-- equality check.
--
-- >>> :{
-- eqPath a b = Common.eqPathBytes (packPosix a) (packPosix b)
-- :}
--
-- >>> eqPath "x//y" "x/y"
-- False
--
-- >>> eqPath "x/./y" "x/y"
-- False
--
-- >>> eqPath "x\\y" "x/y"
-- False
--
-- >>> eqPath "./file" "file"
-- False
--
-- >>> eqPath "file/" "file"
-- False
--
eqPathBytes :: Array a -> Array a -> Bool
eqPathBytes = Array.byteEq

-- | Checks two paths for logical equality. It performs some normalizations on
-- the paths being compared e.g. it drops redundant path separators
-- between path segments and redundant "/./" components. However, it keeps a
-- leading "./" as well as a trailing "/" in the path. In other words, it does
-- not drop the distinction of current directory relative path vs path segments
-- (@./x vs x@) and directory paths from files (@x/ vs x@).
--
-- >>> :{
-- eqPosix a b = Common.eqPathStrict Posix (packPosix a) (packPosix b)
-- eqWindows a b = Common.eqPathStrict Windows (packWindows a) (packWindows b)
-- :}
--
-- >>> eqPosix "x//y" "x/y"
-- True
--
-- >>> eqPosix "x/./y" "x/y"
-- True
--
-- >>> eqWindows "x\\y" "x/y"
-- True
--
-- >>> eqPosix "./file" "file"
-- False
--
-- >>> eqPosix "file/" "file"
-- False
--
eqPathStrict :: (Integral a, Unbox a) => OS -> Array a -> Array a -> Bool
eqPathStrict = undefined

-- | Like eqPathStrict except that a leading "./" as well as a trailing "/" in
-- the path is dropped before comparison.
--
-- >>> :{
-- eqPosix a b = Common.eqPathLax Posix (packPosix a) (packPosix b)
-- eqWindows a b = Common.eqPathLax Windows (packWindows a) (packWindows b)
-- :}
--
-- >>> eqPosix "x//y" "x/y"
-- True
--
-- >>> eqPosix "x/./y" "x/y"
-- True
--
-- >>> eqWindows "x\\y" "x/y"
-- True
--
-- >>> eqPosix "./file" "file"
-- True
--
-- >>> eqPosix "file/" "file"
-- True
--
eqPathLax :: (Integral a, Unbox a) => OS -> Array a -> Array a -> Bool
eqPathLax = undefined

0 comments on commit bc759a1

Please sign in to comment.