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

Splitdir suprising behaviour #57571

Open
tf17270 opened this issue Feb 28, 2025 · 1 comment
Open

Splitdir suprising behaviour #57571

tf17270 opened this issue Feb 28, 2025 · 1 comment

Comments

@tf17270
Copy link

tf17270 commented Feb 28, 2025

This may be intended behaviour but I can't see why it would be- if it is pointing out why would be appreciated

The following is the observed behaviour

julia> println(splitdir("src"))
("", "src")

julia> println(splitdir("src/hi"))
("src", "hi")

I would expect the following where splitting a single length dir yields a 1 length list not a list with an empty string first- is this a bug or a feature? If it is a feature could someone explain why

julia> println(splitdir("src"))
( "src")

julia> println(splitdir("src/hi"))
("src", "hi")
@PallHaraldsson
Copy link
Contributor

PallHaraldsson commented Feb 28, 2025

I believe it's intentional. It's an edge case and in that case you can't join back with join, why you need/should always use joinpath. It takes this edge case into account. For that reason, I feel like the docs of both can be improved, with See also, pointing to ch other. Feel free to make a doc PR.

[The one-param _splitdir_nodrive(path::String) is defined [see below] but never used.]

A bit more surprising was (on my non-Windows, until I realized only option there, and you should not get that on Windows):

julia> splitdrive("C:\file")
("", "C:\file")


function splitdir(path::String)
    a, b = splitdrive(path)
    _splitdir_nodrive(a,b)
end

# Common splitdir functionality without splitdrive, needed for splitpath.
_splitdir_nodrive(path::String) = _splitdir_nodrive("", path)
function _splitdir_nodrive(a::String, b::String)
    splitdrive(path::String) = ("",path)
elseif Sys.iswindows()
..
    function splitdrive(path::String)
        m = match(r"^([^\\]+:|\\\\[^\\]+\\[^\\]+|\\\\\?\\UNC\\[^\\]+\\[^\\]+|\\\\\?\\[^\\]+:|)(.*)$"sa, path)::AbstractMatch
        String(something(m.captures[1])), String(something(m.captures[2]))
    end

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

No branches or pull requests

2 participants