lib/PersonName/Format/PP.pm

filter: Statementstmt 61.4% 67 / 109 Branchbran 35.4% 17 / 48 Conditioncond 37.5% 15 / 40 MC/DCmcdc 21.7% 5 / 23 Subroutinesub 80.0% 12 / 15 Podpod n/a totaltotal 49.3% 116 / 235 CC 42 SCAR 44.8
CC
42
Cov
50%
_first_grapheme
197.5
__ANON__
20.0
_get_name_script
8.0
CRAP 88.6
↑ Summary lib/PersonName/Format/Generic.pm »
1 ##----------------------------------------------------------------------------
2 ## Person Name Format - ~/lib/PersonName/Format/PP.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/07/17
7 ## Modified 2026/07/19
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package PersonName::Format::PP;
14 BEGIN
15 20 ▶{
Subroutine BEGIN: called CC=3 SCAR=14.6
16 20 ▶ use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶ if( $] < 5.013 )
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.013") 0 20 20
20 {
21 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 0 ▶ unless( defined( &warnings::register_categories ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &warnings::register_categories) 0 0 0
23 {
24 *warnings::_mkMask = sub
25 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 my $bit = shift( @_ );
27 0 my $mask = "";
28 0 vec( $mask, $bit, 1 ) = 1;
29 0 return( $mask );
30 0 };
31  
32 *warnings::register_categories = sub
33 0 ▶ {
Subroutine __ANON__: not called CC=4 SCAR=30.0
34 0 my @names = @_;
35 0 foreach my $name ( @names )
36 {
37 0 ▶ if( !defined( $warnings::Bits{ $name } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $warnings::Bits{$name}) 0 0 0
38 {
39 0 $warnings::Offsets{ $name } = $warnings::LAST_BIT;
40 0 $warnings::Bits{ $name } = warnings::_mkMask( $warnings::LAST_BIT++ );
41 0 $warnings::DeadBits{ $name } = warnings::_mkMask( $warnings::LAST_BIT++ );
42 0 ▶ if( length( $warnings::Bits{ $name } ) > length( $warnings::Bits{all} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $warnings::Bits{$name} > length $warnings::Bits{"all"}) 0 0 0
43 {
44 0 $warnings::Bits{all} .= "\x55";
45 0 $warnings::DeadBits{all} .= "\xaa";
46 }
47 }
48 }
49 0 };
50 }
51 }
52 20 warnings::register_categories( 'PersonName::Format' );
53 20 ▶ use vars qw( $VERSION %SCRIPT_NAME_TO_CODE );
Subroutine BEGIN: called CC=1 SCAR=0.0
54 20 ▶ use Unicode::UCD ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55 20 our( $VERSION ) = 'v0.1.0';
56 };
57  
58 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 # Populated on first use by _load_script_names().
62 20 our %SCRIPT_NAME_TO_CODE;
63  
64 20 my $SCRIPT_NAMES_LOADED = 0;
65 20 my $SCRIPT_CACHE = {};
66  
67 sub _first_grapheme
68 42 ▶{
Subroutine _first_grapheme: called CC=24 SCAR=52.9
69 42 my $value = shift( @_ );
70 42 ▶ if( !defined( $value ) || !length( $value ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 4 38 42
Condition100%3/3
defined $value and length $value
!ll&&!rl&&r
2238
MC/DC100%2/2
defined $value and length $value
defined $value length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
71 {
72 4 return( '' );
73 }
74  
75 # Extract the first grapheme cluster using \X.
76 # On Perl < 5.28, \X does not fully implement UAX #29 rev 29+:
77 # - Regional Indicator pairs (U+1F1E6..U+1F1FF): not joined by \X on Perl < 5.18
78 # - ZWJ sequences (U+200D + emoji): not joined by \X on Perl < 5.28
79 # We extend the result manually for those two cases.
80 38 my( $grapheme ) = $value =~ /\A(\X)/s;
81 38 ▶ return( '' ) unless( defined( $grapheme ) && length( $grapheme ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $grapheme and length $grapheme 0 38 38
Condition33%1/3
defined $grapheme and length $grapheme
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $grapheme and length $grapheme
defined $grapheme length $grapheme
Truth table33%1/3
A and B
A= defined $graphemeB= length $grapheme
ABresult
0X0
100
111
82  
83 # UAX #29 rule GB12/GB13: two adjacent Regional Indicators form one grapheme.
84 # If \X returned a single RI codepoint, check whether the next character is also RI.
85 38 ▶ if( length( $grapheme ) == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $grapheme == 1) 12 26 38
86 {
87 12 my $uv = ord( $grapheme );
88 12 ▶ if( $uv >= 0x1F1E6 && $uv <= 0x1F1FF )
Branch: 1 branch
BranchTrueFalseTotal
if ($uv >= 127462 and $uv <= 127487) 0 12 12
Condition33%1/3
$uv >= 127462 and $uv <= 127487
!ll&&!rl&&r
1200
MC/DC0%0/2
$uv >= 127462 and $uv <= 127487
$uv >= 127462 $uv <= 127487
Truth table33%1/3
A and B
A= $uv >= 127462B= $uv <= 127487
ABresult
0X0
100
111
89 {
90 0 my $rest = substr( $value, 1 );
91 0 ▶ if( length( $rest ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $rest) 0 0 0
92 {
93 0 my $next_uv = ord( substr( $rest, 0, 1 ) );
94 0 ▶ if( $next_uv >= 0x1F1E6 && $next_uv <= 0x1F1FF )
Branch: 1 branch
BranchTrueFalseTotal
if ($next_uv >= 127462 and $next_uv <= 127487) 0 0 0
Condition0%0/3
$next_uv >= 127462 and $next_uv <= 127487
!ll&&!rl&&r
000
MC/DC0%0/2
$next_uv >= 127462 and $next_uv <= 127487
$next_uv >= 127462 $next_uv <= 127487
Truth table0%0/3
A and B
A= $next_uv >= 127462B= $next_uv <= 127487
ABresult
0X0
100
111
95 {
96 0 $grapheme .= substr( $rest, 0, 1 );
97 }
98 }
99 0 return( $grapheme );
100 }
101 }
102  
103 # UAX #29 rule GB11: ZWJ sequences.
104 # \X handles ZWJ sequences inconsistently across Perl versions:
105 # Case B (most old Perls): \X consumes [base + ZWJ] but stops before the following emoji.
106 # Case C (very old Perls): \X consumes [base] and stops before ZWJ itself.
107 # We detect both by checking whether $grapheme ends with ZWJ (case B) or
108 # whether the next unconsumed character is ZWJ (case C), then continue consuming.
109 38 my $pos = length( $grapheme );
110 38 while(1)
111 {
112 38 ▶ my $zwj_at_end = ( $pos > 0 && ord( substr( $grapheme, -1, 1 ) ) == 0x200D );
Condition33%1/3
$pos > 0 && ord substr($grapheme, -1, 1) == 8205
!ll&&!rl&&r
0380
MC/DC0%0/2
$pos > 0 && ord substr($grapheme, -1, 1) == 8205
$pos > 0 ord substr($grapheme, -1, 1) == 8205
Truth table33%1/3
A && B
A= $pos > 0B= ord substr($grapheme, -1, 1) == 8205
ABresult
0X0
100
111
113 38 ▶ my $zwj_at_pos = ( $pos < length( $value ) && ord( substr( $value, $pos, 1 ) ) == 0x200D );
Condition33%1/3
$pos < length $value && ord substr($value, $pos, 1) == 8205
!ll&&!rl&&r
0380
MC/DC0%0/2
$pos < length $value && ord substr($value, $pos, 1) == 8205
$pos < length $value ord substr($value, $pos, 1) == 8205
Truth table33%1/3
A && B
A= $pos < length $valueB= ord substr($value, $pos, 1) == 8205
ABresult
0X0
100
111
114  
115 38 ▶ if( $zwj_at_end )
Branch: 2 branches
BranchTrueFalseTotal
if ($zwj_at_end) { } 0 38 38
elsif ($zwj_at_pos) { } 0 38 38
116 {
117 # Case B: ZWJ is the last char of what \X consumed.
118 # The character at $pos must exist and be non-ASCII for us to continue.
119 0 ▶ last unless( $pos < length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $pos < length $value 0 0 0
120 0 my $after = ord( substr( $value, $pos, 1 ) );
121 0 ▶ last unless( $after > 0x7F );
Branch: 1 branch
BranchTrueFalseTotal
unless $after > 127 0 0 0
122 0 $grapheme .= substr( $value, $pos, 1 );
123 0 $pos++;
124 }
125 elsif( $zwj_at_pos )
126 {
127 # Case C: ZWJ is the next unconsumed character.
128 # The character after ZWJ must exist and be non-ASCII.
129 0 ▶ last unless( $pos + 1 < length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $pos + 1 < length $value 0 0 0
130 0 my $after_zwj = ord( substr( $value, $pos + 1, 1 ) );
131 0 ▶ last unless( $after_zwj > 0x7F );
Branch: 1 branch
BranchTrueFalseTotal
unless $after_zwj > 127 0 0 0
132 0 $grapheme .= substr( $value, $pos, 2 );
133 0 $pos += 2;
134 }
135 else
136 {
137 38 last;
138 }
139  
140 # Consume any trailing Extend codepoints: emoji modifiers (U+1F3FB..U+1F3FF)
141 # and variation selectors (U+FE00..U+FE0F) that may follow a ZWJ base.
142 # Combining marks are already consumed by the preceding \X call.
143 0 ▶ while( $pos < length( $value ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $value) 0 0 0
144 {
145 0 my $uv = ord( substr( $value, $pos, 1 ) );
146 last unless(
147 0 ▶ ( $uv >= 0xFE00 && $uv <= 0xFE0F ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless $uv >= 65024 and $uv <= 65039 or $uv >= 127995 and $uv <= 127999 0 0 0
Condition0%0/9
$uv >= 65024 and $uv <= 65039 or $uv >= 127995 and $uv <= 127999
l!l&&r!l&&!r
000
$uv >= 65024 and $uv <= 65039
!ll&&!rl&&r
000
$uv >= 127995 and $uv <= 127999
!ll&&!rl&&r
000
MC/DC0%0/4
$uv >= 65024 and $uv <= 65039 or $uv >= 127995 and $uv <= 127999
$uv >= 65024 $uv <= 65039 $uv >= 127995 $uv <= 127999
Truth table0%0/7
A and B or C and D
A= $uv >= 65024B= $uv <= 65039C= $uv >= 127995D= $uv <= 127999
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
148 ( $uv >= 0x1F3FB && $uv <= 0x1F3FF )
149 );
150 0 $grapheme .= substr( $value, $pos, 1 );
151 0 $pos++;
152 }
153 }
154  
155 38 return( $grapheme );
156 }
157  
158 sub _get_name_script
159 14 ▶{
Subroutine _get_name_script: called CC=8 SCAR=20.8
160 14 my $surname = shift( @_ );
161 14 my $given = shift( @_ );
162  
163 14 foreach my $value ( $surname, $given )
164 {
165 16 ▶ if( !defined( $value ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value and length $value) 1 15 16
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
1015
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
166 !length( $value ) )
167 {
168 1 next;
169 }
170  
171 # Do not use split(//u, ...) here: the /u modifier on split was only
172 # introduced in Perl 5.14 and produces warnings on earlier versions.
173 # The string is already a decoded Unicode character sequence, so
174 # split(//, ...) correctly splits on codepoint boundaries.
175 15 foreach my $char ( split( //, $value ) )
176 {
177 24 my $script = _script_code_for_uv( ord( $char ) );
178 24 ▶ if( $script eq 'Zyyy' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($script eq "Zyyy" or $script eq "Zinh" or $script eq "Zzzz") 11 13 24
Condition83%5/6
$script eq "Zyyy" or $script eq "Zinh" or $script eq "Zzzz"
l!l&&r!l&&!r
11013
$script eq "Zyyy" or $script eq "Zinh"
l!l&&r!l&&!r
10113
MC/DC66%2/3
$script eq "Zyyy" or $script eq "Zinh" or $script eq "Zzzz"
$script eq "Zyyy" $script eq "Zinh" $script eq "Zzzz"
Truth table75%3/4
A or B or C
A= $script eq "Zyyy"B= $script eq "Zinh"C= $script eq "Zzzz"
ABCresult
1XX1
01X1
0011
0000
179 $script eq 'Zinh' ||
180 $script eq 'Zzzz' )
181 {
182 11 next;
183 }
184 13 return( $script );
185 }
186 }
187  
188 1 return( 'Zzzz' );
189 }
190  
191 sub _load_script_names
192 0 ▶{
Subroutine _load_script_names: not called CC=2 SCAR=17.9
193 0 ▶ return if $SCRIPT_NAMES_LOADED;
Branch: 1 branch
BranchTrueFalseTotal
if $SCRIPT_NAMES_LOADED 0 0 0
194 0 require 'PersonName/Format/ScriptNames.pl';
195 0 $SCRIPT_NAMES_LOADED = 1;
196 }
197  
198 sub _script_code_for_uv
199 89 ▶{
Subroutine _script_code_for_uv: called CC=6 SCAR=19.5
200 89 my $uv = shift( @_ );
201 89 ▶ if( exists( $SCRIPT_CACHE->{ $uv } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SCRIPT_CACHE->{$uv}) 47 42 89
202 {
203 47 return( $SCRIPT_CACHE->{ $uv } );
204 }
205  
206 42 my $script_name = Unicode::UCD::charscript( $uv );
207 42 my $script = 'Zzzz';
208  
209 42 ▶ if( defined( $script_name ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script_name) 42 0 42
210 {
211 # Unicode::UCD::prop_value_aliases() was introduced in Perl 5.16.
212 # On earlier versions we fall back to our bundled ScriptNames.pl.
213 42 ▶ if( $] >= 5.016 )
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.016) { } 42 0 42
214 {
215 42 my @aliases = Unicode::UCD::prop_value_aliases( 'sc', $script_name );
216 42 ▶ $script = $aliases[0] || 'Zzzz';
Condition50%1/2
$aliases[0] || 'Zzzz'
l!l
420
MC/DC0%0/1
$aliases[0] || 'Zzzz'
$aliases[0]
Truth table50%1/2
A
A= $aliases[0]
Aresult
11
00
217 }
218 else
219 {
220 0 _load_script_names();
221 0 ▶ $script = $SCRIPT_NAME_TO_CODE{ $script_name } || 'Zzzz';
Condition0%0/2
$PersonName::Format::PP::SCRIPT_NAME_TO_CODE{$script_name} || 'Zzzz'
l!l
00
MC/DC0%0/1
$PersonName::Format::PP::SCRIPT_NAME_TO_CODE{$script_name} || 'Zzzz'
$PersonName::Format::PP::SCRIPT_NAME_TO_CODE{$script_name}
Truth table0%0/2
A
A= $PersonName::Format::PP::SCRIPT_NAME_TO_CODE{$script_name}
Aresult
11
00
222 }
223 }
224  
225 42 $SCRIPT_CACHE->{ $uv } = $script;
226 42 return( $script );
227 }
228  
229 20 1;
230 # NOTE: POD
231 __END__
↑ Summary lib/PersonName/Format/Generic.pm »