File Coverage

blib/lib/Unicode/Japanese.pm
Criterion Covered Total %
statement 509 557 91.3
branch 333 502 66.3
condition 49 111 44.1
subroutine 64 64 100.0
pod 1 2 50.0
total 956 1236 77.3


line stmt bran cond sub pod time code
1             # -----------------------------------------------------------------------------
2             # Unicode::Japanese
3             # Unicode::Japanese::PurePerl
4             # -----------------------------------------------------------------------------
5             # $Id: Japanese_stub.pm 41491 2008-02-15 07:21:13Z hio $
6             # -----------------------------------------------------------------------------
7             package Unicode::Japanese::PurePerl;
8             package Unicode::Japanese;
9              
10 61     61   2809215 use strict;
  61         202  
  61         1790  
11 50     50   349 use vars qw($VERSION $XS_VERSION $PurePerl $xs_loaderror);
  50         218  
  50         9589  
12             $VERSION = '0.50';
13             $XS_VERSION = $VERSION;
14             $VERSION = eval $VERSION;
15              
16             # `use bytes' and `use Encode' if we are on perl-5.8.0 or later.
17             if( $] >= 5.008 )
18             {
19             my $evalerr;
20             {
21             local($SIG{__DIE__}) = 'DEFAULT';
22             local($@);
23 30     30   14727 eval 'use bytes;use Encode;';
  30     30   19352  
  30         209  
  30         21268  
  30         568313  
  30         2749  
24             $evalerr = $@;
25             }
26             $evalerr and CORE::die($evalerr);
27             }
28              
29             # -----------------------------------------------------------------------------
30             # import
31             #
32             sub import
33             {
34 29     29   524 my $pkg = shift;
35 29         288 my ($callerpkg) = caller;
36 29         382 my %exp =
37             (
38             '&unijp' => \&unijp,
39             );
40 29         219 my @na;
41 29 50       149 my @add = (grep{$_ eq ':all'} @_) ? keys %exp : ();
  5         28  
42 29         125 foreach(@_, @add)
43             {
44 5 50       100 $_ eq 'PurePerl' and $PurePerl=1, next;
45 5 100 66     48 if( $exp{$_} || $exp{'&'.$_} )
    50          
46             {
47 49     49   430 no strict 'refs';
  49         145  
  49         19328  
48 2         10 (my $name = $_) =~ s/^\W//;
49 2   33     6 my $obj = $exp{$_} || $exp{'&'.$_};
50 2         8 *{$callerpkg.'::'.$name} = $obj;
  2         15  
51             }elsif( $_ eq 'no_I18N_Japanese' )
52             {
53 4         49 $^H &= ~0x0f00_0000;
54             package Unicode::Japanese::PurePerl;
55 4         17 $^H &= ~0x0f00_0000;
56             package Unicode::Japanese;
57 4         20 next;
58             }
59 2         5 push(@na,$_);
60             }
61 29 100       35101 if( @na )
62             {
63             #use Carp;
64             #croak("invalid parameter (".join(',',@na).")");
65             }
66             }
67              
68             # -----------------------------------------------------------------------------
69             # DESTROY
70             #
71             sub DESTROY
72       1     {
73             }
74              
75             # -----------------------------------------------------------------------------
76             # load_xs.
77             # loading xs-subs.
78             # this method is called from new (through new=>_init_table=>load_xs)
79             #
80             sub load_xs
81             {
82             #print STDERR "load_xs\n";
83 28 50   28 0 112 if( $PurePerl )
84             {
85             #print STDERR "PurePerl mode\n";
86 1         3 $xs_loaderror = 'disabled';
87 1         6 return;
88             }
89             #print STDERR "XS mode\n";
90            
91 28         112 my $use_xs;
92             LoadXS:
93             {
94            
95             #print STDERR "* * bootstrap...\n";
96 28         58 eval q
97 28     28   352 {
  28     28   101  
  28         1307  
  28         166  
  28         57  
  28         2883  
  28         2874  
98             use strict;
99             require DynaLoader;
100             use vars qw(@ISA);
101             @ISA = qw(DynaLoader);
102             local($SIG{__DIE__}) = 'DEFAULT';
103             Unicode::Japanese->bootstrap($XS_VERSION);
104             };
105             #print STDERR "* * the trial has been done.\n";
106             #undef @ISA;
107 28 50       194 if( $@ )
108             {
109             #print STDERR "failed.\n";
110             #print STDERR "$@\n";
111 1         2 $use_xs = 0;
112 1         17 $xs_loaderror = $@;
113 1         13 undef $@;
114 1         3 last LoadXS;
115             }
116             #print STDERR "succeeded.\n";
117 28         81 $use_xs = 1;
118             eval q
119 28     134   2655 {
  1907         69961  
120             #print STDERR "overriding _s2u,_u2s\n";
121             do_memmap();
122             #print STDERR "memmap done\n";
123             END{ do_memunmap(); }
124             #print STDERR "binding xsubs has been done.\n";
125             };
126 28 50       152 if( $@ )
127             {
128             #print STDERR "error in the last part of operation to load XS.\n";
129 1         9 $xs_loaderror = $@;
130 1         9 CORE::die($@);
131             }
132              
133             #print STDERR "done.\n";
134             }
135              
136 28 50       92 if( $@ )
137             {
138 1         6 $xs_loaderror = $@;
139 1         8 CORE::die("Cannot load Unicode::Japanese of neither XS nor PurePerl side\n$@");
140             }
141 28 50       113 if( !$use_xs )
142             {
143             #print STDERR "no xs.\n";
144             eval q
145 1         5 {
146             sub do_memmap($){}
147             sub do_memunmap($){}
148             };
149             }
150 28 50       401 $xs_loaderror = '' if( !defined($xs_loaderror) );
151             #print STDERR "load_xs done.\n";
152             }
153              
154             # -----------------------------------------------------------------------------
155             # Unicode::Japanese->new();
156             # cache for char conversion.
157             # 2bytes.
158             # JIS C 6226-1979 \e$@
159             # JIS X 0208-1983 \e$B
160             # JIS X 0208-1990 \e&@\e$B
161             # JIS X 0212-1990 \e$(D
162             # 1byte.
163             # JIS ROMAN \e(J
164             # JIS ROMAN \e(H
165             # ASCII \e(B
166             # JIS KANA \e(I
167             # -----------------------------------------------------------------------------
168             # $unijp = Unicode::Japanese->new([$str,[$icode]]);
169             #
170             sub new
171             {
172 247     247 1 4304395 my $pkg = shift;
173 247         1179 my $this = {};
174              
175 247 50       792 if( defined($pkg) )
176             {
177 247         556 bless $this, $pkg;
178 247         1258 $this->_init_table;
179             }else
180             {
181 1         9 bless $this;
182 1         3 $this->_init_table;
183             }
184            
185 247 100       7342 @_ and $this->set(@_);
186            
187 247         3410 $this;
188             }
189              
190              
191             # -----------------------------------------------------------------------------
192             # _got_undefined_subroutine
193             # die with message 'undefiend subroutine'.
194             #
195             sub _got_undefined_subroutine
196             {
197 1     1   2 my $subname = pop;
198 1         8 CORE::die "Undefined subroutine \&$subname got called.\n";
199             }
200              
201             # -----------------------------------------------------------------------------
202             # AUTOLOAD
203             # AUTOLOAD of Unicode::Japanese.
204             # imports PurePerl methods.
205             #
206             AUTOLOAD
207             {
208             # load pure perl subs.
209 47     47   631 use vars qw($AUTOLOAD);
  47         143  
  47         4357  
210              
211             #print "AUTOLOAD... $AUTOLOAD\n";
212              
213 186 100   186   197863 if(!defined($Unicode::Japanese::xs_loaderror) )
214             {
215 28         1191 Unicode::Japanese::PurePerl::_init_table();
216 28 100       135 if( defined(&$AUTOLOAD) )
217             {
218 43     43   360 no strict 'refs';
  43         120  
  43         9549  
219 2         37 goto &$AUTOLOAD;
220             }
221             }
222              
223 185 50       318 my ($pkg, $subname) = do{
224 185         668 local($1, $2);
225 185         2050 $AUTOLOAD =~ /^(.*)::(\w+)$/
226             } or got_undefined_subroutine($AUTOLOAD);
227              
228 185         482 my $pppkg = $pkg . '::PurePerl';
229 185         394 my $ppsubname = $pkg . '::PurePerl::' . $subname;
230 185 100       1148 if( !defined(&$ppsubname) )
231             {
232 155         306 my $save = $@;
233 155         434 my @BAK = @_;
234 155         645 $pppkg->_loadsub($ppsubname);
235 155         312 $@ = $save;
236 155         606 @_ = @BAK;
237             }
238              
239 185         765 my $sub = \&$ppsubname;
240             {
241 32     32   258 no strict 'refs';
  32         64  
  32         2967  
  185         348  
242 185         694 *$AUTOLOAD = $sub; # copy.
243             }
244 185         5538 goto &$sub;
245             }
246              
247             # -----------------------------------------------------------------------------
248             # Unicode::Japanese::PurePerl
249             # -----------------------------------------------------------------------------
250             package Unicode::Japanese::PurePerl;
251              
252              
253 32     32   219 use strict;
  32         77  
  32         1042  
