line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package OpusVL::Text::Util; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
313846
|
use 5.014; |
|
4
|
|
|
|
|
9
|
|
4
|
4
|
|
|
4
|
|
14
|
use strict; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
57
|
|
5
|
4
|
|
|
4
|
|
10
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
206
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our @ISA = qw/Exporter/; |
8
|
|
|
|
|
|
|
our @EXPORT_OK = qw/truncate_text wrap_text string_to_id missing_array_items not_blank split_words line_split mask_text split_camel_case/; |
9
|
|
|
|
|
|
|
|
10
|
4
|
|
|
4
|
|
1465
|
use Array::Utils qw/intersect array_minus/; |
|
4
|
|
|
|
|
1064
|
|
|
4
|
|
|
|
|
211
|
|
11
|
4
|
|
|
4
|
|
16
|
use Scalar::Util qw/looks_like_number/; |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
2202
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# ABSTRACT: Simple text utilities |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub truncate_text |
20
|
|
|
|
|
|
|
{ |
21
|
4
|
|
|
4
|
1
|
103
|
my $string = shift; |
22
|
4
|
|
|
|
|
4
|
my $length = shift; |
23
|
|
|
|
|
|
|
|
24
|
4
|
100
|
|
|
|
13
|
return $string if length($string) < $length; |
25
|
3
|
100
|
|
|
|
47
|
if($string =~ /^(.{0,$length}\w\b)/) |
26
|
|
|
|
|
|
|
{ |
27
|
2
|
|
|
|
|
13
|
return $1 . '...'; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
else |
30
|
|
|
|
|
|
|
{ |
31
|
1
|
|
|
|
|
5
|
return substr $string, 0, $length; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub wrap_text |
37
|
|
|
|
|
|
|
{ |
38
|
7
|
|
|
7
|
1
|
13
|
my $string = shift; |
39
|
7
|
|
|
|
|
6
|
my $length = shift; |
40
|
7
|
|
100
|
|
|
22
|
my $separator = shift || "\n"; |
41
|
|
|
|
|
|
|
|
42
|
7
|
100
|
|
|
|
16
|
return $string if length($string) < $length; |
43
|
6
|
|
|
|
|
77
|
my @lines = $string =~ /\G(.{0,$length}\w\b|.*\w\b)\s*/g; |
44
|
6
|
|
|
|
|
30
|
return join $separator, @lines; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub string_to_id |
49
|
|
|
|
|
|
|
{ |
50
|
0
|
|
|
0
|
1
|
0
|
my $text = shift; |
51
|
0
|
|
|
|
|
0
|
my $r = $text =~ s/\s+/_/gr; |
52
|
0
|
|
|
|
|
0
|
$r =~ s/[^\w_]//g; |
53
|
0
|
|
|
|
|
0
|
return lc $r; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub line_split |
58
|
|
|
|
|
|
|
{ |
59
|
9
|
|
|
9
|
1
|
8
|
my $text = shift; |
60
|
9
|
|
|
|
|
44
|
my @lines = split /\r\n|\r|\n/, $text; |
61
|
|
|
|
|
|
|
return @lines |
62
|
9
|
|
|
|
|
21
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub missing_array_items |
66
|
|
|
|
|
|
|
{ |
67
|
2
|
|
|
2
|
1
|
109
|
my ($mandatory_fields, $actual_fields) = @_; |
68
|
|
|
|
|
|
|
|
69
|
2
|
|
|
|
|
9
|
my @mand_found = intersect(@$mandatory_fields, @$actual_fields); |
70
|
2
|
100
|
|
|
|
32
|
unless(scalar @mand_found == scalar @$mandatory_fields) |
71
|
|
|
|
|
|
|
{ |
72
|
1
|
|
|
|
|
4
|
my @missing = array_minus(@$mandatory_fields, @mand_found); |
73
|
1
|
|
|
|
|
14
|
return \@missing; |
74
|
|
|
|
|
|
|
} |
75
|
1
|
|
|
|
|
6
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub not_blank |
80
|
|
|
|
|
|
|
{ |
81
|
9
|
|
|
9
|
1
|
13
|
my $value = shift; |
82
|
9
|
100
|
|
|
|
24
|
return 1 if $value; |
83
|
5
|
100
|
|
|
|
15
|
if(looks_like_number($value)) |
84
|
|
|
|
|
|
|
{ |
85
|
2
|
|
|
|
|
5
|
return 1; |
86
|
|
|
|
|
|
|
} |
87
|
3
|
|
|
|
|
9
|
return 0; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub split_words |
92
|
|
|
|
|
|
|
{ |
93
|
5
|
|
|
5
|
1
|
6
|
my $value = shift; |
94
|
5
|
|
|
|
|
24
|
return split /[\s,]+/, $value; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub mask_text |
99
|
|
|
|
|
|
|
{ |
100
|
5
|
|
|
5
|
1
|
133
|
my ($fill_char, $regex, $text) = @_; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# fudge the regex. |
103
|
5
|
|
|
|
|
102
|
my @values = $text =~ /$regex/s; |
104
|
5
|
100
|
|
|
|
13
|
unless(@values) |
105
|
|
|
|
|
|
|
{ |
106
|
1
|
|
|
|
|
6
|
return $fill_char x length($text); |
107
|
|
|
|
|
|
|
} |
108
|
4
|
|
|
|
|
4
|
my @chars; |
109
|
4
|
|
|
|
|
2
|
my $group = 1; |
110
|
4
|
|
|
|
|
4
|
my $group_inserted = 0; |
111
|
4
|
|
|
|
|
10
|
my $start = $-[$group]; |
112
|
4
|
|
|
|
|
19
|
my $end = $+[$group]; |
113
|
4
|
|
|
|
|
13
|
for (my $i = 0; $i < length($text); $i++) |
114
|
|
|
|
|
|
|
{ |
115
|
69
|
100
|
|
|
|
76
|
if($i > $end) |
116
|
|
|
|
|
|
|
{ |
117
|
2
|
|
|
|
|
2
|
$group++; |
118
|
2
|
|
|
|
|
2
|
$group_inserted = 0; |
119
|
2
|
50
|
|
|
|
4
|
if($group > scalar @values) |
120
|
|
|
|
|
|
|
{ |
121
|
0
|
|
|
|
|
0
|
$start = length($text) + 1; |
122
|
0
|
|
|
|
|
0
|
$end = $start +1; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else |
125
|
|
|
|
|
|
|
{ |
126
|
2
|
|
|
|
|
3
|
$start = $-[$group]; |
127
|
2
|
|
|
|
|
3
|
$end = $+[$group]; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
69
|
100
|
100
|
|
|
136
|
if($i >= $start && $i < $end) |
131
|
|
|
|
|
|
|
{ |
132
|
53
|
100
|
|
|
|
99
|
unless($group_inserted) |
133
|
|
|
|
|
|
|
{ |
134
|
6
|
|
|
|
|
3
|
$group_inserted = 1; |
135
|
6
|
|
|
|
|
28
|
push @chars, $values[$group-1]; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else |
139
|
|
|
|
|
|
|
{ |
140
|
16
|
|
|
|
|
20
|
push @chars, $fill_char; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
4
|
|
|
|
|
25
|
return join '', @chars; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub split_camel_case |
148
|
|
|
|
|
|
|
{ |
149
|
6
|
|
|
6
|
1
|
8
|
my $text = shift; |
150
|
|
|
|
|
|
|
# a lot of the credit for this goes to the Perl Monks. |
151
|
6
|
|
|
|
|
38
|
my @parts = $text =~ /[A-Z0-9](?:[A-Z0-9]+|[^A-Z0-9]*)(?=$|[A-Z0-9])/g; |
152
|
6
|
|
|
|
|
26
|
return \@parts; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; # End of OpusVL::Text::Util |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
__END__ |