| 1 | package ExtUtils::Constant::Utils; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION @EXPORT_OK @ISA); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | @ISA = 'Exporter'; | |||||||||||||||||||
| 8 | 1 | @EXPORT_OK = qw(C_stringify perl_stringify); | |||||||||||||||||||
| 9 | 1 | $VERSION = '0.04'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use constant is_perl55 => ($] < 5.005_50); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use constant is_perl56 => ($] < 5.007 && $] > 5.005_50); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$] < 5.007 && $] > 5.0055
MC/DC0%0/2
$] < 5.007 && $] > 5.0055
$] < 5.007
$] > 5.0055
Truth table33%1/3
A && B
A= $] < 5.007B= $] > 5.0055
| |||||||||||||||||||||
| 13 | 1 | ▶ | use constant is_sane_perl => $] > 5.007; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 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 | 209 | ▶ | local $_ = shift; | ||||||||||||||||||
|
Subroutine
C_stringify: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||
| 45 | 209 | ▶ | return unless defined $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | # grr 5.6.1 | ||||||||||||||||||||
| 47 | 209 | ▶ | confess "Wide character in '$_' intended as a C identifier" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 209 | $_ = pack 'C*', unpack 'U*', $_ . pack 'U*' if is_perl56; | |||||||||||||||||||
| 52 | 209 | s/\\/\\\\/g; | |||||||||||||||||||
| 53 | 209 | s/([\"\'])/\\$1/g; # Grr. fix perl mode. | |||||||||||||||||||
| 54 | 209 | s/\n/\\n/g; # Ensure newlines don't end up in octal | |||||||||||||||||||
| 55 | 209 | s/\r/\\r/g; | |||||||||||||||||||
| 56 | 209 | s/\t/\\t/g; | |||||||||||||||||||
| 57 | 209 | s/\f/\\f/g; | |||||||||||||||||||
| 58 | 209 | s/\a/\\a/g; | |||||||||||||||||||
| 59 | 209 | unless (is_perl55) { | |||||||||||||||||||
| 60 | # This will elicit a warning on 5.005_03 about [: :] being reserved unless | ||||||||||||||||||||
| 61 | # I cheat | ||||||||||||||||||||
| 62 | 209 | my $cheat = '([[:^print:]])'; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 209 | if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike. | |||||||||||||||||||
| 65 | s/$cheat/sprintf "\\%03o", ord $1/ge; | ||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||
| 67 | 209 | s/([^\0-\177])/sprintf "\\%03o", ord $1/ge; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 209 | s/$cheat/sprintf "\\%03o", ord $1/ge; | |||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||
| 72 | require POSIX; | ||||||||||||||||||||
| 73 | s/([^A-Za-z0-9_])/POSIX::isprint($1) ? $1 : sprintf "\\%03o", ord $1/ge; | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 209 | $_; | |||||||||||||||||||
| 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 | ▶ | local $_ = shift; | ||||||||||||||||||
|
Subroutine
perl_stringify: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 88 | 71 | ▶ | return unless defined $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | 71 | s/\\/\\\\/g; | |||||||||||||||||||
| 90 | 71 | s/([\"\'])/\\$1/g; # Grr. fix perl mode. | |||||||||||||||||||
| 91 | 71 | s/\n/\\n/g; # Ensure newlines don't end up in octal | |||||||||||||||||||
| 92 | 71 | s/\r/\\r/g; | |||||||||||||||||||
| 93 | 71 | s/\t/\\t/g; | |||||||||||||||||||
| 94 | 71 | s/\f/\\f/g; | |||||||||||||||||||
| 95 | 71 | s/\a/\\a/g; | |||||||||||||||||||
| 96 | 71 | unless (is_perl55) { | |||||||||||||||||||
| 97 | # This will elicit a warning on 5.005_03 about [: :] being reserved unless | ||||||||||||||||||||
| 98 | # I cheat | ||||||||||||||||||||
| 99 | 71 | my $cheat = '([[:^print:]])'; | |||||||||||||||||||
| 100 | 71 | if (is_sane_perl) { | |||||||||||||||||||
| 101 | 71 | if (ord('A') == 193) { # EBCDIC has no ^\0-\177 workalike. | |||||||||||||||||||
| 102 | s/$cheat/sprintf "\\x{%X}", ord $1/ge; | ||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||
| 104 | 71 | s/([^\0-\177])/sprintf "\\x{%X}", ord $1/ge; | |||||||||||||||||||
| 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 | s/$cheat/sprintf "\\%03o", ord $1/ge; | |||||||||||||||||||
| 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 | $_; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | 1; | |||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||