From 9e575cbe3698fdd0c5abbbbee1b70183107ddfe6 Mon Sep 17 00:00:00 2001 From: TEC Date: Sun, 22 Oct 2023 00:37:41 +0800 Subject: [PATCH] Add filesystem func to transform a path to a URI --- base/path.jl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/base/path.jl b/base/path.jl index 3b8124f34f174a..c826e4232778ef 100644 --- a/base/path.jl +++ b/base/path.jl @@ -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))