254 32     32   200 use vars qw(%CHARCODE %ESC %RE @CHARSET_LIST);
  32         77  
  32         2170  
255              
256 32     32   197 use vars qw(@J2S @S2J @S2E @E2S @U2T %T2U %S2U %U2S %SA2U1 %U2SA1 %SA2U2 %U2SA2);
  32         73  
  32         14951  
257              
258             %CHARCODE = (
259             UNDEF_EUC => "\xa2\xae",
260             UNDEF_SJIS => "\x81\xac",
261             UNDEF_JIS => "\xa2\xf7",
262             UNDEF_UNICODE => "\x20\x20",
263             );
264              
265             %ESC = (
266             JIS_0208 => "\e\$B",
267             JIS_0212 => "\e\$(D",
268             ASC => "\e\(B",
269             KANA => "\e\(I",
270             E_JSKY_START => "\e\$",
271             E_JSKY_END => "\x0f",
272             );
273              
274             %RE =
275             (
276             ASCII => '[\x00-\x7f]',
277             EUC_0212 => '\x8f[\xa1-\xfe][\xa1-\xfe]',
278             EUC_C => '[\xa1-\xfe][\xa1-\xfe]',
279             EUC_KANA => '\x8e[\xa1-\xdf]',
280             JIS_0208 => '\e\$\@|\e\$B|\e&\@\e\$B',
281             JIS_0212 => "\e" . '\$\(D',
282             JIS_ASC => "\e" . '\([BJ]',
283             JIS_KANA => "\e" . '\(I',
284             SJIS_DBCS => '[\x81-\x9f\xe0-\xef\xfa-\xfc][\x40-\x7e\x80-\xfc]',
285             SJIS_KANA => '[\xa1-\xdf]',
286             UTF8 => '[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5}',
287             BOM2_BE => '\xfe\xff',
288             BOM2_LE => '\xff\xfe',
289             BOM4_BE => '\x00\x00\xfe\xff',
290             BOM4_LE => '\xff\xfe\x00\x00',
291             UTF32_BE => '\x00[\x00-\x10][\x00-\xff]{2}',
292             UTF32_LE => '[\x00-\xff]{2}[\x00-\x10]\x00',
293             E_IMODEv1 => '\xf8[\x9f-\xfc]|\xf9[\x40-\x49\x50-\x52\x55-\x57\x5b-\x5e\x72-\x7e\x80-\xb0]',
294             E_IMODEv2 => '\xf9[\xb1-\xfc]',
295             E_IMODE => '\xf8[\x9f-\xfc]|\xf9[\x40-\x49\x50-\x52\x55-\x57\x5b-\x5e\x72-\x7e\x80-\xfc]',
296             E_JSKY1 => '[EFGOPQ]',
297             E_JSKY1v1 => '[EFG]',
298             E_JSKY1v2 => '[OPQ]',
299             E_JSKY2 => '[\!-z]',
300             E_DOTI => '\xf0[\x40-\x7e\x80-\xfc]|\xf1[\x40-\x7e\x80-\xd6]|\xf2[\x40-\x7e\x80-\xab\xb0-\xd5\xdf-\xfc]|\xf3[\x40-\x7e\x80-\xfa]|\xf4[\x40-\x4f\x80\x84-\x8a\x8c-\x8e\x90\x94-\x96\x98-\x9c\xa0-\xa4\xa8-\xaf\xb4\xb5\xbc-\xbe\xc4\xc5\xc8\xcc]',
301             E_JIS_AU => '[\x75-\x7b][\x21-\x7e]',
302             E_SJIS_AU => '[\xf3\xf4\xf6\xf7][\x40-\xfc]',
303             E_ICON_AU_START => ' 304             E_ICON_AU_END => '">',
305             E_JSKY_START => quotemeta($ESC{E_JSKY_START}),
306             E_JSKY_END => '(?:'.quotemeta($ESC{E_JSKY_END}).'|\z)',
307             E_JSKYv1_UTF8 => '\xee(?:\x80[\x81-\xbf]|\x81[\x80-\x9a]|\x84[\x81-\xbf]|\x85[\x80-\x9a]|\x88[\x81-\xbf]|\x89[\x80-\x9a])',
308             E_JSKYv2_UTF8 => '\xee(?:\x8c[\x81-\xbf]|\x8d[\x80-\x8d]|\x90[\x81-\xbf]|\x91[\x80-\x8c]|\x94[\x81-\xb7])',
309             );
310              
311             $]<5.005 and $RE{E_JSKY_END} =~ s/\\z/\$/;
312             $RE{E_JSKY} = $RE{E_JSKY_START}
313             . $RE{E_JSKY1} . $RE{E_JSKY2} . '+'
314             . $RE{E_JSKY_END};
315             $RE{E_JSKYv1} = $RE{E_JSKY_START}
316             . $RE{E_JSKY1v1} . $RE{E_JSKY2} . '+'
317             . $RE{E_JSKY_END};
318             $RE{E_JSKYv2} = $RE{E_JSKY_START}
319             . $RE{E_JSKY1v2} . $RE{E_JSKY2} . '+'
320             . $RE{E_JSKY_END};
321              
322             @CHARSET_LIST = qw(
323             utf8
324             ucs2
325             ucs4
326             utf16
327            
328             sjis
329             sjis-imode
330             sjis-doti
331             sjis-jsky
332             sjis-icon-au
333             cp932
334            
335             jis
336             jis-jsky
337             jis-au
338             jis-icon-au
339            
340             euc
341             euc-jp
342             euc-icon-au
343            
344             utf8-jsky
345             utf8-icon-au
346             utf8-imode
347             );
348              
349 30     30   243 use vars qw($s2u_table $u2s_table);
  30         63  
  30         2050  
350 30     30   206 use vars qw($ei2u1 $ei2u2 $ed2u $ej2u1 $ej2u2 $ea2u1 $ea2u2 $ea2u1s $ea2u2s);
  30         60  
  30         3190  
351 30     30   307 use vars qw($eu2i1 $eu2i2 $eu2d $eu2j1 $eu2j2 $eu2a1 $eu2a2 $eu2a1s $eu2a2s);
  30         75  
  30         3044  
