Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add InetAddr constructor from string #31459

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions stdlib/Sockets/src/IPAddr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,35 @@ struct InetAddr{T<:IPAddr}
port::UInt16
end

"""
InetAddr(ip::IPAddr, port) -> InetAddr

Returns an InetAddr object from ip address `ip` and port number `port`.
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved

# 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)
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved
and port number `port`.

!!! compat "Julia 1.2"
constructor from `AbstractString` requires at least Julia 1.2.
fredrikekre marked this conversation as resolved.
Show resolved Hide resolved

# 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