Skip to content

Commit a5d23b0

Browse files
Merge pull request #14676 from JuliaLang/sk/parseip
parse(IPAddr, str): use the generic parse function for IP parsing
2 parents 0201437 + ae2dee7 commit a5d23b0

File tree

9 files changed

+20
-34
lines changed

9 files changed

+20
-34
lines changed

base/deprecated.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,5 @@ macro boundscheck(yesno,blk)
960960
:(@inbounds $(esc(blk)))
961961
end
962962
end
963+
964+
@deprecate parseip(str::AbstractString) parse(IPAddr, str)

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4048,13 +4048,6 @@ itself). For matrices, returns an identity matrix of the appropriate size and ty
40484048
"""
40494049
one
40504050

4051-
"""
4052-
parseip(addr)
4053-
4054-
Parse a string specifying an IPv4 or IPv6 ip address.
4055-
"""
4056-
parseip
4057-
40584051
"""
40594052
rationalize([Type=Int,] x; tol=eps(x))
40604053

base/exports.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,9 +1121,9 @@ export
11211121

11221122
# IP address stuff
11231123
@ip_str,
1124+
IPAddr,
11241125
IPv4,
11251126
IPv6,
1126-
parseip,
11271127

11281128
# I/O and events
11291129
accept,

base/initdefs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function init_bind_addr()
3030
opts = JLOptions()
3131
if opts.bindto != C_NULL
3232
bind_to = split(bytestring(opts.bindto), ":")
33-
bind_addr = string(parseip(bind_to[1]))
33+
bind_addr = string(parse(IPAddr, bind_to[1]))
3434
if length(bind_to) > 1
3535
bind_port = parse(Int,bind_to[2])
3636
else

base/managers.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ function connect_to_worker(host::AbstractString, port::Integer)
389389
bind_addr = "127.0.0.1"
390390
else
391391
try
392-
bind_addr = string(parseip(host))
392+
bind_addr = string(parse(IPAddr,host))
393393
catch
394394
bind_addr = string(getaddrinfo(host))
395395
end

base/socket.jl

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function IPv4(host::Integer)
3232
end
3333

3434
# constructor: ("1.2.3.4")
35-
IPv4(ipstr::AbstractString) = parseipv4(ipstr)
35+
IPv4(str::AbstractString) = parse(IPv4, str)
3636

3737
show(io::IO,ip::IPv4) = print(io,"ip\"",ip,"\"")
3838
print(io::IO,ip::IPv4) = print(io,dec((ip.host&(0xFF000000))>>24),".",
@@ -75,7 +75,7 @@ function IPv6(host::Integer)
7575
end
7676
end
7777

78-
IPv6(ipstr::AbstractString) = parseipv6(ipstr)
78+
IPv6(str::AbstractString) = parse(IPv6, str)
7979

8080
# Suppress leading '0's and "0x"
8181
print_ipv6_field(io,field::UInt16) = print(io,hex(field))
@@ -138,7 +138,7 @@ end
138138

139139
# Parsing
140140

141-
function parseipv4(str)
141+
function parse(::Type{IPv4}, str::AbstractString)
142142
fields = split(str,'.')
143143
i = 1
144144
ret = 0
@@ -199,15 +199,15 @@ function parseipv6fields(fields,num_fields)
199199
end
200200
parseipv6fields(fields) = parseipv6fields(fields,8)
201201

202-
function parseipv6(str)
202+
function parse(::Type{IPv6}, str::AbstractString)
203203
fields = split(str,':')
204204
if length(fields) > 8
205205
throw(ArgumentError("too many fields in IPv6 address"))
206206
elseif length(fields) == 8
207207
return IPv6(parseipv6fields(fields))
208208
elseif in('.',fields[end])
209209
return IPv6((parseipv6fields(fields[1:(end-1)],6))
210-
| parseipv4(fields[end]).host )
210+
| parse(IPv4, fields[end]).host )
211211
else
212212
return IPv6(parseipv6fields(fields))
213213
end
@@ -219,18 +219,16 @@ end
219219
# of the appropriate size and should use the appropriate constructor
220220
#
221221

222-
function parseip(str)
223-
if in(':',str)
224-
# IPv6 Address
225-
return parseipv6(str)
222+
function parse(::Type{IPAddr}, str::AbstractString)
223+
if ':' in str
224+
return parse(IPv6, str)
226225
else
227-
# IPv4 Address
228-
return parseipv4(str)
226+
return parse(IPv4, str)
229227
end
230228
end
231229

232230
macro ip_str(str)
233-
return parseip(str)
231+
return parse(IPAddr, str)
234232
end
235233

236234
immutable InetAddr{T<:IPAddr}

contrib/BBEditTextWrangler-julia.plist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,6 @@
755755
<string>parse</string>
756756
<string>parsefloat</string>
757757
<string>parseint</string>
758-
<string>parseip</string>
759758
<string>partitions</string>
760759
<string>peakflops</string>
761760
<string>permutations</string>

doc/stdlib/io-network.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -875,12 +875,6 @@ Network I/O
875875
876876
Get the IP address and the port that the given TCP socket is connected to (or bound to, in the case of TCPServer).
877877

878-
.. function:: parseip(addr)
879-
880-
.. Docstring generated from Julia source
881-
882-
Parse a string specifying an IPv4 or IPv6 ip address.
883-
884878
.. function:: IPv4(host::Integer) -> IPv4
885879

886880
.. Docstring generated from Julia source

test/socket.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
1414
@test_throws InexactError Int16(IPv4("1.2.3.4"))
1515
@test_throws InexactError Int64(IPv6("2001:1::2"))
1616

17-
let ipv = parseip("127.0.0.1")
17+
let ipv = parse(IPAddr, "127.0.0.1")
1818
@test isa(ipv, IPv4)
1919
@test ipv == ip"127.0.0.1"
2020
end
2121

22-
@test_throws ArgumentError Base.parseipv4("192.0xFFFFFFF")
22+
@test_throws ArgumentError parse(IPv4, "192.0xFFFFFFF")
2323
@test_throws ArgumentError IPv4(192,255,255,-1)
2424
@test_throws ArgumentError IPv4(192,255,255,256)
2525

26-
@test_throws ArgumentError Base.parseipv4("192.0xFFFFFFFFF")
27-
@test_throws ArgumentError Base.parseipv4("192.")
26+
@test_throws ArgumentError parse(IPv4, "192.0xFFFFFFFFF")
27+
@test_throws ArgumentError parse(IPv4, "192.")
2828

2929
@test ip"::1" == IPv6(1)
3030
@test ip"2605:2700:0:3::4713:93e3" == IPv6(parse(UInt128,"260527000000000300000000471393e3",16))
@@ -33,7 +33,7 @@ end
3333

3434
@test ip"0:0:0:0:0:ffff:127.0.0.1" == IPv6(0xffff7f000001)
3535

36-
let ipv = parseip("0:0:0:0:0:ffff:127.0.0.1")
36+
let ipv = parse(IPAddr, "0:0:0:0:0:ffff:127.0.0.1")
3737
@test isa(ipv, IPv6)
3838
@test ipv == ip"0:0:0:0:0:ffff:127.0.0.1"
3939
end

0 commit comments

Comments
 (0)