Skip to content

Commit 312d888

Browse files
tecosaurfingolfin
authored andcommitted
Add filesystem func to transform a path to a URI
1 parent 4c2633c commit 312d888

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

base/path.jl

+19
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,22 @@ relpath(path::AbstractString, startpath::AbstractString) =
613613
for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
614614
@eval $f(path::AbstractString) = $f(String(path))
615615
end
616+
617+
"""
618+
uripath(path::AbstractString)
619+
620+
Encode `path` as a URI as per RFC1738, RFC3986, and the
621+
[Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).
622+
"""
623+
function uripath(path::String)
624+
percent_escape(s) =
625+
'%' * join(map(b -> string(b, base=16), codeunits(s)), '%')
626+
encode_uri_component(s) = replace(
627+
s, r"[^A-Za-z0-9\-_.~]+" => percent_escape)
628+
string("file://", gethostname(), '/',
629+
join(map(encode_uri_component,
630+
split(path, Filesystem.path_separator, keepempty=false)),
631+
'/'))
632+
end
633+
634+
uripath(path::AbstractString) = uripath(String(path))

0 commit comments

Comments
 (0)