From 7c65bf2761f0bb2b23d218559f2dd994185541aa Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Sat, 23 Mar 2019 11:20:07 +0000 Subject: [PATCH 1/4] add InetAddr constructor from string --- stdlib/Sockets/src/IPAddr.jl | 1 + stdlib/Sockets/test/runtests.jl | 3 +++ 2 files changed, 4 insertions(+) diff --git a/stdlib/Sockets/src/IPAddr.jl b/stdlib/Sockets/src/IPAddr.jl index c77e1f9887064..b36b443d885bd 100644 --- a/stdlib/Sockets/src/IPAddr.jl +++ b/stdlib/Sockets/src/IPAddr.jl @@ -272,6 +272,7 @@ struct InetAddr{T<:IPAddr} end InetAddr(ip::IPAddr, port) = InetAddr{typeof(ip)}(ip, port) +InetAddr(str::AbstractString, port) = InetAddr(parse(IPAddr, str), port) function show(io::IO, addr::InetAddr) show(io, typeof(addr)) print(io, "(") diff --git a/stdlib/Sockets/test/runtests.jl b/stdlib/Sockets/test/runtests.jl index 018b00b90f463..67b57d56d5bac 100644 --- a/stdlib/Sockets/test/runtests.jl +++ b/stdlib/Sockets/test/runtests.jl @@ -66,6 +66,9 @@ end @test inet.port == 1024 str = "Sockets.InetAddr{$(isdefined(Main, :IPv4) ? "" : "Sockets.")IPv4}(ip\"127.0.0.1\", 1024)" @test sprint(show, inet) == str + inet = Sockets.InetAddr("127.0.0.1", 1024) + @test inet.host == ip"127.0.0.1" + @test inet.port == 1024 end @testset "InetAddr invalid port" begin @test_throws InexactError Sockets.InetAddr(IPv4(127,0,0,1), -1) From a9fef99a424b1a7a714541f5fd86ebe505a64bd1 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Wed, 3 Apr 2019 14:10:42 +0100 Subject: [PATCH 2/4] Added news entry for InetAddr constructor --- NEWS.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NEWS.md b/NEWS.md index 3fb47f9296734..ff1c2bcec40a1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -81,6 +81,11 @@ Standard library changes * `quantile` now accepts in all cases collections whose `eltype` is not a subtype of `Number` ([#30938]). +#### Sockets + +* Added `InetAddr` constructor from `AbstractString`, representing IP address, and `Integer`, + representing port number ([#31459]). + #### Miscellaneous * Since environment variables on Windows are case-insensitive, `ENV` now converts its keys From 1e6e0de08e8185945405a7663fdd76ee788a42db Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Wed, 3 Apr 2019 14:48:16 +0100 Subject: [PATCH 3/4] add docs for InetAddr --- stdlib/Sockets/src/IPAddr.jl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/stdlib/Sockets/src/IPAddr.jl b/stdlib/Sockets/src/IPAddr.jl index b36b443d885bd..899881dadda76 100644 --- a/stdlib/Sockets/src/IPAddr.jl +++ b/stdlib/Sockets/src/IPAddr.jl @@ -271,7 +271,34 @@ struct InetAddr{T<:IPAddr} port::UInt16 end +""" + InetAddr(ip::IPAddr, port) -> InetAddr + +Returns an InetAddr object from ip address `ip` and port number `port`. + +# Examples +```jldoctest +julia> Sockets.InetAddr(ip"127.0.0.1", 8000) +Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000) +``` +""" InetAddr(ip::IPAddr, port) = InetAddr{typeof(ip)}(ip, port) + +""" + InetAddr(str::AbstractString, port) -> InetAddr + +Returns an InetAddr object from ip address `str` formatted as [`AbstractString`](@ref) +and port number `port`. + +!!! compat "Julia 1.2" + constructor from `AbstractString` requires at least Julia 1.2. + +# Examples +```jldoctest +julia> Sockets.InetAddr("127.0.0.1", 8000) +Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000) +``` +""" InetAddr(str::AbstractString, port) = InetAddr(parse(IPAddr, str), port) function show(io::IO, addr::InetAddr) show(io, typeof(addr)) From 707965c3752c56febddf3ad1a5e5cdaa632ed740 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Wed, 3 Apr 2019 15:42:17 +0100 Subject: [PATCH 4/4] include feedback in docs --- stdlib/Sockets/src/IPAddr.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/Sockets/src/IPAddr.jl b/stdlib/Sockets/src/IPAddr.jl index 899881dadda76..756c2f67d10e9 100644 --- a/stdlib/Sockets/src/IPAddr.jl +++ b/stdlib/Sockets/src/IPAddr.jl @@ -274,7 +274,7 @@ end """ InetAddr(ip::IPAddr, port) -> InetAddr -Returns an InetAddr object from ip address `ip` and port number `port`. +Return an `InetAddr` object from ip address `ip` and port number `port`. # Examples ```jldoctest @@ -287,11 +287,11 @@ InetAddr(ip::IPAddr, port) = InetAddr{typeof(ip)}(ip, port) """ InetAddr(str::AbstractString, port) -> InetAddr -Returns an InetAddr object from ip address `str` formatted as [`AbstractString`](@ref) +Return an `InetAddr` object from ip address `str` formatted as [`AbstractString`](@ref) and port number `port`. !!! compat "Julia 1.2" - constructor from `AbstractString` requires at least Julia 1.2. + This constructor requires at least Julia 1.2. # Examples ```jldoctest