Skip to content

Commit

Permalink
Work around various 0.7 deprecations (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored Mar 1, 2018
1 parent df1c6a6 commit 3004a1d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
Compat 0.9.5
Compat 0.54.0
6 changes: 6 additions & 0 deletions src/URIParser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if VERSION >= v"0.7.0-DEV.2915"
using Base.Unicode: isascii
end

if VERSION >= v"0.7.0-DEV.3526"
_parse(T, s, b) = Base.parse(Int, s, base=b)
else
_parse(T, s, b) = Base.parse(Int, s, b)
end

include("parser.jl")
include("esc.jl")
include("utils.jl")
Expand Down
12 changes: 6 additions & 6 deletions src/esc.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const escaped_regex = r"%([0-9a-fA-F]{2})"

# Escaping
const control_array = vcat(map(UInt8, 0:parse(Int,"1f",16)))
const control_array = vcat(map(UInt8, 0:_parse(Int, "1f", 16)))
const control = String(control_array)*"\x7f"
const space = String(" ")
const delims = String("%<>\"")
Expand All @@ -22,14 +22,14 @@ function unescape(str)
c = str[i]
i += 1
if c == '%'
c = parse(UInt8, str[i:i+1], 16)
c = _parse(UInt8, str[i:i+1], 16)
i += 2
end
push!(r, c)
end
return String(r)
end
unescape_form(str) = unescape(replace(str, "+", " "))
unescape_form(str) = unescape(replace(str, "+" => " "))

hex_string(x) = string('%', uppercase(hex(x,2)))

Expand All @@ -39,7 +39,7 @@ function escape_with(str, use)
out = IOBuffer()
chars = Set(use)
i = start(str)
e = endof(str)
e = lastindex(str)
while i <= e
i_next = nextind(str, i)
if i_next == i + 1
Expand All @@ -51,7 +51,7 @@ function escape_with(str, use)
end
else
while i < i_next
write(out, hex_string(Vector{UInt8}(str)[i]))
write(out, hex_string(codeunits(str)[i]))
i += 1
end
end
Expand All @@ -61,4 +61,4 @@ function escape_with(str, use)
end

escape(str) = escape_with(str, unescaped)
escape_form(str) = replace(escape_with(str, unescaped_form), " ", "+")
escape_form(str) = replace(escape_with(str, unescaped_form), " " => "+")
12 changes: 9 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
if isdefined(Base, :isnumeric)
_isalnum(c) = isalpha(c) || isnumeric(c)
else
const _isalnum = Base.isalnum
end

is_url_char(c) = ((@assert UInt32(c) < 0x80); 'A' <= c <= '~' || '$' <= c <= '>' || c == '\f' || c == '\t')
is_mark(c) = (c == '-') || (c == '_') || (c == '.') || (c == '!') || (c == '~') ||
(c == '*') || (c == '\'') || (c == '(') || (c == ')')
is_userinfo_char(c) = isalnum(c) || is_mark(c) || (c == '%') || (c == ';') ||
is_userinfo_char(c) = _isalnum(c) || is_mark(c) || (c == '%') || (c == ';') ||
(c == ':') || (c == '&') || (c == '+') || (c == '$' || c == ',')
isnum(c) = ('0' <= c <= '9')
ishex(c) = (isnum(c) || 'a' <= lowercase(c) <= 'f')
is_host_char(c) = isalnum(c) || (c == '.') || (c == '-') || (c == '_') || (c == "~")
is_host_char(c) = _isalnum(c) || (c == '.') || (c == '-') || (c == '_') || (c == "~")


struct URI
Expand Down Expand Up @@ -133,7 +139,7 @@ function parse_authority(authority,seen_at)
error("Unexpected state $state")
end
end
(host, UInt16(port == "" ? 0 : parse(Int,port,10)), user)
(host, UInt16(port == "" ? 0 : _parse(Int, port, 10)), user)
end

function parse_url(url)
Expand Down
6 changes: 4 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
function userinfo(uri::URI)
Base.warn_once("Use of the format user:password is deprecated (rfc3986)")
uinfo = uri.userinfo
sep = search(uinfo, ':')
sep = findfirst(equalto(':'), uinfo)
l = length(uinfo)
username = uinfo[1:(sep-1)]
password = ((sep == l) || (sep == 0)) ? "" : uinfo[(sep+1):l]
Expand Down Expand Up @@ -61,7 +61,9 @@ const uses_fragment = ["hdfs", "ftp", "hdl", "http", "gopher", "news", "nntp", "
function isvalid(uri::URI)
scheme = uri.scheme
isempty(scheme) && error("Can not validate relative URI")
if ((scheme in non_hierarchical) && (search(uri.path, '/') > 1)) || # path hierarchy not allowed
slash = findfirst(equalto('/'), uri.path)
hasslash = slash !== nothing && slash > 1 # For 0.6 compatibility
if ((scheme in non_hierarchical) && hasslash) || # path hierarchy not allowed
(!(scheme in uses_query) && !isempty(uri.query)) || # query component not allowed
(!(scheme in uses_fragment) && !isempty(uri.fragment)) || # fragment identifier component not allowed
(!(scheme in uses_authority) && (!isempty(uri.host) || (0 != uri.port) || !isempty(uri.userinfo))) # authority component not allowed
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ urls = ["hdfs://user:password@hdfshost:9000/root/folder/file.csv#frag",

failed = 0
for url in urls
global failed
u = URI(url)
if !(string(u) == url) || !isvalid(u)
failed += 1
Expand Down

4 comments on commit 3004a1d

@samoconnor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ararslan, what package that uses URIParser is driving you to do compat updates?
I'm considering a PR that merges the parser from URIs.jl back to URIParser.jl so that HTTP.jl can go back to treating URIParser.jl as a dependency. It would be good to know of a 3rd party package that requires URIParser.jl as a test for depwarns, etc.

I think the field names in HTTP.jl's struct URI should like up with this package, so it should be fairly painless.

@ararslan
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BinDeps uses URIParser, which is why I updated it.

@samoconnor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx @ararslan
As a downstream user, who stands to gain from this mild annoyance at best :)
Do you have a preference for me:
A) making a new URIs.jl package and directing people to it from URIParser.jl/README.md; or
B) merging the HTTP.jl stuff into URIParser.jl ?

@ararslan
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no real preference either way.

Please sign in to comment.