blib/lib/Win32/ParseWords.pm

filter: Statementstmt 77.5% 69 / 89 Branchbran 66.0% 33 / 50 Conditioncond 80.9% 17 / 21 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 90.9% 10 / 11 Podpod 0.0% 0 / 6 totaltotal 71.5% 136 / 190 CC 35 SCAR 30.9
CC
35
Cov
74%
old_shellwords
72.7
parse_line
16.0
shellwords
6.0
CRAP 21.9
↑ Summary
1 package Win32::ParseWords;
2 1 $Win32::ParseWords::VERSION = '0.01';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9 1 our @ISA = qw(Exporter);
10 1 our %EXPORT_TAGS = ();
11 1 our @EXPORT = qw(shellwords quotewords nested_quotewords parse_line);
12 1 our @EXPORT_OK = qw(old_shellwords set_perl_squote);
13  
14 1 my $PERL_SINGLE_QUOTE = 0;
15  
16 1 ▶sub set_perl_squote {
Subroutine set_perl_squote: called CC=1 SCAR=0.0
Pod undocumented
17 1 $PERL_SINGLE_QUOTE = $_[0];
18 }
19  
20 4 ▶sub shellwords {
Subroutine shellwords: called CC=6 SCAR=17.9
Pod undocumented
21 4 my (@lines) = @_;
22 4 my @allwords;
23  
24 4 foreach my $line (@lines) {
25 6 $line =~ s/^\s+//;
26 6 my @words = parse_line('\s+', 0, $line);
27 6 ▶ pop @words if (@words and !defined $words[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if @words and not defined $words[-1] 2 4 6
Condition100%3/3
@words and not defined $words[-1]
!ll&&!rl&&r
222
MC/DC100%2/2
@words and not defined $words[-1]
@words not defined $words[-1]
Truth table100%3/3
A and not(B)
A= @wordsB= not defined $words[-1]
ABresult
0X0
100
111
28 6 ▶ return() unless (@words || !length($line));
Branch: 1 branch
BranchTrueFalseTotal
unless @words or not length $line 2 4 6
Condition66%2/3
@words or not length $line
l!l&&r!l&&!r
402
MC/DC50%1/2
@words or not length $line
@words not length $line
Truth table66%2/3
A or not(B)
A= @wordsB= not length $line
ABresult
1X1
011
000
29 4 push(@allwords, @words);
30 }
31 2 return(@allwords);
32 }
33  
34 4 ▶sub quotewords {
Subroutine quotewords: called CC=4 SCAR=13.9
Pod undocumented
35 4 my($delim, $keep, @lines) = @_;
36 4 my($line, @words, @allwords);
37  
38 4 foreach $line (@lines) {
39 4 @words = parse_line($delim, $keep, $line);
40 4 ▶ return() unless (@words || !length($line));
Branch: 1 branch
BranchTrueFalseTotal
unless @words or not length $line 1 3 4
Condition66%2/3
@words or not length $line
l!l&&r!l&&!r
301
MC/DC50%1/2
@words or not length $line
@words not length $line
Truth table66%2/3
A or not(B)
A= @wordsB= not length $line
ABresult
1X1
011
000
41 3 push(@allwords, @words);
42 }
43 3 return(@allwords);
44 }
45  
46 2 ▶sub nested_quotewords {
Subroutine nested_quotewords: called CC=4 SCAR=13.9
Pod undocumented
47 2 my($delim, $keep, @lines) = @_;
48 2 my($i, @allwords);
49  
50 2 ▶ for ($i = 0; $i < @lines; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 4 1 5
51 4 @{$allwords[$i]} = parse_line($delim, $keep, $lines[$i]);
52 4 ▶ return() unless (@{$allwords[$i]} || !length($lines[$i]));
Branch: 1 branch
BranchTrueFalseTotal
unless @{$allwords[$i];} or not length $lines[$i] 1 3 4
Condition66%2/3
@{$allwords[$i];} or not length $lines[$i]
l!l&&r!l&&!r
301
MC/DC50%1/2
@{$allwords[$i];} or not length $lines[$i]
@{$allwords[$i];} not length $lines[$i]
Truth table66%2/3
A or not(B)
A= @{$allwords[$i];}B= not length $lines[$i]
ABresult
1X1
011
000
53 }
54 1 return(@allwords);
55 }
56  
57 25 ▶sub parse_line {
Subroutine parse_line: called CC=16 SCAR=27.7
Pod undocumented
58 25 my($delimiter, $keep, $line) = @_;
59 25 my($word, @pieces);
60  
61 1 ▶ no warnings 'uninitialized'; # we will be testing undef strings
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 25 ▶ while (length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line) 105 20 125
64 # This pattern is optimised to be stack conservative on older perls.
65 # Do not refactor without being careful and testing it on very long strings.
66 # See Perl bug #42980 for an example of a stack busting input.
67 105 ▶ $line =~ s/^
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/^ (?: # double quoted string (") # $quote ((?>[^\^"]*(?:\^.[^\^"]*)*))" # $quoted | # --OR-- # singe quoted string (') # $quote ((?>[^\^']*(?:\^.[^\^']*)*))' # $quoted | # --OR-- # unquoted string ( # $unquoted (?:\^.|[^\^"'])*? ) # followed by ( # $delim \Z(?!\n) # EOL | # --OR-- (?-x:$delimiter) # delimiter | # --OR-- (?!^)(?=["']) # a quote ) )//sx or return 100 5 105
68 (?:
69 # double quoted string
70 (") # $quote
71 ((?>[^\^"]*(?:\^.[^\^"]*)*))" # $quoted
72 | # --OR--
73 # singe quoted string
74 (') # $quote
75 ((?>[^\^']*(?:\^.[^\^']*)*))' # $quoted
76 | # --OR--
77 # unquoted string
78 ( # $unquoted
79 (?:\^.|[^\^"'])*?
80 )
81 # followed by
82 ( # $delim
83 \Z(?!\n) # EOL
84 | # --OR--
85 (?-x:$delimiter) # delimiter
86 | # --OR--
87 (?!^)(?=["']) # a quote
88 )
89 )//xs or return; # extended layout
90 100 ▶ my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6);
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 22 78 100
91  
92 100 ▶ return() unless( defined($quote) || length($unquoted) || length($delim));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quote or length $unquoted or length $delim 0 100 100
Condition83%5/6
defined $quote or length $unquoted or length $delim
l!l&&r!l&&!r
76240
defined $quote or length $unquoted
l!l&&r!l&&!r
245224
MC/DC0%0/3
defined $quote or length $unquoted or length $delim
defined $quote length $unquoted length $delim
Truth table75%3/4
A or B or C
A= defined $quoteB= length $unquotedC= length $delim
ABCresult
1XX1
01X1
0011
0000
93  
94 100 ▶ if ($keep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keep) { } 27 73 100
95 27 $quoted = "$quote$quoted$quote";
96 }
97 else {
98 73 $unquoted =~ s/\^(.)/$1/sg;
99 73 ▶ if (defined $quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $quote) 17 56 73
100 17 ▶ $quoted =~ s/\^(.)/$1/sg if ($quote eq '"');
Branch: 1 branch
BranchTrueFalseTotal
if $quote eq "\"" 15 2 17
101 17 ▶ $quoted =~ s/\^([\^'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
Branch: 1 branch
BranchTrueFalseTotal
if $PERL_SINGLE_QUOTE and $quote eq "'" 1 16 17
Condition100%3/3
$PERL_SINGLE_QUOTE and $quote eq "'"
!ll&&!rl&&r
971
MC/DC100%2/2
$PERL_SINGLE_QUOTE and $quote eq "'"
$PERL_SINGLE_QUOTE $quote eq "'"
Truth table100%3/3
A and B
A= $PERL_SINGLE_QUOTEB= $quote eq "'"
ABresult
0X0
100
111
102 }
103 }
104 100 $word .= substr($line, 0, 0); # leave results tainted
105 100 ▶ $word .= defined $quote ? $quoted : $unquoted;
Branch: 1 branch
BranchTrueFalseTotal
defined $quote ? : 24 76 100
106
107 100 ▶ if (length($delim)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $delim) 55 45 100
108 55 push(@pieces, $word);
109 55 ▶ push(@pieces, $delim) if ($keep eq 'delimiters');
Branch: 1 branch
BranchTrueFalseTotal
if $keep eq "delimiters" 4 51 55
110 55 undef $word;
111 }
112 100 ▶ if (!length($line)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $line) 20 80 100
113 20 push(@pieces, $word);
114 }
115 }
116 20 return(@pieces);
117 }
118  
119 0 ▶sub old_shellwords {
Subroutine old_shellwords: not called CC=9 SCAR=42.9
Pod undocumented
120 # Usage:
121 # use ParseWords;
122 # @words = old_shellwords($line);
123 # or
124 # @words = old_shellwords(@lines);
125 # or
126 # @words = old_shellwords(); # defaults to $_ (and clobbers it)
127  
128 1 ▶ no warnings 'uninitialized'; # we will be testing undef strings
Subroutine BEGIN: called CC=1 SCAR=0.0
129 0 ▶ local *_ = \join('', @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
130 0 my (@words, $snippet);
131  
132 0 s/\A\s+//;
133 0 ▶ while ($_ ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "") 0 0 0
134 0 my $field = substr($_, 0, 0); # leave results tainted
135 0 for (;;) {
136 0 ▶ if (s/\A"(([^"\^]|\^.)*)"//s) {
Branch: 6 branches
BranchTrueFalseTotal
if (s/\A"(([^"\^]|\^.)*)"//s) { } 0 0 0
elsif (/\A"/) { } 0 0 0
elsif (s/\A'(([^'\^]|\^.)*)'//s) { } 0 0 0
elsif (/\A'/) { } 0 0 0
elsif (s/\A\^(.?)//s) { } 0 0 0
elsif (s/\A([^\s\^'"]+)//) { } 0 0 0
137 0 ($snippet = $1) =~ s#\^(.)#$1#sg;
138 }
139 elsif (/\A"/) {
140 0 Carp::carp("Unmatched double quote: $_");
141 0 return();
142 }
143 elsif (s/\A'(([^'\^]|\^.)*)'//s) {
144 0 ($snippet = $1) =~ s#\^(.)#$1#sg;
145 }
146 elsif (/\A'/) {
147 0 Carp::carp("Unmatched single quote: $_");
148 0 return();
149 }
150 elsif (s/\A\^(.?)//s) {
151 0 $snippet = $1;
152 }
153 elsif (s/\A([^\s\^'"]+)//) {
154 0 $snippet = $1;
155 }
156 else {
157 0 s/\A\s+//;
158 0 last;
159 }
160 0 $field .= $snippet;
161 }
162 0 push(@words, $field);
163 }
164 0 return @words;
165 }
166  
167 1 1;
168  
169 __END__
↑ Summary