File Coverage

blib/lib/Unicode/Precis.pm
Criterion Covered Total %
statement 23 62 37.1
branch 0 28 0.0
condition 0 10 0.0
subroutine 8 11 72.7
pod 3 3 100.0
total 34 114 29.8


line stmt bran cond sub pod time code
1             #-*- perl -*-
2             #-*- coding: utf-8 -*-
3              
4             package Unicode::Precis;
5              
6 1     1   48344 use 5.008007; # Use Unicode 4.1.0 or later.
  1         3  
7 1     1   5 use strict;
  1         2  
  1         27  
8 1     1   6 use warnings;
  1         5  
  1         35  
9              
10 1     1   2377 use Encode qw(is_utf8 _utf8_on _utf8_off);
  1         22417  
  1         149  
11 1     1   1231 use Unicode::BiDiRule qw(check);
  1         1229  
  1         110  
12 1     1   1387 use Unicode::Normalize qw(normalize);
  1         3786  
  1         139  
13 1     1   1255 use Unicode::Precis::Preparation qw(prepare FreeFormClass IdentifierClass);
  1         65776  
  1         92  
14             use Unicode::Precis::Utils
15 1     1   555 qw(compareExactly decomposeWidth foldCase mapSpace);
  1         2  
  1         653  
16              
17             our $VERSION = '1.100';
18             $VERSION = eval $VERSION; # see L
19              
20             sub new {
21 0     0 1   my $class = shift;
22 0           my %options = @_;
23              
24 0           bless {%options} => $class;
25             }
26              
27             sub compare {
28 0     0 1   my $self = shift;
29 0           my $stringA = $self->enforce(shift);
30 0           my $stringB = $self->enforce(shift);
31              
32 0           return compareExactly($stringA, $stringB);
33             }
34              
35             sub enforce {
36 0     0 1   my ($self, $string) = @_;
37              
38 0 0         return undef unless defined $string;
39              
40 0 0 0       if (lc($self->{WidthMappingRule} || '') eq 'decomposition') {
41 0           decomposeWidth($string);
42             }
43 0   0       my $mappingrule = lc($self->{AdditionalMappingRule} || '');
44 0 0         if ($mappingrule =~ /\bmapspace/) {
45 0           mapSpace($string);
46             }
47 0 0         if ($mappingrule =~ /\bstripspace/) {
48 0           $string =~ s/\A\x20+//;
49 0           $string =~ s/\x20+\z//;
50             }
51 0 0         if ($mappingrule =~ /\bunifyspace/) {
52 0           $string =~ s/\x20\x20+/\x20/g;
53             }
54 0 0 0       if (lc($self->{CaseMappingRule} || '') eq 'fold') {
55 0           foldCase($string);
56             }
57 0 0         if ($self->{NormalizationRule}) {
58 0 0         if (is_utf8($string)) {
59             $string =
60 0           eval { normalize(uc $self->{NormalizationRule}, $string) };
  0            
61             } elsif ("\t" eq "\005") { # EBCDIC
62             $string = Encode::decode('UTF-8', $string);
63             $string =
64             eval { normalize(uc $self->{NormalizationRule}, $string) };
65             $string = Encode::encode('UTF-8', $string) if defined $string;
66             } else {
67 0           _utf8_on($string);
68             $string =
69 0           eval { normalize(uc $self->{NormalizationRule}, $string) };
  0            
70 0           _utf8_off($string);
71             }
72 0 0         return undef unless defined $string;
73             }
74 0 0 0       if (lc($self->{DirectionalityRule} || '') eq 'bidi') {
75 0 0         return undef unless defined check($string, 0);
76             }
77             my $stringclass = {
78             freeformclass => FreeFormClass,
79             identifierclass => IdentifierClass,
80 0   0       }->{lc($self->{StringClass} || '')}
81             || 0;
82             return undef
83 0 0         unless defined prepare($string, $stringclass);
84 0 0         if (ref $self->{OtherRule} eq 'CODE') {
85             return undef
86 0 0         unless defined($string = $self->{OtherRule}->($string));
87             }
88              
89 0           eval { $_[1] = $string };
  0            
90 0           $string;
91             }
92              
93             1;
94             __END__