Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path refactoring #2918

Merged
merged 7 commits into from
Dec 30, 2024
Merged

Path refactoring #2918

merged 7 commits into from
Dec 30, 2024

Conversation

harendra-kumar
Copy link
Member

No description provided.

-- the places where the predicate matches in the stream.
--
-- >>> Stream.toList $ Stream.indexEndBy_ (== '/') $ Stream.fromList "/home/harendra"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/home/harendra
Now the world knows where your home directory is located.

-- | @C:...@, does not check array length.
{-# INLINE unsafeHasDrive #-}
unsafeHasDrive :: (Unbox a, Integral a) => Array a -> Bool
unsafeHasDrive a
-- Check colon first for quicker return
| unsafeIndexChar 1 a /= ':' = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a /= b == False is a complex way of saying a == b

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is function definition, not a comparison.

-- Assuming the path is not empty.
isSeparator Windows (wordToChar (Array.getIndexUnsafe 0 a))
| wordToChar (Array.getIndexUnsafe 0 a) /= '.' = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe. We don't check the length before indexing.

Copy link
Member

@adithyaov adithyaov Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be written as wordToChar (Array.getIndexUnsafe 0 a) == '.'
It's easier to understand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it cannot be written like that. This is function definition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unsafe. We don't check the length before indexing.

It assumes path is not empty. This assumption is there at some places already. If it is legal to have non-empty path can you raise an issue to fix these?

| otherwise = True
-- | The path starting with a separator. On Windows this is relative to current
-- drive while on Posix this is absolute path as there is only one drive.
isRelativeCurDrive :: (Unbox a, Integral a) => OS -> Array a -> Bool
Copy link
Member

@adithyaov adithyaov Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some examples for this?
isRelativeCurDrive is any path that starts with a separator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"/home/user" is relative to current drive.

Stream.toList
$ Stream.take 3
$ Stream.takeWhile (not . isSeparator os)
$ fmap wordToChar
Copy link
Member

@adithyaov adithyaov Dec 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are converting all words to characters.
If the input is UTF16 (Or UTF8) encoded, will all the words translate into proper characters?
We should instead use isSeperatorIntegral, which checks the underlying Word instead of converting it into a Char.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see a real problem here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. The problem might arise when we are printing it. You can ignore this comment.

-- XXX On posix we just need to check last 3 bytes of the array
-- XXX Display the path in the exception messages.
case s1 of
[] -> throwM $ InvalidPath "A file name cannot have a trailing separator"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even an empty path could result in this. The error message does not indicate that.
So, maybeFile would fail if the path ends with

  1. .
  2. ..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty path is not a valid path.

So, maybeFile would fail if the path ends with

yes.

@harendra-kumar harendra-kumar merged commit 37f9565 into master Dec 30, 2024
31 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants