Skip to content

Commit

Permalink
Add filesystem func to transform a path to a URI
Browse files Browse the repository at this point in the history
  • Loading branch information
tecosaur authored and fingolfin committed Feb 15, 2024
1 parent 4c2633c commit 312d888
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions base/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,22 @@ relpath(path::AbstractString, startpath::AbstractString) =
for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
@eval $f(path::AbstractString) = $f(String(path))
end

"""
uripath(path::AbstractString)
Encode `path` as a URI as per RFC1738, RFC3986, and the
[Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).
"""
function uripath(path::String)
percent_escape(s) =
'%' * join(map(b -> string(b, base=16), codeunits(s)), '%')
encode_uri_component(s) = replace(
s, r"[^A-Za-z0-9\-_.~]+" => percent_escape)
string("file://", gethostname(), '/',
join(map(encode_uri_component,
split(path, Filesystem.path_separator, keepempty=false)),
'/'))
end

uripath(path::AbstractString) = uripath(String(path))

0 comments on commit 312d888

Please sign in to comment.