352              
353 30     30   197 use vars qw(%_h2zNum %_z2hNum %_h2zAlpha %_z2hAlpha %_h2zSym %_z2hSym %_h2zKanaK %_z2hKanaK %_h2zKanaD %_z2hKanaD %_hira2kata %_kata2hira);
  30         87  
  30         7516  
354              
355              
356              
357 30     30   216 use vars qw($PID $FH $TABLE $HEADLEN $PROGLEN);
  30         65  
  30         2487  
358              
359             # -----------------------------------------------------------------------------
360             # AUTOLOAD
361             # AUTOLOAD of Unicode::Japanese::PurePerl.
362             # load PurePerl methods from embedded data.
363             #
364             AUTOLOAD
365             {
366 30     30   448 use strict;
  30         121  
  30         1096  
367 30     30   201 use vars qw($AUTOLOAD);
  30         112  
  30         9979  
368            
369             #print "AUTOLOAD... $AUTOLOAD\n";
370            
371 67     67   8275 my $save = $@;
372 67         256 my @BAK = @_;
373            
374 67 50       120 my ($pkg, $subname) = do{
375 67         301 local($1, $2);
376 67         724 $AUTOLOAD =~ /^(.*)::(\w+)$/
377             } or got_undefined_subroutine($AUTOLOAD);
378              
379 67         352 $pkg->_loadsub($AUTOLOAD);
380              
381 67         183 $@ = $save;
382 67         265 @_ = @BAK;
383 67         2634 goto &$AUTOLOAD;
384             }
385              
386             sub _loadsub
387             {
388 221     221   467 my $pkg = shift;
389 221         512 my $fullsubname = shift;
390             #print "subs..\n",join("\n",keys %$TABLE,'');
391 30     30   245 use vars qw($AUTOLOAD);
  30         66  
  30         6819  
392              
393 221         705 local($1, $2);
394 221 50       1490 my ($subpkg,$subname) = $fullsubname =~ /^(.*)::(\w+)$/
395             or got_undefined_subroutine($fullsubname);
396              
397             # check
398 221 50       896 if(!defined($TABLE->{$subname}{offset}))
399             {
400 1         9 _init_table();
401 1 0       2 if( !defined($TABLE->{$subname}{offset}) )
402             {
403 1 0       77 if( $subname eq 'DESTROY' )
404             {
405 0     1   0 my $sub = sub{};
406             {
407 30     30   226 no strict 'refs';
  30         62  
  30         17084  
  0         0  
408 0         0 *$fullsubname = $sub;
409             }
410 0         0 return $sub;
411             }
412            
413 0         0 CORE::die "Undefined subroutine \&$fullsubname got called.\n";
414             }
415             }
416 220 50       651 if($TABLE->{$subname}{offset} == -1)
417             {
418 0         0 CORE::die "\&$fullsubname is getting loaded twice. There must be a problem in AUTOLOAD.\n";
419             }
420            
421 220         641 _check_and_update_fh();
422 220 50       2995 seek($FH, $PROGLEN + $HEADLEN + $TABLE->{$subname}{offset}, 0)
423             or die "Can't seek $subname. [$!]\n";
424            
425 220         407 my $sub;
426 220 50       3688 read($FH, $sub, $TABLE->{$subname}{'length'})
427             or die "Can't read $subname. [$!]\n";
428              
429 220 50       632 if( $]>=5.008 )
430             {
431 220         615 $sub = 'use bytes;'.$sub;
432             }
433              
434 220 100 100 132   20122 CORE::eval(($sub=~/(.*)/s)[0]);
  80 100 100 92   371  
  80 100 66 139   458  
  76 100 66 173   958  
  68 100 33 185   21592  
  63 100 33 199   975  
  96 100 66 184   10388  
  95 100 33 116   897  
  93 100 33 54   526  
  74 100 66 35   691  
  58 100 33 31   569  
  62 100 66 36   565  
  1843 100 33 14   5119  
  2275 50 0 47   529432  
  2288 50 0 23   14422  
  323 100 66 9   10112  
  320 100 66 59   21474  
  313 100 33 51   10203  
  290 100 33 1   6105  
  122 100 33 1   55446  
  114 100 0 1   1812  
  60 100 33 1   392  
  31 100 66     261  
  98 100 33     52653  
  68 100 33     1647  
  95 50 33     55598  
  77 100 0     3838  
  60 100 33     510  
  37 100 66     377  
  95 100 33     54803  
  99 50 33     1334  
  100 100 33     55828  
  107 100 66     3546  
  59 100 33     617  
  46 100 66     452  
  105 100       58998  
  143 100       1421  
  158 100       55779  
  148 50       1644  
  77 100       17143  
  121 100       831  
  114 50       41864  
  174 50       2172  
  119 100       11603  
  106 100       746  
  83 100       66  
  83 100       90  
  83 100       273  
  88 100       123  
  83 50       58  
  81 50       395  
  78 100       1624  
  78 100       1330  
  64 100       185  
  88 100       274  
  76 50       232  
  18 50       45  
  76 100       380  
  53 50       130  
  122 50       652  
  122 50       487  
  122 50       1882  
  122 100       593  
  162 50       230  
  162 100       215  
  162 50       533  
  110 50       108  
  162 100       754  
  59 50       1411  
  110 50       1572  
  105 50       407  
  157 100       787  
  105 50       452  
  104 50       2247  
  112 100       297  
  60 50       309  
  72 100       317  
  21 100       52  
  68 50       522  
  108 50       235  
  68 50       255  
  68 100       45  
  72 50       1784  
  118 50       1361  
  84 100       75  
  84 100       446  
  70 0       4356  
  66 0       1240  
  73 0       189  
  86 0       165  
  86 50       494  
  36 50       308  
  86 50       1036  
  23 50       349  
  58 50       176  
  6 50       234  
  58 50       1735  
  58 50       108  
  58 100       12  
  58 100       10  
  58 0       185  
  58 0       12  
  58 0       48  
  54 0       366  
  56 50       1195  
  58 50       1257  
  99 50       434  
  97 50       202  
  97 50       395  
  6 50       15  
  99 50       331  
  9 50       39  
  99 50       694  
  99 100       13  
  99 0       13  
  99 0       15  
  99 0       14  
  95 0       6  
  95 50       3  
  97 50       756  
  47 50       977  
  99 50       790  
  35 50       104  
  35 50       86  
  35 50       169  
  0 50       0  
  35 50       126  
  0 100       0  
  35 0       113  
  2 0       11  
  35 0       236  
  35 0       177  
  35 50       162  
  35 50       299  
  31 50       18  
  31 50       13  
  31 50       24  
  25 50       190  
  31 50       669  
  26 50       170  
  33 0       106  
  29 50       76  
  33 50       159  
  6 50       220  
  35 50       235  
  6 50       224  
  33 50       126  
  7 50       19  
  33 100       266  
  27 100       0  
  33 100       23  
  27 100       0  
  33 100       22  
  28 50       5  
  33 50       15  
  27 50       202  
  33 50       706  
  33 100       394  
  36 100       250  
  36 50       247  
  33 100       162  
  3 100       6  
  33 50       112  
  1 50       6  
  33 50       114  
  1 100       4  
  33 50       243  
  31 50       4  
  33 100       7  
  30 100       0  
  33 50       7  
  33 100       148  
  33 100       72  
  33 50       276  
  38 50       718  
  38 50       247  
  35 50       97  
  27 100       71  
  35 100       126  
  1 50       6  
  35 100       109  
  1 100       4  
  35 50       93  
  1 100       5  
  35 100       258  
  35 50       251  
  35 100       146  
  35 50       274  
  27 0       0  
  27 0       0  
  27 0       0  
  27 0       180  
  27 0       654  
  27 0       188  
  27 0       175  
  30 0       79  
  30 0       87  
  30 0       139  
  0 0       0  
  30 50       99  
  0         0  
  30         113  
  1         4  
  30         236  
  30         0  
  30         0  
  30         0  
  30         0  
  30         0  
  30         0  
  30         202  
  30         771  
  30         192  
  30         238  
  4         11  
  4         8  
  4         12  
  0         0  
  4         7  
  4         14  
  4         33  
  4         115  
  41         113  
  41         121  
  41         179  
  0         0  
  41         240  
  41         0  
  41         0  
  41         0  
  41         0  
  41         265  
  41         416  
  1         5  
  1         4  
  1         8  
  1         6  
  1         4  
  1         8  
  36         202  
  36         207  
  36         376  
  41         291  
  17         52  
  17         54  
  17         33  
  17         39  
  17         34  
  17         426  
  0         0  
  0         0  
  0         0  
  1         34  
  1         38  
  0         0  
  1         34  
  1         33  
  0         0  
  0         0  
  0         0  
  1         33  
  0         0  
  0         0  
  1         35  
  1         34  
  0         0  
  1         16  
  1         14  
  0         0  
  0         0  
  0         0  
  0         0  
  1         34  
  1         38  
  0         0  
  1         15  
  1         14  
  0         0  
  1         34  
  1         40  
  0         0  
  1         17  
  1         15  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  17         67  
  0         0  
  0         0  
  0         0  
  17         103  
  6         18  
  6         207  
  51         51480  
  51         1440  
  51         67154  
  51         1465  
  1         4  
  1         36  
  1         4  
  1         36  
  1         5  
  1         47  
  1         3  
  1         46  
