| 1 | package CCCP::Encode; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Text::Unidecode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | $CCCP::Encode::ToText = 0; | |||||||||||||||||||||||||||
| 12 | 1 | $CCCP::Encode::Entities = 'xml'; | |||||||||||||||||||||||||||
| 13 | 1 | $CCCP::Encode::CharMap = {}; | |||||||||||||||||||||||||||
| 14 | 1 | $CCCP::Encode::Regexp = '[^\p{Cyrillic}|\p{IsLatin}|\p{InBasic_Latin}]'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | my $xml_entities = { | |||||||||||||||||||||||||||
| 17 | '&' => '&', | ||||||||||||||||||||||||||||
| 18 | '"' => '"', | ||||||||||||||||||||||||||||
| 19 | "'" => ''', | ||||||||||||||||||||||||||||
| 20 | '>' => '>', | ||||||||||||||||||||||||||||
| 21 | '<' => '<' | ||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | my $f = find_encoding('utf-8'); | |||||||||||||||||||||||||||
| 25 | 1 | my $t = undef; | |||||||||||||||||||||||||||
| 26 | 1 | ▶ | __err_msg("Unknown encoding 'utf-8'") unless defined $f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub utf2cyrillic { | ||||||||||||||||||||||||||
|
Subroutine
utf2cyrillic: not called CC=14 SCAR=53.5
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 0 | my ( $class, $str, $to ) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($CCCP::Encode::ToText and not UNIVERSAL::isa($CCCP::Encode::CharMap,'HASH') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$CCCP::Encode::ToText and not UNIVERSAL::isa($CCCP::Encode::CharMap, "HASH")
MC/DC0%0/2
$CCCP::Encode::ToText and not UNIVERSAL::isa($CCCP::Encode::CharMap, "HASH")
$CCCP::Encode::ToText
not UNIVERSAL::isa($CCCP::Encode::CharMap, "HASH")
Truth table0%0/3
A and not(B)
A= $CCCP::Encode::ToTextB= not UNIVERSAL::isa($CCCP::Encode::CharMap, "HASH")
| |||||||||||||||||||||||||||||
| 32 | 0 | __err_msg("\$CCCP::Encode::CharMap must be hash ref"); | |||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return undef unless defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | __err_msg("missing 'to' argument") unless $to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return $str if ($to =~ /^utf/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $t = find_encoding($to) unless ($t and $t->name eq $to); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$t and $t->name eq $to
MC/DC0%0/2
$t and $t->name eq $to
$t
$t->name eq $to
Truth table0%0/3
A and B
A= $tB= $t->name eq $to
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | __err_msg("Unknown encoding '$to'") unless defined $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | Encode::_utf8_off($str); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ($CCCP::Encode::ToText) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | # decode with html entities | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $CCCP::Encode::Entities ||= 'xml'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$CCCP::Encode::Entities ||= "xml"
MC/DC0%0/1
$CCCP::Encode::Entities ||= "xml"
$CCCP::Encode::Entities
Truth table0%0/2
A
A= $CCCP::Encode::Entities
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $str = $t->encode($f->decode($str), $CCCP::Encode::Entities eq 'xml' ? Encode::FB_XMLCREF : Encode::FB_HTMLCREF); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if ($CCCP::Encode::Entities eq 'xml') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $str =~ s/('|"|<|>|&(?!#x))/$xml_entities->{$1}/geo; | |||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | 0 | return $str; | |||||||||||||||||||||||||||
| 53 | } else { | ||||||||||||||||||||||||||||
| 54 | # decode in text mode | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | ($str = $f->decode($str)) =~ s/($CCCP::Encode::Regexp)/exists $CCCP::Encode::CharMap->{$1} ? $CCCP::Encode::CharMap->{$1} : unidecode($1)/sexg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | return $t->encode($str); | |||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub __err_msg { | ||||||||||||||||||||||||||
|
Subroutine
__err_msg: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 0 | my ($str) = @_; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | require Carp; | |||||||||||||||||||||||||||
| 64 | 0 | Carp::croak(__PACKAGE__.": ".$str); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||