Transliterator/Unaccent.pm

filter: Statementstmt 66.1% 41 / 62 Branchbran 35.0% 7 / 20 Conditioncond 57.1% 8 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 50.0% 7 / 14 Podpod 100% 3 / 3 totaltotal 56.5% 69 / 122 CC 19 SCAR 30.0
CC
19
Cov
58%
char_map
39.3
__ANON__
6.0
__ANON__
6.0
CRAP 20.1
↑ Summary Transliterator.pm »
1 package Text::Transliterator::Unaccent;
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = "1.06";
6  
7 2 ▶use Text::Transliterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Unicode::UCD qw(charinfo charscript charblock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Unicode::Normalize qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub char_map {
Subroutine char_map: called CC=15 SCAR=36.7
Pod documented
12 2 my $class = shift;
13  
14 2 my @all_ranges;
15 2 my $ignore_wide = 0;
16 2 my $ignore_upper = 0;
17 2 my $ignore_lower = 0;
18  
19 # decode arguments to get character ranges and boolean flags
20 2 ▶ while (my ($arg_name, $arg_val) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 2 2
21 0 my $ranges;
22  
23 my $handle_arg = {
24 0 ▶ script => sub { $ranges = charscript($arg_val)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$ranges = charscript($arg_val) or die "$arg_val is not a valid Unicode script" 0 0 0
25 or die "$arg_val is not a valid Unicode script" },
26 0 ▶ block => sub { $ranges = charblock($arg_val)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$ranges = charblock($arg_val) or die "$arg_val is not a valid Unicode block" 0 0 0
27 or die "$arg_val is not a valid Unicode block" },
28 0 ▶ ranges => sub { $ranges = $arg_val },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 ▶ wide => sub { $ignore_wide = !$arg_val },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 ▶ upper => sub { $ignore_upper = !$arg_val },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 ▶ lower => sub { $ignore_lower = !$arg_val },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 };
33 0 ▶ my $coderef = $handle_arg->{$arg_name}
Branch: 1 branch
BranchTrueFalseTotal
my $coderef = $handle_arg->{$arg_name} or die "invalid argument: $arg_name" 0 0 0
34 or die "invalid argument: $arg_name";
35 0 $coderef->();
36 0 ▶ push @all_ranges, @$ranges if $ranges;
Branch: 1 branch
BranchTrueFalseTotal
if $ranges 0 0 0
37 }
38  
39 # default
40 2 ▶ @all_ranges = @{charscript('Latin')} if !@all_ranges;
Branch: 1 branch
BranchTrueFalseTotal
unless @all_ranges 2 0 2
41  
42 # build the map
43 2 my %map;
44 2 foreach my $range (@all_ranges) {
45 72 my ($start, $end) = @$range;
46  
47 # iterate over characters in range
48 CHAR:
49 72 for my $c ($start .. $end) {
50  
51 # maybe drop that char under some conditions
52 2984 ▶ last CHAR if $ignore_wide and $c > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_wide and $c > 255 0 2984 2984
Condition33%1/3
$ignore_wide and $c > 255
!ll&&!rl&&r
298400
MC/DC0%0/2
$ignore_wide and $c > 255
$ignore_wide $c > 255
Truth table33%1/3
A and B
A= $ignore_wideB= $c > 255
ABresult
0X0
100
111
53 2984 ▶ next CHAR if $ignore_upper and chr($c) =~ /\p{Uppercase_Letter}/;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_upper and chr($c) =~ /\p{Uppercase_Letter}/ 0 2984 2984
Condition33%1/3
$ignore_upper and chr($c) =~ /\p{Uppercase_Letter}/
!ll&&!rl&&r
298400
MC/DC0%0/2
$ignore_upper and chr($c) =~ /\p{Uppercase_Letter}/
$ignore_upper chr($c) =~ /\p{Uppercase_Letter}/
Truth table33%1/3
A and B
A= $ignore_upperB= chr($c) =~ /\p{Uppercase_Letter}/
ABresult
0X0
100
111
54 2984 ▶ next CHAR if $ignore_lower and chr($c) =~ /\p{Lowercase_Letter}/;
Branch: 1 branch
BranchTrueFalseTotal
if $ignore_lower and chr($c) =~ /\p{Lowercase_Letter}/ 0 2984 2984
Condition33%1/3
$ignore_lower and chr($c) =~ /\p{Lowercase_Letter}/
!ll&&!rl&&r
298400
MC/DC0%0/2
$ignore_lower and chr($c) =~ /\p{Lowercase_Letter}/
$ignore_lower chr($c) =~ /\p{Lowercase_Letter}/
Truth table33%1/3
A and B
A= $ignore_lowerB= chr($c) =~ /\p{Lowercase_Letter}/
ABresult
0X0
100
111
55  
56 # get canonical decomposition (if any)
57 2984 my $canon = Unicode::Normalize::getCanon($c);
58  
59 # store into map
60 2984 ▶ if ($canon && length($canon) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($canon and length $canon > 1) 996 1988 2984
Condition100%3/3
$canon and length $canon > 1
!ll&&!rl&&r
19862996
MC/DC100%2/2
$canon and length $canon > 1
$canon length $canon > 1
Truth table100%3/3
A and B
A= $canonB= length $canon > 1
ABresult
0X0
100
111
61 # the unaccented char is the the base (first char) of the decomposition
62 996 my $base = substr $canon, 0, 1;
63 996 $map{chr($c)} = $base,
64 }
65 }
66 }
67  
68 2 return \%map;
69 }
70  
71 0 ▶sub char_map_descr {
Subroutine char_map_descr: not called CC=2 SCAR=17.9
Pod documented
72 0 my $class = shift;
73  
74 0 my $map = $class->char_map(@_);
75  
76 0 my $txt = "";
77 0 foreach my $k (sort {$a cmp $b} keys %$map) {
78 0 my $v = $map->{$k};
79 0 my $accented = ord($k);
80 0 my $base = ord($v);
81 $txt .= sprintf "U+%04x %-55s => U+%04x %s\n",
82 $accented,
83 charinfo($accented)->{name},
84 $base,
85 0 charinfo($base)->{name};
86 }
87 0 return $txt;
88 }
89  
90 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
91 2 my ($class, %args) = @_;
92  
93 2 ▶ my $modifiers = delete $args{modifiers} || "";
Condition100%2/2
delete $args{'modifiers'} || ''
l!l
11
MC/DC100%1/1
delete $args{'modifiers'} || ''
delete $args{'modifiers'}
Truth table100%2/2
A
A= delete $args{'modifiers'}
Aresult
11
00
94 2 my $map = $class->char_map(%args);
95 2 return Text::Transliterator->new($map, $modifiers);
96 }
97  
98 2 1; # End of Text::Transliterator::Unaccent
99  
100  
101 __END__
↑ Summary Transliterator.pm »