| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package ExtUtils::Constant::Utils; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION @EXPORT_OK @ISA); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 5 | 1 |  |  | 1 |  | 4 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 74 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | @ISA = 'Exporter'; | 
| 8 |  |  |  |  |  |  | @EXPORT_OK = qw(C_stringify perl_stringify); | 
| 9 |  |  |  |  |  |  | $VERSION = '0.24_01'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 5 | use constant is_perl55 => ($] < 5.005_50); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 80 |  | 
| 12 | 1 |  | 33 | 1 |  | 5 | use constant is_perl56 => ($] < 5.007 && $] > 5.005_50); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 13 | 1 |  |  | 1 |  | 5 | use constant is_sane_perl => $] > 5.007; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 625 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | ExtUtils::Constant::Utils - helper functions for ExtUtils::Constant | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | use ExtUtils::Constant::Utils qw (C_stringify); | 
| 22 |  |  |  |  |  |  | $C_code = C_stringify $stuff; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | ExtUtils::Constant::Utils packages up utility subroutines used by | 
| 27 |  |  |  |  |  |  | ExtUtils::Constant, ExtUtils::Constant::Base and derived classes. All its | 
| 28 |  |  |  |  |  |  | functions are explicitly exportable. | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | =head1 USAGE | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =over 4 | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =item C_stringify NAME | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | A function which returns a 7 bit ASCII correctly \ escaped version of the | 
| 37 |  |  |  |  |  |  | string passed suitable for C's "" or ''. It will die if passed Unicode | 
| 38 |  |  |  |  |  |  | characters. | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =cut | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # Hopefully make a happy C identifier. | 
| 43 |  |  |  |  |  |  | sub C_stringify { | 
| 44 | 494 |  |  | 494 | 1 | 829 | local $_ = shift; | 
| 45 | 494 | 50 |  |  |  | 875 | return unless defined $_; | 
| 46 |  |  |  |  |  |  | # grr 5.6.1 | 
| 47 | 494 | 50 |  |  |  | 880 | confess "Wide character in '$_' intended as a C identifier" | 
| 48 |  |  |  |  |  |  | if tr/\0-\377// != length; | 
| 49 |  |  |  |  |  |  | # grr 5.6.1 more so because its regexps will break on data that happens to | 
| 50 |  |  |  |  |  |  | # be utf8, which includes my 8 bit test cases. | 
| 51 | 494 |  |  |  |  | 586 | $_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if is_perl56; | 
| 52 | 494 |  |  |  |  | 964 | s/\\/\\\\/g; | 
| 53 | 494 |  |  |  |  | 874 | s/([\"\'])/\\$1/g;	# Grr. fix perl mode. | 
| 54 | 494 |  |  |  |  | 701 | s/\n/\\n/g;		# Ensure newlines don't end up in octal | 
| 55 | 494 |  |  |  |  | 638 | s/\r/\\r/g; | 
| 56 | 494 |  |  |  |  | 641 | s/\t/\\t/g; | 
| 57 | 494 |  |  |  |  | 620 | s/\f/\\f/g; | 
| 58 | 494 |  |  |  |  | 624 | s/\a/\\a/g; | 
| 59 | 494 |  |  |  |  | 588 | unless (is_perl55) { | 
| 60 |  |  |  |  |  |  | # This will elicit a warning on 5.005_03 about [: :] being reserved unless | 
| 61 |  |  |  |  |  |  | # I cheat | 
| 62 | 494 |  |  |  |  | 654 | my $cheat = '([[:^print:]])'; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 494 |  |  |  |  | 607 | if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike. | 
| 65 |  |  |  |  |  |  | s/$cheat/sprintf "\\%03o", ord $1/ge; | 
| 66 |  |  |  |  |  |  | } else { | 
| 67 | 494 |  |  |  |  | 884 | s/([^\0-\177])/sprintf "\\%03o", ord $1/ge; | 
|  | 27 |  |  |  |  | 166 |  | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 494 |  |  |  |  | 1032 | s/$cheat/sprintf "\\%03o", ord $1/ge; | 
|  | 0 |  |  |  |  | 0 |  | 
| 71 |  |  |  |  |  |  | } else { | 
| 72 |  |  |  |  |  |  | require POSIX; | 
| 73 |  |  |  |  |  |  | s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge; | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 494 |  |  |  |  | 1086 | $_; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =item perl_stringify NAME | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | A function which returns a 7 bit ASCII correctly \ escaped version of the | 
| 81 |  |  |  |  |  |  | string passed suitable for a perl "" string. | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =cut | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # Hopefully make a happy perl identifier. | 
| 86 |  |  |  |  |  |  | sub perl_stringify { | 
| 87 | 71 |  |  | 71 | 1 | 126 | local $_ = shift; | 
| 88 | 71 | 50 |  |  |  | 120 | return unless defined $_; | 
| 89 | 71 |  |  |  |  | 122 | s/\\/\\\\/g; | 
| 90 | 71 |  |  |  |  | 181 | s/([\"\'])/\\$1/g;	# Grr. fix perl mode. | 
| 91 | 71 |  |  |  |  | 100 | s/\n/\\n/g;		# Ensure newlines don't end up in octal | 
| 92 | 71 |  |  |  |  | 84 | s/\r/\\r/g; | 
| 93 | 71 |  |  |  |  | 87 | s/\t/\\t/g; | 
| 94 | 71 |  |  |  |  | 95 | s/\f/\\f/g; | 
| 95 | 71 |  |  |  |  | 89 | s/\a/\\a/g; | 
| 96 | 71 |  |  |  |  | 81 | unless (is_perl55) { | 
| 97 |  |  |  |  |  |  | # This will elicit a warning on 5.005_03 about [: :] being reserved unless | 
| 98 |  |  |  |  |  |  | # I cheat | 
| 99 | 71 |  |  |  |  | 101 | my $cheat = '([[:^print:]])'; | 
| 100 | 71 |  |  |  |  | 81 | if (is_sane_perl) { | 
| 101 | 71 |  |  |  |  | 82 | if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike. | 
| 102 |  |  |  |  |  |  | s/$cheat/sprintf "\\x{%X}", ord $1/ge; | 
| 103 |  |  |  |  |  |  | } else { | 
| 104 | 71 |  |  |  |  | 143 | s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge; | 
|  | 20 |  |  |  |  | 81 |  | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | } else { | 
| 107 |  |  |  |  |  |  | # Grr 5.6.1. And I don't think I can use utf8; to force the regexp | 
| 108 |  |  |  |  |  |  | # because 5.005_03 will fail. | 
| 109 |  |  |  |  |  |  | # This is grim, but I also can't split on // | 
| 110 |  |  |  |  |  |  | my $copy; | 
| 111 |  |  |  |  |  |  | foreach my $index (0 .. length ($_) - 1) { | 
| 112 |  |  |  |  |  |  | my $char = substr ($_, $index, 1); | 
| 113 |  |  |  |  |  |  | $copy .= ($char le "\177") ? $char : sprintf "\\x{%X}", ord $char; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  | $_ = $copy; | 
| 116 |  |  |  |  |  |  | } | 
| 117 | 71 |  |  |  |  | 159 | s/$cheat/sprintf "\\%03o", ord $1/ge; | 
|  | 0 |  |  |  |  | 0 |  | 
| 118 |  |  |  |  |  |  | } else { | 
| 119 |  |  |  |  |  |  | # Turns out "\x{}" notation only arrived with 5.6 | 
| 120 |  |  |  |  |  |  | s/([^\0-\177])/sprintf "\\x%02X", ord $1/ge; | 
| 121 |  |  |  |  |  |  | require POSIX; | 
| 122 |  |  |  |  |  |  | s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge; | 
| 123 |  |  |  |  |  |  | } | 
| 124 | 71 |  |  |  |  | 196 | $_; | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | 1; | 
| 128 |  |  |  |  |  |  | __END__ |