.: monkey-mind :. |
sheep go to heaven, goats go to hell |
Expanding a compressed IPv6 addressFor those of us who don't want to load Net::IP every single time :-) Straightforward: sub ipv6_expand_1 { my $arg = shift; $arg = '0::' if $arg eq '::'; my @a2; my @addr = split(':', $arg); for my $e (@addr) { if (length($e)) { push (@a2, substr("0000$e", -4)); } else { for my $i (1..(8-int(@addr))) { push(@a2, "0000"); } } } return join(":", @a2); } # Test script for (my $i =0; $i < 0xffff; $i++) { $_ = sprintf("%x::%x", 0xffff-$i, $i); print ipv6_expand_1($_), "\n"; } Fast(er): sub ipv6_expand_2 { local($_) = shift; s/^:/0:/; s/:$/:0/; s/(^|:)([^:]{1,3})(?=:|$)/$1.substr("0000$2", -4)/ge; my $c = tr/:/:/; s/::/":".("0000:" x (8-$c))/e; return $_; } # Test script for (my $i = 0; $i < 0xffff; $i++) { $_ = sprintf("%x::%x", 0xffff-$i, $i); print ipv6_expand_2($_), "\n"; } |
||