File Coverage

lib/mb/Encode.pm
Criterion Covered Total %
statement 79 83 95.1
branch 4 8 50.0
condition n/a
subroutine 35 35 100.0
pod 0 30 0.0
total 118 156 75.6


line stmt bran cond sub pod time code
1             package mb::Encode;
2             ######################################################################
3             #
4             # mb::Encode - provides MBCS encoder and decoder
5             #
6             # https://metacpan.org/dist/mb-Encode
7             #
8             # Copyright (c) 2021, 2022 INABA Hitoshi in a CPAN
9             ######################################################################
10              
11 4     4   13661 use 5.00503; # Universal Consensus 1998 for primetools
  4         30  
12             # use 5.008001; # Lancaster Consensus 2013 for toolchains
13              
14             $VERSION = '0.04';
15             $VERSION = $VERSION;
16              
17             require Exporter;
18             @ISA = qw(Exporter);
19             @EXPORT = qw();
20             @EXPORT_OK = qw(
21             to_big5 big5 by_big5
22             to_big5hkscs big5hkscs by_big5hkscs
23             to_cp932 cp932 by_cp932
24             to_cp936 cp936 by_cp936
25             to_cp949 cp949 by_cp949
26             to_cp950 cp950 by_cp950
27             to_eucjp eucjp by_eucjp
28             to_gbk gbk by_gbk
29             to_sjis sjis by_sjis
30             to_uhc uhc by_uhc
31             );
32              
33 4     4   27 use strict;
  4         14  
  4         181  
34             BEGIN {
35 4 50   4   22 if ($] >= 5.008_001) {
36 4     4   308 eval q{
  4     4   25  
  4         8  
  4         125  
  4         2242  
  4         42209  
  4         3695  
37             use warnings; # pmake.bat catches /^use .../
38             use Encode qw(); # pmake.bat catches /^use .../
39             };
40             }
41             else {
42 0         0 eval q{
43             use Jacode;
44             };
45             }
46             }
47              
48             #-------------------------------------------------------------------------------------
49             # return octets to any encoding
50 2     2 0 334 sub to_big5 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'big5', ); $oct }
  2         17577  
51 2     2 0 155 sub to_big5hkscs ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'big5-hkscs',); $oct }
  2         105  
52 2     2 0 144 sub to_cp936 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp936', ); $oct }
  2         23797  
53 2     2 0 138 sub to_cp949 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp949', ); $oct }
  2         21830  
54 2     2 0 148 sub to_cp950 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp950', ); $oct }
  2         124  
55 2     2 0 68 sub to_eucjp ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'euc-jp', ); $oct }
  2         95  
56 2     2 0 83 sub to_gbk ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'gbk', ); $oct }
  2         760  
57 2     2 0 121 sub to_sjis ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'sjis', ); $oct }
  2         598  
58 2     2 0 113 sub to_uhc ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'uhc', ); $oct }
  2         531  
59             sub to_cp932 ($) {
60 4     4 0 294 my $oct = $_[0];
61 4 50       21 if ($] >= 5.008_001) {
62 4         19 Encode::from_to($oct, 'utf8', 'cp932');
63             }
64             else {
65 0         0 Jacode::convert(\$oct, 'sjis', 'utf8');
66             }
67 4         30739 return $oct;
68             }
69              
70             #-------------------------------------------------------------------------------------
71             # shorthand of mb::Encode::to_XXXX
72 2     2 0 146 sub big5 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'big5', ); $oct }
  2         124  
73 2     2 0 62 sub big5hkscs ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'big5-hkscs',); $oct }
  2         113  
74 2     2 0 55 sub cp936 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp936', ); $oct }
  2         88  
75 2     2 0 57 sub cp949 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp949', ); $oct }
  2         88  
76 2     2 0 54 sub cp950 ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'cp950', ); $oct }
  2         89  
77 2     2 0 54 sub eucjp ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'euc-jp', ); $oct }
  2         90  
78 2     2 0 56 sub gbk ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'gbk', ); $oct }
  2         118  
79 2     2 0 53 sub sjis ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'sjis', ); $oct }
  2         115  
80 2     2 0 52 sub uhc ($) { Encode::from_to(my $oct=$_[0], 'utf8', 'uhc', ); $oct }
  2         111  
81             sub cp932 ($) {
82 4     4 0 206 my $oct = $_[0];
83 4 50       21 if ($] >= 5.008_001) {
84 4         17 Encode::from_to($oct, 'utf8', 'cp932');
85             }
86             else {
87 0         0 Jacode::convert(\$oct, 'sjis', 'utf8');
88             }
89 4         212 return $oct;
90             }
91              
92             #-------------------------------------------------------------------------------------
93             # return octets from any encoding
94 2     2 0 50 sub by_big5 ($) { Encode::from_to(my $oct=$_[0], 'big5', 'utf8',); $oct }
  2         122  
95 2     2 0 51 sub by_big5hkscs ($) { Encode::from_to(my $oct=$_[0], 'big5-hkscs', 'utf8',); $oct }
  2         104  
96 2     2 0 64 sub by_cp936 ($) { Encode::from_to(my $oct=$_[0], 'cp936', 'utf8',); $oct }
  2         84  
97 2     2 0 52 sub by_cp949 ($) { Encode::from_to(my $oct=$_[0], 'cp949', 'utf8',); $oct }
  2         82  
98 2     2 0 53 sub by_cp950 ($) { Encode::from_to(my $oct=$_[0], 'cp950', 'utf8',); $oct }
  2         84  
99 2     2 0 51 sub by_eucjp ($) { Encode::from_to(my $oct=$_[0], 'euc-jp', 'utf8',); $oct }
  2         79  
100 2     2 0 65 sub by_gbk ($) { Encode::from_to(my $oct=$_[0], 'gbk', 'utf8',); $oct }
  2         106  
101 2     2 0 62 sub by_sjis ($) { Encode::from_to(my $oct=$_[0], 'sjis', 'utf8',); $oct }
  2         114  
102 2     2 0 66 sub by_uhc ($) { Encode::from_to(my $oct=$_[0], 'uhc', 'utf8',); $oct }
  2         105  
103             sub by_cp932 ($) {
104 4     4 0 110 my $oct = $_[0];
105 4 50       19 if ($] >= 5.008_001) {
106 4         15 Encode::from_to($oct, 'cp932', 'utf8');
107             }
108             else {
109 0         0 Jacode::convert(\$oct, 'utf8', 'sjis');
110             }
111 4         175 return $oct;
112             }
113              
114             1;
115              
116             __END__