summaryrefslogtreecommitdiff
path: root/lib/ExtUtils/Constant.pm
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2002-08-23 20:21:12 +0100
committerhv <hv@crypt.org>2002-08-25 15:56:37 +0000
commit4f2c4fd8672a69832963d72529d4a1e84c6fcacf (patch)
treeaa33bb6a701ce7dc45d94cad3b26e567a90ec511 /lib/ExtUtils/Constant.pm
parentd5abad05d3a299cfc0ce9e8cbf216938d6fad135 (diff)
downloadperl-4f2c4fd8672a69832963d72529d4a1e84c6fcacf.tar.gz
ExtUtils::Constant 0.13
Message-ID: <20020823182111.GA281@Bagpuss.unfortu.net> p4raw-id: //depot/perl@17776
Diffstat (limited to 'lib/ExtUtils/Constant.pm')
-rw-r--r--lib/ExtUtils/Constant.pm119
1 files changed, 106 insertions, 13 deletions
diff --git a/lib/ExtUtils/Constant.pm b/lib/ExtUtils/Constant.pm
index 1268ce02ba..0772ee8517 100644
--- a/lib/ExtUtils/Constant.pm
+++ b/lib/ExtUtils/Constant.pm
@@ -1,6 +1,6 @@
package ExtUtils::Constant;
use vars qw (@ISA $VERSION %XS_Constant %XS_TypeSet @EXPORT_OK %EXPORT_TAGS);
-$VERSION = '0.12';
+$VERSION = '0.13';
=head1 NAME
@@ -94,8 +94,11 @@ if ($] >= 5.006) {
eval "use warnings; 1" or die $@;
}
use strict;
+use vars '$is_perl56';
use Carp;
+$is_perl56 = ($] < 5.007 && $] > 5.005_50);
+
use Exporter;
use Text::Wrap;
$Text::Wrap::huge = 'overflow';
@@ -150,7 +153,12 @@ characters.
sub C_stringify {
local $_ = shift;
return unless defined $_;
- confess "Wide character in '$_' intended as a C identifier" if tr/\0-\377//c;
+ # grr 5.6.1
+ confess "Wide character in '$_' intended as a C identifier"
+ if tr/\0-\377// != length;
+ # grr 5.6.1 moreso because its regexps will break on data that happens to
+ # be utf8, which includes my 8 bit test cases.
+ $_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if $is_perl56;
s/\\/\\\\/g;
s/([\"\'])/\\$1/g; # Grr. fix perl mode.
s/\n/\\n/g; # Ensure newlines don't end up in octal
@@ -189,13 +197,27 @@ sub perl_stringify {
s/\t/\\t/g;
s/\f/\\f/g;
s/\a/\\a/g;
- s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
unless ($] < 5.006) {
+ if ($] > 5.007) {
+ s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge;
+ } else {
+ # Grr 5.6.1. And I don't think I can use utf8; to force the regexp
+ # because 5.005_03 will fail.
+ # This is grim, but I also can't split on //
+ my $copy;
+ foreach my $index (0 .. length ($_) - 1) {
+ my $char = substr ($_, $index, 1);
+ $copy .= ($char le "\177") ? $char : sprintf "\\x{%X}", ord $char;
+ }
+ $_ = $copy;
+ }
# This will elicit a warning on 5.005_03 about [: :] being reserved unless
# I cheat
my $cheat = '([[:^print:]])';
s/$cheat/sprintf "\\%03o", ord $1/ge;
} else {
+ # Turns out "\x{}" notation only arrived with 5.6
+ s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge;
require POSIX;
s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge;
}
@@ -433,7 +455,7 @@ sub match_clause {
$body .= $indent . " } else {\n";
$body .= return_clause ($no, 4 + length $indent);
}
- $body .= $indent . " }";
+ $body .= $indent . " }\n";
} else {
$body .= return_clause ($item, 2 + length $indent);
}
@@ -466,7 +488,11 @@ sub switch_clause {
}
my @safe_names = @names;
foreach (@safe_names) {
- next unless tr/A-Za-z0-9_//c;
+ confess sprintf "Name '$_' is length %d, not $namelen", length
+ unless length == $namelen;
+ # Argh. 5.6.1
+ # next unless tr/A-Za-z0-9_//c;
+ next if tr/A-Za-z0-9_// == length;
$_ = '"' . perl_stringify ($_) . '"';
# Ensure that the enclosing C comment doesn't end
# by turning */ into *" . "/
@@ -481,9 +507,15 @@ sub switch_clause {
foreach my $i (0 .. ($namelen - 1)) {
my ($min, $max) = (~0, 0);
my %spread;
+ if ($is_perl56) {
+ # Need proper Unicode preserving hash keys for bytes in range 128-255
+ # here too, for some reason. grr 5.6.1 yet again.
+ tie %spread, 'ExtUtils::Constant::Aaargh56Hash';
+ }
foreach (@names) {
my $char = substr $_, $i, 1;
my $ord = ord $char;
+ confess "char $ord is out of range" if $ord > 255;
$max = $ord if $ord > $max;
$min = $ord if $ord < $min;
push @{$spread{$char}}, $_;
@@ -515,6 +547,9 @@ sub switch_clause {
$body .= $indent . "/* Offset $offset gives the best switch position. */\n";
$body .= $indent . "switch (name[$offset]) {\n";
foreach my $char (sort keys %$best) {
+ confess sprintf "'$char' is %d bytes long, not 1", length $char
+ if length ($char) != 1;
+ confess sprintf "char %#X is out of range", ord $char if ord ($char) > 255;
$body .= $indent . "case '" . C_stringify ($char) . "':\n";
foreach my $name (sort @{$best->{$char}}) {
my $thisone = $items->{$name};
@@ -581,7 +616,11 @@ sub dump_names {
next if $_->{utf8} eq 'no';
# Copy the hashref, as we don't want to mess with the caller's hashref.
$_ = {%$_};
- utf8::decode ($_->{name});
+ unless ($is_perl56) {
+ utf8::decode ($_->{name});
+ } else {
+ $_->{name} = pack 'U*', unpack 'U0U*', $_->{name};
+ }
delete $_->{utf8};
}
} else {
@@ -589,7 +628,9 @@ sub dump_names {
$type = $default_type;
}
$used_types{$type}++;
- if ($type eq $default_type and 0 == ($_->{name} =~ tr/A-Za-z0-9_//c)
+ if ($type eq $default_type
+ # grr 5.6.1
+ and length $_->{name} == ($_->{name} =~ tr/A-Za-z0-9_//)
and !defined ($_->{macro}) and !defined ($_->{value})
and !defined ($_->{default}) and !defined ($_->{pre})
and !defined ($_->{post}) and !defined ($_->{def_pre})
@@ -836,6 +877,11 @@ sub C_constant {
# be a hashref, and pinch %$items from our parent to save recalculation.
($namelen, $items) = @$breakout;
} else {
+ if ($is_perl56) {
+ # Need proper Unicode preserving hash keys.
+ $items = {};
+ tie %$items, 'ExtUtils::Constant::Aaargh56Hash';
+ }
$breakout ||= 3;
$default_type ||= 'IV';
if (!ref $what) {
@@ -870,7 +916,10 @@ sub C_constant {
$what->{$default_type} = 1;
}
warn "ExtUtils::Constant doesn't know how to handle values of type $_ used in macro $name" unless defined $XS_Constant{$item->{type}};
- if ($name !~ tr/\0-\177//c) {
+ # tr///c is broken on 5.6.1 for utf8, so my original tr/\0-\177//c
+ # doesn't work. Upgrade to 5.8
+ # if ($name !~ tr/\0-\177//c || $] < 5.005_50) {
+ if ($name =~ tr/\0-\177// == length $name || $] < 5.005_50) {
# No characters outside 7 bit ASCII.
if (exists $items->{$name}) {
die "Multiple definitions for macro $name";
@@ -881,7 +930,12 @@ sub C_constant {
if (exists $items->{$name} and ref $items->{$name} ne 'ARRAY') {
confess "Unexpected ASCII definition for macro $name";
}
- if ($name !~ tr/\0-\377//c) {
+ # Again, 5.6.1 tr broken, so s/5\.6.*/5\.8\.0/;
+ # if ($name !~ tr/\0-\377//c) {
+ if ($name =~ tr/\0-\377// == length $name) {
+# if ($] < 5.007) {
+# $name = pack "C*", unpack "U*", $name;
+# }
$item->{utf8} = 'no';
$items->{$name}[1] = $item;
push @new_items, $item;
@@ -889,7 +943,13 @@ sub C_constant {
$item = {%$item};
}
# Encode the name as utf8 bytes.
- utf8::encode($name);
+ unless ($is_perl56) {
+ utf8::encode($name);
+ } else {
+# warn "Was >$name< " . length ${name};
+ $name = pack 'C*', unpack 'C*', $name . pack 'U*';
+# warn "Now '${name}' " . length ${name};
+ }
if ($items->{$name}[0]) {
die "Multiple definitions for macro $name";
}
@@ -936,7 +996,18 @@ sub C_constant {
next unless $by_length[$i]; # None of this length
$body .= " case $i:\n";
if (@{$by_length[$i]} == 1) {
- $body .= match_clause ($by_length[$i]->[0]);
+ my $only_thing = $by_length[$i]->[0];
+ if ($only_thing->{utf8}) {
+ if ($only_thing->{utf8} eq 'yes') {
+ # With utf8 on flag item is passed in element 0
+ $body .= match_clause ([$only_thing]);
+ } else {
+ # With utf8 off flag item is passed in element 1
+ $body .= match_clause ([undef, $only_thing]);
+ }
+ } else {
+ $body .= match_clause ($only_thing);
+ }
} elsif (@{$by_length[$i]} < $breakout) {
$body .= switch_clause (4, '', $i, $items, @{$by_length[$i]});
} else {
@@ -1224,7 +1295,7 @@ EOT
Writes a file of C code and a file of XS code which you should C<#include>
and C<INCLUDE> in the C and XS sections respectively of your module's XS
-code. You probaby want to do this in your C<Makefile.PL>, so that you can
+code. You probably want to do this in your C<Makefile.PL>, so that you can
easily edit the list of constants without touching the rest of your module.
The attributes supported are
@@ -1301,7 +1372,7 @@ sub WriteConstants {
print $c_fh constant_types(); # macro defs
print $c_fh "\n";
- # indent is still undef. Until anyone implents indent style rules with it.
+ # indent is still undef. Until anyone implements indent style rules with it.
foreach (C_constant ($ARGS{NAME}, $ARGS{C_SUBNAME}, $ARGS{DEFAULT_TYPE},
$types, undef, $ARGS{BREAKOUT_AT}, @{$ARGS{NAMES}})) {
print $c_fh $_, "\n"; # C constant subs
@@ -1313,6 +1384,28 @@ sub WriteConstants {
close $xs_fh or warn "Error closing $ARGS{XS_FILE}: $!";
}
+package ExtUtils::Constant::Aaargh56Hash;
+# A support module (hack) to provide sane Unicode hash keys on 5.6.x perl
+use strict;
+require Tie::Hash if $ExtUtils::Constant::is_perl56;
+use vars '@ISA';
+@ISA = 'Tie::StdHash';
+
+#my $a;
+# Storing the values as concatenated BER encoded numbers is actually going to
+# be terser than using UTF8 :-)
+# And the tests are slightly faster. Ops are bad, m'kay
+sub to_key {pack "w*", unpack "U*", ($_[0] . pack "U*")};
+sub from_key {defined $_[0] ? pack "U*", unpack 'w*', $_[0] : undef};
+
+sub STORE { $_[0]->{to_key($_[1])} = $_[2] }
+sub FETCH { $_[0]->{to_key($_[1])} }
+sub FIRSTKEY { my $a = scalar keys %{$_[0]}; from_key (each %{$_[0]}) }
+sub NEXTKEY { from_key (each %{$_[0]}) }
+sub EXISTS { exists $_[0]->{to_key($_[1])} }
+sub DELETE { delete $_[0]->{to_key($_[1])} }
+
+#END {warn "$a accesses";}
1;
__END__