| 1 | package Octets::To::Unicode; | ||||||||||||||||||
| 2 | 2 | ▶ | use 5.008001; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 2 | our $VERSION = "0.06"; | |||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 2 | require Exporter; | |||||||||||||||||
| 10 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||
| 11 | our @EXPORT = | ||||||||||||||||||
| 12 | 2 | grep { *{ $Octets::To::Unicode::{$_} }{CODE} } keys %Octets::To::Unicode::; | |||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 2 | ▶ | use Encode qw//; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | #========================================= | ||||||||||||||||||
| 17 | #@category Кодировка | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | # Определяет кодировку. | ||||||||||||||||||
| 20 | 16 | ▶ | sub bohemy($) { | ||||||||||||||||
|
Subroutine
bohemy: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||
| 21 | 16 | my ($s) = @_; | |||||||||||||||||
| 22 | 16 | my $c = 0; | |||||||||||||||||
| 23 | 16 | ▶ | while ( $s =~ m![а-яё]+!gi ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 52 | my $len = length $&; | |||||||||||||||||
| 25 | 52 | ▶ | if ( $& =~ /^[А-ЯЁ][а-яё]+$/ ) { $c += $len } | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 26 | 16 | elsif ( $& =~ /^[А-ЯЁ]+$/ ) { $c += $len / 3 } | |||||||||||||||||
| 27 | 24 | elsif ( $& =~ /^[а-яё]+$/ ) { $c += $len / 3 } | |||||||||||||||||
| 28 | 6 | else { $c -= $len } | |||||||||||||||||
| 29 | } | ||||||||||||||||||
| 30 | 16 | $c; | |||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | # Определить кодировку и декодировать | ||||||||||||||||||
| 34 | 4 | ▶ | sub decode(@) { | ||||||||||||||||
|
Subroutine
decode: called CC=7 SCAR=20.1
Pod
documented
| |||||||||||||||||||
| 35 | 4 | my ( $octets, $encodings ) = @_; | |||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 4 | ▶ | return if !length $octets; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | 4 | ▶ | utf8::encode($octets) if utf8::is_utf8($octets); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | 4 | ▶ | $encodings //= [qw/utf-8 cp1251 koi8-r/]; | ||||||||||||||||
|
Condition50%1/2
$encodings //= ["utf-8", "cp1251", "koi8-r"]
MC/DC0%0/1
$encodings //= ["utf-8", "cp1251", "koi8-r"]
$encodings
Truth table50%1/2
A
A= $encodings
| |||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | my @x = grep length $_->[0], map { | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | # В случае ошибки Encode::decode помещает пустую строку в свой второй аргумент. Какой-то баг. | ||||||||||||||||||
| 46 | 4 | my $save = $octets; | |||||||||||||||||
| 47 | 12 | eval { [ Encode::decode( $_, $save, Encode::FB_CROAK ), $_ ] }; | |||||||||||||||||
| 48 | } @$encodings; | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 4 | my ( $unicode, $mem_encoding ); | |||||||||||||||||
| 51 | 0 | ▶ | ( $unicode, $mem_encoding ) = @{ $x[0] } if @x == 1; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 4 | ▶ | if ( @x > 1 ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | ( $unicode, $mem_encoding ) = | ||||||||||||||||||
| 55 | 4 | @{ ( sort { bohemy( $b->[0] ) <=> bohemy( $a->[0] ) } @x )[0] }; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 4 | ▶ | wantarray ? ( $unicode, $mem_encoding ) : $unicode; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 59 | } | ||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | # Определить кодировку | ||||||||||||||||||
| 62 | 0 | ▶ | sub detect(@) { | ||||||||||||||||
|
Subroutine
detect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 63 | 0 | ( decode @_ )[1]; | |||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | #========================================= | ||||||||||||||||||
| 67 | #@category Вспомогательные функции | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | # Ищет файлы в директориях рекурсивно | ||||||||||||||||||
| 70 | sub file_find($); | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 0 | ▶ | sub file_find($) { | ||||||||||||||||
|
Subroutine
file_find: not called CC=2 SCAR=12.2
Pod
documented
| |||||||||||||||||||
| 73 | 0 | my ($file) = @_; | |||||||||||||||||
| 74 | 0 | ▶ | if ( -d $file ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | 0 | $file =~ s!/$!!; | |||||||||||||||||
| 76 | 0 | ▶ | map file_find($_), <$file/*>; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | else { | ||||||||||||||||||
| 79 | 0 | $file; | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | } | ||||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | # Чтение бинарного файла | ||||||||||||||||||
| 84 | 0 | ▶ | sub file_read($) { | ||||||||||||||||
|
Subroutine
file_read: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||
| 85 | 0 | my ($file) = @_; | |||||||||||||||||
| 86 | 0 | ▶ | open my $f, "<", $file | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | or die "При открытии для чтения $file произошла ошибка: $!."; | ||||||||||||||||||
| 88 | 0 | read $f, my $buf, -s $f; | |||||||||||||||||
| 89 | 0 | close $f; | |||||||||||||||||
| 90 | 0 | $buf; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | # Запись в бинарный файл | ||||||||||||||||||
| 94 | 0 | ▶ | sub file_write(@) { | ||||||||||||||||
|
Subroutine
file_write: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 95 | 0 | my ( $file, $unicode ) = @_; | |||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 0 | ▶ | utf8::encode($unicode) if utf8::is_utf8($unicode); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 0 | ▶ | open my $f, ">", $file | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 100 | or die "При открытии для записи $file произошла ошибка: $!."; | ||||||||||||||||||
| 101 | 0 | print $f $unicode; | |||||||||||||||||
| 102 | 0 | close $f; | |||||||||||||||||
| 103 | 0 | return; | |||||||||||||||||
| 104 | } | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | # Определить кодировку и декодировать файл | ||||||||||||||||||
| 107 | 0 | ▶ | sub file_decode(@) { | ||||||||||||||||
|
Subroutine
file_decode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 108 | 0 | my ( $file, $encodings ) = @_; | |||||||||||||||||
| 109 | 0 | decode file_read $file, $encodings; | |||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | # Кодировать в указанной кодировке и записать в файл | ||||||||||||||||||
| 113 | 0 | ▶ | sub file_encode(@) { | ||||||||||||||||
|
Subroutine
file_encode: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 114 | 0 | my ( $file, $encoding, $unicode ) = @_; | |||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | ▶ | utf8::decode($unicode) if !utf8::is_utf8($unicode); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 0 | ▶ | $unicode = Encode::encode( $encoding, $unicode ) if defined $encoding; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 0 | file_write $file, $unicode; | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | #========================================= | ||||||||||||||||||
| 124 | #@category Тестирование файлов | ||||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | # Если расширение отсутствует, то 1-я строка должна содержать #!(.*)$interpreter | ||||||||||||||||||
| 127 | # * $exts — расширения: [qw/pl pm py/] | ||||||||||||||||||
| 128 | # * $interpreters — интерпретаторы: [qw/perl python/] | ||||||||||||||||||
| 129 | # Примечание: $interpreters проверяются только для файлов без расширений и только если пустое расширение было в $exts | ||||||||||||||||||
| 130 | 0 | ▶ | sub test_file(@) { | ||||||||||||||||
|
Subroutine
test_file: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||
| 131 | 0 | my ( $file, $exts, $interpreters ) = @_; | |||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 0 | ▶ | $exts //= []; | ||||||||||||||||
|
Condition0%0/2
$exts //= []
MC/DC0%0/1
$exts //= []
$exts
Truth table0%0/2
A
A= $exts
| |||||||||||||||||||
| 134 | 0 | ▶ | $interpreters //= []; | ||||||||||||||||
|
Condition0%0/2
$interpreters //= []
MC/DC0%0/1
$interpreters //= []
$interpreters
Truth table0%0/2
A
A= $interpreters
| |||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 0 | my ($ext) = $file =~ /\.([^.\/]*)$/; | |||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 0 | ▶ | return @$exts ? ( 1 == grep { $ext eq $_ } @$exts ) : 1 if defined $ext; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 0 | ▶ | open my $f, "<", $file or die "$file: $!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 141 | 0 | read $f, my $buf, 2; | |||||||||||||||||
| 142 | 0 | ▶ | close($f), return 0 if $buf ne "#!"; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 0 | ▶ | return 1 if !@$interpreters; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | 0 | my $first_line = <$f>; | |||||||||||||||||
| 147 | 0 | close $f; | |||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | 0 | $interpreters = join "|", @$interpreters; | |||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | 0 | ▶ | $first_line =~ /.*\b($interpreters)\b/ ? 1 : 0; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 152 | } | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | # Тестирует файлы на соответствия расширениям | ||||||||||||||||||
| 155 | 0 | ▶ | sub test_files(@) { | ||||||||||||||||
|
Subroutine
test_files: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||
| 156 | 0 | my ( $files, $exts, $interpreters ) = @_; | |||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | 0 | ▶ | $exts = [ split /,/, $exts ] if !ref $exts; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 159 | 0 | ▶ | $interpreters = [ split /,/, $interpreters ] if !ref $interpreters; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | 0 | grep test_file( $_, $exts, $interpreters ), @$files; | |||||||||||||||||
| 162 | } | ||||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | # Возвращает изменённые файлы в репозитории git | ||||||||||||||||||
| 165 | 0 | ▶ | sub change_files() { | ||||||||||||||||
|
Subroutine
change_files: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 166 | 0 | map { file_find $_ } | |||||||||||||||||
| 167 | 0 | map { s/^\s*[\w\?]+\s+//; $_ } grep { !/^\s*D / } split /\n/, | |||||||||||||||||
| 168 | `git status -s`; | ||||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | # Возвращает изменённые файлы в ветке | ||||||||||||||||||
| 172 | 0 | ▶ | sub change_files_in_branch() { | ||||||||||||||||
|
Subroutine
change_files_in_branch: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||
| 173 | 0 | grep length, split "\n", | |||||||||||||||||
| 174 | `git diff --name-only --diff-filter=AM origin/master...`; | ||||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | 2 | 1; | |||||||||||||||||
| 178 | __END__ | ||||||||||||||||||