Skip to content

Commit

Permalink
add InetAddr constructor from string (#31459)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Vertechi authored and JeffBezanson committed Jun 26, 2019
1 parent 6f5cefb commit 8453aeb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Standard library changes

* `mean` now accepts both a function argument and a `dims` keyword ([#31576]).

#### Sockets

* Added `InetAddr` constructor from `AbstractString`, representing IP address, and `Integer`,
representing port number ([#31459]).

#### Miscellaneous

* `foldr` and `mapfoldr` now work on any iterator that supports `Iterators.reverse`, not just arrays ([#31781]).
Expand Down
29 changes: 29 additions & 0 deletions stdlib/Sockets/src/IPAddr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,36 @@ struct InetAddr{T<:IPAddr}
port::UInt16
end

"""
InetAddr(ip::IPAddr, port) -> InetAddr
Return 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
Return an `InetAddr` object from ip address `str` formatted as [`AbstractString`](@ref)
and port number `port`.
!!! compat "Julia 1.3"
This constructor requires at least Julia 1.3.
# 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))
print(io, "(")
Expand Down
3 changes: 3 additions & 0 deletions stdlib/Sockets/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8453aeb

Please sign in to comment.