22
3
To mark the anniversary of World IPv6 day, the Internet Society has published a campaign to Turn Off IPv4 on 6 June 2014 for One Day.
IPv6 addresses may be represented in their long form as eight colon-separated 16-bit hex values. Depending on the address, they may also be shortened as described in item 2 of section 2.2 Text Representation of Addresses of RFC 3513:
In order to make writing addresses containing zero bits easier a special syntax is available to compress the zeros. The use of "::" indicates one or more groups of 16 bits of zeros. The "::" can only appear once in an address. The "::" can also be used to compress leading or trailing zeros in an address.
Entries to this challenge will be programs that accept exactly one IPv6 address formatted in either the long or shortened format, and will display the same address in both the long and short formats, in that order.
The input may come from command-line arguments, STDIN, or any other input source that suits your choice of language.
Libraries or utilities specifically for parsing IPv6 addresses are banned (e.g. inet_{ntop,pton}()).
If the input address is invalid, the output will be empty (or some suitable error message indicating the address is invalid is given)
In cases where
::
shortening occurs, only one shortening operation may happen for a given address. If there are more than one potential shortening operations for a given address, the operation that gives the overall shortest address must be used. If there is a tie in this regard, the first operation will be used. This is illustrated in the examples below.
Examples:
Input Output
1080:0:0:0:8:800:200C:417A 1080:0:0:0:8:800:200C:417A
1080::8:800:200C:417A
FF01::101 FF01:0:0:0:0:0:0:101
FF01::101
0:0:0:0:0:0:0:1 0:0:0:0:0:0:0:1
::1
:: 0:0:0:0:0:0:0:0
::
1:0:0:2:0:0:0:3 1:0:0:2:0:0:0:3
1:0:0:2::3
1:0:0:8:8:0:0:3 1:0:0:8:8:0:0:3
1::8:8:0:0:3
1:2:3:4:5:6:7:8 1:2:3:4:5:6:7:8
1:2:3:4:5:6:7:8
ABCD:1234 <Invalid address format - no output>
ABCDE::1234 <Invalid address format - no output>
1:2:3:4:5:6:7:8:9 <Invalid address format - no output>
:::1 <Invalid address format - no output>
codegolf puzzle <Invalid address format - no output>
This is codegolf, so the shortest answer in bytes on June 6th 2014 will be accepted as the winner.
Say the input is
1:0:0:2:2::3
. Would the shortened output be identical to that or1::2:2:0:0:3
? Same for non-optimally shortened input. – Martin Ender – 2014-05-27T20:49:30.247@m.buettner In this case, I'll let you pick either. – Digital Trauma – 2014-05-27T20:52:16.573
Is
1::2:0:0:0:3
a possible input? – user12205 – 2014-05-27T20:59:35.717@ace In accordance with the late great Jon Postel's Robustness principle, yes.
– Digital Trauma – 2014-05-27T21:03:05.7002I think this is the only way anyone's ever going to get me to learn ipv6. +1 – Obversity – 2014-05-27T22:30:44.573
Can we just uncontrollably crash on malformed input? – Nick T – 2014-05-28T00:09:31.433
@NickT Only if the result of the crash is no output or a suitable error message about the invalid address format. Otherwise, no ;-) – Digital Trauma – 2014-05-28T00:12:17.627
What should the short form second line output be for
1:2:3:4:5:6:7:8
? There is no legal place to put::
. – MtnViewMark – 2014-05-28T02:52:18.747@MtnViewMark If a long-form address cannot be shortened, then the long and short forms are identical. – Digital Trauma – 2014-05-28T04:51:54.610
Should we support IPv4-mapped IPv6 addresses like
::ffff:74.125.136.101
? Should we support link-local addresses likefe80::1%eth0
? – Vi. – 2014-05-28T23:02:34.420@Vi. No, don't bother with ipv4-in-ipv6 formats. – Digital Trauma – 2014-05-28T23:05:22.913