Skip to content

Commit 7db750e

Browse files
committed
---
yaml --- r: 143285 b: refs/heads/try2 c: 005ea3b h: refs/heads/master i: 143283: 3b25536 v: v3
1 parent adcdb86 commit 7db750e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: d6e1a6b237d4c3b04a7dff6609c3427eb99bbc60
8+
refs/heads/try2: 005ea3b17375584f823c51d141248e3bcbc04115
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/rt/io/net/ip.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,52 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use num::FromStrRadix;
12+
use to_str::ToStr;
13+
1114
type Port = u16;
1215

1316
#[deriving(Eq, TotalEq)]
1417
pub enum IpAddr {
1518
Ipv4(u8, u8, u8, u8, Port),
1619
Ipv6(u16, u16, u16, u16, u16, u16, u16, u16, Port)
1720
}
21+
22+
impl ToStr for IpAddr {
23+
fn to_str(&self) -> ~str {
24+
match *self {
25+
Ipv4(a, b, c, d, p) =>
26+
fmt!("%u.%u.%u.%u:%u",
27+
a as uint, b as uint, c as uint, d as uint, p as uint),
28+
29+
// Ipv4 Compatible address
30+
Ipv6(0, 0, 0, 0, 0, 0, g, h, p) => {
31+
let a = fmt!("%04x", g as uint);
32+
let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
33+
let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
34+
let c = fmt!("%04x", h as uint);
35+
let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
36+
let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
37+
38+
fmt!("[::%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
39+
}
40+
41+
// Ipv4-Mapped address
42+
Ipv6(0, 0, 0, 0, 0, 1, g, h, p) => {
43+
let a = fmt!("%04x", g as uint);
44+
let b = FromStrRadix::from_str_radix(a.slice(2, 4), 16).unwrap();
45+
let a = FromStrRadix::from_str_radix(a.slice(0, 2), 16).unwrap();
46+
let c = fmt!("%04x", h as uint);
47+
let d = FromStrRadix::from_str_radix(c.slice(2, 4), 16).unwrap();
48+
let c = FromStrRadix::from_str_radix(c.slice(0, 2), 16).unwrap();
49+
50+
fmt!("[::FFFF:%u.%u.%u.%u]:%u", a, b, c, d, p as uint)
51+
}
52+
53+
Ipv6(a, b, c, d, e, f, g, h, p) =>
54+
fmt!("[%x:%x:%x:%x:%x:%x:%x:%x]:%u",
55+
a as uint, b as uint, c as uint, d as uint,
56+
e as uint, f as uint, g as uint, h as uint, p as uint)
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)