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