Skip to content

Commit 6c23c38

Browse files
committed
Add filesystem func to transform a path to a URI
1 parent 55c13d2 commit 6c23c38

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

base/path.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,3 +611,21 @@ relpath(path::AbstractString, startpath::AbstractString) =
611611
for f in (:isdirpath, :splitdir, :splitdrive, :splitext, :normpath, :abspath)
612612
@eval $f(path::AbstractString) = $f(String(path))
613613
end
614+
615+
"""
616+
uripath(path::AbstractString)
617+
618+
Encode `path` as a URI as per RFC1738, RFC3986, and the
619+
[Freedesktop File URI spec](https://www.freedesktop.org/wiki/Specifications/file-uri-spec/).
620+
"""
621+
function uripath(path::AbstractString)
622+
percent_escape(s) = '%' * join(map(
623+
b -> uppercase(string(b, base=16)),
624+
Vector{UInt8}(s)), '%')
625+
encode_uri_component(s) = replace(
626+
s, r"[^A-Za-z0-9\-_.~]+" => percent_escape)
627+
string("file://", gethostname(), '/',
628+
join(map(encode_uri_component,
629+
split(path, Filesystem.path_separator, keepempty=false)),
630+
'/'))
631+
end

0 commit comments

Comments
 (0)