Repository: JuliaLang/julia
File: base/path.jl (Julia)

Code before:
        if path_arr[i] != start_arr[i]
            i -= 1
            break
        end
    end
    pathpart = join(path_arr[i+1:something(findlast(x -> !isempty(x), path_arr), 0)], path_separator)
    prefix_num = something(findlast(x -> !isempty(x), start_arr), 0) - i - 1
    if prefix_num >= 0
        prefix = pardir * path_separator
        relpath_ = isempty(pathpart)     ?
            (prefix^prefix_num) * pardir :
            (prefix^prefix_num) * pardir * path_separator * pathpart
    else
        relpath_ = pathpart
    end
    return isempty(relpath_) ? curdir :  relpath_
end
relpath(path::AbstractString, startpath::AbstractString) =
    relpath(String(path)::String, String(startpath)::String)

for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath, :isabspath)
    @eval $f(path::AbstractString) = $f(String(path)::String)
end

function encode_uri_component(s)
    out = UInt8[]
    for cu in codeunits(s)
        # RFC3986 Section 2.3
        if (UInt8('A') <= cu <= UInt8('Z') || 
            UInt8('a') <= cu <= UInt8('z') || 
            UInt8('0') <= cu <= UInt8('9') || 
            cu in map(UInt8, ('-', '_', '.', '~', '/'))
        )
            push!(out, cu)
        else
            # RFC3986 Section 2.1
            push!(out, UInt8('%'))
            append!(out, codeunits(uppercase(string(cu, base = 16))))
        end
    end
    String(out)
end

"""
    uripath(path::AbstractString)

Encode `path` as a URI as per [RFC8089: The "file" URI
Scheme](https://www.rfc-editor.org/rfc/rfc8089), [RFC3986: Uniform Resource
Identifier (URI): Generic Syntax](https://www.rfc-editor.org/rfc/rfc3986), and
the [Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).


Review comment (suggestion):
```suggestion
    out = empty!(StringVector(sizeof(s)))
```

Code after:
        relpath_ = pathpart
    end
    return isempty(relpath_) ? curdir :  relpath_
end
relpath(path::AbstractString, startpath::AbstractString) =
    relpath(String(path)::String, String(startpath)::String)

for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath, :isabspath)
    @eval $f(path::AbstractString) = $f(String(path)::String)
end

function encode_uri_component(s::AbstractString)
    out = empty!(StringVector(sizeof(s)))
    for cu in utf8units(s)
        # RFC3986 Section 2.3
        if (UInt8('A') <= cu <= UInt8('Z') ||
            UInt8('a') <= cu <= UInt8('z') ||
            UInt8('0') <= cu <= UInt8('9') ||
            cu in map(UInt8, ('-', '_', '.', '~', '/'))
        )
            push!(out, cu)
        else
            # RFC3986 Section 2.1
            push!(out, UInt8('%'))
            append!(out, codeunits(uppercase(string(cu, base = 16))))
        end
    end
    String(out)
end

"""
    uripath(path::AbstractString)

Encode `path` as a URI as per [RFC8089: The "file" URI
Scheme](https://www.rfc-editor.org/rfc/rfc8089), [RFC3986: Uniform Resource
Identifier (URI): Generic Syntax](https://www.rfc-editor.org/rfc/rfc3986), and
the [Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).

## Examples

```julia-repl
julia> uripath("/home/user/example file.jl") # On a unix machine
"file://<hostname>/home/user/example%20file.jl"

julia> uripath("C:\\Users\\user\\example file.jl") # On a windows machine
"file:///C:/Users/user/example%20file.jl"
```
"""
function uripath end

@static if Sys.iswindows()