line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Abbreviate; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
5682
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
542
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
0
|
70
|
my $class = shift; |
12
|
1
|
|
|
|
|
4
|
my $self = bless {}, $class; |
13
|
|
|
|
|
|
|
|
14
|
1
|
50
|
33
|
|
|
11
|
$self->{opts} = shift |
|
|
|
33
|
|
|
|
|
15
|
|
|
|
|
|
|
if @_ and ref($_[0]) and ref($_[0]) eq "HASH"; |
16
|
1
|
|
|
|
|
3
|
$self->{words} = [map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, lc] } @_]; |
|
8
|
|
|
|
|
14
|
|
|
16
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
22
|
|
17
|
1
|
|
|
|
|
3
|
$self->{string} = join("\n", @{ $self->{words} }) . "\n"; |
|
1
|
|
|
|
|
6
|
|
18
|
1
|
|
|
|
|
3
|
$self->{cache} = {}; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
3
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub expand { |
25
|
22
|
|
|
22
|
0
|
121
|
my ($self, $word) = @_; |
26
|
22
|
100
|
|
|
|
52
|
my $i = $self->{opts}{fold} ? "(?i)" : ""; |
27
|
22
|
|
50
|
|
|
428
|
my $m = ($self->{cache}{$i}{$word} ||= [ $self->{string} =~ /^($i\Q$word\E.*)\n/mg ]); |
28
|
22
|
50
|
|
|
|
118
|
return wantarray ? @$m : $m; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub unambiguous { |
33
|
2
|
|
|
2
|
0
|
599
|
my ($self) = @_; |
34
|
2
|
100
|
|
|
|
7
|
my $i = $self->{opts}{fold} ? "(?i)" : ""; |
35
|
2
|
|
|
|
|
2
|
my %abbr; |
36
|
|
|
|
|
|
|
|
37
|
2
|
|
|
|
|
3
|
WORD: for my $word (@{ $self->{words} }) { |
|
2
|
|
|
|
|
5
|
|
38
|
16
|
|
|
|
|
19
|
my $len = length $word; |
39
|
16
|
|
|
|
|
14
|
my $l = 0; |
40
|
|
|
|
|
|
|
|
41
|
16
|
|
|
|
|
37
|
while (++$l < $len) { |
42
|
38
|
|
|
|
|
54
|
my $w = substr $word, 0, $l; |
43
|
38
|
100
|
|
|
|
499
|
last if $self->{string} !~ /^$i\Q$w\E.*\n\Q$w\E/m; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
16
|
|
|
|
|
94
|
$abbr{$word} = [map substr($word, 0, $_), $l .. $len]; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
2
|
50
|
|
|
|
9
|
return wantarray ? %abbr : \%abbr; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub folding { |
54
|
2
|
|
|
2
|
0
|
74
|
my $self = shift; |
55
|
2
|
50
|
|
|
|
5
|
return $self->{opts}{fold} unless @_; |
56
|
2
|
|
|
|
|
6
|
$self->{opts}{fold} = shift; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
__END__ |