Escape/ASCII.pm

filter: Statementstmt 37.1% 23 / 62 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 6 / 15 Podpod 22.2% 2 / 9 totaltotal 29.2% 31 / 106 CC 8 SCAR 17.1
CC
8
Cov
29%
hex2chr
12.0
oct2chr
12.0
chr2hex
6.0
CRAP 5.5
« Escape.pm ↑ Summary
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 2 our $VERSION = do { q$Revision: 1.19 $ =~ /\d+\.(\d+)/; sprintf "%.2f", $1 / 100 };
8  
9 2 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Encode::Encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use base qw(Encode::Encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 __PACKAGE__->Define(qw/ascii-escape ascii_escape/);
17  
18 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
19  
20 2 require Encode;
21 2 Encode->export_to_level(1, @_);
22 }
23  
24 0 ▶sub enmode ($$) {
Subroutine enmode: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my ($class, $mode) = @_;
26
27 }
28  
29 0 ▶sub demode ($$) {
Subroutine demode: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my ($class, $mode) = @_;
31 }
32  
33  
34 0 ▶sub encode($$;$) {
Subroutine encode: not called CC=1 SCAR=6.9
Pod documented
35 0 my ($obj, $str, $chk) = @_;
36 0 my $escaped = escape($str);
37 0 return $escaped;
38 }
39  
40  
41 0 ▶sub decode($$;$) {
Subroutine decode: not called CC=1 SCAR=6.9
Pod documented
42 0 my ($obj, $str, $chk) = @_;
43 0 my $unescaped = unescape($str);
44 0 return $unescaped;
45 }
46  
47 2 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 2 my %UNESCAPED = ( reverse %ESCAPED );
62  
63 0 ▶sub chr2hex {
Subroutine chr2hex: not called CC=2 SCAR=17.9
Pod undocumented
64 0 my($c) = @_;
65 0 ▶ if ( ord($c) < 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $c < 128) { } 0 0 0
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 0 ▶sub escape ($) {
Subroutine escape: not called CC=2 SCAR=17.9
Pod undocumented
78 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 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;
81 0 return $_;
82 }
83  
84 0 ▶sub hex2chr {
Subroutine hex2chr: not called CC=3 SCAR=24.8
Pod undocumented
85 0 my($hex) = @_;
86 0 ▶ if ( hex($hex) >= 0 and hex($hex) < 128) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex $hex >= 0 and hex $hex < 128) { } 0 0 0
Condition0%0/3
hex $hex >= 0 and hex $hex < 128
!ll&&!rl&&r
000
MC/DC0%0/2
hex $hex >= 0 and hex $hex < 128
hex $hex >= 0 hex $hex < 128
Truth table0%0/3
A and B
A= hex $hex >= 0B= hex $hex < 128
ABresult
0X0
100
111
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 0 ▶sub oct2chr {
Subroutine oct2chr: not called CC=3 SCAR=24.8
Pod undocumented
98 0 my($oct) = @_;
99 0 ▶ if ( oct($oct) >= 0 and oct($oct) < 128 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (oct $oct >= 0 and oct $oct < 128) { } 0 0 0
Condition0%0/3
oct $oct >= 0 and oct $oct < 128
!ll&&!rl&&r
000
MC/DC0%0/2
oct $oct >= 0 and oct $oct < 128
oct $oct >= 0 oct $oct < 128
Truth table0%0/3
A and B
A= oct $oct >= 0B= oct $oct < 128
ABresult
0X0
100
111
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 0 ▶sub unescape ($) {
Subroutine unescape: not called CC=2 SCAR=17.9
Pod undocumented
113 0 ▶ local $_ = ( defined $_[0] ? $_[0] : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
114  
115 0 s/((?:\A|\G|[^\\]))\\x([\da-fA-F]{1,2})/$1.hex2chr($2)/gse;
116 0 s/((?:\A|\G|[^\\]))\\x\{([\da-fA-F]{1,4})\}/$1.hex2chr($2)/gse;
117 0 s/((?:\A|\G|[^\\]))\\([0-7]{1,3})/$1.oct2chr($2)/gse;
118  
119 0 s/((?:\A|\G|[^\\]))\\([^aAbBeEfFrRnNtT\\\"\$\@])/$1$2/g;
120 0 s/((?:\A|\G|[^\\]))\\([aAbBeEfFrRnNtT\\\"\$\@])/$1.$UNESCAPED{lc($2)}/gse;
121  
122 0 return $_;
123 }
124  
125  
126 2 1;
127 __END__
« Escape.pm ↑ Summary