File Coverage

blib/lib/Encode/Escape/ASCII.pm
Criterion Covered Total %
statement 17 56 30.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 6 15 40.0
pod 2 9 22.2
total 25 96 26.0


line stmt bran cond sub pod time code
1             # Encoding of ASCII Escape Sequences (or Escaped ASCII)
2              
3             # $Id: ASCII.pm,v 1.19 2007-12-05 22:11:11+09 you Exp $
4              
5             package Encode::Escape::ASCII;
6              
7             our $VERSION = do { q$Revision: 1.19 $ =~ /\d+\.(\d+)/; sprintf "%.2f", $1 / 100 };
8              
9 2     2   94415 use 5.008008;
  2         8  
  2         137  
10 2     2   12 use strict;
  2         3  
  2         75  
11 2     2   11 use warnings;
  2         9  
  2         84  
12              
13 2     2   282570 use Encode::Encoding;
  2         47503  
  2         95  
14 2     2   22 use base qw(Encode::Encoding);
  2         4  
  2         3531  
15              
16             __PACKAGE__->Define(qw/ascii-escape ascii_escape/);
17              
18             sub import {
19              
20 2     2   24 require Encode;
21 2         165 Encode->export_to_level(1, @_);
22             }
23              
24             sub enmode ($$) {
25 0     0 0   my ($class, $mode) = @_;
26            
27             }
28              
29             sub demode ($$) {
30 0     0 0   my ($class, $mode) = @_;
31             }
32              
33              
34             sub encode($$;$) {
35 0     0 1   my ($obj, $str, $chk) = @_;
36 0           my $escaped = escape($str);
37 0           return $escaped;
38             }
39              
40              
41             sub decode($$;$) {
42 0     0 1   my ($obj, $str, $chk) = @_;
43 0           my $unescaped = unescape($str);
44 0           return $unescaped;
45             }
46              
47             my %ESCAPED = (
48             "\\" => '\\',
49             "\r" => 'r',
50             "\n" => 'n',
51             "\t" => 't',
52             "\a" => 'a',
53             "\b" => 'b',
54             "\e" => 'e',
55             "\f" => 'f',
56             "\"" => '"',
57             "\$" => '$',
58             "\@" => '@',
59             );
60              
61             my %UNESCAPED = ( reverse %ESCAPED );
62              
63             sub chr2hex {
64 0     0 0   my($c) = @_;
65 0 0         if ( ord($c) < 128 ) {
66 0           return sprintf("\\x%02x", ord($c));
67             }
68             else {
69 0           require Carp;
70 0           Carp::croak (
71             "'ascii-escape' codec can't encode character: ordinal " . ord($c)
72             );
73             }
74             }
75              
76              
77             sub escape ($) {
78 0 0   0 0   local $_ = ( defined $_[0] ? $_[0] : '' );
79 0           s/([\a\b\e\f\r\n\t\"\\\$\@])/\\$ESCAPED{$1}/sg;
80 0           s/([\x00-\x1f\x7f-\xff])/chr2hex($1)/gse;
  0            
81 0           return $_;
82             }
83              
84             sub hex2chr {
85 0     0 0   my($hex) = @_;
86 0 0 0       if ( hex($hex) >= 0 and hex($hex) < 128) {
87 0           return chr(hex($hex));
88             }
89             else {
90 0           require Carp;
91 0           Carp::croak(
92             "'ascii-escape' codec can't decode escape sequence: "
93             . "\\x$hex (ordinal " . hex($hex) . ")"
94             );
95             }
96             }
97             sub oct2chr {
98 0     0 0   my($oct) = @_;
99 0 0 0       if ( oct($oct) >= 0 and oct($oct) < 128 ) {
100 0           return chr(oct($oct));
101             }
102             else {
103 0           require Carp;
104 0           Carp::croak (
105             "'ascii-escape' codec can't decode escape sequence: "
106             . "\\$oct (ordinal " . oct($oct). ")"
107             );
108             }
109             }
110              
111             # $original_string = unprintable( $special_characters_escaped );
112             sub unescape ($) {
113 0 0   0 0   local $_ = ( defined $_[0] ? $_[0] : '' );
114              
115 0           s/((?:\A|\G|[^\\]))\\x([\da-fA-F]{1,2})/$1.hex2chr($2)/gse;
  0            
116 0           s/((?:\A|\G|[^\\]))\\x\{([\da-fA-F]{1,4})\}/$1.hex2chr($2)/gse;
  0            
117 0           s/((?:\A|\G|[^\\]))\\([0-7]{1,3})/$1.oct2chr($2)/gse;
  0            
118              
119 0           s/((?:\A|\G|[^\\]))\\([^aAbBeEfFrRnNtT\\\"\$\@])/$1$2/g;
120 0           s/((?:\A|\G|[^\\]))\\([aAbBeEfFrRnNtT\\\"\$\@])/$1.$UNESCAPED{lc($2)}/gse;
  0            
121              
122 0           return $_;
123             }
124              
125              
126             1;
127             __END__