| 1 | package Unicode::CheckUTF8::PP; | ||||||||||||||||||||||||||||||
| 2 | 3 | $Unicode::CheckUTF8::PP::VERSION = '0.003'; | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Pure Perl implementation of Unicode::CheckUTF8 | ||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | 3 | our @EXPORT = qw(); | |||||||||||||||||||||||||||||
| 10 | 3 | our @EXPORT_OK = qw(is_utf8); | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||
| 12 | 3 | my @validSingleByte = ( | |||||||||||||||||||||||||||||
| 13 | 0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 14 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | ||||||||||||||||||||||||||||||
| 15 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | ||||||||||||||||||||||||||||||
| 16 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | ||||||||||||||||||||||||||||||
| 17 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 18 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 19 | 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | ||||||||||||||||||||||||||||||
| 20 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0 | ||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||
| 23 | 3 | my @trailingBytesForUTF8 = ( | |||||||||||||||||||||||||||||
| 24 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 25 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 26 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 27 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 28 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 29 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||||||||||||||||||||||||||||||
| 30 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, | ||||||||||||||||||||||||||||||
| 31 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 | ||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||
| 34 | 47096 | ▶ | my $_isLegalUTF8 = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=21 SCAR=37.2
| |||||||||||||||||||||||||||||||
| 35 | 47096 | my ($bytes, $length) = @_; | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||
| 37 | 47096 | my $i = $length; | |||||||||||||||||||||||||||||
| 38 | 47096 | ▶ | if ( $length <= 4 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 39 | 47095 | my $c; | |||||||||||||||||||||||||||||
| 40 | 47095 | ▶ | if ( $length == 4 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 41 | 1 | $c = $bytes->[--$i]; | |||||||||||||||||||||||||||||
| 42 | 1 | ▶ | return 0 unless defined $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | return 0 if $c < 0x80 || $c > 0xBF; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c < 128 or $c > 191
MC/DC0%0/2
$c < 128 or $c > 191
$c < 128
$c > 191
Truth table33%1/3
A or B
A= $c < 128B= $c > 191
| |||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||
| 46 | 47095 | ▶ | if ( $length >= 3 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 47 | 502 | $c = $bytes->[--$i]; | |||||||||||||||||||||||||||||
| 48 | 502 | ▶ | return 0 unless defined $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 49 | 502 | ▶ | return 0 if $c < 0x80 || $c > 0xBF; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c < 128 or $c > 191
MC/DC0%0/2
$c < 128 or $c > 191
$c < 128
$c > 191
Truth table33%1/3
A or B
A= $c < 128B= $c > 191
| |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||
| 52 | 47095 | ▶ | if ( $length >= 2 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 53 | 771 | $c = $bytes->[--$i]; | |||||||||||||||||||||||||||||
| 54 | 771 | ▶ | return 0 unless defined $c; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 55 | 770 | ▶ | return 0 if $c > 0xBF; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( $bytes->[$i] == 0xE0 ) { return 0 if $c < 0xA0; } | ||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | elsif ( $bytes->[$i] == 0xF0 ) { return 0 if $c < 0x90; } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | elsif ( $bytes->[$i] == 0xF0 ) { return 0 if $c > 0x8F; } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 60 | 770 | ▶ | else { return 0 if $c < 0x80; } | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | 47094 | ▶ | if ( $length >= 1 ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 64 | 47094 | return $validSingleByte[ $bytes->[0] ]; | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||||
| 68 | 1 | return 0; | |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | 0 | return 1; | |||||||||||||||||||||||||||||
| 72 | 3 | }; | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||
| 74 | 176 | ▶ | my $_isLegalUTF8String = sub { | ||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||
| 75 | 176 | my ($str) = @_; | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||
| 77 | 176 | my @bytes = unpack 'U*', $str; | |||||||||||||||||||||||||||||
| 78 | 176 | my $len = @bytes; | |||||||||||||||||||||||||||||
| 79 | 176 | my $l = 0; | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||
| 81 | 176 | my $i = 0; | |||||||||||||||||||||||||||||
| 82 | 176 | ▶ | while ( $i < $len ) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 83 | 47097 | my $byte = $bytes[$i]; | |||||||||||||||||||||||||||||
| 84 | 47097 | my $length = $trailingBytesForUTF8[$byte] + 1; | |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||
| 86 | # check for early termination of string | ||||||||||||||||||||||||||||||
| 87 | 47097 | foreach my $j ( 1 .. $length - 1 ) { | |||||||||||||||||||||||||||||
| 88 | 1279 | ▶ | return 0 unless defined $bytes[$j]; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 89 | 1278 | ▶ | return 0 if $bytes[$j] == 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | 47096 | ▶ | return 0 unless | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 93 | $_isLegalUTF8->([@bytes[$i..$i+$length-1]], $length); | ||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||
| 95 | 47032 | $l = $bytes[$i]; | |||||||||||||||||||||||||||||
| 96 | 47032 | $i += $length; | |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||
| 99 | 111 | ▶ | return ($l || 0) == ($bytes[-1] || 0) ? 1 : 0; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$l || 0
$bytes[-1] || 0
MC/DC0%0/1
$l || 0
$l
MC/DC0%0/1
$bytes[-1] || 0
$bytes[-1]
Truth table50%1/2
A
A= $l
Truth table50%1/2
A
A= $bytes[-1]
| |||||||||||||||||||||||||||||||
| 100 | 3 | }; | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||
| 102 | 176 | ▶ | sub is_utf8 { | ||||||||||||||||||||||||||||
|
Subroutine
is_utf8: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 103 | 176 | return $_isLegalUTF8String->(@_); | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||
| 106 | 3 | 1; | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||||