lib/Chj/singlequote.pm

filter: Statementstmt 57.6% 45 / 78 Branchbran 20.0% 8 / 40 Conditioncond 25.0% 3 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 60.0% 9 / 15 Podpod 25.0% 2 / 8 totaltotal 42.2% 68 / 161 CC 25 SCAR 31.3
CC
25
Cov
43%
_quote_C
72.0
singlequote_sh
30.0
possibly_singlequote_sh
12.0
CRAP 22.8
« lib/Chj/xperlfunc.pm ↑ Summary lib/Chj/IO/CommandCommon.pm »
1 #
2 # Copyright (c) 2004-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::singlequote
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use Chj::singlequote qw(singlequote singlequote_many with_maxlen);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 is with_maxlen(9, sub { singlequote "Darc's place" }),
19 "'Darc\\'s...'";
20  
21  
22 =head1 DESCRIPTION
23  
24 Turn strings to quoted strings.
25  
26 =over 4
27  
28 =item singlequote ($str, $maybe_alternative)
29  
30 Perl style quoting.
31  
32 If $maybe_alternative is not given, uses the string "undef" for the
33 undef value.
34  
35 =item singlequote_sh ($str, $maybe_alternative)
36  
37 Shell style quoting.
38  
39 Also currently uses the "undef" value as default alternative, although
40 not making much sense.
41  
42 =item singlequote_many (@maybe_strs)
43  
44 In list context returns each argument quoted. In scalar context, join
45 them with a comma inbetween.
46  
47 Unlike the separate ones above, this captures exceptions during the
48 quoting process (stringification errors) and returns "<stringification
49 error: $@>" in that case.
50  
51 =back
52  
53 =head1 NOTE
54  
55 This is alpha software! Read the status section in the package README
56 or on the L<website|http://functional-perl.org/>.
57  
58 =cut
59  
60 package Chj::singlequote;
61 86 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 86 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 86 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
64 86 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
65 86 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 86 our @EXPORT = qw(singlequote);
68 86 our @EXPORT_OK = qw(singlequote_sh singlequote_many many with_maxlen
69 possibly_singlequote_sh singlequote_sh_many
70 quote_javascript
71 quote_C _quote_C
72 );
73  
74 # importing 'many' is probably not a good idea (depreciated)
75 86 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
76  
77 86 our $maybe_maxlen;
78  
79 2 ▶sub with_maxlen ($&) {
Subroutine with_maxlen: called CC=1 SCAR=0.0
Pod undocumented
80 2 local $maybe_maxlen = $_[0];
81 2 &{ $_[1] }()
82 }
83  
84 # Perl style:
85  
86 24 ▶sub singlequote {
Subroutine singlequote: called CC=8 SCAR=23.7
Pod documented
87 24 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 24 0 24
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0024
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
88 24 my ($str, $alternative) = @_;
89 24 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 24 0 24
90 24 ▶ if (defined $maybe_maxlen and length($str) > $maybe_maxlen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Chj::singlequote::maybe_maxlen and length $str > $Chj::singlequote::maybe_maxlen) 2 22 24
Condition66%2/3
defined $Chj::singlequote::maybe_maxlen and length $str > $Chj::singlequote::maybe_maxlen
!ll&&!rl&&r
2202
MC/DC50%1/2
defined $Chj::singlequote::maybe_maxlen and length $str > $Chj::singlequote::maybe_maxlen
defined $Chj::singlequote::maybe_maxlen length $str > $Chj::singlequote::maybe_maxlen
Truth table66%2/3
A and B
A= defined $Chj::singlequote::maybe_maxlenB= length $str > $Chj::singlequote::maybe_maxlen
ABresult
0X0
100
111
91 2 $str = substr($str, 0, $maybe_maxlen - 3) . "...";
92 }
93 24 $str =~ s/\'/\\\'/sg;
94  
95 # avoid newlines (and more?), try to follow the Carp::confess
96 # format, if maxlen is given:
97 0 ▶ $str =~ s/([\t\n\r])/sprintf ('\\x{%x}', ord $1)/sge
Branch: 1 branch
BranchTrueFalseTotal
if defined $Chj::singlequote::maybe_maxlen 2 22 24
98 if defined $maybe_maxlen;
99 24 "'$str'"
100 } else {
101 0 ▶ defined($alternative) ? $alternative : "undef"
Branch: 1 branch
BranchTrueFalseTotal
defined $alternative ? : 0 0 0
102 }
103 }
104 86 *Chj::singlequote = \&singlequote;
105  
106 4 ▶sub many {
Subroutine many: called CC=3 SCAR=12.5
Pod undocumented
107 my @strs = map {
108 4 my $str;
109 20 ▶ if (eval { $str = singlequote($_); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $str = singlequote($_); 1 } }) { } 20 0 20
110 20 $str
111 } else {
112 0 my $e = "$@";
113 0 $e =~ s/\n.*//s;
114 0 "<stringification error: $e>"
115 }
116 } @_;
117 4 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 4 4
118 @strs
119 0 } else {
120 4 join ", ", @strs
121 }
122 }
123 86 *singlequote_many = \&many;
124  
125 # Shell (Bash) style:
126  
127 0 ▶sub singlequote_sh {
Subroutine singlequote_sh: not called CC=5 SCAR=34.0
Pod documented
128 0 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 0 0 0
Condition0%0/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
129 0 my ($str, $alternative) = @_;
130 0 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 0 0 0
131 0 $str =~ s/\'/'\\\''/sg;
132 0 "'$str'"
133 } else {
134 0 ▶ defined($alternative) ? $alternative : "undef"
Branch: 1 branch
BranchTrueFalseTotal
defined $alternative ? : 0 0 0
135 }
136 }
137 86 *Chj::singlequote_sh = \&singlequote_sh;
138  
139 # don't quote bare words or simply formatted paths that don't need to
140 # be quoted
141 0 ▶sub possibly_singlequote_sh {
Subroutine possibly_singlequote_sh: not called CC=3 SCAR=24.8
Pod undocumented
142 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
143 0 my ($str) = @_;
144 0 ▶ if ($str =~ m{^[=\w/.-]+\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[^[=\w/.-]+\z]) { } 0 0 0
145 0 $str
146 } else {
147 0 singlequote_sh $str
148 }
149 }
150  
151 0 ▶sub singlequote_sh_many {
Subroutine singlequote_sh_many: not called CC=1 SCAR=6.9
Pod undocumented
152 0 join " ", map { possibly_singlequote_sh $_ } @_
153 }
154  
155 0 ▶sub quote_javascript {
Subroutine quote_javascript: not called CC=2 SCAR=17.9
Pod undocumented
156 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
157 0 my ($str) = @_;
158  
159 #require JSON::MaybeXS;
160 #JSON->new->allow_nonref(1)->encode($str)
161  
162 # this doesn't turn special characters into backslash sequences
163 #$str =~ s|\\|\\\\|sg;
164 #$str =~ s|\"|\\\"|sg;
165 #'"'.$str.'"'
166  
167 # <mst> if you're obsessed with avoiding dependencies, just use
168 # JSON::PP directly and suck up the terrible performance
169 0 require JSON::PP;
170 0 JSON::PP->new->allow_nonref(1)->encode($str)
171  
172 # <mst> note that JSON::MaybeXS is trivial and fatpacks fine
173 # <mst> intentionally
174 }
175  
176 0 ▶sub _quote_C {
Subroutine _quote_C: not called CC=8 SCAR=42.8
177 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
178 0 my ($str) = @_;
179 0 $str =~ s{(.)}{
180 0 my $c = $1;
181 0 my $i = ord $c;
182 # https://en.wikipedia.org/wiki/Ascii
183 0 ▶ ($i >= 32 and $i < 127) ?
Branch: 5 branches
BranchTrueFalseTotal
$i >= 32 && $i < 127 ? : 0 0 0
$c eq '"' ? : 0 0 0
$c eq "\n" ? : 0 0 0
$c eq "\r" ? : 0 0 0
$c eq "\t" ? : 0 0 0
Condition0%0/3
$i >= 32 and $i < 127
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= 32 and $i < 127
$i >= 32 $i < 127
Truth table0%0/3
A and B
A= $i >= 32B= $i < 127
ABresult
0X0
100
111
184 ( $c eq '"' ? "\\\"" :
185 $c )
186 :
187 ( $c eq "\n" ? "\\n" :
188 $c eq "\r" ? "\\r" :
189 $c eq "\t" ? "\\t" :
190 sprintf "\\%o", $i )
191 }sge;
192 0 $str
193 }
194  
195 0 ▶sub quote_C {
Subroutine quote_C: not called CC=2 SCAR=17.9
Pod undocumented
196 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
197 0 my ($str) = @_;
198 0 '"' . _quote_C($str) . '"'
199 }
200  
201 86 1
« lib/Chj/xperlfunc.pm ↑ Summary lib/Chj/IO/CommandCommon.pm »