File Coverage

blib/lib/App/Greple/Util.pm
Criterion Covered Total %
statement 25 58 43.1
branch 1 16 6.2
condition 0 6 0.0
subroutine 8 18 44.4
pod 0 1 0.0
total 34 99 34.3


line stmt bran cond sub pod time code
1             package App::Greple::Util;
2              
3 134     134   4027 use v5.24;
  134         623  
4 134     134   685 use warnings;
  134         218  
  134         7829  
5              
6 134     134   893 use Exporter 'import';
  134         261  
  134         8277  
7             our @EXPORT_OK = qw(shellquote);
8              
9 134     134   802 use App::Greple::Common;
  134         331  
  134         43194  
10              
11             ##
12             ## easy implementation. don't be serious.
13             ##
14             sub shellquote {
15 0     0 0 0 state $option = qr/-++[-:\w]+=/;
16 0         0 state $special = qr/[\s\\(){}\|\*?]/;
17 0         0 map { s/^(${option}?)(.*${special}.*)\z/$1\'$2\'/sr } @_;
  0         0  
18             }
19              
20             package #
21             UniqIndex
22             {
23 134     134   1057 use strict;
  134         244  
  134         3205  
24 134     134   602 use warnings;
  134         238  
  134         97102  
25              
26             sub new {
27 129     129   387 my $class = shift;
28 129         846 my $obj = bless {
29             HASH => {},
30             LIST => [],
31             COUNT => [],
32             }, $class;
33 129 50       1079 $obj->configure(@_) if @_;
34 129         431 $obj;
35             }
36              
37 0     0   0 sub hash { shift->{HASH} }
38 0     0   0 sub list { shift->{LIST} }
39 0     0   0 sub count { shift->{COUNT} }
40              
41             sub configure {
42 129     129   388 my $obj = shift;
43 129         588 while (@_ >= 2) {
44 258         1384 $obj->{$_[0]} = $_[1];
45 258         1149 splice @_, 0, 2;
46             }
47             }
48              
49             sub index {
50 0 0   0     my $opt = ref $_[0] eq 'HASH' ? shift : {};
51 0           my $obj = shift;
52 0           local $_ = shift;
53              
54 0   0       my $index = $obj->{HASH}->{$_} //= do {
55 0 0         if (my $prepare = $obj->{prepare}) {
56 0           for my $sub (@$prepare) {
57 0           $_ = $sub->call();
58             }
59             }
60 0 0         s/\n+//g if $obj->{ignore_newline};
61 0 0         s/\s+//g if $obj->{ignore_space};
62 0 0         s/\pS+//g if $obj->{ignore_symbol};
63 0 0         s/\pP+//g if $obj->{ignore_punct};
64 0 0         $_ = lc($_) if $obj->{ignore_case};
65 0   0       $obj->{HASH}->{$_} //= do {
66 0           my $list = $obj->{LIST};
67 0           push @$list, $_;
68 0           $#{ $list };
  0            
69             };
70             };
71              
72 0           $obj->{COUNT}->[$index] += 1;
73              
74 0           $index;
75             }
76             }
77              
78             package #
79             Indexer
80             {
81             sub new {
82 0     0     my $class = shift;
83 0           my $obj = { @_ };
84 0           bless $obj, $class;
85             }
86 0     0     sub index { shift->{index}->() }
87 0     0     sub reset { shift->{reset}->() }
88 0     0     sub block { shift->{block} }
89 0     0     sub reverse { shift->{reverse} }
90             }
91              
92             1;