| 1 | #-*- perl -*- | ||
| 2 | #-*- coding: utf-8 -*- | ||
| 3 | |||
| 4 | package Unicode::Precis; | ||
| 5 | |||
| 6 | 1 | ▶ | use 5.008007; # Use Unicode 4.1.0 or later. |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use Encode qw(is_utf8 _utf8_on _utf8_off); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Unicode::BiDiRule qw(check); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Unicode::Normalize qw(normalize); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Unicode::Precis::Preparation qw(prepare FreeFormClass IdentifierClass); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | use Unicode::Precis::Utils | ||
| 15 | 1 | ▶ | qw(compareExactly decomposeWidth foldCase mapSpace); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 16 | |||
| 17 | our $VERSION = '1.000'; | ||
| 18 | $VERSION = eval $VERSION; # see L<perlmodstyle> | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $class = shift; | ||
| 22 | my %options = @_; | ||
| 23 | |||
| 24 | bless {%options} => $class; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub compare { | ||
| 28 | my $self = shift; | ||
| 29 | my $stringA = $self->enforce(shift); | ||
| 30 | my $stringB = $self->enforce(shift); | ||
| 31 | |||
| 32 | return compareExactly($stringA, $stringB); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub enforce { | ||
| 36 | my ($self, $string) = @_; | ||
| 37 | |||
| 38 | return undef unless defined $string; | ||
| 39 | |||
| 40 | if (lc($self->{WidthMappingRule} || '') eq 'decomposition') { | ||
| 41 | decomposeWidth($string); | ||
| 42 | } | ||
| 43 | my $mappingrule = lc($self->{AdditionalMappingRule} || ''); | ||
| 44 | if ($mappingrule =~ /\bmapspace/) { | ||
| 45 | mapSpace($string); | ||
| 46 | } | ||
| 47 | if ($mappingrule =~ /\bstripspace/) { | ||
| 48 | $string =~ s/\A\x20+//; | ||
| 49 | $string =~ s/\x20+\z//; | ||
| 50 | } | ||
| 51 | if ($mappingrule =~ /\bunifyspace/) { | ||
| 52 | $string =~ s/\x20\x20+/\x20/g; | ||
| 53 | } | ||
| 54 | if (lc($self->{CaseMappingRule} || '') eq 'fold') { | ||
| 55 | foldCase($string); | ||
| 56 | } | ||
| 57 | if ($self->{NormalizationRule}) { | ||
| 58 | if (is_utf8($string)) { | ||
| 59 | $string = | ||
| 60 | eval { normalize(uc $self->{NormalizationRule}, $string) }; | ||
| 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 | _utf8_on($string); | ||
| 68 | $string = | ||
| 69 | eval { normalize(uc $self->{NormalizationRule}, $string) }; | ||
| 70 | _utf8_off($string); | ||
| 71 | } | ||
| 72 | return undef unless defined $string; | ||
| 73 | } | ||
| 74 | if (lc($self->{DirectionalityRule} || '') eq 'bidi') { | ||
| 75 | return undef unless defined check($string, 0); | ||
| 76 | } | ||
| 77 | my $stringclass = { | ||
| 78 | freeformclass => FreeFormClass, | ||
| 79 | identifierclass => IdentifierClass, | ||
| 80 | }->{lc($self->{StringClass} || '')} | ||
| 81 | || 0; | ||
| 82 | return undef | ||
| 83 | unless defined prepare($string, $stringclass); | ||
| 84 | if (ref $self->{OtherRule} eq 'CODE') { | ||
| 85 | return undef | ||
| 86 | unless defined($string = $self->{OtherRule}->($string)); | ||
| 87 | } | ||
| 88 | |||
| 89 | eval { $_[1] = $string }; | ||
| 90 | $string; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; | ||
| 94 | __END__ | ||