1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
package Code::TidyAll::Zglob;
use strict;
use warnings 'all', FATAL => 'recursion';
use 5.008008;
our $VERSION = '0.83';
use base qw(Exporter);
our @EXPORT = qw(zglob);
use File::Basename;
our $SEPCHAR = '/';
our $NOCASE = $^O =~ /^(?:MSWin32|VMS|os2|dos|riscos|MacOS|darwin)$/ ? 1 : 0;
our $DIRFLAG = \"DIR?";
our $DEEPFLAG = \"**";
our $PARENTFLAG = \"..";
our $DEBUG = 0;
our $STRICT_LEADING_DOT = 1;
our $STRICT_WILDCARD_SLASH = 1;
sub zglob {
my ($pattern) = @_;
#dbg("FOLDING: $pattern");
# support ~tokuhirom/
if ($^O eq 'MSWin32') {
require Win32;
$pattern =~ s!^(\~[^$SEPCHAR]*)!Win32::GetLongPathName([glob($1)]->[0])!e;
} else {
$pattern =~ s!^(\~[^$SEPCHAR]*)![glob($1)]->[0]!e;
}
my ($node, $matcher) = glob_prepare_pattern($pattern);
# $node : \0 if absolute path, \1 if relative.
#dbg("pattern: ", $node, $matcher);
return _rec($node, $matcher, []);
}
sub dbg(@) {
return unless $DEBUG;
my ($pkg, $filename, $line, $sub) = caller(1);
my $i = 0;
while (caller($i++)) { 1 }
my $msg;
$msg .= ('-' x ($i-5));
$msg .= " [$sub] ";
for (@_) {
$msg .= ' ';
if (not defined $_) {
$msg .= '<<undef>>';
} elsif (ref $_) {
require Data::Dumper;
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
$msg .= Data::Dumper::Dumper($_);
} else {
$msg .= $_;
}
}
$msg .= " at $filename line $line\n";
print($msg);
}
sub _recstar {
my ($node, $matcher) = @_;
#dbg("recstar: ", $node, $matcher, $seed);
return (
_rec( $node, $matcher ),
(
map { _recstar( $_, $matcher ) }
glob_fs_fold( $node, qr{^[^.].*$}, 1 )
)
);
}
sub _rec {
my ($node, $matcher) = @_;
# $matcher: ArrayRef[Any]
my ($current, @rest) = @{$matcher};
if (!defined $current) {
#dbg("FINISHED");
return ();
} elsif (ref($current) eq 'SCALAR' && $current == $DEEPFLAG) {
#dbg("** mode");
return _recstar($node, \@rest);
} elsif (ref($current) eq 'SCALAR' && $current == $PARENTFLAG) {
if (ref($node) eq 'SCALAR' && $$node eq 1) { #t
die "You cannot get a parent directory of root dir.";
} elsif (ref($node) eq 'SCALAR' && $$node eq 0) { #f
return _rec("..", \@rest);
} else {
return _rec("$node$SEPCHAR..", \@rest);
}
} elsif (@rest == 0) {
#dbg("file name");
# (folder proc seed node (car matcher) #f)
return glob_fs_fold($node, $current, 0);
} else {
return glob_fs_fold($node, $current, 1, \@rest);
}
}
# /^home$/ のような固定の文字列の場合に高速化をはかるための最適化予定地なので、とりあえず undef をかえしておいても問題がない
sub fixed_regexp_p {
return undef;
die "TBI"
}
# returns arrayref of seeds.
sub glob_fs_fold {
my ($node, $regexp, $non_leaf_p, $rest) = @_;
my $prefix = do {
if (ref $node eq 'SCALAR') {
if ($$node eq 1) { #t
$SEPCHAR
} elsif ($$node eq '0') { #f
'';
} else {
die "FATAL";
}
} elsif ($node !~ m{/$}) {
$node . '/';
} else {
$node;
}
};
dbg("prefix: $prefix");
dbg("regxp: ", $regexp);
if ($^O eq 'MSWin32' && ref $regexp eq 'SCALAR' && $$regexp =~ /^[a-zA-Z]\:$/) {
return _rec($$regexp . '/', $rest);
}
if (ref $regexp eq 'SCALAR' && $regexp == $DIRFLAG) {
if ($rest) {
return _rec($prefix, $rest);
} else {
return ($prefix);
}
# } elsif (my $string_portion = fixed_regexp_p($regexp)) { # /^path$/
# die "TBI";
# my $full = $prefix . $string_portion;
# if (-e $full && (!$non_leaf_p || -d $full)) {
# $proc->($full, $seed);
# } else {
# $proc;
# }
} else { # normal regexp
#dbg("normal regexp");
my $dir = do {
if (ref($node) eq 'SCALAR' && $$node eq 1) {
$SEPCHAR
} elsif (ref($node) eq 'SCALAR' && $$node eq 0) {
'.';
} else {
$node;
}
};
#dbg("dir: $dir");
opendir my $dirh, $dir or do {
#dbg("cannot open dir: $dir: $!");
return ();
};
my @ret;
while (defined(my $child = readdir($dirh))) {
next if $child eq '.' or $child eq '..';
my $full;
#dbg("non-leaf: ", $non_leaf_p);
if (($child =~ $regexp) && ($full = $prefix . $child) && (!$non_leaf_p || -d $full)) {
#dbg("matched: ", $regexp, $child, $full);
if ($rest) {
push @ret, _rec($full, $rest);
} else {
push @ret, $full;
}
# } else {
#dbg("Don't match: $child");
}
}
return @ret;
}
}
sub glob_prepare_pattern {
my ($pattern) = @_;
my @path = split $SEPCHAR, $pattern;
my $is_absolute = $path[0] eq '' ? 1 : 0;
if ($is_absolute) {
shift @path;
}
if ($^O eq 'MSWin32' && $path[0] =~ /^[a-zA-Z]\:$/) {
$is_absolute = 1;
}
@path = map {
if ($_ eq '**') {
$DEEPFLAG
} elsif ($_ eq '') {
$DIRFLAG
} elsif ($_ eq '.') {
()
} elsif ($_ eq '..') {
$PARENTFLAG
} elsif ($^O eq 'MSWin32' && $_ =~ '^[a-zA-Z]\:$') {
\$_
} else {
glob_to_regex($_) # TODO: replace with original implementation?
}
} @path;
return ( \$is_absolute, \@path );
}
# this is not a private function. '**' was handled at glob_prepare_pattern() function.
sub glob_to_regex {
my $glob = shift;
my $regex = glob_to_regex_string($glob);
return $NOCASE ? qr/^$regex$/i : qr/^$regex$/;
}
sub glob_to_regex_string {
my $glob = shift;
my ($regex, $in_curlies, $escaping);
local $_;
my $first_byte = 1;
for ($glob =~ m/(.)/gs) {
if ($first_byte) {
if ($STRICT_LEADING_DOT) {
$regex .= '(?=[^\.])' unless $_ eq '.';
}
$first_byte = 0;
}
if ($_ eq '/') {
$first_byte = 1;
}
if ($_ eq '.' || $_ eq '(' || $_ eq ')' || $_ eq '|' ||
$_ eq '+' || $_ eq '^' || $_ eq '$' || $_ eq '@' || $_ eq '%' ) {
$regex .= "\\$_";
}
elsif ($_ eq '*') {
$regex .= $escaping ? "\\*" :
$STRICT_WILDCARD_SLASH ? "[^/]*" : ".*";
}
elsif ($_ eq '?') {
$regex .= $escaping ? "\\?" :
$STRICT_WILDCARD_SLASH ? "[^/]" : ".";
}
elsif ($_ eq '{') {
$regex .= $escaping ? "\\{" : "(";
++$in_curlies unless $escaping;
}
elsif ($_ eq '}' && $in_curlies) {
$regex .= $escaping ? "}" : ")";
--$in_curlies unless $escaping;
}
elsif ($_ eq ',' && $in_curlies) {
$regex .= $escaping ? "," : "|";
}
elsif ($_ eq "\\") {
if ($escaping) {
$regex .= "\\\\";
$escaping = 0;
}
else {
$escaping = 1;
}
next;
}
else {
$regex .= $_;
$escaping = 0;
}
$escaping = 0;
}
return $regex;
}
1;
# ABSTRACT: A borged copy of File::Zglob
__END__
=pod
=encoding UTF-8
=head1 NAME
Code::TidyAll::Zglob - A borged copy of File::Zglob
=head1 VERSION
version 0.83
=head1 DESCRIPTION
File::Zglob has not been installable under Perl 5.26 and this has been an open
issue for 8+ months
(https://rt.cpan.org/Public/Bug/Display.html?id=120445). This module will go
away once File::Zglob is installable.
=head1 LICENSE
Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SUPPORT
Bugs may be submitted at L<https://github.com/houseabsolute/perl-code-tidyall/issues>.
=head1 SOURCE
The source code repository for Code-TidyAll can be found at L<https://github.com/houseabsolute/perl-code-tidyall>.
=head1 AUTHORS
=over 4
=item *
Jonathan Swartz <swartz@pobox.com>
=item *
Dave Rolsky <autarch@urth.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 - 2022 by Jonathan Swartz.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
The full text of the license can be found in the
F<LICENSE> file included with this distribution.
=cut
|