blib/lib/App/Uni.pm

filter: Statementstmt 64.3% 83 / 129 Branchbran 41.9% 26 / 62 Conditioncond 27.7% 5 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 59.0% 13 / 22 Podpod 0.0% 0 / 12 totaltotal 50.3% 128 / 254 CC 45 SCAR 28.0
CC
45
Cov
55%
do_hex_octets
30.0
chars_by_name
26.0
print_chars
23.2
CRAP 16.5
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package App::Uni 9.006;
4 # ABSTRACT: command-line utility to find or display Unicode characters
5  
6 #pod =encoding utf8
7 #pod
8 #pod =head1 NAME
9 #pod
10 #pod App::Uni - Command-line utility to grep UnicodeData.txt
11 #pod
12 #pod =head1 SYNOPSIS
13 #pod
14 #pod $ uni smiling face
15 #pod 263A ☺ WHITE SMILING FACE
16 #pod 263B ☻ BLACK SMILING FACE
17 #pod
18 #pod $ uni ☺
19 #pod 263A ☺ WHITE SMILING FACE
20 #pod
21 #pod # Only on Perl 5.14+
22 #pod $ uni wry
23 #pod 1F63C <U+1F63C> CAT FACE WITH WRY SMILE
24 #pod
25 #pod =head1 DESCRIPTION
26 #pod
27 #pod This module installs a simple program, F<uni>, that helps grepping through
28 #pod the Unicode database included in the current Perl 5 installation.
29 #pod
30 #pod For information on how to use F<uni> consult the L<uni> documentation.
31 #pod
32 #pod =head1 ACKNOWLEDGEMENTS
33 #pod
34 #pod This is a re-implementation of a program written by Audrey Tang in Taiwan. I
35 #pod used that program for years before deciding I wanted to add a few features,
36 #pod which I did by rewriting from scratch.
37 #pod
38 #pod That program, in turn, was a re-implementation of a same-named program Larry
39 #pod copied to me, which accompanied Audrey for years. However, that program was
40 #pod lost during a hard disk failure, so she coded it up from memory.
41 #pod
42 #pod Thank-you, Larry, for everything. ♡
43 #pod
44 #pod =cut
45  
46 1 ▶use 5.10.0; # for \v
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use charnames ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use Unicode::GCString;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 0 ▶sub _do_help {
Subroutine _do_help: not called CC=2 SCAR=17.9
56 0 my $class = shift;
57  
58 0 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
59 join qq{\n }, join(qq{\n}, @_, @_ ? "" : (), "usage:"),
60 "uni SEARCH-TERMS... - find codepoints with matching names or values",
61 "uni [-s] ONE-CHARACTER - print the codepoint and name of one character",
62 "uni -n SEARCH-TERMS... - find codepoints with matching names",
63 "uni -c STRINGS... - print out the codepoints in a string",
64 "uni -u CODEPOINTS... - look up and print hex codepoints",
65 "uni -x HEX-OCTETS... - given the sequence of octets, in hex, decode",
66 "",
67 "Other switches:",
68 " -8 - also show the UTF-8 bytes to encode\n";
69 }
70  
71 1 ▶sub run {
Subroutine run: called CC=11 SCAR=26.9
Pod undocumented
72 1 my ($class, @argv) = @_;
73  
74 1 my %opt;
75 {
76 1 my $exit;
77 1 local @ARGV = @argv;
78 GetOptions(
79 "c" => \$opt{explode},
80 "u" => \$opt{u_numbers},
81 "n" => \$opt{names},
82 "s" => \$opt{single},
83 "x" => \$opt{hex_octets},
84 "8" => \$opt{utf8},
85 "help|?" => \$opt{help},
86 1 );
87 1 @argv = @ARGV;
88 }
89  
90 1 ▶ $class->_do_help if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 1 1
91  
92 1 my $n = grep { $_ } @opt{qw(explode u_numbers names single hex_octets)};
93  
94 1 ▶ $class->_do_help("ERROR: only one mode switch allowed!") if $n > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 1 0 1 1
95  
96 1 ▶ $class->_do_help if ! @argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 0 1 1
97  
98 my $todo = $opt{explode} ? \&do_explode
99 : $opt{u_numbers} ? \&do_u_numbers
100 : $opt{names} ? \&do_names
101 : $opt{single} ? \&do_single
102 1 ▶ : $opt{hex_octets} ? \&do_hex_octets
Branch: 6 branches
BranchTrueFalseTotal
$opt{'explode'} ? : 0 1 1
$opt{'u_numbers'} ? : 0 1 1
$opt{'names'} ? : 0 1 1
$opt{'single'} ? : 0 1 1
$opt{'hex_octets'} ? : 0 1 1
@argv == 1 && length $argv[0] == 1 ? : 0 1 1
Condition33%1/3
@argv == 1 and length $argv[0] == 1
!ll&&!rl&&r
100
MC/DC0%0/2
@argv == 1 and length $argv[0] == 1
@argv == 1 length $argv[0] == 1
Truth table33%1/3
A and B
A= @argv == 1B= length $argv[0] == 1
ABresult
0X0
100
111
103 : @argv == 1 && length $argv[0] == 1 ? \&do_single
104 : \&do_dwim;
105  
106 1 $todo->(\@argv, \%opt);
107 }
108  
109 0 ▶sub do_single {
Subroutine do_single: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my @chars = grep { length } @{ $_[0] };
111 0 ▶ if (my @too_long = grep { length > 1 } @chars) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@too_long) = grep({length $_ > 1;} @chars)) 0 0 0
112 0 die "some arguments were too long for use with -s: @too_long\n";
113 }
114 0 print_chars(\@chars, $_[1]);
115 }
116  
117 0 ▶sub do_explode {
Subroutine do_explode: not called CC=1 SCAR=6.9
Pod undocumented
118 0 print_chars( explode_strings($_[0]), $_[1] );
119 }
120  
121 0 ▶sub do_hex_octets {
Subroutine do_hex_octets: not called CC=5 SCAR=34.0
Pod undocumented
122 0 my $string = '';
123 0 for my $hunk (@{ $_[0] }) {
124 0 ▶ die "input hunk $hunk is not an even-length hex string\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $hunk =~ /\A[0-9A-F]+\z/i and length($hunk) % 2 == 0 0 0 0
Condition0%0/3
$hunk =~ /\A[0-9A-F]+\z/i and length($hunk) % 2 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$hunk =~ /\A[0-9A-F]+\z/i and length($hunk) % 2 == 0
$hunk =~ /\A[0-9A-F]+\z/i length($hunk) % 2 == 0
Truth table0%0/3
A and B
A= $hunk =~ /\A[0-9A-F]+\z/iB= length($hunk) % 2 == 0
ABresult
0X0
100
111
125 unless $hunk =~ /\A[0-9A-F]+\z/i && length($hunk) % 2 == 0;
126  
127 0 $string .= chr oct "0x$_" for $hunk =~ /(..)/g;
128 }
129  
130 0 print_chars( explode_strings([ Encode::decode_utf8($string) ], $_[1]) );
131 }
132  
133 0 ▶sub explode_strings {
Subroutine explode_strings: not called CC=3 SCAR=24.8
Pod undocumented
134 0 my ($strings) = @_;
135  
136 0 my @chars;
137  
138 0 ▶ while (my $str = shift @$strings) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $str = shift @$strings) 0 0 0
139 0 push @chars, split '', $str;
140 0 ▶ push @chars, undef if @$strings;
Branch: 1 branch
BranchTrueFalseTotal
if @$strings 0 0 0
141 }
142  
143 0 return \@chars;
144 }
145  
146 0 ▶sub do_u_numbers {
Subroutine do_u_numbers: not called CC=1 SCAR=6.9
Pod undocumented
147 0 print_chars( chars_by_u_numbers($_[0]), $_[1] );
148 }
149  
150 1 ▶sub print_chars {
Subroutine print_chars: called CC=11 SCAR=31.4
Pod undocumented
151 1 my ($chars, $opt) = @_;
152  
153 my @to_print = $opt->{utf8}
154 0 ▶ ? (map {; [ $_ => defined && encode_utf8($_) ] } @$chars)
Condition0%0/3
defined $_ && &encode_utf8($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && &encode_utf8($_)
defined $_ &encode_utf8($_)
Truth table0%0/3
A && B
A= defined $_B= &encode_utf8($_)
ABresult
0X0
100
111
155 1 ▶ : (map {; [ $_ ] } @$chars);
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'utf8'} ? : 0 1 1
156  
157 1 my $width;
158 1 ▶ if ($opt->{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"utf8"}) 0 1 1
159 0 my $max_bytes = 0;
160 0 for my $todo (@to_print) {
161 0 $max_bytes = max($max_bytes, length $todo->[1]);
162 0 ▶ last if $max_bytes == 4; # maximum ever
Branch: 1 branch
BranchTrueFalseTotal
if $max_bytes == 4 0 0 0
163 }
164  
165 0 $width = 2 * $max_bytes + $max_bytes - 1;
166 }
167  
168 1 for my $todo (@to_print) {
169 22 my ($c, $u) = @$todo;
170  
171 0 ▶ unless (defined $c) { print "\n"; next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $c) 0 22 22
172  
173 # U+25CC DOTTED CIRCLE
174 22 ▶ my $c2 = Unicode::GCString->new(
Branch: 1 branch
BranchTrueFalseTotal
$c =~ /\pM/ ? : 0 22 22
175 $c =~ /\pM/ ? "\x{25CC}$c" : $c
176 );
177 22 my $l = $c2->columns;
178  
179 # I'm not 100% sure why I need this in all cases. It would make sense in
180 # some, since for example COMBINING GRAVE beginning a line becomes its
181 # own extended grapheme cluster (right?), but why does INVISIBLE TIMES at
182 # the beginning of a line take up a column despite being printing width
183 # zero? The world may never know. Until Tom tells me.
184 # -- rjbs, 2014-10-04
185 22 ▶ $l = 1 if $l == 0; # ???
Branch: 1 branch
BranchTrueFalseTotal
if $l == 0 0 22 22
186  
187 # Yeah, probably there's some insane %*0s$ invocation of printf to use
188 # here, but... just no. -- rjbs, 2014-10-04
189 22 (my $p = "$c2") =~ s/\v/ /g;
190 22 $p .= (' ' x (2 - $l));
191  
192 22 my $chr = ord($c);
193 22 my $name = charnames::viacode($chr);
194 my $utf8 = $opt->{utf8}
195 ? (sprintf " - %${width}s",
196 0 ▶ join q{ }, map {; sprintf '%02X', ord } split //, $u)
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'utf8'} ? : 0 22 22
197 : '';
198  
199 22 printf "%s- U+%05X%s - %s\n", $p, $chr, $utf8, $name;
200 }
201 }
202  
203 0 ▶sub chars_by_u_numbers {
Subroutine chars_by_u_numbers: not called CC=1 SCAR=6.9
Pod undocumented
204 0 my ($points) = @_;
205 0 my @chars = map {; /\A(?:u\+)?(.+)/; chr hex $1 } @$points;
206 0 return \@chars;
207 }
208  
209 0 ▶sub do_names {
Subroutine do_names: not called CC=1 SCAR=6.9
Pod undocumented
210 0 my ($terms, $opt) = @_;
211  
212 0 print_chars( chars_by_name( $terms ), $opt );
213 }
214  
215 1 ▶sub chars_by_name {
Subroutine chars_by_name: called CC=17 SCAR=32.6
Pod undocumented
216 1 my ($input_terms, $arg) = @_;
217 1 ▶ my @terms = map {; { pattern => s{\A/(.+)/\z}{$1} ? qr/$_/i : qr/\b$_\b/i } }
Branch: 1 branch
BranchTrueFalseTotal
s[\A/(.+)/\z][$1] ? : 0 2 2
218 @$input_terms;
219  
220 1 ▶ if ($arg && $arg->{match_codepoints}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg and $arg->{"match_codepoints"}) 1 0 1
Condition33%1/3
$arg and $arg->{"match_codepoints"}
!ll&&!rl&&r
001
MC/DC0%0/2
$arg and $arg->{"match_codepoints"}
$arg $arg->{"match_codepoints"}
Truth table33%1/3
A and B
A= $argB= $arg->{"match_codepoints"}
ABresult
0X0
100
111
221 1 for (0 .. $#terms) {
222 2 ▶ $terms[$_]{ord} = hex $input_terms->[$_]
Branch: 1 branch
BranchTrueFalseTotal
if $input_terms->[$_] =~ /\A[0-9A-Fa-f]+\z/ 1 1 2
223 if $input_terms->[$_] =~ /\A[0-9A-Fa-f]+\z/;
224 }
225 }
226  
227 1 state $corpus = do 'unicore/Name.pl';
228 1 ▶ unless (defined $corpus) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $corpus) 0 1 1
229 0 ▶ die "couldn't parse unicore/Name.pl: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
230 0 ▶ die "couldn't read unicore/Name.pl: $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
231 0 die "unicore/Name.pl returned undef";
232 }
233  
234 # https://github.com/perl/perl5/commit/b555069b72f93a232deba173dc7bf7892cfa5868
235 1 ▶ my ($entry_sep, $field_sep) = "$]" >= 5.031010 ? ("\n\n", "\n") : ("\n", "\t");
Branch: 1 branch
BranchTrueFalseTotal
"$]" >= 5.03101 ? : 1 0 1
236 1 my @entries = split $entry_sep, $corpus;
237 1 my @chars;
238  
239 my %seen;
240 1 ENTRY: for my $entry (@entries) {
241 35540 my $i = index($entry, $field_sep);
242 35540 ▶ next if rindex($entry, " ", $i) >= 0; # no sequences
Branch: 1 branch
BranchTrueFalseTotal
if rindex($entry, " ", $i) != -1 461 35079 35540
243  
244 35079 my $name = substr($entry, $i+1);
245 35079 my $ord = hex substr($entry, 0, $i);
246  
247 35079 for (@terms) {
248 next ENTRY unless $name =~ $_->{pattern}
249 35101 ▶ or defined $_->{ord} && $_->{ord} == $ord;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /$_->{"pattern"}/i or defined $_->{"ord"} and $_->{"ord"} == $ord 35057 44 35101
Condition50%3/6
$name =~ /$_->{"pattern"}/i or defined $_->{"ord"} and $_->{"ord"} == $ord
l!l&&r!l&&!r
44035057
defined $_->{"ord"} and $_->{"ord"} == $ord
!ll&&!rl&&r
3505700
MC/DC33%1/3
$name =~ /$_->{"pattern"}/i or defined $_->{"ord"} and $_->{"ord"} == $ord
$name =~ /$_->{"pattern"}/i defined $_->{"ord"} $_->{"ord"} == $ord
Truth table50%2/4
A or B and C
A= $name =~ /$_->{"pattern"}/iB= defined $_->{"ord"}C= $_->{"ord"} == $ord
ABCresult
1XX1
0111
00X0
0100
250 }
251  
252 22 my $c = chr hex substr $entry, 0, $i;
253 22 ▶ next if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 0 22 22
254 22 push @chars, chr hex substr $entry, 0, $i;
255 }
256  
257 1 return \@chars;
258 }
259  
260 0 ▶sub smerge {
Subroutine smerge: not called CC=1 SCAR=6.9
Pod undocumented
261 0 my %splat = map {; $_ => 1 } map { @$_ } @_;
262 0 return [ sort keys %splat ];
263 }
264  
265 1 ▶sub do_dwim {
Subroutine do_dwim: called CC=1 SCAR=0.0
Pod undocumented
266 1 my ($argv, $opt) = @_;
267 1 my $chars = chars_by_name($argv, { match_codepoints => 1 });
268 1 print_chars($chars, $opt);
269 }
270  
271 1 1;
272  
273 __END__
↑ Summary