File Coverage

blib/lib/Sub/CharacterProperties.pm
Criterion Covered Total %
statement 37 38 97.3
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 52 56 92.8


line stmt bran cond sub pod time code
1 1     1   598 use 5.008;
  1         4  
  1         44  
2 1     1   6 use strict;
  1         1  
  1         31  
3 1     1   4 use warnings;
  1         2  
  1         60  
4              
5             package Sub::CharacterProperties;
6             our $VERSION = '1.100860';
7             # ABSTRACT: Support for user-defined character properties
8 1     1   782 use Number::Rangify 'rangify';
  1         14096  
  1         59  
9 1     1   1123 use charnames ':full';
  1         48970  
  1         8  
10 1     1   1366 use parent 'Class::Accessor::Complex';
  1         416  
  1         6  
11             __PACKAGE__
12             ->mk_new
13             ->mk_array_accessors(qw(characters));
14              
15             sub get_ranges {
16 1     1 1 2 my $self = shift;
17              
18             # Convert the values to their actual Unicode character equivalent. For
19             # defining a character, we accept Unicode character names (the "..." part
20             # of the "\N{...}" notation) or hex code points (indicated by a leading
21             # "0x"; useful for characters that don't have a name).
22 1         2 my @characters;
23 1         4 for ($self->characters) {
24 70 50       152 if (/^0x(.*)$/) {
25 0         0 push @characters => sprintf '%c' => hex($1);
26             } else {
27 70         159 push @characters => sprintf '%c' => charnames::vianame($_);
28             }
29 70         18318 utf8::upgrade($_);
30             }
31 1         8 my @ranges = rangify(map { ord($_) } @characters);
  70         71  
32 1 50       691 wantarray ? @ranges : \@ranges;
33             }
34              
35             sub as_code {
36 1     1 1 111 my ($self, $name) = @_;
37 1 50       5 $name = 'InFoo' unless defined $name;
38 1         4 my $code = "sub $name { <<'END' }\n";
39 1         5 for my $range ($self->get_ranges) {
40 7         19 my ($lower, $upper) = $range->Size;
41 7 100       114 if ($lower == $upper) {
42 3         7 $code .= sprintf "%X\n", $lower;
43             } else {
44 4         16 $code .= sprintf "%X %X\n", $lower, $upper;
45             }
46             }
47 1         27 $code .= "END\n";
48 1         9 $code;
49             }
50             1;
51              
52              
53             __END__