diff --git a/Changes b/Changes index 7c9bc51..9efd710 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,12 @@ Revision history for Perl extension NetAddr::IP -4.022 +4.023 Fri Jan 16 14:30:40 PST 2009 + added the capability to set the CASE of ipV6 text return + values to either upper or lower. Thanks to + Rob Riepel for developing + this improvement and providing a comprehensive patch + +4.022 Sat Dec 20 13:05:01 PST 2008 In Util.xs 1.28 set uninitialized "carry" in XS bin2bcd to zero diff --git a/IP.pm b/IP.pm index 092bd33..0126671 100644 --- a/IP.pm +++ b/IP.pm @@ -34,7 +34,7 @@ @ISA = qw(Exporter NetAddr::IP::Lite); -$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.22 $ =~ /\d+/g) }; +$VERSION = do { sprintf " %d.%03d", (q$Revision: 4.23 $ =~ /\d+/g) }; =pod @@ -53,6 +53,8 @@ V4net netlimit :aton DEPRECATED + :lower + :upper :old_storable :old_nth ); @@ -104,6 +106,15 @@ $arrayref = Coalesce($masklen, $number, @list_of_subnets) +* By default B functions and methods return string IPv6 +addresses in uppercase. To change that to lowercase: + + use NetAddr::IP qw(:lower); + +* To ensure the current IPv6 string case behavior even if the default changes: + + use NetAddr::IP qw(:upper); + * To set a limit on the size of B processed or returned by NetAddr::IP. Set the maximum number of nets beyond which NetAddr::IP will return and @@ -307,6 +318,9 @@ sub import { + our $full_format = "%04X:%04X:%04X:%04X:%04X:%04X:%D.%D.%D.%D"; + our $full6_format = "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X"; + if (grep { $_ eq ':old_storable' } @_) { @_ = grep { $_ ne ':old_storable' } @_; } else { @@ -339,6 +353,20 @@ $NetAddr::IP::Lite::Old_nth = 1; @_ = grep { $_ ne ':old_nth' } @_; } + if (grep { $_ eq ':lower' } @_) + { + $full_format = lc($full_format); + $full6_format = lc($full6_format); + NetAddr::IP::Util::lower(); + @_ = grep { $_ ne ':lower' } @_; + } + if (grep { $_ eq ':upper' } @_) + { + $full_format = uc($full_format); + $full6_format = uc($full6_format); + NetAddr::IP::Util::upper(); + @_ = grep { $_ ne ':upper' } @_; + } NetAddr::IP->export_to_level(1, @_); } @@ -758,7 +786,7 @@ $hex[8] = $hex[7] >> 8; $hex[7] = $hex[6] & 0xff; $hex[6] >>= 8; - return sprintf("%04X:%04X:%04X:%04X:%04X:%04X:%d.%d.%d.%d",@hex); + return sprintf($full_format,@hex); } else { &full6; } @@ -766,7 +794,7 @@ sub full6($) { my @hex = (unpack("n8",$_[0]->{addr})); - return sprintf("%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X",@hex); + return sprintf($full6_format,@hex); } =item C<$me-Econtains($other)> diff --git a/Lite/Util/README b/Lite/Util/README index 5240290..7a7c264 100644 --- a/Lite/Util/README +++ b/Lite/Util/README @@ -74,6 +74,9 @@ $bits128 = bcd2bin($bcdtxt); $modetext = mode; + NetAddr::IP::Util::lower(); + NetAddr::IP::Util::upper(); + INSTALLATION Un-tar the distribution in an appropriate directory and type: @@ -298,6 +301,12 @@ returns: "Pure Perl" or "CC XS" + * NetAddr::IP::Util::lower(); + Return IPv6 strings in lowercase. + + * NetAddr::IP::Util::upper(); + Return IPv6 strings in uppercase. This is the default. + EXAMPLES # convert any textual IP address into a 128 bit vector # diff --git a/Lite/Util/Util.pm b/Lite/Util/Util.pm index b0b2336..0b55e96 100644 --- a/Lite/Util/Util.pm +++ b/Lite/Util/Util.pm @@ -110,6 +110,14 @@ $Mode = 'CC XS'; } +# allow user to choose upper or lower case + +our $n2x_format = "%X:%X:%X:%X:%X:%X:%X:%X"; +our $n2d_format = "%X:%X:%X:%X:%X:%X:%D.%D.%D.%D"; + +sub upper { $n2x_format = uc($n2x_format); $n2d_format = uc($n2d_format); } +sub lower { $n2x_format = lc($n2x_format); $n2d_format = lc($n2d_format); } + # if Socket lib is broken in some way, check for overange values # my $overange = yinet_aton('256.1') ? 1:0; @@ -210,6 +218,9 @@ $bits128 = bcd2bin($bcdtxt); $modetext = mode; + NetAddr::IP::Util::lower(); + NetAddr::IP::Util::upper(); + =head1 INSTALLATION Un-tar the distribution in an appropriate directory and type: @@ -320,7 +331,7 @@ sub ipv6_n2x { die "Bad arg length for 'ipv6_n2x', length is ". length($_[0]) ." should be 16" unless length($_[0]) == 16; - return sprintf("%X:%X:%X:%X:%X:%X:%X:%X",unpack("n8",$_[0])); + return sprintf($n2x_format,unpack("n8",$_[0])); } =item * $dec_text = ipv6_n2d($ipv6addr); @@ -342,7 +353,7 @@ $hex[8] = $hex[7] >> 8; $hex[7] = $hex[6] & 0xff; $hex[6] >>= 8; - return sprintf("%X:%X:%X:%X:%X:%X:%d.%d.%d.%d",@hex); + return sprintf($n2d_format,@hex); } =item * $ipv6naddr = inet_any2n($dotquad or $ipv6_text); @@ -587,6 +598,14 @@ returns: "Pure Perl" or "CC XS" +=item * NetAddr::IP::Util::lower(); + +Return IPv6 strings in lowercase. + +=item * NetAddr::IP::Util::upper(); + +Return IPv6 strings in uppercase. This is the default. + =back =head1 EXAMPLES diff --git a/MANIFEST b/MANIFEST index d54cc6d..4d98238 100644 --- a/MANIFEST +++ b/MANIFEST @@ -108,6 +108,7 @@ t/full6.t t/imhoff.t t/loops.t +t/lower.t t/masklen.t t/new-store.t t/old-store.t diff --git a/META.yml b/META.yml index f8920bc..2be4f45 100644 --- a/META.yml +++ b/META.yml @@ -1,6 +1,6 @@ --- #YAML:1.0 name: NetAddr-IP -version: 4.022 +version: 4.023 abstract: Manages IPv4 and IPv6 addresses and subnets license: ~ author: diff --git a/t/lower.t b/t/lower.t new file mode 100644 index 0000000..6e09e0c --- /dev/null +++ b/t/lower.t @@ -0,0 +1,11 @@ + +#use diagnostics; +use Test::More tests => 2; + +use_ok ('NetAddr::IP', qw(:lower)); + +my $exp = 'ff:0:0:0:0:0:0:eeaa/128'; +my $ip = new NetAddr::IP('FF::eeAA'); +my $got = sprintf $ip; +ok ($got eq $exp,"lower case $got"); +