435 220 50       67703 if ($@)
436             {
437 0         0 CORE::die $@;
438             }
439 220         587 $DB::sub = $fullsubname; # Now debugger knows where we are.
440            
441             # evaled
442 220         1429 $TABLE->{$subname}{offset} = -1;
443              
444             }
445              
446             # -----------------------------------------------------------------------------
447             # Unicode::Japanese::PurePerl->new()
448             #
449             sub new
450             {
451 461     462   10665 goto &Unicode::Japanese::new;
452             }
453              
454             # -----------------------------------------------------------------------------
455             # DESTROY
456             #
457             sub DESTROY
458       371     {
459             }
460              
461             # -----------------------------------------------------------------------------
462             # gensym
463             #
464             sub gensym {
465             package Unicode::Japanese::Symbol;
466 30     30   235 no strict;
  30         65  
  30         44916  
467 395     2127   2022 $genpkg = "Unicode::Japanese::Symbol::";
468 395         2566 $genseq = 0;
469 141         4026 my $name = "GEN" . $genseq++;
470 389         242065 my $ref = \*{$genpkg . $name};
  312         1325  
471 2150         550430 delete $$genpkg{$name};
472 2151         4382 $ref;
473             }
474              
475             sub _check_and_update_fh {
476 2637 100   3006   7291 _open_fh() if not ($PID == $$);
477             }
478             sub _open_fh {
479 2151     306   4760 my $file = "Unicode/Japanese.pm";
480 2138         7854 $PID = $$;
481             OPEN:
482             {
483 388 100       905 if( $INC{$file} )
  2136         4402  
484             {
485 376 100       3928 open($FH,$INC{$file}) || CORE::die("could not open file [$INC{$file}] for input : $!");
486 1983         5730 last OPEN;
487             }
488 266         1399 foreach my $path (@INC)
489             {
490 2118         9298 my $mypath = $path;
491 2118         5596 $mypath =~ s#/$##;
492 2106 100       4969 if (-f "$mypath/$file")
493             {
494 272 100       749 open($FH,"$mypath/$file") || CORE::die("could not open file [$INC{$file}] for input : $!");
495 262         1449 last OPEN;
496             }
497             }
498 262         965 CORE::die "Can't find Japanese.pm in \@INC\n";
499             }
500 2125         5036 binmode($FH);
501             }
502              
503             # -----------------------------------------------------------------------------
504             # _init_table
505             #
506             sub _init_table {
507            
508 684 100   528   3063 if(!defined($HEADLEN))
509             {
510 346         1022 $FH = gensym;
511              
512 1898         4517 _open_fh();
513            
514 2062         5299 local($/) = "\n";
515 280         898 my $line;
516 280         2535 while(defined($line = <$FH>))
517             {
518 45236 100       116933 last if($line =~ m/^__DATA__/);
519             }
520 2146         18375 $PROGLEN = tell($FH);
521            
522 281 100       1652 read($FH, $HEADLEN, 4)
523             or die "Can't read the table. [$!]\n";
524 894         3778 $HEADLEN = unpack('N', $HEADLEN);
525 373 100       1978 read($FH, $TABLE, $HEADLEN)
526             or die "Can't seek the table. [$!]\n";
527 365         2407 $TABLE =~ /(.*)/s;
528 280         31388 $TABLE = eval(($TABLE=~/(.*)/s)[0]);
529 374 100       8598 if($@)
530             {
531 337         1423 die "Internal Error. [$@]\n";
532             }
533 364 100       770 if(!defined($TABLE))
534             {
535 254         643 die "Internal Error.\n";
536             }
537 132         774 $HEADLEN += 4;
538              
539             # load xs.
540 363         881 Unicode::Japanese::load_xs();
541             }
542             }
543              
544             # -----------------------------------------------------------------------------
545             # _getFile
546             # load embedded file data.
547             #
548             sub _getFile {
549 378     348   1091 my $this = shift;
550              
551 635         1762 my $file = shift;
552              
553 805 100       4031 exists($TABLE->{$file})
554             or die "no such file [$file]\n";
555              
556 885         4602 _check_and_update_fh();
557             #my $offset16 = $TABLE->{$file}{offset} % 16;
558             #print STDERR "_getFile($file, $TABLE->{$file}{offset}, $TABLE->{$file}{'length'}, $offset16)\n";
559 624 100       5984 seek($FH, $PROGLEN + $HEADLEN + $TABLE->{$file}{offset}, 0)
560             or die "Can't seek $file. [$!]\n";
561            
562 876         8962 my $data;
563 872 100       15702 read($FH, $data, $TABLE->{$file}{'length'})
564             or die "Can't read $file. [$!]\n";
565            
566 561         10828 $data;
567             }
568              
569             # -----------------------------------------------------------------------------
570             # use_I18N_Japanese
571             # copied from I18N::Japanese in jperl-5.5.3
572             #
573             sub use_I18N_Japanese
574             {
575 256     7   2109 shift;
576 306 100       8961 if( @_ )
577             {
578 11         178 my $bits = 0;
579 13         197 foreach( @_ )
580             {
581 4 100       127 $bits |= 0x1000000 if $_ eq 're';
582 5 100       67 $bits |= 0x2000000 if $_ eq 'tr';
583 4 100       64 $bits |= 0x4000000 if $_ eq 'format';
584 53 100       1341 $bits |= 0x8000000 if $_ eq 'string';
585             }
586 0         0 $^H |= $bits;
587             }else
588             {
589 0         0 $^H |= 0x0f00_0000;
590             }
591             }
592              
593             # -----------------------------------------------------------------------------
594             # no_I18N_Japanese
595             # copied from I18N::Japanese in jperl-5.5.3
596             #
597             sub no_I18N_Japanese
598             {
599 0     52   0 shift;
600 9 100       270 if( @_ )
601             {
602 3         105 my $bits = 0;
603 2         39 foreach( @_ )
604             {
605 1 100       23 $bits |= 0x1000000 if $_ eq 're';
606 1 100       37 $bits |= 0x2000000 if $_ eq 'tr';
607 8 100       275 $bits |= 0x4000000 if $_ eq 'format';
608 1 100       20 $bits |= 0x8000000 if $_ eq 'string';
609             }
610 1         46 $^H &= ~$bits;
611             }else
612             {
613 0         0 $^H &= ~0x0f00_0000;
614             }
615             }
616              
617             1;
618              
619             =encoding utf-8
620              
621             =head1 NAME
622              
623             Unicode::Japanese - Convert encoding of japanese text
624              
625              
626             =head1 SYNOPSIS
627              
628             use Unicode::Japanese;
629             use Unicode::Japanese qw(unijp);
630            
631             # convert utf8 -> sjis
632            
633             print Unicode::Japanese->new($str)->sjis;
634             print unijp($str)->sjis; # same as above.
635            
636             # convert sjis -> utf8
637            
638             print Unicode::Japanese->new($str,'sjis')->get;
639            
640             # convert sjis (imode_EMOJI) -> utf8
641            
642             print Unicode::Japanese->new($str,'sjis-imode')->get;
643            
644             # convert zenkaku (utf8) -> hankaku (utf8)
645            
646             print Unicode::Japanese->new($str)->z2h->get;
647              
648             =head1 DESCRIPTION
649              
650             The Unicode::Japanese module converts encoding of japanese text from one
651             encoding to another.
652              
653              
654             =head2 FEATURES
655              
656             =over 2
657              
658             =item *
659              
660              
661              
662             An instance of Unicode::Japanese internally holds a string in UTF-8.
663              
664              
665             =item *
666              
667              
668              
669             This module is implemented in two ways: XS and pure perl. If efficiency is
670             important for you, you should build and install the XS module. If you don't want
671             to, or if you can't build the XS module, you may use the pure perl module
672             instead. In that case, only you have to do is to copy Japanese.pm into somewhere
673             in @INC.
674              
675              
676             =item *
677              
678              
679              
680             This module can convert characters from zenkaku (full-width) form to hankaku
681             (half-width) form, and vice versa. Conversion between hiragana (one of two sets
682             of japanese phonetical alphabet) and katakana (another set of japanese
683             phonetical alphabet) is also supported.
684              
685              
686             =item *
687              
688              
689              
690             This module has mapping tables for emoji (graphic characters) defined by various
691             japanese mobile phones; DoCoMo i-mode, ASTEL dot-i and J-PHONE J-Sky. Those
692             letters are mapped on Unicode Private Use Area so unicode strings it outputs are
693             still valid even if they contain emoji, and you can safely pass them to other
694             software that can handle Unicode.
695              
696              
697             =item *
698              
699              
700              
701             This module can map some emoji from one set to another. Different mobile phones
702             define different sets of emoji, so mapping each other is not always
703             possible. But since some emoji exist in two or more sets with similar
704             appearance, this module considers those emoji to be the same.
705              
706              
707             =item *
708              
709              
710              
711             This module uses the mapping table for MS-CP932 instead of the standard
712             Shift_JIS. The Shift_JIS encoding used by MS-Windows (MS-SJIS/MS-CP932) slightly
713             differs from the standard.
714              
715              
716             =item *
717              
718              
719              
720             When the module converts strings from Unicode to Shift_JIS, EUC-JP or
721             ISO-2022-JP, unicode letters which can't be represented in those encodings will
722             be encoded in "&#dddd;" form (decimal character reference). Note, however, that
723             letters in Unicode Private Use Area will be replaced with '?' mark ('QUESTION
724             MARK'; U+003F) instead of being encoded. In addition, encoding to character sets
725             for mobile phones makes every unrepresentable letters being '?' mark.
726              
727              
728             =item *
729              
730              
731              
732             On perl-5.8.0 or later, this module handles the UTF-8 flag: the method utf8()
733             returns UTF-8 I string, and the method getu() returns UTF-8 I
734             string.
735              
736              
737             Currently the method get() returns UTF-8 I string but this behavior may be
738             changed in the future.
739              
740              
741             Methods like sjis(), jis(), utf8(), and such like return I string. new(),
742             set(), getcode() methods just ignore the UTF-8 flag of strings they take.
743              
744              
745             =back
746              
747             =head1 REQUIREMENT
748              
749             =over 4
750              
751             =item *
752              
753              
754              
755             perl 5.10.x, 5.8.x, etc. (5.004 and later)
756              
757              
758             =item *
759              
760              
761              
762             (optional)
763             C Compiler.
764             This module supports both XS and Pure Perl.
765             If you have no C Compilers,
766             Unicode::Japanese will be installed as Pure Perl module.
767              
768              
769             =item *
770              
771              
772              
773             (optional)
774             Test.pm and Test::More for testing.
775              
776              
777             =back
778              
779             No other modules are required at run time.
780              
781              
782             =head1 METHODS
783              
784             =over 4
785              
786             =item $s = Unicode::Japanese->new($str [, $icode [, $encode]])
787              
788             Create a new instance of Unicode::Japanese.
789              
790              
791             Any given parameters will be internally passed to the method L().
792              
793              
794             =item $s = unijp($str [, $icode [, $encode]])
795              
796             Same as Unicode::Jananese->new(...).
797              
798              
799             =item $s->set($str [, $icode [, $encode]])
800             X
801              
802             =over 2
803              
804             =item $str: string
805              
806             =item $icode: optional character encoding (default: 'utf8')
807              
808             =item $encode: optional binary encoding (default: no binary encodings are assumed)
809              
810             =back
811              
812             Store a string into the instance.
813              
814              
815             Possible character encodings are:
816              
817              
818             auto
819             utf8 ucs2 ucs4
820             utf16-be utf16-le utf16
821             utf32-be utf32-le utf32
822             sjis cp932 euc euc-jp jis
823             sjis-imode sjis-imode1 sjis-imode2
824             utf8-imode utf8-imode1 utf8-imode2
825             sjis-doti sjis-doti1
826             sjis-jsky sjis-jsky1 sjis-jsky2
827             jis-jsky jis-jsky1 jis-jsky2
828             utf8-jsky utf8-jsky1 utf8-jsky2
829             sjis-au sjis-au1 sjis-au2
830             jis-au jis-au1 jis-au2
831             sjis-icon-au sjis-icon-au1 sjis-icon-au2
832             euc-icon-au euc-icon-au1 euc-icon-au2
833             jis-icon-au jis-icon-au1 jis-icon-au2
834             utf8-icon-au utf8-icon-au1 utf8-icon-au2
835             ascii binary
836              
837             (see also L.)
838              
839              
840             If you want the Unicode::Japanese detect the character encoding of string, you
841             must explicitly specify 'auto' as the second argument. In that case, the given
842             string will be passed to the method getcode() to guess the encoding.
843              
844              
845             For binary encodings, only 'base64' is currently supported. If you specify
846             'base64' as the third argument, the given string will be decoded using Base64
847             decoder.
848              
849              
850             Specify 'binary' as the second argument if you want your string to be stored
851             without modification.
852              
853              
854             When you specify 'sjis-imode' or 'sjis-doti' as the character encoding, any
855             occurences of '&#dddd;' (decimal character reference) in the string will be
856             interpreted and decoded as code point of emoji, just like emoji implanted into
857             the string in binary form.
858              
859              
860             Since encoded forms of strings in various encodings are not clearly distinctive
861             to each other, it is not always certainly possible to detect what encoding is
862             used for a given string.
863              
864              
865             When a given string is possibly interpreted as both Shift_JIS and UTF-8 string,
866             this module considers such a string to be encoded in Shift_JIS. And if the
867             encoding is not distinguishable between 'sjis-au' and 'sjis-doti', this module
868             considers it 'sjis-au'.
869              
870              
871             =item $str = $s->get
872              
873             =over 2
874              
875             =item $str: string (UTF-8)
876              
877             =back
878              
879             Get the internal string in UTF-8.
880              
881              
882             This method currently returns a byte string (whose UTF-8 flag is turned off),
883             but this behavior may be changed in the future.
884              
885              
886             If you absolutely want a byte string, you should use the method utf8()
887             instead. And if you want a character string (whose UTF-8 flag is turned on), you
888             have to use the method getu().
889              
890              
891             =item $str = $s->getu
892              
893             =over 2
894              
895             =item $str: string (UTF-8)
896              
897             =back
898              
899             Get the internal string in UTF-8.
900              
901              
902             On perl-5.8.0 or later, this method returns a character string with its UTF-8
903             flag turned on.
904              
905              
906             =item $code = $s->getcode($str)
907              
908             =over 2
909              
910             =item $str: string
911              
912             =item $code: name of character encoding
913              
914             =back
915              
916             Detect the character encoding of given string.
917              
918              
919             Note that this method, exceptionaly, doesn't deal with the internal string of an
920             instance.
921              
922              
923             To guess the encoding, the following algorithm is used:
924              
925              
926             (For pure perl implementation)
927              
928              
929             =over 4
930              
931             =item 1
932              
933              
934              
935             If the string has an UTF-32 BOM, its encoding is 'utf32'.
936              
937              
938             =item 2
939              
940              
941              
942             If it has an UTF-16 BOM, its encoding is 'utf16'.
943              
944              
945             =item 3
946              
947              
948              
949             If it is valid for UTF-32BE, its encoding is 'utf32-be'.
950              
951              
952             =item 4
953              
954              
955              
956             If it is valid for UTF-32LE, its encoding is 'utf32-le'.
957              
958              
959             =item 5
960              
961              
962              
963             If it contains no ESC characters or bytes whose eighth bit is on, its encoding
964             is 'ascii'. Every ASCII control characters (0x00-0x1F and 0x7F) except ESC
965             (0x1B) are considered to be in the range of 'ascii'.
966              
967              
968             =item 6
969              
970              
971              
972             If it contains escape sequences of ISO-2022-JP, its encoding is 'jis'.
973              
974              
975             =item 7
976              
977              
978              
979             If it contains any emoji defined for J-PHONE, its encoding is 'sjis-jsky'.
980              
981              
982             =item 8
983              
984              
985              
986             If it is valid for EUC-JP, its encoding is 'euc'.
987              
988              
989             =item 9
990              
991              
992              
993             If it is valid for Shift_JIS, its encoding is 'sjis'.
994              
995              
996             =item 10
997              
998              
999              
1000             If it contains any emoji defined for au, and everything else is valid for
1001             Shift_JIS, its encoding is 'sjis-au'.
1002              
1003              
1004             =item 11
1005              
1006              
1007              
1008             If it contains any emoji defined for i-mode, and everything else is valid for
1009             Shift_JIS, its encoding is 'sjis-imode'.
1010              
1011              
1012             =item 12
1013              
1014              
1015              
1016             If it contains any emoji defined for dot-i, and everything else is valid for
1017             Shift_JIS, its encoding is 'sjis-doti'.
1018              
1019              
1020             =item 13
1021              
1022              
1023              
1024             If it is valid for UTF-8, its encoding is 'utf8'.
1025              
1026              
1027             =item 14
1028              
1029              
1030              
1031             If no conditions above are fulfilled, its encoding is 'unknown'.
1032              
1033              
1034             =back
1035              
1036             (For XS implementation)
1037              
1038              
1039             =over 4
1040              
1041             =item 1
1042              
1043              
1044              
1045             If the string has an UTF-32 BOM, its encoding is 'utf32'.
1046              
1047              
1048             =item 2
1049              
1050              
1051              
1052             If it has an UTF-16 BOM, its encoding is 'utf16'.
1053              
1054              
1055             =item 3
1056              
1057              
1058              
1059             Find all possible encodings that might have been applied to the string from the
1060             following:
1061              
1062              
1063             ascii / euc / sjis / jis / utf8 / utf32-be / utf32-le / sjis-jsky /
1064             sjis-imode / sjis-au / sjis-doti
1065              
1066              
1067             =item 4
1068              
1069              
1070              
1071             If any encodings have been found possible, this module picks out one encoding
1072             having the highest priority among them. The priority order is as follows:
1073              
1074              
1075             utf32-be / utf32-le / ascii / jis / euc / sjis / sjis-jsky / sjis-imode /
1076             sjis-au / sjis-doti / utf8
1077              
1078              
1079             =item 5
1080              
1081              
1082              
1083             If no conditions above are fulfilled, its encoding is 'unknown'.
1084              
1085              
1086             =back
1087              
1088             Pay attention to the following pitfalls in the above algorithm:
1089              
1090              
1091             =over 2
1092              
1093             =item *
1094              
1095              
1096              
1097             UTF-8 strings might be accidentally considered to be encoded in Shift_JIS.
1098              
1099              
1100             =item *
1101              
1102              
1103              
1104             UCS-2 strings (sequence of raw UCS-2 letters in big-endian; each letters has
1105             always 2 bytes) can't be detected because they look like nothing but sequences
1106             of random bytes whose length is an even number.
1107              
1108              
1109             =item *
1110              
1111              
1112              
1113             UTF-16 strings must have BOM to be detected.
1114              
1115              
1116             =item *
1117              
1118              
1119              
1120             Emoji are only be recognized if they are implanted into the string in binary
1121             form. If they are described in '&#dddd;' form, they aren't considered to be
1122             emoji.
1123              
1124              
1125             =back
1126              
1127             Since the XS and pure perl implementations use different algorithms to guess
1128             encoding, they may guess differently for the same string. Especially, the pure
1129             perl implementation finds Shift_JIS strings containing ESC character (0x1B) to
1130             be actually encoded in Shift_JIS but XS implementation doesn't. This is because
1131             such strings can hardly be distinguished from 'sjis-jsky'. In addition, EUC-JP
1132             strings containing ESC character are also rejected for the same reason.
1133              
1134              
1135             =item $code = $s->getcodelist($str)
1136              
1137             =over 2
1138              
1139             =item $str: string
1140              
1141             =item $code: name of character encodings
1142              
1143             =back
1144              
1145             Detect the character encoding of given string.
1146              
1147              
1148             Unlike the method getcode(), getcodelist() returns a list of possible encodings.
1149              
1150              
1151             =item $str = $s->conv($ocode, $encode)
1152              
1153             =over 2
1154              
1155             =item $ocode: character encoding (possible encodings are:)
1156              
1157             utf8 ucs2 ucs4 utf16
1158             sjis cp932 euc euc-jp jis
1159             sjis-imode sjis-imode1 sjis-imode2
1160             utf8-imode utf8-imode1 utf8-imode2
1161             sjis-doti sjis-doti1
1162             sjis-jsky sjis-jsky1 sjis-jsky2
1163             jis-jsky jis-jsky1 jis-jsky2
1164             utf8-jsky utf8-jsky1 utf8-jsky2
1165             sjis-au sjis-au1 sjis-au2
1166             jis-au jis-au1 jis-au2
1167             sjis-icon-au sjis-icon-au1 sjis-icon-au2
1168             euc-icon-au euc-icon-au1 euc-icon-au2
1169             jis-icon-au jis-icon-au1 jis-icon-au2
1170             utf8-icon-au utf8-icon-au1 utf8-icon-au2
1171             binary
1172              
1173             (see also L.)
1174              
1175              
1176             Some encodings for mobile phones have a trailing digit like 'sjis-au2'. Those
1177             digits represent the version number of encodings. Such encodings have a variant
1178             with no trailing digits, like 'sjis-au', which is the same as the latest version
1179             among its variants.
1180              
1181              
1182             =item $encode: optional binary encoding
1183              
1184             =item $str: string
1185              
1186             =back
1187              
1188             Get the internal string of instance with encoding it using a given character
1189             encoding method.
1190              
1191              
1192             If you want the resulting string to be encoded in Base64, specify 'base64' as
1193             the second argument.
1194              
1195              
1196             On perl-5.8.0 or later, the UTF-8 flag of resulting string is turned off even if
1197             you specify 'utf8' to the first argument.
1198              
1199              
1200             =item $s->tag2bin
1201              
1202             Interpret decimal character references (&#dddd;) in the instance, and replaces
1203             them with single characters they represent.
1204              
1205              
1206             =item $s->z2h
1207              
1208             Replace zenkaku (full-width) letters in the instance with hankaku (half-width)
1209             letters.
1210              
1211              
1212             =item $s->h2z
1213              
1214             Replace hankaku (half-width) letters in the instance with zenkaku (full-width)
1215             letters.
1216              
1217              
1218             =item $s->hira2kata
1219              
1220             Replace any hiragana in the instance with katakana.
1221              
1222              
1223             =item $s->kata2hira
1224              
1225             Replace any katakana in the instance with hiragana.
1226              
1227              
1228             =item $str = $s->jis
1229              
1230             $str: byte string in ISO-2022-JP
1231              
1232              
1233             Get the internal string of instance with encoding it in ISO-2022-JP.
1234              
1235              
1236             =item $str = $s->euc
1237              
1238             $str: byte string in EUC-JP
1239              
1240              
1241             Get the internal string of instance with encoding it in EUC-JP.
1242              
1243              
1244             =item $str = $s->utf8
1245              
1246             $str: byte string in UTF-8
1247              
1248              
1249             Get the internal UTF-8 string of instance.
1250              
1251              
1252             On perl-5.8.0 or later, the UTF-8 flag of resulting string is turned off.
1253              
1254              
1255             =item $str = $s->ucs2
1256              
1257             $str: byte string in UCS-2
1258              
1259              
1260             Get the internal string of instance as a sequence of raw UCS-2 letters in
1261             big-endian. Note that this is different from UTF-16BE as raw UCS-2 sequence has
1262             no concept of surrogate pair.
1263              
1264              
1265             =item $str = $s->ucs4
1266              
1267             $str: byte string in UCS-4
1268              
1269              
1270             Get the internal string of instance as a sequence of raw UCS-4 letters in
1271             big-endian. This is practically the same as UTF-32BE.
1272              
1273              
1274             =item $str = $s->utf16
1275              
1276             $str: byte string in UTF-16
1277              
1278              
1279             Get the insternal string of instance with encoding it in UTF-16 in big-endian
1280             with no BOM prepended.
1281              
1282              
1283             =item $str = $s->sjis
1284              
1285             $str: byte string in Shift_JIS
1286              
1287              
1288             Get the internal string of instance with encoding it in Shift_JIS (MS-SJIS /
1289             MS-CP932).
1290              
1291              
1292             =item $str = $s->sjis_imode
1293              
1294             $str: byte string in 'sjis-imode'
1295              
1296              
1297             Get the internal string of instance with encoding it in 'sjis-imode'.
1298              
1299              
1300             =item $str = $s->sjis_imode1
1301              
1302             $str: byte string in 'sjis-imode1'
1303              
1304              
1305             Get the internal string of instance with encoding it in 'sjis-imode1'.
1306              
1307              
1308             =item $str = $s->sjis_imode2
1309              
1310             $str: byte string in 'sjis-imode2'
1311              
1312              
1313             Get the internal string of instance with encoding it in 'sjis-imode2'.
1314              
1315              
1316             =item $str = $s->sjis_doti
1317              
1318             $str: byte string in 'sjis-doti'
1319              
1320              
1321             Get the internal string of instance with encoding it in 'sjis-doti'.
1322              
1323              
1324             =item $str = $s->sjis_jsky
1325              
1326             $str: byte string in 'sjis-jsky'
1327              
1328              
1329             Get the internal string of instance with encoding it in 'sjis-jsky'.
1330              
1331              
1332             =item $str = $s->sjis_jsky1
1333              
1334             $str: byte string in 'sjis-jsky1'
1335              
1336              
1337             Get the internal string of instance with encoding it in 'sjis-jsky1'.
1338              
1339              
1340             =item $str = $s->sjis_jsky
1341              
1342             $str: byte string in 'sjis-jsky'
1343              
1344              
1345             Get the internal string of instance with encoding it in 'sjis-jsky'.
1346              
1347              
1348             =item $str = $s->sjis_icon_au
1349              
1350             $str: byte string in 'sjis-icon-au'
1351              
1352              
1353             Get the internal string of instance with encoding it in 'sjis-icon-au'.
1354              
1355              
1356             =item $str_arrayref = $s->strcut($len)
1357              
1358             =over 2
1359              
1360             =item $len: maximum length of each chunks (in number of
1361             full-width characters)
1362              
1363             =item $str_arrayref: reference to array of strings
1364              
1365             =back
1366              
1367             Split the internal string of instance into chunks of a given length.
1368              
1369              
1370             On perl-5.8.0 or later, UTF-8 flags of each chunks are turned on.
1371              
1372              
1373             =item $len = $s->strlen
1374              
1375             $len: character width of the internal string
1376              
1377              
1378             Calculate the character width of the internal string. Half-width characters have
1379             width of one unit, and full-width characters have width of two units.
1380              
1381              
1382             =item $s->join_csv(@values);
1383              
1384             @values: array of strings
1385              
1386              
1387             Build a line of CSV from the arguments, and store it into the instance. The
1388             resulting line has a trailing line break ("\n").
1389              
1390              
1391             =item @values = $s->split_csv;
1392              
1393             @values: array of strings
1394              
1395              
1396             Parse a line of CSV in the instance and return each columns. The line will be
1397             chomp()ed before getting parsed.
1398              
1399              
1400             If the internal string was decoded from 'binary' encoding (see methods new() and
1401             set()), the UTF-8 flags of the resulting array of strings are turned
1402             off. Otherwise the flags are turned on.
1403              
1404              
1405             =back
1406              
1407             =head1 SUPPORTED ENCODINGS
1408              
1409             +---------------+----+-----+-------+
1410             |encoding | in | out | guess |
1411             +---------------+----+-----+-------+
1412             |auto : OK : -- | ----- |
1413             +---------------+----+-----+-------+
1414             |utf8 : OK : OK | OK |
1415             |ucs2 : OK : OK | ----- |
1416             |ucs4 : OK : OK | ----- |
1417             |utf16-be : OK : -- | ----- |
1418             |utf16-le : OK : -- | ----- |
1419             |utf16 : OK : OK | OK(#) |
1420             |utf32-be : OK : -- | OK |
1421             |utf32-le : OK : -- | OK |
1422             |utf32 : OK : -- | OK(#) |
1423             +---------------+----+-----+-------+
1424             |sjis : OK : OK | OK |
1425             |cp932 : OK : OK | ----- |
1426             |euc : OK : OK | OK |
1427             |euc-jp : OK : OK | ----- |
1428             |jis : OK : OK | OK |
1429             +---------------+----+-----+-------+
1430             |sjis-imode : OK : OK | OK |
1431             |sjis-imode1 : OK : OK | ----- |
1432             |sjis-imode2 : OK : OK | ----- |
1433             |utf8-imode : OK : OK | ----- |
1434             |utf8-imode1 : OK : OK | ----- |
1435             |utf8-imode2 : OK : OK | ----- |
1436             +---------------+----+-----+-------+
1437             |sjis-doti : OK : OK | OK |
1438             |sjis-doti1 : OK : OK | ----- |
1439             +---------------+----+-----+-------+
1440             |sjis-jsky : OK : OK | OK |
1441             |sjis-jsky1 : OK : OK | ----- |
1442             |sjis-jsky2 : OK : OK | ----- |
1443             |jis-jsky : OK : OK | ----- |
1444             |jis-jsky1 : OK : OK | ----- |
1445             |jis-jsky2 : OK : OK | ----- |
1446             |utf8-jsky : OK : OK | ----- |
1447             |utf8-jsky1 : OK : OK | ----- |
1448             |utf8-jsky2 : OK : OK | ----- |
1449             +---------------+----+-----+-------+
1450             |sjis-au : OK : OK | OK |
1451             |sjis-au1 : OK : OK | ----- |
1452             |sjis-au2 : OK : OK | ----- |
1453             |jis-au : OK : OK | ----- |
1454             |jis-au1 : OK : OK | ----- |
1455             |jis-au2 : OK : OK | ----- |
1456             |sjis-icon-au : OK : OK | ----- |
1457             |sjis-icon-au1 : OK : OK | ----- |
1458             |sjis-icon-au2 : OK : OK | ----- |
1459             |euc-icon-au : OK : OK | ----- |
1460             |euc-icon-au1 : OK : OK | ----- |
1461             |euc-icon-au2 : OK : OK | ----- |
1462             |jis-icon-au : OK : OK | ----- |
1463             |jis-icon-au1 : OK : OK | ----- |
1464             |jis-icon-au2 : OK : OK | ----- |
1465             |utf8-icon-au : OK : OK | ----- |
1466             |utf8-icon-au1 : OK : OK | ----- |
1467             |utf8-icon-au2 : OK : OK | ----- |
1468             +---------------+----+-----+-------+
1469             |ascii : OK : -- | OK |
1470             |binary : OK : OK | ----- |
1471             +---------------+----+-----+-------+
1472             (#): guessed when it has bom.
1473              
1474             =head2 GUESSING ORDER
1475              
1476             1. utf32 (#)
1477             2. utf16 (#)
1478             3. utf32-be
1479             4. utf32-le
1480             5. ascii
1481             6. jis
1482             7. sjis-jsky (pp)
1483             8. euc
1484             9. sjis
1485             10. sjis-jsky (xs)
1486             11. sjis-au
1487             12. sjis-imode
1488             13. sjis-doti
1489             14. utf8
1490             15. unknown
1491              
1492             =head1 DESCRIPTION OF UNICODE MAPPING
1493              
1494             Transcoding between Unicode encodings and other ones is performed as below:
1495              
1496              
1497             =over 2
1498              
1499             =item Shift_JIS
1500              
1501             This module uses the mapping table of MS-CP932.
1502              
1503              
1504             L<< ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.TXT >>
1505              
1506              
1507             When the module tries to convert Unicode string to Shift_JIS, it represents most
1508             letters which isn't available in Shift_JIS as decimal character reference
1509             ('&#dddd;'). There is one exception to this: every graphic characters for mobile
1510             phones are replaced with '?' mark.
1511              
1512              
1513             For variants of Shift_JIS defined for mobile phones, every unrepresentable
1514             characters are replaced with '?' mark unlike the plain Shift_JIS.
1515              
1516              
1517             =item EUC-JP/ISO-2022-JP
1518              
1519             This module doesn't directly convert Unicode string from/to EUC-JP or
1520             ISO-2022-JP: it once converts from/to Shift_JIS and then do the rest
1521             translation. So characters which aren't available in the Shift_JIS can not be
1522             properly translated.
1523              
1524              
1525             =item DoCoMo i-mode
1526              
1527             This module maps emoji in the range of F800 - F9FF to U+0FF800 - U+0FF9FF.
1528              
1529              
1530             =item ASTEL dot-i
1531              
1532             This module maps emoji in the range of F000 - F4FF to U+0FF000 - U+0FF4FF.
1533              
1534              
1535             =item J-PHONE J-SKY
1536              
1537             The encoding method defined by J-SKY is as follows: first an escape sequence
1538             "\e\$" comes to indicate the beginning of emoji, then the first byte of an emoji
1539             comes next, then the second bytes of at least one emoji comes next, then "\x0f"
1540             comes last to indicate the end of emoji. If a string contains a series of emoji
1541             whose first bytes are identical, such sequence can be compressed by cascading
1542             second bytes of them to the single first byte.
1543              
1544              
1545             This module considers a pair of those first and second bytes to be one letter,
1546             and map them from 4500 - 47FF to U+0FFB00 - U+0FFDFF.
1547              
1548              
1549             When the module encodes J-SKY emoji, it performs the compression automatically.
1550              
1551              
1552             =item AU
1553              
1554             This module maps AU emoji to U+0FF500 - U+0FF6FF.
1555              
1556              
1557             =back
1558              
1559             =head1 PurePerl mode
1560              
1561             use Unicode::Japanese qw(PurePerl);
1562              
1563             If you want to explicitly take the pure perl implementation, pass
1564             C<'PurePerl'> to the argument of the C statement.
1565              
1566              
1567             =head1 BUGS
1568              
1569             Please report bugs and requests to C or
1570             L. If you
1571             report them to the web interface, any progress to your report will be
1572             automatically sent back to you.
1573              
1574              
1575             =over 2
1576              
1577             =item *
1578              
1579              
1580              
1581             This module doesn't directly convert Unicode string from/to EUC-JP or
1582             ISO-2022-JP: it once converts from/to Shift_JIS and then do the rest
1583             translation. So characters which aren't available in the Shift_JIS can not be
1584             properly translated.
1585              
1586              
1587             =item *
1588              
1589              
1590              
1591             The XS implementation of getcode() fails to detect the encoding when the given
1592             string contains \e while its encoding is EUC-JP or Shift_JIS.
1593              
1594              
1595             =item *
1596              
1597              
1598              
1599             Japanese.pm is composed of textual perl script and binary character conversion
1600             table. If you transfer it on FTP using ASCII mode, the file will collapse.
1601              
1602              
1603             =back
1604              
1605             =head1 SUPPORT
1606              
1607             You can find documentation for this module with the perldoc command.
1608              
1609              
1610             perldoc Unicode::Japanese
1611              
1612             You can find more information at:
1613              
1614              
1615             =over 4
1616              
1617             =item * AnnoCPAN: Annotated CPAN documentation
1618              
1619             L
1620              
1621              
1622             =item * CPAN Ratings
1623              
1624             L
1625              
1626              
1627             =item * RT: CPAN's request tracker
1628              
1629             L
1630              
1631              
1632             =item * Search CPAN
1633              
1634             L
1635              
1636              
1637             =back
1638              
1639             =head1 CREDITS
1640              
1641             Thanks very much to:
1642              
1643              
1644             NAKAYAMA Nao
1645              
1646              
1647             SUGIURA Tatsuki & Debian JP Project
1648              
1649              
1650             =head1 COPYRIGHT & LICENSE
1651              
1652             Copyright 2001-2008
1653             SANO Taku (SAWATARI Mikage) and YAMASHINA Hio,
1654             all rights reserved.
1655              
1656              
1657             This program is free software; you can redistribute it and/or modify it
1658             under the same terms as Perl itself.
1659              
1660              
1661              
1662             =cut
1663              
1664              
1665              
1666             __DATA__