lib/FP/Path.pm

filter: Statementstmt 96.2% 128 / 133 Branchbran 81.6% 49 / 60 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 94.4% 34 / 36 Podpod 0.0% 0 / 16 totaltotal 87.4% 236 / 270 CC 37 SCAR 11.3
CC
37
Cov
92%
perhaps_clean_dotdot
6.0
perhaps_segment_error
5.1
string
5.0
CRAP 3.1
« lib/FP/PureArray.pm ↑ Summary lib/FP/Abstract/Sequence.pm »
1 #
2 # Copyright (c) 2011-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 FP::Path
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 my $p = FP::Path->new_from_string ("a/../b/C")
20 ->add(FP::Path->new_from_string("../d/../e"), 0);
21 is $p->string, 'a/../b/C/../d/../e';
22 is $p->xclean->string, 'b/e';
23 ok not equal($p->xclean, $p);
24 ok equal($p->xclean, $p->xclean); # obviously, assuming purity
25  
26 # or use the (evil?) constructor function export feature:
27 1 ▶ use FP::Path "path";
Subroutine BEGIN: called CC=1 SCAR=0.0
28 is path("a/../b/C")->xclean->string, "b/C";
29  
30 =head1 DESCRIPTION
31  
32 Not really sure why I'm creating something from scratch here? It might
33 be cleaner:
34  
35 This doesn't do I/O (access the file system, ask the system for the
36 hostname, etc.), and it doesn't resolve ".." unless when told to
37 (`perhaps_clean_dotdot` and derived methods (incl. `xclean` etc.)).
38  
39 =head1 TODO
40  
41 Port / merge with
42 https://github.com/pflanze/chj-schemelib/blob/master/cj-posixpath.scm
43 ?
44  
45 Provide `string_to_path` constructor function?
46  
47 =head1 SEE ALSO
48  
49 Implements: L<FP::Abstract::Show>, L<FP::Abstract::Pure>
50  
51 L<FP::Path::t> for the test suite
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 FP::Path;
61  
62 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 3 ▶use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
67 3 ▶use Chj::constructorexporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 3 ▶use FP::Predicates qw(is_string is_boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
69 3 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 3 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 126 ▶sub perhaps_segment_error {
Subroutine perhaps_segment_error: called CC=5 SCAR=16.2
74 126 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 126 0 126
75 126 my ($segment) = @_;
76 126 ▶ return "segments must be strings" unless is_string $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless is_string($segment) 0 126 126
77 126 ▶ return "segments cannot be the empty string" unless length $segment;
Branch: 1 branch
BranchTrueFalseTotal
unless length $segment 1 125 126
78 125 ▶ return "segment contains slash: " . show($segment) if $segment =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if $segment =~ m[/] 2 123 125
79 ()
80 123 }
81  
82 sub is_segment {
83 @_ == 1 or fp_croak_arity 1;
84 not perhaps_segment_error $_[0]
85 }
86  
87 126 ▶sub check_segment {
Subroutine check_segment: called CC=3 SCAR=11.1
88 126 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 126 0 126
89 126 ▶ if (my ($e) = perhaps_segment_error $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($e) = perhaps_segment_error($_[0])) 3 123 126
90 3 die $e
91 }
92 }
93  
94 # Toggle typing, off for speed (checking FP::List costs O(length);
95 # better use FP::StrictList if really interested in strict typing!)
96 12 ▶sub use_costly_typing {
Subroutine use_costly_typing: called CC=2 SCAR=7.2
97 12 ▶ @_ == 0 or fp_croak_arity 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 or fp_croak_arity(0) 12 0 12
98 12 0
99 }
100 3 our $use_costly_typing = use_costly_typing; # for access from FP::Path::t
101  
102 9 ▶sub typed {
Subroutine typed: called CC=3 SCAR=12.0
103 9 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 9 0 9
104 9 my ($pred, $name) = @_;
105 0 ▶ if (use_costly_typing) { [$pred, $name] }
Branch: 1 branch
BranchTrueFalseTotal
if (use_costly_typing()) { } 0 9 9
106 else {
107 9 $name
108 }
109 }
110  
111 3 ▶use FP::Struct [
Subroutine BEGIN: called CC=1 SCAR=0.0
112 3 typed(list_of(\&is_segment), 'rsegments'), # reversed list
113 typed(\&is_boolean, 'has_endslash')
114 , # whether the path is forcibly specifying a
115 # dir by using a slash at the end (forcing a
116 # dir by ending in "." isn't setting this
117 # flag)
118 typed(\&is_boolean, 'is_absolute'), # bool
119 ],
120 'FP::Struct::Show',
121 'FP::Abstract::Equal',
122 3 'FP::Abstract::Pure';
123  
124 3 *import = constructorexporter new_from_string => "path";
125  
126 124 ▶sub new_from_string {
Subroutine new_from_string: called CC=4 SCAR=13.9
Pod undocumented
127 124 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 124 0 124
128 124 my ($class, $str) = @_;
129 124 my @p = split m{/+}, $str;
130 124 ▶ shift @p if (@p and $p[0] eq "");
Branch: 1 branch
BranchTrueFalseTotal
if @p and $p[0] eq "" 51 73 124
Condition100%3/3
@p and $p[0] eq ""
!ll&&!rl&&r
175651
MC/DC100%2/2
@p and $p[0] eq ""
@p $p[0] eq ""
Truth table100%3/3
A and B
A= @pB= $p[0] eq ""
ABresult
0X0
100
111
131 124 $class->new(
132 array_to_list_reverse(\@p),
133 scalar $str =~ m{/$}s,
134 scalar $str =~ m{^/}s
135 )
136 }
137  
138 17 ▶sub FP_Equal_equal {
Subroutine FP_Equal_equal: called CC=4 SCAR=13.9
Pod undocumented
139 17 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 17 0 17
140 17 my ($a, $b) = @_;
141  
142 # no need to compare is_absolute, since it is being distinguished
143 # anyway? Or better be safe than sorry?
144 17 ▶ ( (!!$a->is_absolute eq !!$b->is_absolute)
Branch: 1 branch
BranchTrueFalseTotal
!!$a->is_absolute eq !!$b->is_absolute and !!$a->has_endslash eq !!$b->has_endslash and equal($a->rsegments, $b->rsegments) 12 5 17
Condition100%3/3
!!$a->is_absolute eq !!$b->is_absolute and !!$a->has_endslash eq !!$b->has_endslash
!ll&&!rl&&r
2312
MC/DC100%2/2
!!$a->is_absolute eq !!$b->is_absolute and !!$a->has_endslash eq !!$b->has_endslash
!!$a->is_absolute eq !!$b->is_absolute !!$a->has_endslash eq !!$b->has_endslash
Truth table100%3/3
A and B
A= !!$a->is_absolute eq !!$b->is_absoluteB= !!$a->has_endslash eq !!$b->has_endslash
ABresult
0X0
100
111
145 and (!!$a->has_endslash eq !!$b->has_endslash)
146 and equal($a->rsegments, $b->rsegments))
147 }
148  
149 29 ▶sub segments {
Subroutine segments: called CC=1 SCAR=0.0
Pod undocumented
150 29 my $s = shift;
151 29 $s->rsegments->reverse
152 }
153  
154 85 ▶sub string {
Subroutine string: called CC=5 SCAR=16.1
Pod undocumented
155 85 my $s = shift;
156 85 my $rs = $s->rsegments;
157  
158 # check that no invalid segments have creeped in (by way of using
159 # the "lowlevel" accessors like segments_set, or the new or new_
160 # constructors directly; adding a type check to the segments field
161 # would solve this, but is less efficient as it would have to walk
162 # the list on every change instead of only stringification):
163 85 $rs->for_each(\&check_segment);
164  
165 # force "." for empty relative paths:
166 84 ▶ my $rs1 = is_null($rs) && not($s->is_absolute) ? list(".") : $rs;
Branch: 1 branch
BranchTrueFalseTotal
is_null($rs) && !$s->is_absolute ? : 18 66 84
Condition100%3/3
is_null($rs) and not $s->is_absolute
!ll&&!rl&&r
60618
MC/DC100%2/2
is_null($rs) and not $s->is_absolute
is_null($rs) not $s->is_absolute
Truth table100%3/3
A and not(B)
A= is_null($rs)B= not $s->is_absolute
ABresult
0X0
100
111
167  
168 # add end slash
169 84 ▶ my $ss = ($s->has_endslash ? $rs1->cons("") : $rs1)->reverse;
Branch: 1 branch
BranchTrueFalseTotal
$s->has_endslash ? : 42 42 84
170  
171 # add start slash
172 84 ▶ ($s->is_absolute ? $ss->cons("") : $ss)->strings_join("/")
Branch: 1 branch
BranchTrueFalseTotal
$s->is_absolute ? : 31 53 84
173 }
174  
175 # remove "." entries: (leave ".." in, because these cannot be resolved
176 # without reading the file system or knowing the usage)
177 53 ▶sub clean_dot {
Subroutine clean_dot: called CC=3 SCAR=11.0
Pod undocumented
178 53 my $s = shift;
179 53 my $rseg = $s->rsegments;
180 175 ▶ $s->rsegments_set($rseg->filter(sub { not($_[0] eq ".") }))
Subroutine __ANON__: called CC=1 SCAR=0.0
181 ->has_endslash_set(
182  
183 # set forced dir flag if the last segment was a ".", even
184 # if previously it didn't end in "/"
185 53 ▶ $$s{has_endslash} or do {
Condition100%3/3
$s->{'has_endslash'} || do { if (is_null($rseg)) { 0; } else { $rseg->first eq '.'; } }
l!l&&r!l&&!r
51038
MC/DC100%2/2
$s->{'has_endslash'} || do { if (is_null($rseg)) { 0; } else { $rseg->first eq '.'; } }
$s->{'has_endslash'} do { if (is_null($rseg)) { 0; } else { $rseg->first eq '.'; } }
Truth table100%3/3
A || B
A= $s->{'has_endslash'}B= do { if (is_null($rseg)) { 0; } else { $rseg->first eq '.'; } }
ABresult
1X1
011
000
186 48 ▶ if (is_null $rseg) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($rseg)) { } 3 45 48
187 3 0
188 } else {
189 45 $rseg->first eq "."
190 }
191 }
192 );
193 }
194  
195 # This is only valid to be applied to paths that have already been
196 # `clean_dot`ed !
197 35 ▶sub perhaps_clean_dotdot {
Subroutine perhaps_clean_dotdot: called CC=6 SCAR=17.9
Pod undocumented
198 35 my $s = shift;
199  
200 # XX this might actually be more efficient when working on the reverse
201 # order? But leaving old imperative algorithm for now.
202 35 my $rs = $s->rsegments;
203 35 ▶ my $ends_in_dotdot = is_pair($rs) && $rs->first eq "..";
Condition100%3/3
is_pair($rs) && $rs->first eq '..'
!ll&&!rl&&r
3239
MC/DC100%2/2
is_pair($rs) && $rs->first eq '..'
is_pair($rs) $rs->first eq '..'
Truth table100%3/3
A && B
A= is_pair($rs)B= $rs->first eq '..'
ABresult
0X0
100
111
204 35 my @s;
205 35 for my $seg ($rs->reverse_values) {
206 111 ▶ if ($seg eq "..") {
Branch: 1 branch
BranchTrueFalseTotal
if ($seg eq '..') { } 38 73 111
207 38 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) { } 32 6 38
208 32 pop @s;
209 } else {
210 return ()
211 6 }
212 } else {
213 73 push @s, $seg
214 }
215 }
216 29 my $s1 = $s->rsegments_set(array_to_list_reverse \@s);
217 29 ▶ $ends_in_dotdot ? $s1->has_endslash_set(1) : $s1
Branch: 1 branch
BranchTrueFalseTotal
$ends_in_dotdot ? : 5 24 29
218 }
219  
220 # (should have those functions without the Path wrapper? Maybe, maybe not.)
221  
222 # This is only valid to be applied to paths that have already been
223 # `clean_dot`ed !
224 35 ▶sub xclean_dotdot {
Subroutine xclean_dotdot: called CC=2 SCAR=6.9
Pod undocumented
225 35 my $s = shift;
226 35 ▶ if (my ($v) = $s->perhaps_clean_dotdot) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($v) = $s->perhaps_clean_dotdot) { } 29 6 35
227 29 $v
228 } else {
229 6 die "can't take '..' of root directory"
230 }
231 }
232  
233 0 ▶sub perhaps_clean {
Subroutine perhaps_clean: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my $s = shift;
235 0 $s->clean_dot->perhaps_clean_dotdot
236 }
237  
238 15 ▶sub xclean {
Subroutine xclean: called CC=1 SCAR=0.0
Pod undocumented
239 15 my $s = shift;
240 15 $s->clean_dot->xclean_dotdot
241 }
242  
243 2 ▶sub add_segment { # functionally. hm.
Subroutine add_segment: called CC=1 SCAR=0.6
Pod undocumented
244 2 my $s = shift;
245 2 my ($segment) = @_;
246 2 check_segment $segment;
247 $s->rsegments_update(
248 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 cons $segment, $_[0]
250 }
251 )
252  
253 # no forced endslash anymore
254 0 ->has_endslash_set(0);
255 }
256  
257 13 ▶sub add {
Subroutine add: called CC=4 SCAR=13.9
Pod undocumented
258 13 my $a = shift;
259 13 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 13 0 13
260 13 my ($b, $is_url) = @_; # when is_url is true, it cleans dit
261 13 ▶ if ($b->is_absolute) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_absolute) { } 2 11 13
262 2 $b
263 } else {
264 11 my $c = $a->rsegments_set($b->rsegments->append($a->rsegments))
265 ->clean_dot;
266 11 ▶ $is_url ? $c->xclean_dotdot : $c
Branch: 1 branch
BranchTrueFalseTotal
$is_url ? : 6 5 11
267 }
268 }
269  
270 5 ▶sub dirname {
Subroutine dirname: called CC=2 SCAR=6.9
Pod undocumented
271 5 my $s = shift;
272 5 ▶ is_null $$s{rsegments} and die "can't take dirname of empty path";
Branch: 1 branch
BranchTrueFalseTotal
is_null($s->{"rsegments"}) and die "can't take dirname of empty path" 2 3 5
273 3 $s->rsegments_update(\&rest)->has_endslash_set(1);
274 }
275  
276 4 ▶sub to_relative {
Subroutine to_relative: called CC=2 SCAR=6.9
Pod undocumented
277 4 my $s = shift;
278 4 ▶ die "is already relative" unless $s->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
unless $s->is_absolute 1 3 4
279  
280 # keep has_endslash, # XX hm always? what about the dropping of first entry?
281 3 $s->is_absolute_set(0);
282 }
283  
284 5 ▶sub contains_dotdot {
Subroutine contains_dotdot: called CC=1 SCAR=0.0
Pod undocumented
285 5 my $s = shift;
286 7 ▶ $s->rsegments->any(sub { $_[0] eq ".." })
Subroutine __ANON__: called CC=1 SCAR=0.0
287 5 }
288  
289 # These are used as helpers for Chj::Path::Filesystem's touched_paths
290  
291 # split a path into two parts, one with the first segment and one with
292 # the rest
293 17 ▶sub perhaps_split_first_segment {
Subroutine perhaps_split_first_segment: called CC=2 SCAR=7.1
Pod undocumented
294 17 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 17 0 17
295 17 my ($p) = @_;
296 17 perhaps_resplit_next_segment($p->rsegments_set(null), $p)
297 }
298  
299 # re-split two paths so that the first gains another segment from the
300 # second
301 29 ▶sub perhaps_resplit_next_segment {
Subroutine perhaps_resplit_next_segment: called CC=4 SCAR=13.9
Pod undocumented
302 29 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 29 0 29
303 29 my ($p0, $p1) = @_;
304  
305 # XX the reversing makes this O(n). Use a better list
306 # representation.
307 29 my $ss = $p1->segments;
308 29 ▶ if (is_pair $ss) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_pair($ss)) { } 21 8 29
309 21 my $class = ref($p0);
310 21 my ($first, $rest) = $ss->first_and_rest;
311 (
312 21 ▶ $class->new(
Branch: 1 branch
BranchTrueFalseTotal
is_null($rest) ? : 11 10 21
313 $p0->rsegments->cons($first),
314 is_null($rest) ? $p1->has_endslash : 1,
315 $p0->is_absolute
316 ),
317 $class->new($rest->reverse, $p1->has_endslash, '')
318 )
319 } else {
320 ()
321 8 }
322 }
323  
324 3 _END_
325  
« lib/FP/PureArray.pm ↑ Summary lib/FP/Abstract/Sequence.pm »