blib/lib/Scalar/Quote.pm

filter: Statementstmt 73.4% 69 / 94 Branchbran 50.0% 21 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 75.0% 6 / 8 totaltotal 68.3% 108 / 158 CC 22 SCAR 16.7
CC
22
Cov
66%
quote_cut
9.6
D
7.3
N
6.8
CRAP 5.3
↑ Summary
1 package Scalar::Quote;
2  
3 1 our $VERSION = '0.26';
4  
5 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10 1 our @ISA = qw(Exporter);
11  
12 1 our %EXPORT_TAGS = ( 'quote' => [ qw( quote quote_number quote_cut quote_start ) ],
13 'diff' => [ qw( str_diff str_diffix ) ],
14 'short' => [ qw( Q N S D ) ] );
15 1 our @EXPORT_OK = (@{$EXPORT_TAGS{quote}},
16 1 @{$EXPORT_TAGS{diff}},
17 1 @{$EXPORT_TAGS{short}});
18 1 our @EXPORT = qw();
19  
20 # converts a char to its hex representation
21 51778 ▶sub char_to_hex ($ ) {
Subroutine char_to_hex: called CC=2 SCAR=7.2
Pod undocumented
22 51778 my $c=ord(shift);
23 51778 ▶ sprintf( ($c < 256 ? '\x%02x' : '\x{%x}'), $c);
Branch: 1 branch
BranchTrueFalseTotal
$c < 256 ? : 51778 0 51778
24 }
25  
26 1 my %esc = ( "\n" => '\n',
27 "\t" => '\t',
28 "\r" => '\r',
29 "\\" => '\\\\',
30 "\a" => '\a',
31 "\b" => '\b',
32 "\f" => '\f' );
33  
34 55109 ▶sub escape_char($ ) {
Subroutine escape_char: called CC=2 SCAR=6.9
Pod undocumented
35 55109 my $char=shift;
36 55109 ▶ exists $esc{$char} ? $esc{$char} : char_to_hex($char)
Branch: 1 branch
BranchTrueFalseTotal
exists $esc{$char} ? : 3331 51778 55109
37 }
38  
39 # converts unprintable chars to \x{XX} and also escapes '"' and '\' if
40 # required
41 350 ▶sub Q ($ ) {
Subroutine Q: called CC=3 SCAR=11.8
Pod documented
42 350 my $s=shift;
43 350 ▶ defined $s or return 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $s or return "undef" 350 0 350
44 350 ▶ if ($s=~s/([^!#&()*+,\-.\/0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz{|}~ ])/escape_char($1)/ge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s(([^!#&()*+,\-./0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz{|}~ ]))[&escape_char($1);]eg) 350 0 350
45 350 return qq("$s");
46 }
47 0 return qq('$s');
48 }
49 1 *quote=\&Q;
50  
51 # compares two strings and returns the position where they start to be
52 # diferent, i.e diffix('good morning', 'good afternoon') == 5
53  
54 202 ▶sub str_diffix ($$) {
Subroutine str_diffix: called CC=6 SCAR=18.1
Pod documented
55 202 my ($a, $b)=@_;
56  
57 202 ▶ $a='' unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 0 202 202
58 202 ▶ $b='' unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 202 202
59  
60 202 ▶ return -1 if $a eq $b;
Branch: 1 branch
BranchTrueFalseTotal
if $a eq $b 0 202 202
61  
62 # my $c;
63 # for (my $i=0;;$i++) {
64 # $c=substr($a,$i,1);
65 # return $i
66 # unless ( $c ne '' and $c eq substr($b,$i,1));
67 # }
68  
69 202 my $la = length $a;
70 202 my $lb = length $b;
71  
72 202 ▶ my $min = $la < $lb ? $la : $lb;
Branch: 1 branch
BranchTrueFalseTotal
$la < $lb ? : 101 101 202
73  
74 202 my $c = substr($a, 0, $min) ^ substr($b, 0, $min);
75 202 ▶ if ($c =~ m/[^\0]/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /[^\0]/g) 201 1 202
76 201 return pos($c) - 1;
77 }
78 1 return $min;
79 }
80  
81 # quote_cut($string, $start, $len), like substr() but adds a head and a tail
82 # to the substring reported how many chars have been left alone. It
83 # also escapes the string.
84  
85 200 ▶sub quote_cut ($$$ ) {
Subroutine quote_cut: called CC=6 SCAR=22.6
Pod documented
86 200 ▶ return 'undef' unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 200 200
87 200 my (undef, $start, $len)=@_;
88 200 my $end=length($_[0])-$len-$start;
89 200 ▶ if ($end<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 200 200
90 0 $start+=$end;
91 0 $end=0;
92 }
93 200 ▶ if ($start<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 200 200
94 0 $start=0;
95 }
96 200 my $s=sprintf("[%d chars omitted]", $start);
97 200 ▶ if (length $s>=$start) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $s >= $start) 0 200 200
98 0 $len+=$start;
99 0 $start=0;
100 0 $s='';
101 }
102 200 my $e=sprintf("[%d chars omitted]", $end);
103 200 ▶ if (length $e>=$end) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $e >= $end) 0 200 200
104 0 $len+=$end;
105 0 $e='';
106 }
107 200 quote($s.substr($_[0], $start, $len).$e);
108 }
109  
110  
111 # escape and quote string start operator, like Q but truncates the
112 # string if it is to long.
113 0 ▶sub S ($;$ ) {
Subroutine S: not called CC=2 SCAR=17.9
Pod documented
114 0 ▶ my $len=defined $_[1] ? $_[1] : 32;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
115 0 quote_cut ($_[0], 0, $len);
116 }
117 1 *quote_start=\&S;
118  
119 1 my $number_re=qr/^\s*[+-]?(?:\d+|\d*\.\d*)(?i:E[+-]?\d+)?\s*$/;
120  
121 # quote number
122 0 ▶sub N ($ ) {
Subroutine N: not called CC=3 SCAR=19.2
Pod documented
123 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
125 0 ▶ if ($_[0]=~/$number_re/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /$number_re/o) 0 0 0
126 0 return sprintf("%f", $_[0]);
127 }
128 0 return sprintf("%f (str: %s)", $_[0], S($_[0]));
129 }
130 'undef'
131 0 }
132 1 *quote_number=\&N;
133  
134 # D computes the difference between two strings.
135 100 ▶sub D ($$;$$ ) {
Subroutine D: called CC=5 SCAR=19.8
Pod documented
136 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
137 100 ▶ return () if $_[0] eq $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq $_[1] 0 100 100
138  
139 100 ▶ my $len=defined $_[3] ? $_[3] : 32;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 100 100
140 100 ▶ my $start=(defined $_[2] ? $_[2] : -8)
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 100 100
141 + str_diffix($_[0], $_[1]);
142 100 my $a=quote_cut($_[0], $start, $len);
143 100 my $b=quote_cut($_[1], $start, $len);
144  
145 100 ▶ return ($a, $b) if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 100 0 100
146  
147 {
148 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
149 0 my $caller = caller;
150 0 my $pa=$caller."::a";
151 0 my $pb=$caller."::b";
152 0 ${$pa}=$a;
153 0 ${$pb}=$b;
154 }
155 0 return 1;
156 }
157 1 *str_diff=\&D;
158  
159  
160  
161  
162 1 1;
163 __END__
↑ Summary